Please attach the payment method to this Customer before using it with a SetupIntent | Migrating stripe subscription to be SCA compliant

梦想的初衷 提交于 2019-12-22 01:29:22

问题


I have a subscription, I collect card details on signup with a 7 day trial, after which the subscription bills monthly.

I have followed the documentation to create a subscription.

Testing the flow with a fake card 4000002500003155 which requires setup, I encountered an issue.

My setup is as follows.

  1. Frontend does stripe.createToken()
  2. Server does stripe.customers.update(customer_id, { source }) with token
  3. Server creates the subscription

      stripe.subscriptions.create({
        customer: self.stripe_customer_id,
        tax_percent: 20,
        expand: ['latest_invoice.payment_intent', 'pending_setup_intent'],
        ...plan
      }
    
  4. As expected the subscription response subscription.pending_setup_intent is not null and has { client_secret: 'seti__', status: 'requires_action', payment_method: 'card___', ... }.
  5. On the frontend I attempt to handle the pending setup intent on the frontend with stripe.handleCardSetup(client_secret, {}); but I got the following error.

The payment method supplied (pm____) does not belong to a Customer, but you supplied Customer cus____. Please attach the payment method to this Customer before using it with a SetupIntent.

I tried to remedy this by attaching the paymentMethod to the user before step 5

stripe.paymentMethods.attach('card____', {
  customer: customer_id
});

But I got the following error

The payment method you provided has already been attached to a customer.


Update

I replaced steps 1 and 2 by attaching payment method from setupIntent to the customer using handleCardSetup and paymentMethods.attach. I was able to succesfully create subscription after that point. subscription.pending_setup_intent is then returned as null which means it is successful.

For those who want some starter code, I created a working playground here with subscriptions, frontend (react) and backend (express) on glitch.

来源:https://stackoverflow.com/questions/57200228/please-attach-the-payment-method-to-this-customer-before-using-it-with-a-setupin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!