From 0bd8db127f0f2bf9f1e141318b67942192563d70 Mon Sep 17 00:00:00 2001 From: Ram Prakash Singh <45222925+ramth05@users.noreply.github.com> Date: Fri, 17 Sep 2021 10:28:54 +0530 Subject: [PATCH] Added Refund through credit memo (#272) * added Refund through credit memo --- Model/PaymentMethod.php | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Model/PaymentMethod.php b/Model/PaymentMethod.php index 03fb893e..98a9c6bf 100644 --- a/Model/PaymentMethod.php +++ b/Model/PaymentMethod.php @@ -486,6 +486,50 @@ protected function getPostData() */ public function refund(InfoInterface $payment, $amount) { + $order = $payment->getOrder(); + + $creditmemo = $this->request->getPost('creditmemo'); + + $reason = (!empty($creditmemo['comment_text'])) ? $creditmemo['comment_text'] : 'Refunded by site admin'; + + $refundId = $payment->getTransactionId(); + + $paymentId = substr($refundId, 0, -7); + + try + { + $data = array( + 'amount' => (int) round($amount * 100), + 'receipt' => $order->getIncrementId(), + 'notes' => array( + 'reason' => $reason, + 'order_id' => $order->getIncrementId(), + 'refund_from_website' => true, + 'source' => 'Magento', + ) + ); + + $refund = $this->rzp->payment + ->fetch( $paymentId ) + ->refund( $data ); + + $payment->setAmountPaid($amount) + ->setLastTransId($refund->id) + ->setTransactionId($refund->id) + ->setIsTransactionClosed(true) + ->setShouldCloseParentTransaction(true); + } + catch(\Razorpay\Api\Errors\Error $e) + { + $this->_logger->critical($e); + throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage())); + } + catch(\Exception $e) + { + $this->_logger->critical($e); + throw new LocalizedException(__('Razorpay Error: %1.', $e->getMessage())); + } + return $this; }