#juiceman

1 messages ยท Page 1 of 1 (latest)

fair beaconBOT
hollow berry
#

Hi there. What's the issue you're facing?

simple coyote
#

i cant charge a users credit card

#

this is what i done so far

hollow berry
#

But what's the error you are seeing? And what guide are you following?

simple coyote
#

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?

hollow berry
#

Well you'd want to save the payment method id so that you can create payment intents with it

#

Up to your flow though

#

And webhooks are the preferable route, not relying on the return url

simple coyote
#

i dont know how to do the webhook

hollow berry
#

Up to you though. It's just a customer reaching the return url isn't guaranteed

simple coyote
#

none of that information helped .. can you give me some examples

hollow berry
#

The browser could crash, they could accidentally close the tab, etc

simple coyote
#

i been batteling this for like 2 days

hollow berry
#

Then you need to be more specific with your questions because I shared resources on how to achieve this

simple coyote
#

could you give me the snip of code that charges the customer

hollow berry
#

Right hand side

simple coyote
#

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

steady perch
#

Hello ๐Ÿ‘‹
Taking over as codename_duchess needs to step away soon

#

I can help ๐Ÿ™‚

simple coyote
#

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
steady perch
#

Gotcha. So have you been able to save their payment methods using this flow or are you seeing any errors here?

simple coyote
#

i save the following two:
token: session.setup_intent.payment_method,
stripeUserId: session.customer

#

i dont see errors

steady perch
simple coyote
#

let me try it, dont close the chat

steady perch
#

Yup, take your time ๐Ÿ™‚

simple coyote
#

got it, thank yuo!

#

ur the best

steady perch
#

NP! ๐Ÿ™‚ Happy to help