Stripe Subscription with Payment Method

I'm using the latest version of iOS Stripe SDK (1.27), where the STPAddCardViewControllerDelegate instead of returning a STPToken, now returns a PaymentMethod object.

func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: @escaping STPErrorBlock) {
   dismiss(animated: true, completion: { [weak self] in
       // Send the paymentMethod.stripeId to my Server
   })
}

On the server side, I verified if the user on my system already has a customer object and create or update accordingly the Customer, like:

Customer::create(['email'=> email, 'payment_method' => payment_method_id])

But after trying to create a subscription, with the following piece of code:

Subscription::create(['customer' => customer, 'plan' => plan_id])

It returns the following error:

This customer has no attached payment source

Note: As a workaround I can return to the previous version of the iOS SDK when STPToken was generated and use as source (in that way was working). But I want to use the new PaymentMethod API.

  1. Is there a way to convert a PaymentMethod to a token? (or is it correct to send to server the PaymentMethod.stripeId)?

  2. What is missing?

Upvotes: 1

Views: 1112

Answers (1)

Spawnrad
Spawnrad

Reputation: 465

Hope that can help you. I had the same issue and solved it. You can use this code :

Subscription::create([
  'customer' => $customer,
  'plan' => $plan_id,
  'default_payment_method' => $paymentMethodId
]);

or

To use this PaymentMethod as the default for invoice or subscription payments, set invoice_settings.default_payment_method, on the Customer to the PaymentMethod’s ID.

Customer::create([
'email'=> email,
'payment_method' => $paymentMethodId
'invoice_settings' => ['default_payment_method' => $paymentMethodId]
])

Upvotes: 1

Related Questions