Using coupon codes with a strip

I have a website that uses Stripe to process subscription payments. There is only one type of subscription. I completed this tutorial on NetTuts to complete the initial setup. Had a form that handled subtle subscription processing, and it worked. Customer requested coupon code. Stripe supports this, so I intended to add a coupon code to an existing form.

I set the coupon codes in Stripe, set my test keys and switched to test mode in the strip. I am doing a couple of checks in my code:

  • Check if a coupon has been entered if you do not create a new customer object without a coupon option
  • Check if the coupon is valid if you do not return an error.

If a coupon has been entered and it is valid, then when creating a new client, pass the corresponding Stripe coupon object as an option.

if(isset($couponCode) && strlen($couponCode) > 0) { $using_discount = true; try { $coupon = Stripe_Coupon::retrieve($couponCode); if($coupon !== NULL) { $cCode = $coupon; } // if we got here, the coupon is valid } catch (Exception $e) { // an exception was caught, so the code is invalid $message = $e->getMessage(); returnErrorWithMessage($message); } } try { if($using_discount == true) { $customer = Stripe_Customer::create(array( "card" => $token, "plan" => "basic_plan", "email" => $email, "coupon" => $cCode )); } else { $customer = Stripe_Customer::create(array( "card" => $token, "plan" => "basic_plan", "email" => $email )); } 

$ couponCode is filled with the form field in the same way as all other fields, I checked three times that it is correctly stretched.

When I try to submit a form without a coupon code, it charges the entire amount and passes through Stripe correctly.

However, if I enter either a valid OR incorrect coupon code, it does not transfer the coupon object with the client object when creating a new client object and charges the full amount when passing through Stripe.

I looked at the code for hours and cannot understand why it always does not recognize the discount code and transfers the corresponding coupon object to Stripe.

+4
source share
2 answers

This is probably a bit outdated and you have found a solution for your code. But it seems that all you have to do is pass the original $ couponCode as the array value for the coupon. As codasaurus pointed out, you simply get the array back from the coupon strip that you don't need if you did not make $ cCode-> id and passed the identifier back to your array to create the client.

I changed when you set $ using_discount to true, as this will send a coupon code if the coupon was valid or not.

As soon as the coupon is valid, we send the coupon. You only need the value of your sent state, so the coupon is a link to a discount in the system. Or you can use $ coupon-> id if you want to create it this way.

Here is my approach to a solution based on your code, it might be better, but I hope this helps others find a solution like me.

 if($coupon!='' && strlen($coupon) > 0) { try { $coupon = Stripe_Coupon::retrieve($coupon); //check coupon exists if($coupon !== NULL) { $using_discount = true; //set to true our coupon exists or take the coupon id if you wanted to. } // if we got here, the coupon is valid } catch (Exception $e) { // an exception was caught, so the code is invalid $message = $e->getMessage(); returnErrorWithMessage($message); } } if($using_discount==true) { $customer = Stripe_Customer::create(array( "card" => $token, "plan" => $plan, "email" => $id, "coupon"=>$coupon) //original value input example: "75OFFMEMBER" stripe should be doing the rest. ); } else { $customer = Stripe_Customer::create(array( "card" => $token, "plan" => $plan, "email" => $id) ); } 
+4
source

Looking at the documentation https://stripe.com/docs/api#create_customer , calling Stripe_Customer :: create () looks only for the coupon code. It looks like you are going through the entire object of the coupon.

If your first attempt is not caught, $ couponCode already has your coupon code. In addition, there are several other checks that must be performed to determine if the coupon is valid. For example, if coupon-> times_redeemed <coupon-> max_redemptions or if coupon-> redeem_by passed, etc. You can also check if the customer uses the coupon by checking the customer discount object.

If any of these checks fail, simply set the value to $ using_discount = false;

+2
source

All Articles