@@ -38,12 +38,13 @@ public function __construct(
38
38
}
39
39
40
40
public function execute ( WP_REST_Request $ request ): void {
41
- $ data = $ request ->get_body ();
42
-
43
- $ data = json_decode ( $ data , true , 512 , JSON_THROW_ON_ERROR );
44
- $ event = $ data ['event ' ] ?? null ;
45
- $ data = $ data ['data ' ] ?? null ;
46
- $ invoice_id = $ data ['id ' ] ?? null ;
41
+ $ data = $ request ->get_body ();
42
+ $ data = json_decode ( $ data , true , 512 , JSON_THROW_ON_ERROR );
43
+ $ event = $ data ['event ' ] ?? null ;
44
+ $ data = $ data ['data ' ] ?? null ;
45
+ $ data ['event ' ] = $ event ;
46
+ $ data ['requestDate ' ] = date ( 'Y-m-d H:i:s ' );
47
+ $ invoice_id = $ data ['id ' ] ?? null ;
47
48
48
49
$ this ->logger ->execute ( $ data , 'INCOMING IPN ' , true );
49
50
if ( ! $ event || ! $ data || ! $ invoice_id ) {
@@ -142,28 +143,28 @@ private function get_bitpay_dashboard_link( string $invoice_id ): string {
142
143
}
143
144
144
145
private function process_confirmed ( Invoice $ bitpay_invoice , WC_Order $ order ): void {
145
- $ this ->validate_bitpay_status_in_available_statuses ( $ bitpay_invoice , array ( 'confirmed ' ) );
146
+ $ this ->validate_bitpay_status_in_available_statuses ( $ bitpay_invoice , array ( 'confirmed ' , ' complete ' ) );
146
147
147
148
$ invoice_id = $ bitpay_invoice ->getId ();
148
149
$ wordpress_order_status = $ this ->bitpay_wordpress_helper
149
150
->get_bitpay_gateway_option ( 'bitpay_checkout_order_process_confirmed_status ' );
150
151
if ( WcGatewayBitpay::IGNORE_STATUS_VALUE === $ wordpress_order_status ) {
151
152
$ order ->add_order_note (
152
- $ this ->get_start_order_note ( $ invoice_id ) . 'has changed to Confirmed. The order status has not been updated due to your settings. '
153
+ $ this ->get_start_order_note ( $ invoice_id ) . 'has changed to Confirmed. The order status has not been updated due to your settings. '
153
154
);
154
155
return ;
155
156
}
156
157
157
- $ new_status = $ this ->get_wc_order_statuses ()[ $ wordpress_order_status ] ?? ' Processing ' ;
158
+ $ new_status = $ this ->get_wc_order_statuses ()[ $ wordpress_order_status ] ?? null ;
158
159
if ( ! $ new_status ) {
159
160
$ new_status = 'Processing ' ;
160
- $ wordpress_order_status = 'wc- pending ' ;
161
+ $ wordpress_order_status = 'pending ' ;
161
162
}
162
163
163
164
$ order ->add_order_note (
164
165
$ this ->get_start_order_note ( $ invoice_id ) . 'has changed to ' . $ new_status . '. '
165
166
);
166
- if ( 'wc-completed ' === $ wordpress_order_status ) {
167
+ if ( 'wc-completed ' === $ wordpress_order_status ) { // statuses with 'wc' prefix.
167
168
$ order ->payment_complete ();
168
169
$ order ->add_order_note ( 'Payment Completed ' );
169
170
} else {
@@ -174,26 +175,32 @@ private function process_confirmed( Invoice $bitpay_invoice, WC_Order $order ):
174
175
175
176
private function process_completed ( Invoice $ bitpay_invoice , WC_Order $ order ): void {
176
177
$ this ->validate_bitpay_status_in_available_statuses ( $ bitpay_invoice , array ( 'complete ' ) );
178
+ $ wc_order_status = $ order ->get_status ();
177
179
178
180
$ invoice_id = $ bitpay_invoice ->getId ();
179
181
$ wordpress_order_status = $ this ->bitpay_wordpress_helper
180
182
->get_bitpay_gateway_option ( 'bitpay_checkout_order_process_complete_status ' );
181
183
if ( WcGatewayBitpay::IGNORE_STATUS_VALUE === $ wordpress_order_status ) {
182
184
$ order ->add_order_note (
183
185
$ this ->get_start_order_note ( $ invoice_id )
184
- . 'has changed to Complete. The order status has not been updated due to your settings. '
186
+ . 'has changed to Complete. The order status has not been updated due to your settings. '
185
187
);
186
188
return ;
187
189
}
188
190
189
- $ new_status = $ this ->get_wc_order_statuses ()[ $ wordpress_order_status ] ?? 'Processing ' ;
191
+ if ( ! $ this ->should_process_completed_action ( $ wc_order_status , $ wordpress_order_status ) ) {
192
+ return ;
193
+ }
194
+
195
+ $ new_status = $ this ->get_wc_order_statuses ()[ $ wordpress_order_status ] ?? null ;
196
+ $ new_status = apply_filters ( 'bitpay_checkout_order_process_complete_status ' , $ new_status , $ wordpress_order_status );
190
197
if ( ! $ new_status ) {
191
198
$ new_status = 'Processing ' ;
192
- $ wordpress_order_status = 'wc- pending ' ;
199
+ $ wordpress_order_status = 'pending ' ;
193
200
}
194
201
195
202
$ order ->add_order_note ( $ this ->get_start_order_note ( $ invoice_id ) . 'has changed to ' . $ new_status . '. ' );
196
- if ( 'wc-completed ' === $ wordpress_order_status ) {
203
+ if ( 'wc-completed ' === $ wordpress_order_status ) { // statuses with 'wc' prefix.
197
204
$ order ->payment_complete ();
198
205
$ order ->add_order_note ( 'Payment Completed ' );
199
206
} else {
@@ -244,8 +251,7 @@ private function process_abandoned( Invoice $bitpay_invoice, WC_Order $order ):
244
251
245
252
$ invoice_id = $ bitpay_invoice ->getId ();
246
253
if ( $ underpaid_amount ) {
247
- $ order ->add_order_note ( $ this ->get_start_order_note ( $ invoice_id ) . $ underpaid_amount . ' has been refunded. ' );
248
- $ order ->update_status ( 'refunded ' , __ ( 'BitPay payment refunded ' , 'woocommerce ' ) );
254
+ $ this ->process_refunded ( $ bitpay_invoice , $ order );
249
255
return ;
250
256
}
251
257
@@ -258,12 +264,20 @@ private function process_abandoned( Invoice $bitpay_invoice, WC_Order $order ):
258
264
}
259
265
260
266
private function process_refunded ( Invoice $ bitpay_invoice , WC_Order $ order ): void {
267
+ if ( ! $ this ->should_process_refund () ) {
268
+ $ order ->add_order_note (
269
+ $ this ->get_start_order_note ( $ bitpay_invoice ->getId () )
270
+ . 'has changed to Refunded. The order status has not been updated due to your settings. '
271
+ );
272
+ return ;
273
+ }
274
+
261
275
$ order ->add_order_note ( $ this ->get_start_order_note ( $ bitpay_invoice ->getId () ) . 'has been refunded. ' );
262
276
$ order ->update_status ( 'refunded ' , __ ( 'BitPay payment refunded ' , 'woocommerce ' ) );
263
277
}
264
278
265
279
private function process_processing ( Invoice $ bitpay_invoice , WC_Order $ order ): void {
266
- $ this ->validate_bitpay_status_in_available_statuses ( $ bitpay_invoice , array ( 'paid ' ) );
280
+ $ this ->validate_bitpay_status_in_available_statuses ( $ bitpay_invoice , array ( 'paid ' , ' confirmed ' , ' complete ' ) );
267
281
$ invoice_id = $ bitpay_invoice ->getId ();
268
282
$ order ->add_order_note ( $ this ->get_start_order_note ( $ invoice_id ) . 'is paid and awaiting confirmation. ' );
269
283
@@ -280,4 +294,32 @@ private function process_processing( Invoice $bitpay_invoice, WC_Order $order ):
280
294
$ new_status = $ this ->get_wc_order_statuses ()[ $ wordpress_order_status ] ?? 'processing ' ;
281
295
$ order ->update_status ( $ new_status , __ ( 'BitPay payment processing ' , 'woocommerce ' ) );
282
296
}
297
+
298
+ private function has_final_status ( WC_Order $ order ): bool {
299
+ return \in_array ( $ order ->get_status (), self ::FINAL_WC_ORDER_STATUSES , true );
300
+ }
301
+
302
+ /**
303
+ * We don't want to change complete order to process.
304
+ *
305
+ * @param string $wc_order_status WC order status.
306
+ * @param string|null $wordpress_order_status_from_settings status to event from BitPay settings.
307
+ * @return bool
308
+ */
309
+ private function should_process_completed_action ( string $ wc_order_status , ?string $ wordpress_order_status_from_settings ): bool {
310
+ if ( 'completed ' !== $ wc_order_status ) {
311
+ return true ;
312
+ }
313
+
314
+ if ( 'pending ' === $ wordpress_order_status_from_settings || 'processing ' === $ wordpress_order_status_from_settings ) {
315
+ return false ;
316
+ }
317
+
318
+ return true ;
319
+ }
320
+
321
+ private function should_process_refund (): bool {
322
+ $ should_process_refund_status = $ this ->get_wc_order_statuses ()['bitpay_checkout_order_process_refund ' ] ?? '1 ' ;
323
+ return '1 ' === $ should_process_refund_status ;
324
+ }
283
325
}
0 commit comments