问题
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.
- Frontend does
stripe.createToken()
- Server does
stripe.customers.update(customer_id, { source })
with token Server creates the subscription
stripe.subscriptions.create({ customer: self.stripe_customer_id, tax_percent: 20, expand: ['latest_invoice.payment_intent', 'pending_setup_intent'], ...plan }
- As expected the subscription response
subscription.pending_setup_intent
is not null and has{ client_secret: 'seti__', status: 'requires_action', payment_method: 'card___', ... }
. - 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