@@ -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 *
0 commit comments