From b66997c77005a57590c14d341ee0b49d3deaf846 Mon Sep 17 00:00:00 2001 From: Dinwy Date: Fri, 7 Jun 2024 13:16:36 +0900 Subject: [PATCH 1/2] Prototype of refund --- .../Controller/HostedPage/Webhook.php | 4 ++-- src/app/code/Komoju/Payments/Model/Refund.php | 7 ++++-- .../Model/ResourceModel/Refund/Collection.php | 16 ++++++++------ .../Payments/Plugin/WebhookEventProcessor.php | 22 ++++++++++++++----- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/app/code/Komoju/Payments/Controller/HostedPage/Webhook.php b/src/app/code/Komoju/Payments/Controller/HostedPage/Webhook.php index 73b81bf47..dc7d5281d 100644 --- a/src/app/code/Komoju/Payments/Controller/HostedPage/Webhook.php +++ b/src/app/code/Komoju/Payments/Controller/HostedPage/Webhook.php @@ -166,8 +166,8 @@ public function validateForCsrf(RequestInterface $request): ?bool * If it can't find a matching Order then we're assuming that the order belongs * to a separate system and ignoring any events sent for it. * @var string $externalOrderNum - * @return Magento\Sales\Model\Order - * @throws Magento\Framework\Exception\NoSuchEntityException + * @return \Magento\Sales\Api\Data\OrderInterface + * @throws \Magento\Framework\Exception\NoSuchEntityException */ private function getOrder($externalOrderNum) { diff --git a/src/app/code/Komoju/Payments/Model/Refund.php b/src/app/code/Komoju/Payments/Model/Refund.php index e2a58df69..9cd14e795 100644 --- a/src/app/code/Komoju/Payments/Model/Refund.php +++ b/src/app/code/Komoju/Payments/Model/Refund.php @@ -1,6 +1,9 @@ _init(\Komoju\Payments\Model\ResourceModel\Refund::class); + $this->_init(RefundResourceModel::class); } } diff --git a/src/app/code/Komoju/Payments/Model/ResourceModel/Refund/Collection.php b/src/app/code/Komoju/Payments/Model/ResourceModel/Refund/Collection.php index 742481d09..9cf0c7454 100644 --- a/src/app/code/Komoju/Payments/Model/ResourceModel/Refund/Collection.php +++ b/src/app/code/Komoju/Payments/Model/ResourceModel/Refund/Collection.php @@ -1,13 +1,16 @@ _init(\Komoju\Payments\Model\Refund::class, Komoju\Payments\Model\ResourceModel\Refund::class); + $this->_init(RefundModel::class, RefundResourceModel::class); } /** * Returns the record that matches the $refundId. If there's no matching * record then we assume that the refund has not been processed yet. * @param string $refundId - * @return Komoju\Payments\Model\Refund|null + * @return DataObject|null */ public function getRecordForRefundId($refundId) { - $collection = $this - ->addFieldToFilter('refund_id', ['eq' => $refundId]); + $collection = $this->addFieldToFilter('refund_id', ['eq' => $refundId]); if ($collection->getSize() < 1) { return null; diff --git a/src/app/code/Komoju/Payments/Plugin/WebhookEventProcessor.php b/src/app/code/Komoju/Payments/Plugin/WebhookEventProcessor.php index b969f5bbd..59349b82c 100644 --- a/src/app/code/Komoju/Payments/Plugin/WebhookEventProcessor.php +++ b/src/app/code/Komoju/Payments/Plugin/WebhookEventProcessor.php @@ -75,7 +75,10 @@ public function processEvent() if ($this->webhookEvent->eventType() == 'payment.captured') { $paymentAmount = $this->webhookEvent->amount(); $currentTotalPaid = $this->order->getTotalPaid(); + $baseTotalPaid = $this->order->getBaseTotalPaid(); + $this->order->setTotalPaid($paymentAmount + $currentTotalPaid); + $this->order->setBaseTotalPaid($paymentAmount + $baseTotalPaid); $this->order->setState(Order::STATE_PROCESSING); $this->order->setStatus(Order::STATE_PROCESSING); @@ -115,9 +118,6 @@ public function processEvent() $this->order->addStatusHistoryComment($statusHistoryComment); $this->order->save(); } elseif ($this->webhookEvent->eventType() == 'payment.refunded') { - $refundedAmount = $this->webhookEvent->amountRefunded(); - $refundCurrency = $this->webhookEvent->currency(); - $statusHistoryComment = $this->prependExternalOrderNum(__('Order has been fully refunded.')); $this->order->setState(Order::STATE_COMPLETE); @@ -125,12 +125,9 @@ public function processEvent() $this->order->addStatusHistoryComment($statusHistoryComment); $this->order->save(); } elseif ($this->webhookEvent->eventType() == 'payment.refund.created') { - $grandTotal = $this->order->getBaseGrandTotal(); $totalAmountRefunded = $this->webhookEvent->amountRefunded(); $refundCurrency = $this->webhookEvent->currency(); - $this->order->setTotalRefunded($totalAmountRefunded); - $refunds = $this->webhookEvent->getRefunds(); $refundsToProcess = []; @@ -151,8 +148,14 @@ public function processEvent() __('Refund for order created. Amount: %1 %2', $refundedAmount, $refundCurrency) ); + // Create a credit memo for the refund $creditmemo = $this->creditmemoFactory->createByOrder($this->order); + + $creditmemo->setShippingAmount(0); $creditmemo->setSubtotal($refundedAmount); + $creditmemo->setGrandTotal($refundedAmount); + $creditmemo->setBaseGrandTotal($refundedAmount); + $creditmemo->addComment($statusHistoryComment); $this->creditmemoService->refund($creditmemo, true); @@ -174,6 +177,13 @@ public function processEvent() // $creditmemo->setBaseGrandTotal($refundAmount); // $creditmemo->setGrandTotal($refundAmount * $baseToOrderRate); + $this->order->setTotalRefunded($totalAmountRefunded); + + if ($this->order->getBaseGrandTotal() == $totalAmountRefunded) { + $this->order->setState(Order::STATE_COMPLETE); + $this->order->setStatus(Order::STATE_COMPLETE); + } + $this->order->save(); } else { throw new UnknownEventException(__('Unknown event type: %1', $this->webhookEvent->eventType())); From 9e7d502d86e16b81bf407745380ca240f814577a Mon Sep 17 00:00:00 2001 From: Dinwy Date: Fri, 7 Jun 2024 14:17:31 +0900 Subject: [PATCH 2/2] Add missing customer name and email --- src/app/code/Komoju/Payments/Controller/HostedPage/Redirect.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/code/Komoju/Payments/Controller/HostedPage/Redirect.php b/src/app/code/Komoju/Payments/Controller/HostedPage/Redirect.php index c1469c3d8..228f1fa74 100644 --- a/src/app/code/Komoju/Payments/Controller/HostedPage/Redirect.php +++ b/src/app/code/Komoju/Payments/Controller/HostedPage/Redirect.php @@ -125,7 +125,9 @@ private function createHostedPageUrl() 'return_url' => $returnUrl, 'default_locale' => $this->config->getKomojuLocale(), 'payment_types' => [$paymentMethod], + 'email' => $order->getCustomerEmail(), 'payment_data' => [ + 'name' => $order->getCustomerName(), 'amount' => $order->getGrandTotal(), 'currency' => $currencyCode, 'external_order_num' => $externalOrderNum,