Subscription Payment Using Stripe iOS(SwiftUI) SDK
Stripe has amazing documentation covering all the use cases of their products with sample codes and step by step guide. But I had a use case where i needed to do subscription payment using the new Stripe SwiftUI SDK and I had a hard time finding some documentation or some samples to achieve this. Finally, we have a solution and this blog is dedicated to the implementation of that solution.
Stripe payment integration needs some backend and frontend mobile effort so let’s begin with first thing first, project setup and pod installations.
1.1 Podfile (Add dependencies)
pod 'Stripe'
pod 'Alamofire', '~> 5.4'
Add these to your Podfile, Stripe SDK, and Alamofire for API calls, next step is to add the Publishing keys in the AppDelegate file.
1.2 AppDelegate (Provide the SDK with publishing key)
You can get your test publishing key from the Stripe developer console.
1.3 ContentView (Let’s Do Some quick UI work)
Here we have created a sample UI just for demonstration but you guys can integrate it into your existing SwiftUI views.
1.4 SubscriptionViewModel (ViewModel for Stripe configuration)
Here we are calling test backend API hosted on Glitch at the start of the app to get the secret keys and payment intention necessary to launch the stripe Prebuilt UI. This is all we need to configure the SDK and launch it. Now follow the backend part below which is the key step in order to enable the subscription payment in native UI.
1.5 Backend Code (Important Part to create payment intention for subscription)
Here we are creating a subscription payment intent with payment behavior as default_incomplete
. Also do remember to add a valid price id which you can get from the developer console in the products tab for testing but in the real use case, you will be calling stripe API to fetch the price id dynamically.
So the issue was we were creating the payment intention for a normal one-time payment instead of a subscription payment at the server-side code as given below. So be aware if you want to do the subscription-based payment then need to create a subscription payment intention instead of a regular one.
One time payment
//use this code for one time payment
const paymentIntent = await stripe.paymentIntents.create({
amount: 1099,
currency: 'eur',
customer: customer.id,
payment_method_types: ['bancontact', 'card', 'ideal', 'sepa_debit', 'sofort'],
});
Subscription Payment
//use this code for subscription payment
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ price: "price_2JfB2RKdI6ztUgqfnckNNSg3" }],
payment_behavior: "default_incomplete",
expand: ["latest_invoice.payment_intent"],
});
Now finally we have a running app that takes subscription payment in the SwiftUI native SDK. I hope this helps a bit to those looking for samples for this use case. Please find the sample project attached if needed to run the code.
Sample Github Project iOS
Sample Glitch Server Side Project
https://glitch.com/edit/#!/lowly-evening-plant
for the new Glitch Project click here,
Note: Please replace publishing keys and secret keys associated with your Stripe account in order to work properly.
References:
- https://stripe.com/docs/payments/accept-a-payment?platform=ios
- https://github.com/stripe/stripe-ios/issues/1868
Special Thanks to @Jack murphy for help on the issue.