From 712e05ef69c3129a88eae4fc7bea4ad8f3b4c31b Mon Sep 17 00:00:00 2001 From: mahbub-zip <62676065+mahbub-zip@users.noreply.github.com> Date: Wed, 9 Jun 2021 13:58:12 +1000 Subject: [PATCH 1/4] cancel payment and order when charge amount is not same as order grand total amount --- Model/Charge.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Model/Charge.php b/Model/Charge.php index 68a5d55..6a481b2 100644 --- a/Model/Charge.php +++ b/Model/Charge.php @@ -153,7 +153,12 @@ public function charge() if (!$charge->getState() || !$charge->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Charge')); } - + if ($charge->getAmount() != $this->_order->getGrandTotal()) { + $messageWithAmount = $this->_helper->__("Charge Amount is not same as Order amount. Charge amount:- %s and order amount :-%s", $charge->getAmount(),$this->_order->getGrandTotal()); + $this->_logger->debug($messageWithAmount); + $this->_helper->cancelOrder($this->_order, $messageWithAmount); + throw new \Magento\Framework\Exception\LocalizedException(__('Could not process the payment.')); + } $this->_logger->debug($this->_helper->__("Charge State:- %s", $charge->getState())); if ($charge->getId()) { From 02c22b0cf136840efe0ee2234f8269a66b1e30b1 Mon Sep 17 00:00:00 2001 From: mahbub-zip <62676065+mahbub-zip@users.noreply.github.com> Date: Tue, 15 Jun 2021 18:17:18 +1000 Subject: [PATCH 2/4] refund the charged amount if charge amount is not same as order amount and throwing payment process error. --- Model/Charge.php | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Model/Charge.php b/Model/Charge.php index 6a481b2..d416afb 100644 --- a/Model/Charge.php +++ b/Model/Charge.php @@ -65,6 +65,8 @@ class Charge extends AbstractCheckout */ protected $_dataObjectHelper; + protected $_refundApi = null; + /** * Set quote and config instances * @@ -90,6 +92,7 @@ public function __construct( \Zip\ZipPayment\Helper\Data $helper, \Zip\ZipPayment\Model\Config $config, \Zip\ZipPayment\MerchantApi\Lib\Api\ChargesApi $chargesApi, + \Zip\ZipPayment\MerchantApi\Lib\Api\RefundsApi $refundsApi, array $data = [] ) { $this->_quoteManagement = $cartManagement; @@ -103,6 +106,7 @@ public function __construct( $this->_objectCopyService = $objectCopyService; $this->_dataObjectHelper = $dataObjectHelper; $this->_api = $chargesApi; + $this->_refundApi = $refundsApi; parent::__construct( $customerSession, @@ -153,12 +157,7 @@ public function charge() if (!$charge->getState() || !$charge->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Charge')); } - if ($charge->getAmount() != $this->_order->getGrandTotal()) { - $messageWithAmount = $this->_helper->__("Charge Amount is not same as Order amount. Charge amount:- %s and order amount :-%s", $charge->getAmount(),$this->_order->getGrandTotal()); - $this->_logger->debug($messageWithAmount); - $this->_helper->cancelOrder($this->_order, $messageWithAmount); - throw new \Magento\Framework\Exception\LocalizedException(__('Could not process the payment.')); - } + $this->_logger->debug($this->_helper->__("Charge State:- %s", $charge->getState())); if ($charge->getId()) { @@ -170,6 +169,30 @@ public function charge() $this->_orderPaymentRepository->save($payment); } + // checking charge amount is same as order grand total + if ($charge->getAmount() != $this->_order->getGrandTotal()) { + $messageWithAmount = $this->_helper->__("Charge Amount is not same as Order amount. Charge amount:- %s and order amount :-%s", $charge->getAmount(),$this->_order->getGrandTotal()); + $this->_logger->debug($messageWithAmount); + $this->_helper->cancelOrder($this->_order, $messageWithAmount); + $apiConfig = new \Zip\ZipPayment\MerchantApi\Lib\Configuration(); + $apiConfig->setApiKey('Authorization', $this->_config->getMerchantPrivateKey()) + ->setApiKeyPrefix('Authorization', 'Bearer') + ->setEnvironment($this->_config->getEnvironment()) + ->setPlatform("Magento/" . $this->_helper->getMagentoVersion() + . "Zip_ZipPayment/" + . $this->_helper->getExtensionVersion()); + $payload = $this->_payloadHelper->getRefundPayload( + $this->_order, + $charge->getAmount(), + $messageWithAmount + ); + // doing refund if the amount is captured by the charge api + $this->_refundApi->setApiClient(new \Zip\ZipPayment\MerchantApi\Lib\ApiClient($apiConfig)); + $refund = $this->_refundApi->refundsCreate($payload, $this->_helper->generateIdempotencyKey()); + $this->_logger->debug("Refund Response:- " . $this->_helper->jsonEncode($refund)); + throw new \Magento\Framework\Exception\LocalizedException(__('Could not process the payment.')); + } + $this->_chargeResponse($charge, $this->_config->isCharge()); } catch (\Zip\ZipPayment\MerchantApi\Lib\ApiException $e) { list($apiError, $message, $logMessage) = $this->_helper->handleException($e); From f860684b70d35170d2027e4b6a7530b788f51b94 Mon Sep 17 00:00:00 2001 From: mahbub-zip <62676065+mahbub-zip@users.noreply.github.com> Date: Fri, 18 Jun 2021 09:12:19 +1000 Subject: [PATCH 3/4] added new region --- MerchantApi/Lib/Model/CommonUtil.php | 4 ++++ Model/Config/HealthCheck.php | 2 ++ 2 files changed, 6 insertions(+) diff --git a/MerchantApi/Lib/Model/CommonUtil.php b/MerchantApi/Lib/Model/CommonUtil.php index 4c2915b..4e97e7e 100644 --- a/MerchantApi/Lib/Model/CommonUtil.php +++ b/MerchantApi/Lib/Model/CommonUtil.php @@ -24,6 +24,8 @@ class CommonUtil const SOUTH_AFRICA = 'za'; const UNITED_STATE = 'us'; const MEXICO = 'mx'; + const CZECH_REPUBLIC = 'cz'; + const POLAND = 'pl'; // Zip widget will display inside the iframe const IFRAME = 'iframe'; @@ -75,6 +77,8 @@ public static function getAvailableRegionList() ['value' => self::SOUTH_AFRICA, 'label' => __('South Africa')], ['value' => self::UNITED_STATE, 'label' => __('United State')], ['value' => self::MEXICO, 'label' => __('Mexico')], + ['value' => self::CZECH_REPUBLIC, 'label' => __('Czech Republic')], + ['value' => self::POLAND, 'label' => __('Poland')], ]; } diff --git a/Model/Config/HealthCheck.php b/Model/Config/HealthCheck.php index aabfaa7..c269d2b 100644 --- a/Model/Config/HealthCheck.php +++ b/Model/Config/HealthCheck.php @@ -39,6 +39,8 @@ class HealthCheck 'ae' => 'United Arab Emirate', 'mx' => 'Mexico', 'sa' => 'Saudi Arabia', + 'cz' => 'Czech Republic', + 'pl' => 'Poland', ]; /** From 7b5772cdc98eb0c45a0daee1cdcad0a331b569be Mon Sep 17 00:00:00 2001 From: mahbub-zip <62676065+mahbub-zip@users.noreply.github.com> Date: Wed, 23 Jun 2021 15:14:21 +1000 Subject: [PATCH 4/4] added missing country --- MerchantApi/Lib/Model/CommonUtil.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MerchantApi/Lib/Model/CommonUtil.php b/MerchantApi/Lib/Model/CommonUtil.php index 4e97e7e..7f3d5e2 100644 --- a/MerchantApi/Lib/Model/CommonUtil.php +++ b/MerchantApi/Lib/Model/CommonUtil.php @@ -26,6 +26,7 @@ class CommonUtil const MEXICO = 'mx'; const CZECH_REPUBLIC = 'cz'; const POLAND = 'pl'; + const UNITED_ARAB_EMIRATES = 'ae'; // Zip widget will display inside the iframe const IFRAME = 'iframe'; @@ -79,6 +80,7 @@ public static function getAvailableRegionList() ['value' => self::MEXICO, 'label' => __('Mexico')], ['value' => self::CZECH_REPUBLIC, 'label' => __('Czech Republic')], ['value' => self::POLAND, 'label' => __('Poland')], + ['value' => self::UNITED_ARAB_EMIRATES, 'label' => __('United Arab Emirates')], ]; }