简体   繁体   中英

How do I check Stripe coupon validity in Stripe elements?

I'm trying to check coupon validity while using a stripe elements form on the client side without going to server. The idea is that I want to tell a customer if their coupon is valid or invalid before signing them up for something.

Ideally I would be able to just do something like this:

        stripe.coupons.retrieve(coupon).then((stripe_coupon)=> {
          createToken(additionalData);
        }).catch((error) => {
          throwCardError("Invalid coupon, please try again.");
        });

The issue is that stripe.coupons is not a part of the elements API, it's only part of the server-side API.

The reason I'm hesitant to check on the server side is because I use firestore to store all my data. So that means in order to get this to work, I would have to first write the coupon to firebase, have a cloud function that calls the stripe API to check the coupon validity and then writes that to a separate part of the database just for that, and then set up a listener on the client side that listens for changes to that part of the database to check coupon validity, and all of that would be really slow.

Is there any way to check the validity of a stripe coupon client side in stripe elements?

There is no function within Elements to check the validity of a coupon ( you can see all available methods for Stripe.js / Elements here ).

If you're using Firebase, what you can do here is setup an endpoint using a HTTP function , in your client-side code make a request to this endpoint using fetch , passing the coupon id you wish to check.

When this request hits this endpoint you will make a call out to the Stripe API to attempt to retrieve the coupon , returning the result to the user; there's no need to store a copy of the coupon within your Firebase database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM