Skip to content

Commit

Permalink
Merge pull request #4 from prestonchoate/feature/add-before-plugin-fo…
Browse files Browse the repository at this point in the history
…r-save-payment-info-function

Feature/add before plugin for save payment info function
  • Loading branch information
prestonchoate authored Jan 28, 2022
2 parents 5833af0 + b1399dc commit 1c88916
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nohup.out
.idea
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 1.1.0
* Extracted call for checking cart to its own function
* Changed type of exception that is thrown to prevent redirects
15 changes: 12 additions & 3 deletions Plugin/Magento/Checkout/Model/CardingPreventionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace PrestonChoate\CardingPrevention\Plugin\Magento\Checkout\Model;
use PrestonChoate\CardingPrevention\Model\Config;
use Magento\Checkout\Exception as CheckoutException;
use Magento\Checkout\Model\GuestPaymentInformationManagement;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Exception\AuthorizationException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;

Expand Down Expand Up @@ -41,14 +41,23 @@ public function __construct(
* @param $email
* @param PaymentInterface $paymentMethod
* @param AddressInterface|null $billingAddress
* @throws AuthorizationException
*/
public function beforeSavePaymentInformationAndPlaceOrder(
GuestPaymentInformationManagement $subject,
$cartId,
$email,
PaymentInterface $paymentMethod,
AddressInterface $billingAddress = null)
{
$this->checkCartId($cartId);
}

/**
* Checks cart ID and throws exception if it has been used too frequently
*
* @param $cartId
*/
protected function checkCartId($cartId)
{
$lifetime = $this->config->getThreshold();
$lifetime = ctype_digit($lifetime) ? intval($lifetime) : 0;
Expand All @@ -62,7 +71,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
$value = $this->cache->load($cartId);
$this->cache->save($data, $cartId, $tags, $lifetime);
if ($value) {
throw new AuthorizationException(__('Too many requests'));
throw new CheckoutException(__('Too many requests'));
}

}
Expand Down

0 comments on commit 1c88916

Please sign in to comment.