invertase / stripe-firebase-extensions

Repository of Firebase Extensions built by Stripe.
https://firebase.google.com/products/extensions
Apache License 2.0
438 stars 174 forks source link

Apply Coupons Programatically #107

Closed hpflatorre closed 3 years ago

hpflatorre commented 4 years ago

Feature request

Is your feature request related to a problem? Please describe.

it would be very nice to be able to add coupons and promotion_codes to checkout sessions (and customers) programmatically.

Describe the solution you'd like

I need to apply discounts by coupon_id or promotion_code just like in this example from API DOC

    const stripe = await loadStripe(environment.stripePublishableKey);
    const docRef = await this.firestore
    .collection('customers')
    .doc(this.app.user.uid)
    .collection('checkout_sessions')
    .add({
      price: priceID,
      success_url: 'https://my.app/subscription-success',
      cancel_url: 'https://my.app,
      billing_address_collection: 'required',
      allow_promotion_codes: false, // To enforce programmatically set discount
      discounts: {
        promotion_code: this.discountPromoCode // eg. 'promo_0123456789'
      }
    });

Issue #15 suggested a similar approach, but was closed when the option for the user to add coupon/promo code manually into the Checkout interface was release. I do need to do it programmatically.

Describe alternatives you've considered

I considered using the bare REST API directly within my project, but it would be way too cumbersome since my project is all inside firebase infrastructure.

jaytavares commented 3 years ago

I'll second this request. I have a need to apply promotion codes via the discounts property as well. Perhaps we could avoid these one-off feature requests (billing_address_collection, accept_promo_codes, etc.) by allowing passthrough of all checkout_session properties to the stripe.checkout.sessions.create method? This would unlock the full capabilities of the Stripe Checkout Sessions API without having to manually add support in the Firebase extension for each feature.

Edit: I've just submitted a feature request for this #116

thorsten-stripe commented 3 years ago

Unfortunately it's not really desirable to expose all these options on the client-side. You run the risk of a malicious party injecting parameters without them being validated on the server-side. Therefore we need to carefully evaluate which parameters we expose and how we expose them.

Can you please outline your specific use case? What's your logic to decide whether a coupon/discount should be applied?

hpflatorre commented 3 years ago

I need a way to distribute coupons using referral links instead of the customer typing it manually in the Stripe hosted checkout page. My application uses dynamic referral links which carry a coupon code as a query string. Users who subscribe using these referral links should have coupons applied in the checkout.

What's your logic to decide whether a coupon/discount should be applied?

The usage of the coupons can be restricted only by the parameters set when the coupon code is created in the Stripe dashboard (time window, quantity, products and users). Anyone with the referral link containing an active coupon code should be able to get the discount without any further validation from my application.

In practice I just need a method to open the Stripe hosted checkout page with the coupon code pre-filled.