Skip to content

Commit cd49f93

Browse files
authored
Merge pull request #83 from SwedbankPay/feature/refunds
Added method `isCreditMemoExist($transactionId)`
2 parents df0fa1e + ad86942 commit cd49f93

File tree

6 files changed

+86
-42
lines changed

6 files changed

+86
-42
lines changed

changelog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
Version 5.2.0
2+
Added
3+
- Added method `isCreditMemoExist($transactionId)`
4+
25
Changed
36
- Changed method parameters `captureCheckout($orderId, array $items = [])`
47
- Changed method parameters `refundCheckout($orderId, array $items = [])`
58
- Changed method parameters `captureInvoice($orderId, array $items = [])`
69

10+
Fixed
11+
- Fixed deprecation notice
12+
713
Version 5.1.0
814
Changed
915
- Fix User Agent string

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"php": ">=7.0",
2626
"ext-json": "*",
2727
"ext-bcmath": "*",
28-
"swedbank-pay/swedbank-pay-sdk-php": "~5.3",
28+
"swedbank-pay/swedbank-pay-sdk-php": "~5.4",
2929
"squizlabs/php_codesniffer": "^3.5"
3030
},
3131
"require-dev": {

src/SwedbankPay/Core/Adapter/WC_Adapter.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,48 +1091,24 @@ public function generatePayeeReference($orderId)
10911091
*/
10921092
public function createCreditMemo($orderId, $amount, $transactionId, $description)
10931093
{
1094+
if (!$transactionId) {
1095+
throw new Exception('Unable to create refund memo. Transaction ID is missing.', 500);
1096+
}
1097+
10941098
// Prevent refund credit memo creation through Callback
10951099
if (get_transient('sb_refund_block_' . $orderId)) {
1096-
delete_transient('sb_refund_block_' . $orderId);
1100+
//delete_transient('sb_refund_block_' . $orderId);
10971101
return;
10981102
}
10991103

1100-
global $wpdb;
1101-
1102-
// Get refunds by transaction ID
1103-
if ($transactionId) {
1104-
$query = "
1105-
SELECT post_id FROM `{$wpdb->prefix}postmeta` postmeta
1106-
LEFT JOIN `{$wpdb->prefix}posts` AS posts ON postmeta.post_id = posts.ID
1107-
WHERE meta_key='_transaction_id' AND meta_value=%s AND posts.post_type='shop_order_refund';
1108-
";
1109-
1110-
if ($wpdb->get_var($wpdb->prepare($query, $transactionId))) {
1111-
// Credit Memo is already exists
1112-
return;
1113-
}
1114-
} else {
1115-
// Get refunds by amount
1116-
$order = wc_get_order($orderId);
1117-
$refunds = $order->get_refunds();
1118-
1119-
foreach ($refunds as $refund) {
1120-
/** @var \WC_Order_Refund $refund */
1121-
if (bccomp($refund->get_amount(), $amount, 2) === 0) {
1122-
// Credit Memo is already exists
1123-
return;
1124-
}
1125-
}
1126-
}
1127-
11281104
// Create the refund
11291105
$refund = wc_create_refund(
11301106
array(
11311107
'order_id' => $orderId,
11321108
'amount' => $amount,
11331109
'reason' => $description,
11341110
'refund_payment' => false,
1135-
'restock_items' => true,
1111+
'restock_items' => false,
11361112
)
11371113
);
11381114

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

1147-
if ($transactionId) {
1148-
$refund->update_meta_data('_transaction_id', $transactionId);
1149-
$refund->save_meta_data();
1150-
}
1123+
// Add transaction id to identify refund memo
1124+
$refund->update_meta_data('_transaction_id', $transactionId);
1125+
$refund->save_meta_data();
11511126

11521127
$this->log(
11531128
'info',
@@ -1164,6 +1139,31 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
11641139
);
11651140
}
11661141

1142+
/**
1143+
* Check if Credit Memo exist by Transaction ID.
1144+
*
1145+
* @param string $transactionId
1146+
*
1147+
* @return bool
1148+
*/
1149+
public function isCreditMemoExist($transactionId)
1150+
{
1151+
global $wpdb;
1152+
1153+
$query = "
1154+
SELECT post_id FROM `{$wpdb->prefix}postmeta` postmeta
1155+
LEFT JOIN `{$wpdb->prefix}posts` AS posts ON postmeta.post_id = posts.ID
1156+
WHERE meta_key='_transaction_id' AND meta_value=%s AND posts.post_type='shop_order_refund';
1157+
";
1158+
1159+
if ($wpdb->get_var($wpdb->prepare($query, $transactionId))) {
1160+
// Credit Memo is already exists
1161+
return true;
1162+
}
1163+
1164+
return false;
1165+
}
1166+
11671167
/**
11681168
* Get Order Lines
11691169
*

src/SwedbankPay/Core/Library/OrderAction.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -995,13 +995,6 @@ public function processTransaction($orderId, $transaction)
995995
$paymentBody = $paymentInfo['payment'];
996996
}
997997

998-
// Check if the payment was refunded fully
999-
// `remainingReversalAmount` is missing if the payment was refunded fully
1000-
$isFullRefund = false;
1001-
if (!isset($paymentBody['remainingReversalAmount'])) {
1002-
$isFullRefund = true;
1003-
}
1004-
1005998
// Create Credit Memo
1006999
$this->createCreditMemo(
10071000
$orderId,
@@ -1010,6 +1003,13 @@ public function processTransaction($orderId, $transaction)
10101003
$transaction->getDescription()
10111004
);
10121005

1006+
// Check if the payment was refunded fully
1007+
// `remainingReversalAmount` is missing if the payment was refunded fully
1008+
$isFullRefund = false;
1009+
if (!isset($paymentBody['remainingReversalAmount'])) {
1010+
$isFullRefund = true;
1011+
}
1012+
10131013
// Update order status
10141014
if ($isFullRefund) {
10151015
$this->updateOrderStatus(
@@ -1063,6 +1063,11 @@ public function generatePayeeReference($orderId)
10631063
*/
10641064
public function createCreditMemo($orderId, $amount, $transactionId, $description)
10651065
{
1066+
// Check if a credit memo was created before
1067+
if ($this->isCreditMemoExist($transactionId)) {
1068+
return;
1069+
}
1070+
10661071
try {
10671072
$this->adapter->createCreditMemo($orderId, $amount, $transactionId, $description);
10681073
} catch (Exception $e) {
@@ -1076,6 +1081,18 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
10761081
}
10771082
}
10781083

1084+
/**
1085+
* Check if Credit Memo exist.
1086+
*
1087+
* @param string $transactionId
1088+
*
1089+
* @return bool
1090+
*/
1091+
public function isCreditMemoExist($transactionId)
1092+
{
1093+
return $this->adapter->isCreditMemoExist($transactionId);
1094+
}
1095+
10791096
/**
10801097
* Update Transactions On Failure.
10811098
*

src/SwedbankPay/Core/PaymentAdapterInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,13 @@ public function generatePayeeReference($orderId);
227227
* @throws Exception
228228
*/
229229
public function createCreditMemo($orderId, $amount, $transactionId, $description);
230+
231+
/**
232+
* Check if Credit Memo exist.
233+
*
234+
* @param string $transactionId
235+
*
236+
* @return bool
237+
*/
238+
public function isCreditMemoExist($transactionId);
230239
}

tests/Adapter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,16 @@ public function createCreditMemo($orderId, $amount, $transactionId, $description
403403
{
404404
throw new Exception('createCreditMemo exception');
405405
}
406+
407+
/**
408+
* Check if Credit Memo exist.
409+
*
410+
* @param string $transactionId
411+
*
412+
* @return bool
413+
*/
414+
public function isCreditMemoExist($transactionId)
415+
{
416+
return false;
417+
}
406418
}

0 commit comments

Comments
 (0)