Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Refund #16

Merged
merged 2 commits into from
Jun 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
7 changes: 5 additions & 2 deletions src/app/code/Komoju/Payments/Model/Refund.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Komoju\Payments\Model;

use Magento\Framework\Model\AbstractModel;
use Komoju\Payments\Model\ResourceModel\Refund as RefundResourceModel;

/**
* The Refund model is for programmatic access to the komoju_refund data.
*
Expand All @@ -12,10 +15,10 @@
* creditmemo's that have been created, but that's more of a future proofing act
* in the event we may need that data.
*/
class Refund extends \Magento\Framework\Model\AbstractModel
class Refund extends AbstractModel
{
protected function _construct()
{
$this->_init(\Komoju\Payments\Model\ResourceModel\Refund::class);
$this->_init(RefundResourceModel::class);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php
namespace Komoju\Payments\Model\ResourceModel\Refund;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\DataObject;
use Komoju\Payments\Model\Refund as RefundModel;
use Komoju\Payments\Model\ResourceModel\Refund as RefundResourceModel;

/**
* The Collection class is an abstraction on top of queries to the database.
* It's the main interface for access ExternalPayment resources
* It's the main interface for access ExternalPayment resources.
*/
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
class Collection extends AbstractCollection
{
/**
* Defines the resource models the collection is matched to
Expand All @@ -16,19 +19,18 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
*/
protected function _construct()
{
$this->_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;
Expand Down
22 changes: 16 additions & 6 deletions src/app/code/Komoju/Payments/Plugin/WebhookEventProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -115,22 +118,16 @@ 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);
$this->order->setStatus(Order::STATE_COMPLETE);
$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 = [];

Expand All @@ -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);

Expand All @@ -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()));
Expand Down
Loading