简体   繁体   中英

How to check if Coupon is valid for customer before checkout in stripe api

I have a use case where I need to find that whether applied code is valid for a specific customer or not before making the payment using the Stripe. Assume there is one coupon code as "50%off" created in the Stripe which can only be used once by customers. There will be two scenario:

  1. If customer using it for first time, they discount will be applied which is fine.
  2. If customer using it second time, I dont want to show them discounted price at application level. And at the Stripe level, discount will not be provided as well.

I am not managing coupon code at my application level and using below code to retrieve coupon code data:

\Stripe\Stripe::setApiKey($this->config->item('stripe_secret_key'));
$couponData = \Stripe\Coupon::retrieve($coupon);

The above code share with me below details

[body] => {
  "id": "50%off",
  "object": "coupon",
  "amount_off": 600,
  "created": 1494427220,
  "currency": "usd",
  "duration": "once",
  "duration_in_months": null,
  "livemode": false,
  "max_redemptions": null,
  "metadata": {
  },
  "name": null,
  "percent_off": null,
  "percent_off_precise": null,
  "redeem_by": null,
  "times_redeemed": 4,
  "valid": true
}

but I can not figure out if the customer has already used this coupon code earlier or not. I tried to pass customer id as well as below:

\Stripe\Stripe::setApiKey($this->config->item('stripe_secret_key'));
$couponData = \Stripe\Coupon::retrieve($coupon, ['customer' => $stripe_customer_id]);

but it shares the same response.

Can anyone please help me in this, thanks!

Stripe doesn't tell you whether the Coupon has already been used by a customer before or not. This is something you will need to track on your end in your own database instead every time a customer redeems a Coupon.

As @koopajah mentions details on customer redemptions as see in Stripe coupon dashboard under Active Redemptions are not available via the API.

From stripe mailing list :

This is not something that we support via the API at the moment. You would need to keep track of coupon redemptions on your end if you want to get the list and number of customers that have claimed those. You can keep track of redemptions via webhooks.

This would apply to one-off coupons however recurring coupons are available via discount attributes for Stripe customer and subscription objects:

The Discount object contains details of the coupon or promotion code in use.

Therefore should be possible to parse a Stripe customer or their subscription for any applied coupons.

If using promotion codes there are limits that can be applied which Stripe would check to stop the coupon code being used again.

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