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.
source share