#juiceman

1 messages ยท Page 1 of 1 (latest)

echo ridgeBOT
orchid dagger
#

Hi there. What is your issue/question? Let's chat in this thread

paper birch
#

hi

#

my understanding is that I use CHECKOUT_SESSION_ID as the token to generate a charge against the card

#

const charge = await stripe.charges.create({
amount: amount,
currency: 'usd',
description: description,
source: CHECKOUT_SESSION_ID,
})

#

but that does not work

#

so.... how do i charge the user?

#

the goal is to save the users credit card so that I can charge it later on

orchid dagger
#

You need to use the payment method id generated from the setup

#

That guide shows how to achieve this

paper birch
#

how do i do this in node?

#

do i pass an object into retrieve

#

I can do the following:

const session = await stripe.checkout.sessions.retrieve(CHECKOUT_SESSION_ID);
console.log(session)
#

{
id: 'cs_test_c1K9GRmHCa6YnGkAXQPu5PdbLD4DtD83c1J8pFy1Cgr4tMLH2aQsr0gekv',
object: 'checkout.session',
after_expiration: null,
allow_promotion_codes: null,
amount_subtotal: null,
amount_total: null,
automatic_tax: { enabled: false, status: null },
billing_address_collection: null,
cancel_url: 'https://example.com/cancel',
client_reference_id: null,
consent: null,
consent_collection: null,
created: 1674146726,
currency: null,
custom_text: { shipping_address: null, submit: null },
customer: null,
customer_creation: 'if_required',
customer_details: {
address: null,
email: 'jcasasmail@gmail.com',
name: null,
phone: null,
tax_exempt: null,
tax_ids: null
},
customer_email: null,
expires_at: 1674233126,
invoice: null,
invoice_creation: null,
livemode: false,
locale: null,
metadata: {},
mode: 'setup',
payment_intent: null,
payment_link: null,
payment_method_collection: 'always',
payment_method_options: {},
payment_method_types: [ 'card' ],
payment_status: 'no_payment_required',
phone_number_collection: { enabled: false },
recovered_from: null,
setup_intent: 'seti_1MS1EwKvlLDAjAUzQW62W7m6',
shipping: null,
shipping_address_collection: null,
shipping_options: [],
shipping_rate: null,
status: 'complete',
submit_type: null,
subscription: null,
success_url: 'http://localhost:3000/api/stripe/saveCardToken/{CHECKOUT_SESSION_ID}__63a3c9a02ab0953934a5f1af',
total_details: null,
url: null
}

balmy harness
#

Hi there ๐Ÿ‘‹ taking over as my colleague needs to step away

Give me a few minutes to get caught up.

paper birch
#

great

balmy harness
#

I'm confused about what you're trying to do. You're creating a Checkout Session where your customer goes to a Stripe-hosted page to make a payment. But then you're trying to charge the Customer a second time?

I noticed you said:

my understanding is that I use CHECKOUT_SESSION_ID as the token to generate a charge against the card
That's not true. A Checkout Session is has a URL where you send your customer to make a payment. Once the Checkout Session succeeds, a payment is automatically created.

#

There's no need to go back in the code and charge them again.

paper birch
#

i dont want to charge them the first time. i want to store the creditcard payment to use later

balmy harness
#

Okay, and are you able to successfully create a Checkout Session in setup mode that you navigate to and complete?

paper birch
#

yes

#

i can redirect the user to the success url, where I am able to grab the CHECKOUT_SESSION_ID token

balmy harness
#

So from there you need to retrieve the Checkout Session object and expand the customer object from it in order to get that Customer's Payment Method ID

Expanding responses: https://stripe.com/docs/expand

Learn how to reduce the number of requests you make to the Stripe API by expanding objects in responses.

#

Let me know once you've done that and we can talk about the next step, which is to create a Payment Intent to actually use the Payment Method to create a payment

paper birch
#

so i can print the following :

const session = await stripe.checkout.sessions.retrieve(

    req.params.id.split('__')[0],{expand: ['customer']}
      
);
console.log(session)

// CONSOLE.LOG

{
id: 'cs_test_c1kBbEDo0vZqmkNLXV7myZ2YQZCzVn59wpR3XMiPKTVfMu3nMLPvO74eOY',
object: 'checkout.session',
after_expiration: null,
allow_promotion_codes: null,
amount_subtotal: null,
amount_total: null,
automatic_tax: { enabled: false, status: null },
billing_address_collection: null,
cancel_url: 'https://example.com/cancel',
client_reference_id: null,
consent: null,
consent_collection: null,
created: 1674147614,
currency: null,
custom_text: { shipping_address: null, submit: null },
customer: null,
customer_creation: 'if_required',
customer_details: {
address: null,
email: 'jcasasmail@gmail.com',
name: null,
phone: null,
tax_exempt: null,
tax_ids: null
},
customer_email: null,
expires_at: 1674234014,
invoice: null,
invoice_creation: null,
livemode: false,
locale: null,
metadata: {},
mode: 'setup',
payment_intent: null,
payment_link: null,
payment_method_collection: 'always',
payment_method_options: {},
payment_method_types: [ 'card' ],
payment_status: 'no_payment_required',
phone_number_collection: { enabled: false },
recovered_from: null,
setup_intent: 'seti_1MS1TGKvlLDAjAUzIZRXvGYD',
shipping: null,
shipping_address_collection: null,
shipping_options: [],
shipping_rate: null,
status: 'complete',
submit_type: null,
subscription: null,
success_url: 'http://localhost:3000/api/stripe/saveCardToken/{CHECKOUT_SESSION_ID}__63a3c9a02ab0953934a5f1af',
total_details: null,
url: null
}

//

I dont see the payment method you refer to

balmy harness
#

Hmmmm, let me look into this

#

Ahhh, okay. So setup mode creates a Setup Intent object, not a Customer object. My apologies for the red herring.

expand the setup_intent property and get the Payment Method from that object instead

paper birch
#

charge = await stripe.charges.create({
amount: amount,
currency: 'usd',
description: description,
source: session.setup_intent.payment_method,
})

so that does not work

balmy harness
#

Correct. You need to use Payment Intents instead of Charges to do this.

paper birch
#

whats the api for that?

balmy harness
paper birch
#

๐Ÿ˜ฆ

#

i looked at that before and have not been able to make sense of it

#

could you provide me with the snip

spiral gull
#

Hello! I'm taking over and catching up...

paper birch
#

i cant follow it

#

once I get the session.setup_intent.payment_method, what do I do with it?

#

i just need to charge the credit card lol

#

?

#

what do i do once i get the session.setup_intent.payment_method?

#

?

spiral gull
#

Attach the Payment Method to a Customer if it's not already attached to one, then create a Payment Intent using that Customer and Payment Method.

paper birch
#

how do i do step 5?

#

can i get an example of that?

#

do i need to create a customer ?

spiral gull
#

That depends on how the Checkout Session was configured, it may have created a Customer for you. If you retrieve the Payment Method does it have a customer set?

paper birch
#

i didnt add a customer

#

on the checkout session