I am trying to apply a coupon to a user’s subscription upon creation.
To do this I am using the following code:
$user = Auth::user();
if(!empty($request->coupon)){
$user->subscription('monthly')->withCoupon($request->coupon)->create($request->stripeToken);
} else {
$user->subscription('monthly')->create($request->stripeToken);
}
I have checked the value of $request->coupon
and it contains my coupon. The coupon exists and is in Stripe (this is validated programatically so I know the coupon is OK).
The subscription is created within Stripe, but billed at the full amount, not taking into account the coupon.
The coupon is in the test environment, as is the website—the transaction comes through in the test environment too.
Checking if an object property is set using the empty
function will not have the desired effect, because the class would need to have the __isset
magic method defined for empty
to work on inaccesible properties.
That aside, the Http\Illuminate\Request
class uses __get
to access input parameters, which means that __isset
wouldn't even work in this case, because the coupon
property is not really defined within the class. So your condition almost surely goes to the else
block with your current code (even though you have the coupon
input parameter present).
That's why the method has
exists, to determine if the request contains a non-empty value for an input item. So use this instead:
if ($request->has('coupon'))