Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Checkout
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 ensure_one_address_for_payment
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * TODO: Removed *required on billing fields.
4 *
5 * @see https://github.com/helgatheviking/wc-remove-billing-address-on-free-checkout
6 *
7 * @package    brianhenryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
11
12use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface;
13use Psr\Log\LoggerAwareInterface;
14use Psr\Log\LoggerAwareTrait;
15use Psr\Log\LoggerInterface;
16
17/**
18 * When the purchase button is clicked, make one or more query-transaction requests to find an unused address.
19 * Normally, there will be one address lined up ready to check, and in the unlikely case that that address is now
20 * used, the next one will almost definitely be unused.
21 */
22class Checkout implements LoggerAwareInterface {
23    use LoggerAwareTrait;
24
25    /**
26     * Constructor.
27     *
28     * @param API_Interface             $api The main plugin functions (to get/create the wallet).
29     * @param API_WooCommerce_Interface $api_woocommerce WooCommerce specific functions (to get the gateways).
30     * @param LoggerInterface           $logger PSR logger instance.
31     */
32    public function __construct(
33        protected API_Interface $api,
34        protected API_WooCommerce_Interface $api_woocommerce,
35        LoggerInterface $logger,
36    ) {
37        $this->setLogger( $logger );
38    }
39
40    /**
41     * When the checkout is loaded, synchronously ensure there is one address available so the gateway displays.
42     *
43     * This hopefully will never be the case because of scheduled jobs regularly checking.
44     *
45     * There is still the possibility of two customers at the checkout vying for the one address, but a second address
46     * would be created/checked/found after "Place Order" is clicked anyway.
47     *
48     * @hooked woocommerce_checkout_init
49     */
50    public function ensure_one_address_for_payment(): void {
51
52        $bitcoin_gateways = $this->api_woocommerce->get_bitcoin_gateways();
53
54        foreach ( $bitcoin_gateways as $bitcoin_gateway ) {
55            /**
56             * Distinct from {@see Bitcoin_Gateway::is_available()} which checks the number of unused addresses.
57             */
58            if ( 'yes' !== $bitcoin_gateway->enabled ) {
59                continue;
60            }
61
62            $master_public_key = $bitcoin_gateway->get_xpub();
63
64            // Probably a new gateway that is not configured.
65            if ( ! $master_public_key ) {
66                // TODO: log.
67                continue;
68            }
69
70            // Although it's safe to assume here that there was a Wallet created when the xpub was saved in the UI,
71            // this would create it anyway.
72            $wallet_result = $this->api->get_or_save_wallet_for_master_public_key( $master_public_key );
73
74            if ( ! $wallet_result->did_schedule_ensure_addresses ) {
75                $this->api->ensure_unused_addresses_for_wallet_synchronously( $wallet_result->wallet, 1 );
76            }
77        }
78    }
79}