Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Woo_Cancel_Abandoned_Order
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 enable_cao_for_bitcoin
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 abort_canceling_partially_paid_order
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
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
14namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\Woo_Cancel_Abandoned_Order;
15
16use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface;
17use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\API_WooCommerce_Interface;
18use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\Model\WC_Bitcoin_Order;
19use Exception;
20use RVOLA\WOO\CAO\CAO;
21use WC_Order;
22
23/**
24 * Adds additional settings to automatically cancel on-hold orders at
25 * `wp-admin/admin.php?page=wc-settings&tab=checkout&section=bitcoin_gateway`.
26 */
27class Woo_Cancel_Abandoned_Order {
28
29    /**
30     * Constructor.
31     *
32     * @param API_Interface             $api The main plugin functions.
33     * @param API_WooCommerce_Interface $api_woocommerce The WooCommerce related functions.
34     */
35    public function __construct(
36        protected API_Interface $api,
37        protected API_WooCommerce_Interface $api_woocommerce,
38    ) {
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_woocommerce->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_woocommerce->is_order_has_bitcoin_gateway( $order_id ) ) {
79            return $should_cancel;
80        }
81
82        try {
83            /** @var WC_Bitcoin_Order $bitcoin_order */
84            $bitcoin_order = $this->api_woocommerce->get_order_details( $order );
85        } catch ( Exception ) {
86            // If something is going wrong, do not automatically cancel the order.
87            return false;
88        }
89
90        $address_transaction = $this->api->get_saved_transactions( $bitcoin_order->get_address() );
91
92        return empty( $address_transaction );
93    }
94}