#shaitan1126-attaching-pm

1 messages · Page 1 of 1 (latest)

gritty rover
#

Pulling additional messages from main chat for context:

I did whatever the turbotine suggested but it's still not attaching the payment_method id to customer

Getting this error: Cannot charge a customer that has no active card
while recharging the user via customerID

mossy granite
#

This was the conversation between turbotime and me

#

====================

#

shaitan1126 — Today at 17:48
Hello guys,

I've implemented the stripe session checkout to make payment, now i want to attached the payment_method to returned customer, i tried but it gives me an error which is below:

"This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again."

turbotime — Today at 17:52
What's the PaymentMethod's id? (pm_******)

shaitan1126 — Today at 17:52
pm_1JkpK3BtJRSvW01wuG7KeCjk

1
[17:53]
This is the payment_method id which I retrieved from payment_intent api response.
[17:55]
When a user goes back to the website from stripe platform after making payment then we got the cstest* id, using this id we call an api to get cs_test** full data and in this api we got the payment_intent id and all

turbotime — Today at 17:58
OK, so the thing about CheckoutSessions is they will always have a Customer associated with them, and the PaymentMethod will be attached to that Customer. If you pass a customer id when creating the CheckoutSession, it will use an existing Customer, but otherwise it will create a new Customer automatically.

For that PaymentMethod, you can see this on https://dashboard.stripe.com/test/payments/pi_3JkpIoBtJRSvW01w0BJa6dCy

So if you want a CheckoutSession to attach a PaymentMethod to an existing Customer, you need to pass that Customer as customer when creating the CheckoutSession.

shaitan1126 — Today at 18:11
Ok gotcha

gritty rover
#

Thanks for that, please give me a moment to parse through it.

#

Do you have the IDs for the customer, payment method, and checkout session that we could take a closer look at?

mossy granite
#

Ok give me a sec, sending you

#

cs_test_a1y7Hdv1u6KmyMFOEDtG6uzYV6ZvMJESAjLfgTeN5MkbIdqfBtYO7aWHPe
pm_1JkqgJBtJRSvW01wI9q6QLti
cus_KPg2D5spvnMzfE

#

Please check

gritty rover
#

Thanks, taking a look

#

Apologies for the delay! I've looked at the request where you created the Checkout Session and see that there is one more parameter you'll need to include in those requests.

It's the payment_intent_data.setup_future_usage parameter that causes the PM to be attached to the customer rather than the PaymentIntent. You'll need to specify either on_session or off_session for that parameter based on your scenario.

https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-setup_future_usage

mossy granite
#

what's on_session and off_session?

gritty rover
#

on_session means that you intend to reuse the card, but only while your customer is on your site. Kind of like when you save your information on a marketplace to buy additional items later.
off_session indicates that you intend to try charging the PM without the user being present on your site. Think of this more like for a monthly subscription where you are billed behind the scenes without needing to log into a site.

mossy granite
#

ok thanks

#

gotcha

#

appreciate your help

gritty rover
#

Not a problem! Let us know if that still doesn't do what you're expecting.

mossy granite
#

ok sure