Skip to content

Commit

Permalink
Solving the issue of Config "Refund strategy Giftcard" is not working…
Browse files Browse the repository at this point in the history
… as expected in version 9
  • Loading branch information
khushboo-singhvi committed Oct 30, 2024
1 parent 5ff9a68 commit 59fcf74
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
41 changes: 41 additions & 0 deletions Helper/Webhook/OrderClosedWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,48 @@ public function handleWebhook(
Notification $notification,
string $transitionState
): MagentoOrder {
$additionalData = $notification->getAdditionalData();
if (is_string($additionalData)) {
$additionalData = @unserialize($additionalData) ?: [];
}
if ($notification->isSuccessful()) {
foreach ($additionalData as $key => $value) {
// Check if the key matches the pattern "order-X-pspReference"
if (preg_match('/^order-(\d+)-pspReference$/', $key, $matches)) {
$orderIndex = (int) $matches[1]; // Get the order number, e.g., 1, 2
$pspReference = $value;
$sortValue = $orderIndex; // Set status based on order index

// Retrieve adyen_order_payment for this pspReference
$adyenOrderPayment = $this->adyenOrderPaymentCollectionFactory->create()
->addFieldToFilter('pspreference', $pspReference)
->getFirstItem();

if ($adyenOrderPayment->getId()) {
// Update the status with the order index
$adyenOrderPayment->setSortOrder($sortValue);
$adyenOrderPayment->save();

$this->adyenLogger->addAdyenNotification(
sprintf("Updated adyen_order_payment with order status %d for pspReference %s", $sortValue, $pspReference),
[
'pspReference' => $pspReference,
'status' => $sortValue,
'merchantReference' => $notification->getMerchantReference()
]
);
} else {
// Log if no matching record was found for the given pspReference
$this->adyenLogger->addAdyenNotification(
sprintf("No adyen_order_payment record found for pspReference %s", $pspReference),
[
'pspReference' => $pspReference,
'merchantReference' => $notification->getMerchantReference()
]
);
}
}
}
$order->addCommentToStatusHistory(__('This order has been successfully completed.'));
} else {
/** @var OrderPaymentInterface $orderPayment */
Expand Down
21 changes: 21 additions & 0 deletions Model/Order/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,25 @@ public function getFormattedTotalCapturedWithCurrency(): string
false
);
}

/**
* Set sort order.
*
* @param int $sortOrder
* @return $this
*/
public function setSortOrder($sortOrder)
{
return $this->setData('order_sort', $sortOrder);
}

/**
* Get sort order.
*
* @return int|null
*/
public function getSortOrder()
{
return $this->getData('order_sort');
}
}
4 changes: 2 additions & 2 deletions Model/ResourceModel/Order/Payment/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getTotalAmount($paymentId, $captured = false)
public function addPaymentFilterAscending($paymentId)
{
$this->addFieldToFilter('payment_id', $paymentId);
$this->getSelect()->order(['created_at ASC']);
$this->getSelect()->order(['order_sort ASC']);
return $this;
}

Expand All @@ -83,7 +83,7 @@ public function addPaymentFilterAscending($paymentId)
public function addPaymentFilterDescending($paymentId)
{
$this->addFieldToFilter('payment_id', $paymentId);
$this->getSelect()->order(['created_at DESC', 'entity_id DESC']);
$this->getSelect()->order(['order_sort DESC']);
return $this;
}
}
1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<column xsi:type="datetime" name="updated_at" on_update="false" nullable="false" comment="Updated at"/>
<column xsi:type="text" name="capture_status" nullable="true" comment="Field to determine if and how order payment was captured"/>
<column xsi:type="decimal" name="total_captured" scale="4" precision="12" unsigned="true" nullable="true" comment="Field to determine the amount that has been captured."/>
<column xsi:type="int" name="order_sort" unsigned="true" nullable="true" comment="Sort order of Partial Payment"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
Expand Down

0 comments on commit 59fcf74

Please sign in to comment.