user2997418 user2997418 - 2 months ago 20
PHP Question

Discount coupon with Stripe-PHP not working

I am new with Stripe, I have a payment form
enter image description here

I want to use discount Coupons, I created it already in my dashboard, and I added the coupon in my Customer::create but I see no discount in my Stripe dashboard, The full amount is charger (€18 as test here) also Redemption is not incremented!!!
HTML :

<input type="text" class="form-control" name="couponId" />


My Stripe dashboard:


enter image description here

enter image description here


Am I missing something ?

require_once('Stripe/init.php');
\Stripe\Stripe::setApiKey("sk_test_CRfnHrlBeZxCjk1eV5PgWSrj"); //Secret Key

$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => $loggedInUser->email,
'source' => $token,
'coupon' => $_POST['couponId']
));

$charge = \Stripe\Charge::create(array(
"amount" => $sprint['price'] * 100,
"currency" => "eur",
//"card" => $token,
'customer' => $customer->id,
"description" => $loggedInUser->user_id .': '. strftime("%a, %e %b %Y", date_create($sprint['date'])->getTimestamp()) . ' at: ' . $sprint['time'],
));


UPDATE

I used this :

$coupon = \Stripe\Coupon::retrieve($_POST['couponId']);
$customer = \Stripe\Customer::create(array(
'email' => $loggedInUser->email,
'source' => $token,
'coupon' => $coupon

));


I got it in my dashboard but not retrieved from the amount :

enter image description here

Answer

You only retrieved the coupon but didn't apply it to the amount. Try this :

            $token = $_POST['stripeToken'];
            $coupon = \Stripe\Coupon::retrieve($_POST['couponId']);
            $discount = $coupon->percent_off; //Get the percent of this coupon
            $customer = \Stripe\Customer::create(array(
                'email' => $loggedInUser->email,
                'source'  => $token,
                'coupon' => $coupon
            ));

            $charge =  \Stripe\Charge::create(array(
                "amount" => $sprint['price'] * 100 * (1-$discount/100), //apply the discount to the amount