Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Having a weird issue here. Following the docs, I am attaching the PaymentMethod to an existing customer, but it's not working. Roughly, I:

  1. create a customer
  2. create a payment intent with the customer
  3. create a card element with the payment intent
  4. customer enters card info
  5. confirm payment succeeded and sent intent back to backend
  6. if the intent has succeeded and the customer chose to save their card, create the payment method with the intent method and customer
  7. get error

The code:

  1. python: stripe.Customer.create(email=user.email, name=user.full_name)
  2. python: stripe.PaymentIntent.create(amount=amount, currency="aud", customer=user.stripe_customer_id)
  3. js: Stripe('{{ stripe_publishable_key }}').elements().create("card");
  4. user: enters card info
  5. js: stripe.confirmCardPayment('{{ clientSecret }}', { payment_method: { card: card, billing_details: { // name: 'Jenny Rosen' }, } }).then(function (result) { if (result.error) { // Show error to your customer (e.g., insufficient funds) console.log(result.error.message); var displayError = document.getElementById('card-errors'); displayError.textContent = result.error.message; } else { // The payment has been processed! if (result.paymentIntent.status === 'succeeded') { // Show a success message to your customer // There's a risk of the customer closing the window before callback // execution. Set up a webhook or plugin to listen for the // payment_intent.succeeded event that handles any business critical // post-payment actions. $('#fake-submit').click(); } } });
  6. python: stripe.PaymentMethod.attach(stripe.PaymentIntent.retrieve(intent_id).payment_method, customer=user.stripe_customer_id)
  7. error: Request req_request_id: This PaymentMethod was previously used without being attached to a Customer or was detached from a Customer, and may not be used again.
like image 519
Josheee Avatar asked Feb 21 '20 06:02

Josheee


People also ask

How do I add another payment method on stripe?

Visit the Manage payment methods for your connected accounts page in your Dashboard to configure which payment methods your connected accounts accept. Changes to default settings apply to all new and existing connected accounts. Your connected accounts accept this payment method during checkout.


1 Answers

It looks like there is an issue with the Stripe documentation.

On https://stripe.com/docs/payments/save-after-payment#web-collect-card-details they have:

    setup_future_usage: 'off_session'

But on https://stripe.com/docs/payments/save-and-reuse#web-collect-card-details they are missing this critical line.

But in your case, does the user select if they want to save their card on the frontend? Then you don't need to save the card on the backend and can save it in the confirmCardPayment call: https://stripe.com/docs/js/payment_intents/confirm_card_payment#stripe_confirm_card_payment-data-save_payment_method :

save_payment_method boolean

If the PaymentIntent is associated with a customer and this parameter is set to true, the provided payment method will be attached to the customer. Default is false.

like image 127
Jayen Avatar answered Oct 22 '22 10:10

Jayen