nicholas
nicholas

Reputation: 14563

Cancel a subscription using react-native-stripe-api

I'm using react-native-stripe-id to connect to Stripe from a react-native app. I create a token from a card, then a customer, and then a subscription for them with:

...
.then(card => {
  return stripeClient.createToken(card.cardNumber, card.expiryMonth, card.expiryYear, card.cvv);
})
.then(token => {
  return stripeClient.createCustomer(token.id, user.email, user.uid, user.firstName,  user.lastName);
})
.then(customer => {
  return stripeClient.createSubscription(customer.id, "MyPlan");
})
.then(subscription => {
  ...
});

This seems to work perfectly.

From here, with a customer and subscription, how can I change or cancel their subscription?

Simply creating a new subscription to a new plan subscribes the customer to both plans and double charging them.

I can retrieve the customer's subscription with retrieveSubscription, but don't know what I can do with that.

Upvotes: 0

Views: 704

Answers (1)

hpar
hpar

Reputation: 738

I would advise against calling createCustomer, createSubscription, etc from your React Native app because they can only be made with your secret key. If you put your secret key in your application, anyone with a copy of your app can trivially extract the secret key and use it to maliciously make API calls against your account—for example, refunding your charges, deleting your data, etc.

The usual approach is to create a backend application that your iOS app makes API calls to. Your backend application securely stores your secret key and communicates with with the Stripe API. Besides security, the other advantage is that you will end up with a simpler and lighter weight frontend app.

Assuming you're using Stripe's Node library, you want to make this call: https://stripe.com/docs/api/node#cancel_subscription

I'd review Stripe's Standard Integration guide to understand how to design your backend application: https://stripe.com/docs/mobile/ios/standard#prepare-your-api

Upvotes: 2

Related Questions