Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MerchantApi/Lib/Model/CommonUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CommonUtil
const SOUTH_AFRICA = 'za';
const UNITED_STATE = 'us';
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';
Expand Down Expand Up @@ -75,6 +78,9 @@ 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')],
['value' => self::UNITED_ARAB_EMIRATES, 'label' => __('United Arab Emirates')],
];
}

Expand Down
28 changes: 28 additions & 0 deletions Model/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Charge extends AbstractCheckout
*/
protected $_dataObjectHelper;

protected $_refundApi = null;

/**
* Set quote and config instances
*
Expand All @@ -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;
Expand All @@ -103,6 +106,7 @@ public function __construct(
$this->_objectCopyService = $objectCopyService;
$this->_dataObjectHelper = $dataObjectHelper;
$this->_api = $chargesApi;
$this->_refundApi = $refundsApi;

parent::__construct(
$customerSession,
Expand Down Expand Up @@ -165,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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a check that the charge has been made? as this looks like will call refund irrespective? Is this the correct/safe behavior?

$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);
Expand Down
2 changes: 2 additions & 0 deletions Model/Config/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class HealthCheck
'ae' => 'United Arab Emirate',
'mx' => 'Mexico',
'sa' => 'Saudi Arabia',
'cz' => 'Czech Republic',
'pl' => 'Poland',
];

/**
Expand Down