Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.00% |
8 / 10 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
Woo_Cancel_Abandoned_Order | |
80.00% |
8 / 10 |
|
66.67% |
2 / 3 |
6.29 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
enable_cao_for_bitcoin | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
abort_canceling_partially_paid_order | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 |
1 | <?php |
2 | /** |
3 | * Add support for plugin: WooCommerce Cancel Abandoned Order. |
4 | * |
5 | * * Enables the options for Bitcoin gateways |
6 | * * Prevents partially paid orders from being canceled |
7 | * |
8 | * @see https://github.com/rvola/woo-cancel-abandoned-order |
9 | * @see https://wordpress.org/plugins/woo-cancel-abandoned-order/ |
10 | * |
11 | * @package brianhenryie/bh-wp-bitcoin-gateway |
12 | */ |
13 | |
14 | namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations; |
15 | |
16 | use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface; |
17 | use Exception; |
18 | use RVOLA\WOO\CAO\CAO; |
19 | use WC_Order; |
20 | |
21 | /** |
22 | * Adds additional settings to automatically cancel on-hold orders at |
23 | * `wp-admin/admin.php?page=wc-settings&tab=checkout§ion=bitcoin_gateway`. |
24 | */ |
25 | class Woo_Cancel_Abandoned_Order { |
26 | |
27 | /** |
28 | * Used to check is the order a Bitcoin order, and get the order details/transactions. |
29 | */ |
30 | protected API_Interface $api; |
31 | |
32 | /** |
33 | * Constructor. |
34 | * |
35 | * @param API_Interface $api The main plugin functions. |
36 | */ |
37 | public function __construct( API_Interface $api ) { |
38 | $this->api = $api; |
39 | } |
40 | |
41 | /** |
42 | * Enable Woo Cancel Abandoned Order settings for Bitcoin gateway. |
43 | * |
44 | * I don't know why it's not just enabled for all gateways, but whatever. |
45 | * |
46 | * @hooked woo_cao_gateways |
47 | * @see CAO::add_field_gateways() |
48 | * |
49 | * @param string[] $gateway_ids The set of WooCommerce gateways that Cancel Abandoned Order is enabled for. |
50 | * |
51 | * @return string[] |
52 | */ |
53 | public function enable_cao_for_bitcoin( array $gateway_ids ): array { |
54 | |
55 | foreach ( $this->api->get_bitcoin_gateways() as $bitcoin_gateway ) { |
56 | $gateway_ids[] = $bitcoin_gateway->id; |
57 | } |
58 | |
59 | return $gateway_ids; |
60 | } |
61 | |
62 | /** |
63 | * Before cancelling an old Bitcoin order, make sure to check it is not partially paid. |
64 | * |
65 | * Cancel the order if there are no transactions. |
66 | * |
67 | * @hooked woo_cao_before_cancel_order |
68 | * @see CAO::cancel_order() |
69 | * |
70 | * @param bool $should_cancel The already determined decision to cancel, default true. |
71 | * @param int $order_id The order id. |
72 | * @param WC_Order $order The order object itself. |
73 | * |
74 | * @return bool Should the order be cancelled? |
75 | */ |
76 | public function abort_canceling_partially_paid_order( bool $should_cancel, int $order_id, WC_Order $order ): bool { |
77 | |
78 | if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) { |
79 | return $should_cancel; |
80 | } |
81 | |
82 | try { |
83 | $bitcoin_order = $this->api->get_order_details( $order ); |
84 | } catch ( Exception $exception ) { |
85 | // If something is going wrong, do not automatically cancel the order. |
86 | return false; |
87 | } |
88 | |
89 | return empty( $bitcoin_order->get_address()->get_blockchain_transactions() ); |
90 | } |
91 | } |