Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APF-397 adds detection of invalid coupon code #1210

Open
wants to merge 2 commits into
base: spc
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 51 additions & 5 deletions Model/Spc/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
use Magento\Framework\Phrase;
use Magento\Quote\Api\CartRepositoryInterface;
use Amazon\Pay\Helper\Spc\Cart;
use Magento\SalesRule\Api\Data\CouponInterface as SalesRuleCouponInterface;
use Magento\Store\Api\Data\StoreInterface;

class Coupon implements CouponInterface
{
const NOT_APPLICABLE = 'CouponNotApplicable';

const INVALID = 'InvalidCouponCode';

/**
* @var StoreInterface
*/
Expand All @@ -33,23 +38,32 @@ class Coupon implements CouponInterface
*/
protected $checkoutSessionHelper;

/**
* @var SalesRuleCouponInterface
*/
protected $salesRuleCoupon;


/**
* @param StoreInterface $store
* @param CartRepositoryInterface $cartRepository
* @param Cart $cartHelper
* @param CheckoutSession $checkoutSessionHelper
* @param SalesRuleCouponInterface $salesRuleCoupon
*/
public function __construct(
StoreInterface $store,
CartRepositoryInterface $cartRepository,
Cart $cartHelper,
CheckoutSession $checkoutSessionHelper
CheckoutSession $checkoutSessionHelper,
SalesRuleCouponInterface $salesRuleCoupon
)
{
$this->store = $store;
$this->cartRepository = $cartRepository;
$this->cartHelper = $cartHelper;
$this->checkoutSessionHelper = $checkoutSessionHelper;
$this->salesRuleCoupon = $salesRuleCoupon;
}

/**
Expand Down Expand Up @@ -84,7 +98,18 @@ public function applyCoupon(int $cartId, $cartDetails = null)

$couponCode = $cartDetails['coupons'][0]['coupon_code'];

// Empty out the quote items' rule ids, because of Magento bug
// Check if coupon exists
if (!$this->couponExists($couponCode)) {
$this->cartHelper->logError(
'SPC Coupon: '. self::INVALID .' - The coupon '. $couponCode .' is invalid. CartId: ' . $cartId . ' - ', $cartDetails
);

throw new \Magento\Framework\Webapi\Exception(
new Phrase("The coupon code '". $couponCode ."' is invalid"), self::INVALID, 400
);
}

// Empty out the quote items' rule ids, because Magento does not
foreach ($quote->getItems() as &$item) {
$item->setAppliedRuleIds(null);
}
Expand All @@ -107,18 +132,18 @@ public function applyCoupon(int $cartId, $cartDetails = null)
}

$this->cartHelper->logError(
'SPC Coupon: CouponNotApplicable - The coupon '. $couponCode .' could not be applied to the cart. CartId: ' . $cartId . ' - ', $cartDetails
'SPC Coupon: '. self::NOT_APPLICABLE .' - The coupon '. $couponCode .' could not be applied to the cart. CartId: ' . $cartId . ' - ', $cartDetails
);

throw new \Magento\Framework\Webapi\Exception(
new Phrase("The coupon code '". $couponCode ."' does not apply"), "CouponNotApplicable", 400
new Phrase("The coupon code '". $couponCode ."' does not apply"), self::NOT_APPLICABLE, 400
);
}
}
else {
if (!isset($cartDetails['coupons'][0]['coupon_code']) || $cartDetails['coupons'][0]['coupon_code'] === null) {
throw new \Magento\Framework\Webapi\Exception(
new Phrase("Coupon code is missing"), "CouponNotApplicable", 400
new Phrase("Coupon code is missing"), self::NOT_APPLICABLE, 400
);
}
}
Expand All @@ -132,4 +157,25 @@ public function applyCoupon(int $cartId, $cartDetails = null)

return $this->cartHelper->createResponse($quote->getId(), $checkoutSessionId);
}

/**
* @param $couponCode
* @return bool
*/
protected function couponExists($couponCode)
{
try {
$coupon = $this->salesRuleCoupon->loadByCode($couponCode);

if ($coupon->getRuleId()) {
return true;
}

}
catch (\Exception $e) {
return false;
}

return false;
}
}