X

Node.js

Stripe Api One Time Charge with User Info

August 10, 2021

/*

One Time Charge with User Info


*/

In the last article, I made a one time charge with the stripe api. I only used the card information, I didn't save any of the user's info. This time I'm going to create a charge with some of the user's details such as email, to send them a receipt of the purchase & the user's address info. By taking the user's address stripe will do a security check to see if the address info matches the card's info.

/*

Stripe.createToken() User Info


*/

The second parameter of the stripe.createToken method is data object that takes the user's name & address info. We're going to use this method like we did in the last article except we're going to fill out the user's billing details.

Code for stripe.createToken() with user info

/*

Retrieving the Token


*/

On the server, we're going to retrieve the token from the client using the Stripe.tokens.retrieve() method. This gives us access to the user's address info & allows us to fill out the shipping information when I create the charge.

/*

Stripe.tokens.retrieve() parameters


*/

This method only takes one parameter the id of the token you want to get. In this case it's the token we made on the client & passed to the server.

Code for Stripe.tokens.retrieve() method

/*

Filling Out the Charge with the User's Info


*/

Now I can fill out the email & shipping info with what the user gave me. Their is a receipt_email property in the stripe.create.charge() data object that will send you an email of the transaction when filled out. Their is also a shipping address object that will take your shipping address info.

Code for stripe.create.charges() with user info

/*

Stripe Checks the Billing Details


*/

When stripe creates the charge it will check the billing details, if any, with card number, zipcode & cvc code to see if the card is valid. Just a nice little security verification added by stripe.

You can see a finished project here.

About the Author

Christopher Howard

Chris is a Javascript developer with a minor in UI design. He values programming in vanilla code. Fill out the form below to contact him.