I am using PHP and have a webhook generated with invoice.created
. When the invoice is created, Stripe lets the draft invoice sit there for about an hour which gives us time to edit the invoice or adjust it. I use this time to check the database (via webhook) to see how many referrals this person has in the system to the adjust the coupons accordingly. I can’t seem to adjust the invoice properly as I get errors in all the ways I have tried, which are:
$inv = {the invoice id and I get no error from the invoice ID being incorrect
define('STRIPE_API_KEY', 'sk_test_******************'); $stripe = new StripeStripeClient( STRIPE_API_KEY ); $stripe->invoices->update( $inv, ['discounts.coupon' => '20OFF'] );
Error is unknown object discounts.coupon
and
$stripe->invoices->update( $inv, ['discount' => '20OFF'] );
Error = Fatal error: Uncaught (Status 400) (Request req_hsX1QTOxzWMiQn) Received unknown parameter: discount. Did you mean discounts? thrown in /home/searchoscn/public_html/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38
and
$stripe->invoices->update( $inv, ['coupon' => '20OFF'] );
ERROR = Fatal error: Uncaught (Status 400) (Request req_yEwwOLoukR6j6Z) Received unknown parameter: coupon thrown in /home/searchoscn/public_html/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38
$stripe->invoices->update( $inv, ['discounts' => ['coupon' => '20OFF']] );
Fatal error: Uncaught (Status 400) (Request req_IDRP1Rjv1YoBZR) Invalid array thrown in /home/searchoscn/public_html/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38
I’m pretty sure I need to use ‘discounts’ but don’t know how to structure the array to pass the coupon properly.
Trying to predict some questions or comments:
Yes, I do have an existing coupon with id of 20OFF. This is for 20% off
$inv does fetch the correct ID of the invoice at the time the webhook is sent.
Here is the link of the API documentation that I’ve been reading, but it isn’t very helpful.
Advertisement
Answer
Instead of:
$stripe->invoices->update( $inv, ['discounts.coupon' => '20OFF'] );
try:
$stripe->invoices->update( $inv, ['discounts' => ['coupon' => $couponId]] );
It looks like it must be the id of the coupon and not the customer facing code, e.g. 20OFF
.
You would be able to get the id of the coupon from the dashboard (https://dashboard.stripe.com/coupons/) or by making another api request.