#juiceman
1 messages ยท Page 1 of 1 (latest)
Hi there. What is your issue/question? Let's chat in this thread
hi
I want to save a users credit card information
I do so using the following code:
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'setup',
success_url: 'http://localhost:3000/api/stripe/saveCardToken/{CHECKOUT_SESSION_ID}',
cancel_url: 'https://example.com/cancel',
});
when they complete the card sign up, the user is sent to success url.
I am able to capture the CHECKOUT_SESSION_ID
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
You need to use the payment method id generated from the setup
That guide shows how to achieve this
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
}
Hi there ๐ taking over as my colleague needs to step away
Give me a few minutes to get caught up.
great
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.
i dont want to charge them the first time. i want to store the creditcard payment to use later
Okay, and are you able to successfully create a Checkout Session in setup mode that you navigate to and complete?
yes
i can redirect the user to the success url, where I am able to grab the CHECKOUT_SESSION_ID token
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
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
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
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
charge = await stripe.charges.create({
amount: amount,
currency: 'usd',
description: description,
source: session.setup_intent.payment_method,
})
so that does not work
Correct. You need to use Payment Intents instead of Charges to do this.
whats the api for that?
Here are the docs for that API: https://stripe.com/docs/payments/payment-intents
๐ฆ
i looked at that before and have not been able to make sense of it
could you provide me with the snip
Hello! I'm taking over and catching up...
Have you looked at this guide? It's for exactly what you want to do (save a Payment Method with Checkout and then use it later): https://stripe.com/docs/payments/save-and-reuse?platform=checkout
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?
?
You follow the steps in #5 of that guide: https://stripe.com/docs/payments/save-and-reuse?platform=checkout#use-payment-method
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.
how do i do step 5?
can i get an example of that?
do i need to create a customer ?
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?