getting No such PaymentMethod: error message when trying to attach paymentMethodId to customerId

Viewed 8736

new to stripe but the documentation looked pretty forward but now stock on something very rudimentary, I can see the customer created in stripe console, same as the product and the price but when I try to attach the payment method retrieved from cardElement, and send it to server i get the message that paymentMethod is not valid. I'm following stripe docs in this page https://stripe.com/docs/billing/subscriptions/fixed-price#create-customer

In javascript i have:

    const payload = await stripe.createPaymentMethod({
        type: 'card',
        card: elements.getElement(CardElement),
        billing_details: billingDetails,
    });

this comes back with an paymentMethod object with id

 paymentMethod.id be like pm_1GoHU32eZvKYloXXXXXXX

on the server side:

await stripe.paymentMethods.attach(paymentMethodId, {
    customer: customerId,
});

but i get an exception like below

StripeInvalidRequestError: No such PaymentMethod: pm_1GoHU32eZvKYloXXXXXXX
at Function.generate (/root/coco/node_modules/stripe/lib/Error.js:39:16)
at IncomingMessage.<anonymous> 
(/root/coco/node_modules/stripe/lib/StripeResource.js:175:33)
at Object.onceWrapper (events.js:421:28)
at IncomingMessage.emit (events.js:327:22)
at IncomingMessage.EventEmitter.emit (domain.js:485:12)
at endReadableNT (_stream_readable.js:1225:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
type: 'StripeInvalidRequestError',
raw: {
code: 'resource_missing',
doc_url: 'https://stripe.com/docs/error-codes/resource-missing',
message: 'No such PaymentMethod: pm_1GoHU32eZvKYlo2CYLA3SCaX',
3 Answers

OK, figured it out. I copied and pasted from stripe example, Stripe is usually good with replacing their document examples with you keys but this document (posted in the question) is using a sample publishable key!

It's basic but i was the same problem. I resolved it when i used my public key

I was using stripe connect and was creating a payment profile on the connect account. Then while retrieving I wasn't passing the stripe account correctly. Once I did, the payment method was retrieved correctly. Here is the code I used. Might help someone

//retrieve account
$stripe = new \Stripe\StripeClient({platform api key});
$payment_method = $stripe->paymentMethods->retrieve(
  {payment method id from payment_intent},
  [],
  ['stripe_account' => {stripe connect account id}]
);
Related