Skip to content

Commit ea5193f

Browse files
nagpaiNagesh Paikalessil
authored
Use dedicated enum class for order status constants (#5320)
The PR creates an enum class for WooCommerce Order statuses. All order statuses reffered as static strings are replaced by the enum constants across the codebase. Commits: * Add Enum class for order statuses * Merge changelog and latest develop branch updates * Replace status strings with enum class constants * Rename Enum file and constants * Change path in WC_Payments class file * Rename Class name in declaration Co-authored-by: Nagesh Pai <[email protected]> Co-authored-by: Vladimir Reznichenko <[email protected]>
1 parent cbfc040 commit ea5193f

16 files changed

+209
-131
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: dev
3+
4+
The PR replaces hard-coded order status constants, with a dedicated Enum class to make it reusable across the codebase.

includes/admin/class-wc-rest-payments-orders-controller.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
defined( 'ABSPATH' ) || exit;
99

10-
use WCPay\Constants\Payment_Method;
1110
use WCPay\Logger;
11+
use WCPay\Constants\Order_Status;
12+
use WCPay\Constants\Payment_Method;
1213

1314
/**
1415
* REST controller for order processing.
@@ -303,7 +304,15 @@ public function create_customer( $request ) {
303304
return new WP_Error( 'wcpay_missing_order', __( 'Order not found', 'woocommerce-payments' ), [ 'status' => 404 ] );
304305
}
305306

306-
$disallowed_order_statuses = apply_filters( 'wcpay_create_customer_disallowed_order_statuses', [ 'completed', 'cancelled', 'refunded', 'failed' ] );
307+
$disallowed_order_statuses = apply_filters(
308+
'wcpay_create_customer_disallowed_order_statuses',
309+
[
310+
Order_Status::COMPLETED,
311+
Order_Status::CANCELLED,
312+
Order_Status::REFUNDED,
313+
Order_Status::FAILED,
314+
]
315+
);
307316
if ( $order->has_status( $disallowed_order_statuses ) ) {
308317
return new WP_Error( 'wcpay_invalid_order_status', __( 'Invalid order status', 'woocommerce-payments' ), [ 'status' => 400 ] );
309318
}

includes/class-wc-payment-gateway-wcpay.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
exit; // Exit if accessed directly.
1010
}
1111

12-
use WCPay\Exceptions\{ Add_Payment_Method_Exception, Amount_Too_Small_Exception, Process_Payment_Exception, Intent_Authentication_Exception, API_Exception };
13-
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
14-
use WCPay\Logger;
15-
use WCPay\Payment_Information;
12+
use WCPay\Constants\Order_Status;
1613
use WCPay\Constants\Payment_Type;
1714
use WCPay\Constants\Payment_Initiated_By;
1815
use WCPay\Constants\Payment_Capture_Type;
1916
use WCPay\Constants\Payment_Method;
20-
use WCPay\Tracker;
17+
use WCPay\Exceptions\{ Add_Payment_Method_Exception, Amount_Too_Small_Exception, Process_Payment_Exception, Intent_Authentication_Exception, API_Exception };
18+
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
19+
use WCPay\Logger;
20+
use WCPay\Payment_Information;
2121
use WCPay\Payment_Methods\UPE_Payment_Gateway;
22-
use WCPay\Session_Rate_Limiter;
2322
use WCPay\Payment_Methods\Link_Payment_Method;
2423
use WCPay\Platform_Checkout\Platform_Checkout_Order_Status_Sync;
2524
use WCPay\Platform_Checkout\Platform_Checkout_Utilities;
25+
use WCPay\Session_Rate_Limiter;
26+
use WCPay\Tracker;
2627

2728
/**
2829
* Gateway class for WooCommerce Payments
@@ -680,7 +681,7 @@ public function process_payment( $order_id ) {
680681
* information is not added to the order, as shown by tests.
681682
*/
682683
if ( empty( $payment_information ) || ! $payment_information->is_changing_payment_method_for_subscription() ) {
683-
$order->update_status( 'failed' );
684+
$order->update_status( Order_Status::FAILED );
684685
}
685686

686687
if ( $e instanceof API_Exception && $this->should_bump_rate_limiter( $e->get_error_code() ) ) {

includes/class-wc-payments-order-service.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,16 @@
55
* @package WooCommerce\Payments
66
*/
77

8-
use WCPay\Logger;
8+
use WCPay\Constants\Order_Status;
99
use WCPay\Constants\Payment_Method;
10+
use WCPay\Logger;
1011

1112
defined( 'ABSPATH' ) || exit;
1213

1314
/**
1415
* Class handling order functionality.
1516
*/
1617
class WC_Payments_Order_Service {
17-
/**
18-
* Order status constants.
19-
*/
20-
const STATUS_CANCELLED = 'cancelled';
21-
const STATUS_COMPLETED = 'completed';
22-
const STATUS_FAILED = 'failed';
23-
const STATUS_ON_HOLD = 'on-hold';
24-
const STATUS_PENDING = 'pending';
25-
2618
const ADD_FEE_BREAKDOWN_TO_ORDER_NOTES = 'wcpay_add_fee_breakdown_to_order_notes';
2719

2820
/**
@@ -92,7 +84,7 @@ public function mark_payment_completed( $order, $intent_id, $intent_status, $cha
9284
* @return void
9385
*/
9486
public function mark_payment_failed( $order, $intent_id, $intent_status, $charge_id, $message = '' ) {
95-
if ( $order->has_status( [ self::STATUS_FAILED ] )
87+
if ( $order->has_status( [ Order_Status::FAILED ] )
9688
|| 'failed' === $order->get_meta( '_intention_status' )
9789
|| ! $this->order_prepared_for_processing( $order, $intent_id ) ) {
9890
return;
@@ -104,7 +96,7 @@ public function mark_payment_failed( $order, $intent_id, $intent_status, $charge
10496
return;
10597
}
10698

107-
$this->update_order_status( $order, self::STATUS_FAILED );
99+
$this->update_order_status( $order, Order_Status::FAILED );
108100
$order->add_order_note( $note );
109101
$this->complete_order_processing( $order, $intent_status );
110102
}
@@ -120,12 +112,12 @@ public function mark_payment_failed( $order, $intent_id, $intent_status, $charge
120112
* @return void
121113
*/
122114
public function mark_payment_authorized( $order, $intent_id, $intent_status, $charge_id ) {
123-
if ( $order->has_status( [ self::STATUS_ON_HOLD ] )
115+
if ( $order->has_status( [ Order_Status::ON_HOLD ] )
124116
|| ! $this->order_prepared_for_processing( $order, $intent_id ) ) {
125117
return;
126118
}
127119

128-
$this->update_order_status( $order, self::STATUS_ON_HOLD );
120+
$this->update_order_status( $order, Order_Status::ON_HOLD );
129121
$this->add_payment_authorized_note( $order, $intent_id, $charge_id );
130122
$this->complete_order_processing( $order, $intent_status );
131123
}
@@ -141,7 +133,7 @@ public function mark_payment_authorized( $order, $intent_id, $intent_status, $ch
141133
* @return void
142134
*/
143135
public function mark_payment_started( $order, $intent_id, $intent_status, $charge_id ) {
144-
if ( ! $order->has_status( [ self::STATUS_PENDING ] )
136+
if ( ! $order->has_status( [ Order_Status::PENDING ] )
145137
|| ! $this->order_prepared_for_processing( $order, $intent_id ) ) {
146138
return;
147139
}
@@ -211,7 +203,7 @@ public function mark_payment_capture_expired( $order, $intent_id, $intent_status
211203
return;
212204
}
213205

214-
$this->update_order_status( $order, self::STATUS_CANCELLED );
206+
$this->update_order_status( $order, Order_Status::CANCELLED );
215207
$order->add_order_note( $note );
216208
$this->complete_order_processing( $order, $intent_status );
217209
}
@@ -230,7 +222,7 @@ public function mark_payment_capture_cancelled( $order, $intent_id, $intent_stat
230222
return;
231223
}
232224

233-
$this->update_order_status( $order, self::STATUS_CANCELLED );
225+
$this->update_order_status( $order, Order_Status::CANCELLED );
234226
$this->add_capture_cancelled_note( $order );
235227
$this->complete_order_processing( $order, $intent_status );
236228
}
@@ -255,7 +247,7 @@ public function mark_payment_dispute_created( $order, $dispute_id, $reason ) {
255247
return;
256248
}
257249

258-
$this->update_order_status( $order, self::STATUS_ON_HOLD );
250+
$this->update_order_status( $order, Order_Status::ON_HOLD );
259251
$order->add_order_note( $note );
260252
$order->save();
261253

@@ -292,7 +284,7 @@ public function mark_payment_dispute_closed( $order, $dispute_id, $status ) {
292284
);
293285
} else {
294286
// TODO: This should revert to the status the order was in before the dispute was created.
295-
$this->update_order_status( $order, self::STATUS_COMPLETED );
287+
$this->update_order_status( $order, Order_Status::COMPLETED );
296288
$order->save();
297289
}
298290

@@ -309,7 +301,7 @@ public function mark_payment_dispute_closed( $order, $dispute_id, $status ) {
309301
* @return void
310302
*/
311303
public function mark_terminal_payment_completed( $order, $intent_id, $intent_status ) {
312-
$this->update_order_status( $order, self::STATUS_COMPLETED, $intent_id );
304+
$this->update_order_status( $order, Order_Status::COMPLETED, $intent_id );
313305
$this->complete_order_processing( $order, $intent_status );
314306
}
315307

includes/class-wc-payments-webhook-processing-service.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
* @package WooCommerce\Payments
66
*/
77

8+
use WCPay\Constants\Order_Status;
89
use WCPay\Constants\Payment_Method;
10+
use WCPay\Database_Cache;
911
use WCPay\Exceptions\Invalid_Payment_Method_Exception;
1012
use WCPay\Exceptions\Invalid_Webhook_Data_Exception;
1113
use WCPay\Exceptions\Rest_Request_Exception;
1214
use WCPay\Logger;
13-
use WCPay\Database_Cache;
1415

1516
if ( ! defined( 'ABSPATH' ) ) {
1617
exit; // Exit if accessed directly.
@@ -306,8 +307,8 @@ private function process_webhook_refund_updated( $event_body ) {
306307

307308
// Update order status if order is fully refunded.
308309
$current_order_status = $order->get_status();
309-
if ( 'refunded' === $current_order_status ) {
310-
$order->update_status( 'failed' );
310+
if ( Order_Status::REFUNDED === $current_order_status ) {
311+
$order->update_status( Order_Status::FAILED );
311312
}
312313

313314
$order->add_order_note( $note );

includes/class-wc-payments.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ public static function init() {
282282
include_once __DIR__ . '/exceptions/class-invalid-webhook-data-exception.php';
283283
include_once __DIR__ . '/exceptions/class-invalid-price-exception.php';
284284
include_once __DIR__ . '/compat/class-wc-payment-woo-compat-utils.php';
285+
include_once __DIR__ . '/constants/class-order-status.php';
285286
include_once __DIR__ . '/constants/class-payment-type.php';
286287
include_once __DIR__ . '/constants/class-payment-initiated-by.php';
287288
include_once __DIR__ . '/constants/class-payment-capture-type.php';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Class Order_Status
4+
*
5+
* @package WooCommerce\Payments
6+
*/
7+
8+
namespace WCPay\Constants;
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit; // Exit if accessed directly.
12+
}
13+
14+
use MyCLabs\Enum\Enum;
15+
16+
/**
17+
* This class gives a list of all the possible order status constants.
18+
*
19+
* @psalm-immutable
20+
*/
21+
class Order_Status extends Enum {
22+
const CANCELLED = 'cancelled';
23+
const COMPLETED = 'completed';
24+
const FAILED = 'failed';
25+
const ON_HOLD = 'on-hold';
26+
const PENDING = 'pending';
27+
const PROCESSING = 'processing';
28+
const REFUNDED = 'refunded';
29+
}

includes/payment-methods/class-upe-payment-gateway.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,32 @@
77

88
namespace WCPay\Payment_Methods;
99

10-
use WC_Order;
11-
use WC_Payment_Token_WCPay_SEPA;
12-
use WC_Payments_Explicit_Price_Formatter;
10+
use Exception;
11+
use WCPay\Constants\Order_Status;
1312
use WCPay\Constants\Payment_Method;
14-
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
15-
use WP_User;
13+
use WCPay\Constants\Payment_Type;
14+
use WCPay\Exceptions\Amount_Too_Small_Exception;
1615
use WCPay\Exceptions\Add_Payment_Method_Exception;
16+
use WCPay\Exceptions\Process_Payment_Exception;
17+
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
1718
use WCPay\Logger;
1819
use WCPay\Payment_Information;
19-
use WCPay\Constants\Payment_Type;
2020
use WCPay\Session_Rate_Limiter;
21-
use WC_Payment_Gateway_WCPay;
21+
use WC_Order;
22+
use WC_Payments;
2223
use WC_Payments_Account;
2324
use WC_Payments_Action_Scheduler_Service;
2425
use WC_Payments_API_Client;
2526
use WC_Payments_Customer_Service;
27+
use WC_Payments_Explicit_Price_Formatter;
28+
use WC_Payment_Gateway_WCPay;
2629
use WC_Payments_Order_Service;
27-
use WC_Payments_Token_Service;
2830
use WC_Payment_Token_CC;
29-
use WC_Payments;
31+
use WC_Payments_Token_Service;
32+
use WC_Payment_Token_WCPay_SEPA;
3033
use WC_Payments_Utils;
34+
use WP_User;
3135

32-
use Exception;
33-
use WCPay\Exceptions\Amount_Too_Small_Exception;
34-
use WCPay\Exceptions\Process_Payment_Exception;
3536

3637

3738
/**
@@ -614,7 +615,13 @@ public function process_redirect_payment( $order_id, $intent_id, $save_payment_m
614615
return;
615616
}
616617

617-
if ( $order->has_status( [ 'processing', 'completed', 'on-hold' ] ) ) {
618+
if ( $order->has_status(
619+
[
620+
Order_Status::PROCESSING,
621+
Order_Status::COMPLETED,
622+
Order_Status::ON_HOLD,
623+
]
624+
) ) {
618625
return;
619626
}
620627

tests/unit/admin/test-class-wc-rest-payments-orders-controller.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
*/
77

88
use PHPUnit\Framework\MockObject\MockObject;
9-
use WCPay\Exceptions\Rest_Request_Exception;
9+
use WCPay\Constants\Order_Status;
1010
use WCPay\Constants\Payment_Method;
11+
use WCPay\Exceptions\Rest_Request_Exception;
1112

1213
/**
1314
* WC_REST_Payments_Orders_Controller unit tests.
@@ -137,7 +138,7 @@ public function test_capture_terminal_payment_success() {
137138
$result_order = wc_get_order( $order->get_id() );
138139
$this->assertEquals( 'woocommerce_payments', $result_order->get_payment_method() );
139140
$this->assertEquals( 'WooCommerce In-Person Payments', $result_order->get_payment_method_title() );
140-
$this->assertEquals( 'completed', $result_order->get_status() );
141+
$this->assertEquals( Order_Status::COMPLETED, $result_order->get_status() );
141142
$url = '/wc/v3/' . ( $this->is_wpcom() ? 'sites/3/' : '' ) . 'payments/readers/receipts/' . $this->mock_intent_id;
142143
$this->assertStringEndsWith( $url, $result_order->get_meta( 'receipt_url' ) );
143144
}
@@ -205,7 +206,7 @@ public function test_capture_terminal_payment_succeeded_intent() {
205206
$result_order = wc_get_order( $order->get_id() );
206207
$this->assertSame( 'woocommerce_payments', $result_order->get_payment_method() );
207208
$this->assertSame( 'WooCommerce In-Person Payments', $result_order->get_payment_method_title() );
208-
$this->assertSame( 'completed', $result_order->get_status() );
209+
$this->assertSame( Order_Status::COMPLETED, $result_order->get_status() );
209210
$url = '/wc/v3/' . ( $this->is_wpcom() ? 'sites/3/' : '' ) . 'payments/readers/receipts/';
210211
$this->assertStringEndsWith( $url . $this->mock_intent_id, $result_order->get_meta( 'receipt_url' ) );
211212
}
@@ -214,7 +215,7 @@ public function test_capture_terminal_payment_completed_order() {
214215
// This scenario may occur when `process_webhook_payment_intent_succeeded`
215216
// is triggered before the terminal payment is captured in the backend.
216217
$order = $this->create_mock_order();
217-
$order->update_status( 'completed' );
218+
$order->update_status( Order_Status::COMPLETED );
218219

219220
$mock_intent = WC_Helper_Intention::create_intention(
220221
[
@@ -277,7 +278,7 @@ public function test_capture_terminal_payment_completed_order() {
277278
$result_order = wc_get_order( $order->get_id() );
278279
$this->assertSame( 'woocommerce_payments', $result_order->get_payment_method() );
279280
$this->assertSame( 'WooCommerce In-Person Payments', $result_order->get_payment_method_title() );
280-
$this->assertSame( 'completed', $result_order->get_status() );
281+
$this->assertSame( Order_Status::COMPLETED, $result_order->get_status() );
281282
$url = '/wc/v3/' . ( $this->is_wpcom() ? 'sites/3/' : '' ) . 'payments/readers/receipts/';
282283
$this->assertStringEndsWith( $url . $this->mock_intent_id, $result_order->get_meta( 'receipt_url' ) );
283284
}
@@ -348,7 +349,7 @@ public function test_capture_terminal_succeeded_payment_intent_missing_order_id(
348349
$data = $response->get_error_data();
349350
$this->assertArrayHasKey( 'status', $data );
350351
$this->assertSame( 409, $data['status'] );
351-
$this->assertFalse( $order->has_status( 'completed' ) );
352+
$this->assertFalse( $order->has_status( Order_Status::COMPLETED ) );
352353
}
353354

354355
public function test_capture_terminal_payment_refunded_order() {
@@ -711,7 +712,7 @@ public function test_capture_authorization_with_succeeded_payment_intent_and_mis
711712
$data = $response->get_error_data();
712713
$this->assertArrayHasKey( 'status', $data );
713714
$this->assertSame( 409, $data['status'] );
714-
$this->assertFalse( $order->has_status( 'completed' ) );
715+
$this->assertFalse( $order->has_status( Order_Status::COMPLETED ) );
715716
}
716717

717718
public function test_capture_authorization_refunded_order() {
@@ -1076,7 +1077,7 @@ public function test_create_customer_from_order_non_guest_with_customer_id() {
10761077
*/
10771078
public function test_create_customer_from_order_with_invalid_status() {
10781079
$order = WC_Helper_Order::create_order();
1079-
$order->set_status( 'completed' );
1080+
$order->set_status( Order_Status::COMPLETED );
10801081
$order->save();
10811082

10821083
$this->mock_customer_service
@@ -1307,7 +1308,7 @@ private function create_mock_order() {
13071308
$order->update_meta_data( '_intent_id', $this->mock_intent_id );
13081309
$order->update_meta_data( '_charge_id', $charge->get_id() );
13091310
$order->update_meta_data( '_intention_status', 'requires_capture' );
1310-
$order->update_status( 'on-hold' );
1311+
$order->update_status( Order_Status::ON_HOLD );
13111312
return $order;
13121313
}
13131314

0 commit comments

Comments
 (0)