Skip to content

Commit

Permalink
Coupons@redeem: Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
alexblunck committed Nov 15, 2017
1 parent d5154a4 commit 2db894c
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/Coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,27 @@ public function check($code, $user = null)
}

/**
* Redeem a given coupon code for a specific user.
* Redeem a given coupon for a specific user.
*
* @param Coupon $code
* @param Coupon $coupon
* @param User $user
*
* @return Coupon|false
*/
public function redeem(Coupon $code, $user)
public function redeem(Coupon $coupon, $user)
{
try {
$coupon = $this->check($code);

// Check if coupon is valid
if ($coupon) {
// Check if user has already used coupon
if ($this->isSecondUsageAttempt($coupon, $user)) {
throw new AlreadyUsedCouponException();
}

$coupon->users()->attach($user->id, [
'used_at' => Carbon::now(),
]);

return $coupon;
}
} catch (InvalidCouponCodeException $exception) {
$coupon = $this->check($coupon->code);

// Create pivot table record
$coupon->users()->attach($user->id, [
'used_at' => Carbon::now(),
]);
} catch (CouponException $e) {
return false;
}

return false;
return $coupon;
}

/**
Expand Down

0 comments on commit 2db894c

Please sign in to comment.