2
votes

I am creating a setup intent and receiving the payment method's id from it. Then I am creating a customer using the following code

customer = Stripe::Customer.create({
      email: current_user.email,
      description: "Customer for subscription",
      payment_method: params[:payment_method]
    })

It is returning the following response

{
  "id": "cus_IZmhg4VhIwFUBI",
  "object": "customer",
  "address": null,
  "balance": 0,
  "created": 1608037176,
  "currency": null,
  "default_source": null,
  "delinquent": false,
  "description": "Customer for subscription",
  "discount": null,
  "email": "[email protected]",
  "invoice_prefix": "76BF0C5E",
  "invoice_settings": {
    "custom_fields": null,
    "default_payment_method": null,
    "footer": null
  },
  "livemode": false,
  "metadata": {
  },
  "name": null,
  "next_invoice_sequence": 1,
  "phone": null,
  "preferred_locales": [

  ],
  "shipping": null,
  "tax_exempt": "none"
}

The problem is, it is not attaching the payment method to the customer, when I try to create a subscription using the customer object, it returns the following error:

Stripe::InvalidRequestError (This customer has no attached payment source or default payment method.):

But when I look in the dashboard, the payment method is already attached with the customer

2
What kind data are you passing in for the payment_method? - Daniel Sindrestean
I am passing the payment id which I got from the setup intent. When I check the dashboard, the payment is already attached with the customer but somehow, when I pass customer id to the subscription, it returns that customer has no attached payment - Aaqib

2 Answers

1
votes

The payment_method is attached, and you can list them for the customer. Then, you need to set explicitly the invoice_settings.default_payment_method for the Customer, which is what will be used for the subscription creation.

1
votes

Inspired by the @Nolan's answer, I have fixed the issue by passing payment id to invoice_settings.default_payment_method while customer creation. Following is the code snippet:

customer = Stripe::Customer.create({
      email: current_user.email,
      description: "Customer for subscription",
      payment_method: params[:payment_method],
      invoice_settings: {
        default_payment_method: params[:payment_method] 
      } 
    })