I'm using the Stripe API like so:
$coupon = $_POST["coupon"];
$subscription = \Stripe\Subscription::create(array(
"customer" => $customer->id,
"plan" => $plan,
"coupon" => $coupon,
));
This works great when $coupon is set and it's a valid coupon code. If I leave it blank (i.e., customer didn't include a coupon code in form submitted), Stripe throws an error. If I include a coupon code that doesn't exist in Stripe, it throws an error.
So, the question is, how do I build this array to NOT have the coupon
key when the variable $coupon
is empty?