Reputation: 535
I have added the Firebase 'Run Subscription Payments with Stripe' extension to my Firebase project and have gotten it to work to process the payment of a single subscription, but the app I'm building will sell many different subscriptions and the user needs to be able to purchase several at the same time.
I am struggling to find how to process multiple subscriptions at once. I am using something very similar to the code in the Firebase extension docs for redirecting to the Stripe checkout:
const purchase = {
price: 'price_1GqIC8HYgolSBA35zoTTN2Zl',
success_url: window.location.origin,
cancel_url: window.location.origin,
}
const docRef = await db
.collection('users')
.doc(currentUser.uid)
.collection('checkout_sessions')
.add(purchase);
// Wait for the CheckoutSession to get attached by the extension
docRef.onSnapshot((snap) => {
const { error, sessionId } = snap.data();
if (error) {
// Show an error to your customer and
// inspect your Cloud Function logs in the Firebase console.
alert(`An error occured: ${error.message}`);
}
if (sessionId) {
// We have a session, let's redirect to Checkout
// Init Stripe
const stripe = Stripe('pk_test_1234');
stripe.redirectToCheckout({ sessionId });
}
});
I have tried replacing the price
value with an array line_items
which works well as far as enabling a checkout with the multiple products listed, BUT back in Firebase, this creates a single subscription document for the user, whereas I need a subscription document created for EACH subscription (ideally with metadata). It seems as though line_items is only supposed to be used for when there are multiple prices being implemented on the same subscription (such as an initial setup fee).
I have also tried to use instead an array called items
which is in the Stripe docs, but when trying this approach, the error states:
Missing required param: line_items[0][currency]
It seems as though I can't call add()
multiple times, because the call to add
returns the sessionId
required to redirect to checkout. Please tell me this is possible somehow with this extension. Thank you!
Upvotes: 0
Views: 597
Reputation: 1179
Creating multiple Subscriptions using the same Checkout session is not supported. As you've rightly said, you can create one Subscription with multiple Prices, which is supported.
In order to create multiple Subscriptions with Checkout, your system will need to send the user through Checkout N times for N Subscriptions.
If that isn't a favourable option, the other avenue, would be to not use Checkout and to collect a customer's payment details using Elements [1]. Then you would have the opportunity to optimise the user experience to not have the journey through Checkout each time by attempting to start a Subscription using a saved payment method.
[1] https://stripe.com/docs/billing/subscriptions/fixed-price
Upvotes: 2