#juiceman
1 messages ยท Page 1 of 1 (latest)
Hi there. What's the issue you're facing?
But what's the error you are seeing? And what guide are you following?
let customer = await getCustomer(req.userData)
if (customer == null) {
customer = await createCustomer(req.userData)
}else{
console.log('no customer created')
}
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'setup',
customer: customer.id,
success_url: 'http://localhost:3000/api/stripe/saveCardToken/{CHECKOUT_SESSION_ID}__' + req.userData.userId,
cancel_url: 'https://example.com/cancel',
});
res.status(201).json({
url: session.url
})
then when the user goes to the success_url, the following occurs
I save CHECKOUT_SESSION_ID
then
const CHECKOUT_SESSION_ID = req.params.id.split('__')[0] ;
const session = await stripe.checkout.sessions.retrieve(
CHECKOUT_SESSION_ID, { expand: ['setup_intent'] }
);
after this, I dont know what to do moving forward
the whole goal is to save their credit card info so that i can charge them later on
so what data do i have to save moving forward, and how can i charge the credit card that was inputed by the user?
does that make sense?
Well you'd want to save the payment method id so that you can create payment intents with it
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Probably also the customer id: https://stripe.com/docs/api/setup_intents/object#setup_intent_object-customer
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Up to your flow though
And webhooks are the preferable route, not relying on the return url
i dont know how to do the webhook
Up to you though. It's just a customer reaching the return url isn't guaranteed
none of that information helped .. can you give me some examples
The browser could crash, they could accidentally close the tab, etc
i been batteling this for like 2 days
Then you need to be more specific with your questions because I shared resources on how to achieve this
could you give me the snip of code that charges the customer
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Right hand side
so let me tell you where I am, and what I want to do, and then you can tell me what i need to do
Hello ๐
Taking over as codename_duchess needs to step away soon
I can help ๐
So from what you've described so far, you seem to be following this guide right here
https://stripe.com/docs/payments/save-and-reuse?platform=checkout
The client visits clicks a button on the page which makes a post request to the server and runs the following code:
let customer = await getCustomer(req.userData)
if (customer == null) {
customer = await createCustomer(req.userData)
}else{
console.log('no customer created')
}
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'setup',
customer: customer.id,
success_url: 'http://localhost:3000/api/stripe/saveCardToken/%7BCHECKOUT_SESSION_ID%7D__' + req.userData.userId,
cancel_url: 'https://example.com/cancel',
});
after the client completes storing the credit card information, they are send to the success url.
when they get to the success url, the following code runs
const CHECKOUT_SESSION_ID = req.params.id.split('__')[0] ;
const session = await stripe.checkout.sessions.retrieve(
CHECKOUT_SESSION_ID, { expand: ['setup_intent'] }
);
now that i have this information.
- what data from the session obj do i need to save?
- what is the code that i need to charge the credit card
yes
Gotcha. So have you been able to save their payment methods using this flow or are you seeing any errors here?
i save the following two:
token: session.setup_intent.payment_method,
stripeUserId: session.customer
i dont see errors
Okay great! So since you're providing a customer ID already when you create a Checkout Session, you don't need to make a call to attach the PaymentMethod to a customer separately as shown in step 5.
https://stripe.com/docs/payments/save-and-reuse?platform=checkout#use-payment-method
All you need to do is this
https://stripe.com/docs/payments/save-and-reuse?platform=web#charge-saved-payment-method
let me try it, dont close the chat
Yup, take your time ๐
NP! ๐ Happy to help