Skip to content

Commit

Permalink
Merge pull request #83 from SwedbankPay/feature/refunds
Browse files Browse the repository at this point in the history
Added method `isCreditMemoExist($transactionId)`
  • Loading branch information
aait authored Aug 15, 2022
2 parents df0fa1e + ad86942 commit cd49f93
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 42 deletions.
6 changes: 6 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
Version 5.2.0
Added
- Added method `isCreditMemoExist($transactionId)`

Changed
- Changed method parameters `captureCheckout($orderId, array $items = [])`
- Changed method parameters `refundCheckout($orderId, array $items = [])`
- Changed method parameters `captureInvoice($orderId, array $items = [])`

Fixed
- Fixed deprecation notice

Version 5.1.0
Changed
- Fix User Agent string
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"php": ">=7.0",
"ext-json": "*",
"ext-bcmath": "*",
"swedbank-pay/swedbank-pay-sdk-php": "~5.3",
"swedbank-pay/swedbank-pay-sdk-php": "~5.4",
"squizlabs/php_codesniffer": "^3.5"
},
"require-dev": {
Expand Down
68 changes: 34 additions & 34 deletions src/SwedbankPay/Core/Adapter/WC_Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,48 +1091,24 @@ public function generatePayeeReference($orderId)
*/
public function createCreditMemo($orderId, $amount, $transactionId, $description)
{
if (!$transactionId) {
throw new Exception('Unable to create refund memo. Transaction ID is missing.', 500);
}

// Prevent refund credit memo creation through Callback
if (get_transient('sb_refund_block_' . $orderId)) {
delete_transient('sb_refund_block_' . $orderId);
//delete_transient('sb_refund_block_' . $orderId);
return;
}

global $wpdb;

// Get refunds by transaction ID
if ($transactionId) {
$query = "
SELECT post_id FROM `{$wpdb->prefix}postmeta` postmeta
LEFT JOIN `{$wpdb->prefix}posts` AS posts ON postmeta.post_id = posts.ID
WHERE meta_key='_transaction_id' AND meta_value=%s AND posts.post_type='shop_order_refund';
";

if ($wpdb->get_var($wpdb->prepare($query, $transactionId))) {
// Credit Memo is already exists
return;
}
} else {
// Get refunds by amount
$order = wc_get_order($orderId);
$refunds = $order->get_refunds();

foreach ($refunds as $refund) {
/** @var \WC_Order_Refund $refund */
if (bccomp($refund->get_amount(), $amount, 2) === 0) {
// Credit Memo is already exists
return;
}
}
}

// Create the refund
$refund = wc_create_refund(
array(
'order_id' => $orderId,
'amount' => $amount,
'reason' => $description,
'refund_payment' => false,
'restock_items' => true,
'restock_items' => false,
)
);

Expand All @@ -1144,10 +1120,9 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
throw new Exception('Cannot create order refund, please try again.', 500);
}

if ($transactionId) {
$refund->update_meta_data('_transaction_id', $transactionId);
$refund->save_meta_data();
}
// Add transaction id to identify refund memo
$refund->update_meta_data('_transaction_id', $transactionId);
$refund->save_meta_data();

$this->log(
'info',
Expand All @@ -1164,6 +1139,31 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
);
}

/**
* Check if Credit Memo exist by Transaction ID.
*
* @param string $transactionId
*
* @return bool
*/
public function isCreditMemoExist($transactionId)
{
global $wpdb;

$query = "
SELECT post_id FROM `{$wpdb->prefix}postmeta` postmeta
LEFT JOIN `{$wpdb->prefix}posts` AS posts ON postmeta.post_id = posts.ID
WHERE meta_key='_transaction_id' AND meta_value=%s AND posts.post_type='shop_order_refund';
";

if ($wpdb->get_var($wpdb->prepare($query, $transactionId))) {
// Credit Memo is already exists
return true;
}

return false;
}

/**
* Get Order Lines
*
Expand Down
31 changes: 24 additions & 7 deletions src/SwedbankPay/Core/Library/OrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,13 +995,6 @@ public function processTransaction($orderId, $transaction)
$paymentBody = $paymentInfo['payment'];
}

// Check if the payment was refunded fully
// `remainingReversalAmount` is missing if the payment was refunded fully
$isFullRefund = false;
if (!isset($paymentBody['remainingReversalAmount'])) {
$isFullRefund = true;
}

// Create Credit Memo
$this->createCreditMemo(
$orderId,
Expand All @@ -1010,6 +1003,13 @@ public function processTransaction($orderId, $transaction)
$transaction->getDescription()
);

// Check if the payment was refunded fully
// `remainingReversalAmount` is missing if the payment was refunded fully
$isFullRefund = false;
if (!isset($paymentBody['remainingReversalAmount'])) {
$isFullRefund = true;
}

// Update order status
if ($isFullRefund) {
$this->updateOrderStatus(
Expand Down Expand Up @@ -1063,6 +1063,11 @@ public function generatePayeeReference($orderId)
*/
public function createCreditMemo($orderId, $amount, $transactionId, $description)
{
// Check if a credit memo was created before
if ($this->isCreditMemoExist($transactionId)) {
return;
}

try {
$this->adapter->createCreditMemo($orderId, $amount, $transactionId, $description);
} catch (Exception $e) {
Expand All @@ -1076,6 +1081,18 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
}
}

/**
* Check if Credit Memo exist.
*
* @param string $transactionId
*
* @return bool
*/
public function isCreditMemoExist($transactionId)
{
return $this->adapter->isCreditMemoExist($transactionId);
}

/**
* Update Transactions On Failure.
*
Expand Down
9 changes: 9 additions & 0 deletions src/SwedbankPay/Core/PaymentAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,13 @@ public function generatePayeeReference($orderId);
* @throws Exception
*/
public function createCreditMemo($orderId, $amount, $transactionId, $description);

/**
* Check if Credit Memo exist.
*
* @param string $transactionId
*
* @return bool
*/
public function isCreditMemoExist($transactionId);
}
12 changes: 12 additions & 0 deletions tests/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,16 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
{
throw new Exception('createCreditMemo exception');
}

/**
* Check if Credit Memo exist.
*
* @param string $transactionId
*
* @return bool
*/
public function isCreditMemoExist($transactionId)
{
return false;
}
}

0 comments on commit cd49f93

Please sign in to comment.