diff --git a/src/Coupons/Fixed.php b/src/Coupons/Fixed.php index 8b25ef3..ad62c3e 100644 --- a/src/Coupons/Fixed.php +++ b/src/Coupons/Fixed.php @@ -43,12 +43,13 @@ public function __construct($code, $value, $options = []) public function discount($throwErrors = false) { $subTotal = app(LaraCart::SERVICE)->subTotal(false); - $total = $subTotal - $this->value; if (config('laracart.discountOnFees', false)) { - $total = $subTotal + app(LaraCart::SERVICE)->feeTotals(false) - $this->value; + $subTotal = $subTotal + app(LaraCart::SERVICE)->feeTotals(false); } + $total = $subTotal - $this->value; + if ($total < 0) { return $subTotal; } diff --git a/src/Coupons/Percentage.php b/src/Coupons/Percentage.php index d25ee29..1ba5327 100644 --- a/src/Coupons/Percentage.php +++ b/src/Coupons/Percentage.php @@ -4,6 +4,7 @@ use LukePOLO\LaraCart\CartItem; use LukePOLO\LaraCart\Contracts\CouponContract; +use LukePOLO\LaraCart\Exceptions\CouponException; use LukePOLO\LaraCart\LaraCart; use LukePOLO\LaraCart\Traits\CouponTrait; @@ -27,10 +28,14 @@ class Percentage implements CouponContract * @param $code * @param $value * @param array $options + * @throws \Exception */ public function __construct($code, $value, $options = []) { $this->code = $code; + if($value > 1) { + throw new CouponException('Invalid value for a percentage coupon. The value must be between 0 and 1.'); + } $this->value = $value; $this->setOptions($options); @@ -62,8 +67,13 @@ public function forItem(CartItem $item) */ public function discount($throwErrors = false) { + $subTotal = app(LaraCart::SERVICE)->subTotal(false); + if (config('laracart.discountOnFees', false)) { + $subTotal = $subTotal + app(LaraCart::SERVICE)->feeTotals(false); + } + return LaraCart::formatMoney( - app(LaraCart::SERVICE)->subTotal(false) * $this->value, + $subTotal * $this->value, null, null, false diff --git a/tests/CouponsTest.php b/tests/CouponsTest.php index ba7fd3c..454b1cd 100644 --- a/tests/CouponsTest.php +++ b/tests/CouponsTest.php @@ -16,8 +16,17 @@ public function testAddPercentageCoupon() { $this->addItem(3, 1); + try { + $percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('10%OFF', '23'); + $this->setExpectedException(\LukePOLO\LaraCart\Exceptions\CouponException::class); + } catch (\LukePOLO\LaraCart\Exceptions\CouponException $e) { + $this->assertEquals('Invalid value for a percentage coupon. The value must be between 0 and 1.', $e->getMessage()); + } + $percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('10%OFF', '.1'); + + $this->laracart->addCoupon($percentCoupon); $this->assertEquals($percentCoupon, $this->laracart->findCoupon('10%OFF')); @@ -426,5 +435,13 @@ public function testFixedCouponWithFeeWithTotalLessThanCoupon() $this->app['config']->set('laracart.discountOnFees', true); $this->assertEquals('500', $fixedCoupon->discount()); + + + $this->app['config']->set('laracart.discountOnFees', true); + + $percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('100% Off', 1); + $this->laracart->addCoupon($percentCoupon); + + $this->assertEquals(0, $this->laracart->total(false)); } }