From 0cf8348580ee3c64fd4735df43cabec119520d34 Mon Sep 17 00:00:00 2001 From: Zain ul abidin Date: Thu, 24 May 2018 13:54:13 +0930 Subject: [PATCH 1/3] refund: basic refund implemented online transactions are detected by payment information sent by magneto --- Model/Payment.php | 76 ++++++++++++++++++++++++++++++++++++++++++----- Model/Result.php | 7 ++++- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/Model/Payment.php b/Model/Payment.php index d2a8114..7c26dd9 100644 --- a/Model/Payment.php +++ b/Model/Payment.php @@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface; use Magento\Quote\Api\Data\PaymentInterface; use Aligent\Pinpay\Helper\Pinpay as PinHelper; +use Magento; /** * @@ -37,6 +38,8 @@ class Payment implements MethodInterface const REQUEST_TYPE_CAPTURE_ONLY = 'CAPTURE_ONLY'; + const REQUEST_TYPE_REFUND = 'REFUND'; + /** * @var string */ @@ -186,8 +189,7 @@ public function canCaptureOnce() */ public function canRefund() { - // TODO: Implement canRefund() method. - return false; + return true; } /** @@ -354,6 +356,10 @@ public function getClient($payment, $order, $amount, $transactionType = self::RE $endpoint .= '/' . $payment->getCcTransId() . '/capture'; $method = \Zend_Http_Client::PUT; } + elseif ($transactionType == static::REQUEST_TYPE_REFUND){ + $endpoint .= '/' . $payment->getData('refund_transaction_id') . '/refunds'; + $method = \Zend_Http_Client::POST; + } $client->setAuth($this->getConfigData('secret_key', $order->getStoreId())); $client->setConfig(['maxredirects' => 0, 'timeout' => 30]); @@ -366,7 +372,11 @@ public function getClient($payment, $order, $amount, $transactionType = self::RE */ if ($transactionType === self::REQUEST_TYPE_CAPTURE_ONLY) { $data = ['amount' => $this->_pinHelper->getRequestAmount($order->getBaseCurrencyCode(), $amount)]; - } else { + } + elseif ($transactionType === self::REQUEST_TYPE_REFUND){ + $data = ['amount' => $this->_pinHelper->getRequestAmount($order->getBaseCurrencyCode(), $amount)]; + } + else { $capture = $transactionType === self::REQUEST_TYPE_AUTH_CAPTURE; $data = $this->_buildAuthRequest($order, $payment, $amount, $capture); } @@ -461,6 +471,11 @@ protected function _handleResponse($response, $payment) } elseif ($error) { throw new LocalizedException(__($result->getErrorDescription())); } + //success is set to null in response but approved (like in pending credit memo) + elseif($result->isApproved()){ + $payment->setCcTransId($result->getToken()); + $payment->setTransactionId($result->getToken()); + } } /** @@ -488,14 +503,61 @@ protected function _buildAuthRequest($order, $payment, $amount, $capture = true) 'capture' => $capture ]; } + /** + * @inheritDoc + */ /** * @inheritDoc */ - public function refund(\Magento\Payment\Model\InfoInterface $payment, $amount) + public function refund(Magento\Payment\Model\InfoInterface $payment, $amount) { - // TODO: Implement refund() method. - return $this; + if ($amount <= 0) { + $this->_logger->error('Expected amount for transaction is zero or below'); + throw new LocalizedException(__("Invalid payment amount.")); + } + /** + * @var $payment Magento\Sales\Api\Data\OrderPaymentInterface + */ + if (!($payment instanceof Magento\Sales\Api\Data\OrderPaymentInterface)){ + throw new LocalizedException(__("refund payment needs to be instance of Magento\Sales\Api\Data\OrderPaymentInterface")); + } + if (!$this->canRefund()) { + throw new LocalizedException (__('Refund action is not available.')); + } + /* Rounding error may occur, checking if the differences is not less than 0.5c */ + if ($amount - $payment->getAmountPaid() - $payment->getAmountRefunded() >= 0.005) { + throw new LocalizedException(__("Invalid refund amount")); + } + + $order = null; + $online = null; + if ($payment instanceof Magento\Sales\Model\Order\Payment){ + $order = $payment->getOrder(); + $creditMemo = $payment->getCreditmemo(); + if ($creditMemo){ + $online = $creditMemo->getDoTransaction(); + } + } + $online = is_null($online) ? $this->isOffline() : $online; + if (!$online) { + $payment->setCcTransId($payment->getAdditionalInformation('reference_number')); + $payment->setTransactionId($payment->getAdditionalInformation('reference_number')); + } + //online transaction + else { + $transactionType = self::REQUEST_TYPE_REFUND; + $client = $this->getClient($payment, $order, $amount, $transactionType); + + $response = null; + try { + $response = $client->request(); + $this->_handleResponse($response, $payment); + } catch (\Exception $e) { + $this->_logger->error("Payment Error: " . $e->getMessage()); + throw new LocalizedException(__($e->getMessage())); + } + } } /** @@ -618,4 +680,4 @@ public function getPaymentUrl($storeId = 0) return $isTest ? self::TEST_GATEWAY_URL : self::GATEWAY_URL; } -} \ No newline at end of file +} diff --git a/Model/Result.php b/Model/Result.php index b55cfe0..e3de2f9 100644 --- a/Model/Result.php +++ b/Model/Result.php @@ -67,6 +67,11 @@ public function getToken() return ''; } + + public function isApproved() + { + return $this->_responseCode === static::HTTP_RESPONSE_CODE_APPROVED; + } /** * @return string | null */ @@ -81,7 +86,7 @@ public function getCCType() * * @return mixed */ - protected function getResponseValue($vKey) + public function getResponseValue($vKey) { if (!isset($this->_response) || (!isset($this->_response->response)) || (!isset($this->_response->response->$vKey))){ return null; From 26eb94514460e06ac543c238bf05d365b771b915 Mon Sep 17 00:00:00 2001 From: Zain ul abidin Date: Thu, 24 May 2018 14:02:39 +0930 Subject: [PATCH 2/3] refund: fixed typo in code --- Model/Payment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Payment.php b/Model/Payment.php index 7c26dd9..b105d05 100644 --- a/Model/Payment.php +++ b/Model/Payment.php @@ -539,7 +539,7 @@ public function refund(Magento\Payment\Model\InfoInterface $payment, $amount) $online = $creditMemo->getDoTransaction(); } } - $online = is_null($online) ? $this->isOffline() : $online; + $online = is_null($online) ? !$this->isOffline() : $online; if (!$online) { $payment->setCcTransId($payment->getAdditionalInformation('reference_number')); $payment->setTransactionId($payment->getAdditionalInformation('reference_number')); From b66e3c0f23e743fecf08fb9be488adb176ec3ca2 Mon Sep 17 00:00:00 2001 From: Zain ul abidin Date: Thu, 24 May 2018 17:49:27 +0930 Subject: [PATCH 3/3] refund: return in refund() as required by interface --- Model/Payment.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Model/Payment.php b/Model/Payment.php index b105d05..fe8b9b0 100644 --- a/Model/Payment.php +++ b/Model/Payment.php @@ -503,10 +503,6 @@ protected function _buildAuthRequest($order, $payment, $amount, $capture = true) 'capture' => $capture ]; } - /** - * @inheritDoc - */ - /** * @inheritDoc */ @@ -558,6 +554,8 @@ public function refund(Magento\Payment\Model\InfoInterface $payment, $amount) throw new LocalizedException(__($e->getMessage())); } } + //required by interface + return $this; } /**