Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.75% covered (danger)
43.75%
7 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Thank_You
43.75% covered (danger)
43.75%
7 / 16
50.00% covered (danger)
50.00%
1 / 2
6.85
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 print_instructions
40.00% covered (danger)
40.00%
6 / 15
0.00% covered (danger)
0.00%
0 / 1
4.94
1<?php
2/**
3 * Print the payment details on the Thank You / order-received page.
4 *
5 * TODO: JS to scroll to the payment details.
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 Exception;
14use Psr\Log\LoggerAwareTrait;
15use Psr\Log\LoggerInterface;
16use WC_Order;
17
18/**
19 * Get the order details and pass them to the thank you page template.
20 */
21class Thank_You {
22    use LoggerAwareTrait;
23
24    const string TEMPLATE_NAME = 'checkout/thankyou-bitcoin-instructions-status.php';
25
26    /**
27     * Constructor
28     *
29     * @param API_WooCommerce_Interface $api The main plugin functions.
30     * @param LoggerInterface           $logger A PSR logger.
31     */
32    public function __construct(
33        protected API_WooCommerce_Interface $api,
34        LoggerInterface $logger,
35    ) {
36        $this->setLogger( $logger );
37    }
38
39    /**
40     * When the thank you page loads, if the order loading is a Bitcoin order, print the payment instructions (via
41     * the template).
42     *
43     * @uses API_WooCommerce_Interface::is_order_has_bitcoin_gateway() to check is the gateway relevant for this thank you page load.
44     *
45     * @hooked woocommerce_thankyou
46     *
47     * @param int $order_id The order if of the (presumably new) order.
48     *
49     * @return void Prints its output.
50     */
51    public function print_instructions( int $order_id ): void {
52
53        if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) {
54            return;
55        }
56
57        /**
58         * No need to check again does `wc_get_order()` return a `WC_Order` object because `API::is_order_has_bitcoin_gateway()`
59         * already has.
60         *
61         * @var WC_Order $order
62         */
63        $order = wc_get_order( $order_id );
64
65        try {
66            $template_args = $this->api->get_formatted_order_details( $order );
67        } catch ( Exception $exception ) {
68            // Exception sometimes occurs when an order has no Bitcoin address, although that's not likely the case here.
69            $this->logger->warning(
70                "Failed to get `shop_order:{$order_id}` details for Thank You template: {$exception->getMessage()}",
71                array(
72                    'order_id'  => $order_id,
73                    'exception' => $exception,
74                )
75            );
76            return;
77        }
78
79        $template_args['template'] = self::TEMPLATE_NAME;
80
81        wc_get_template( self::TEMPLATE_NAME, $template_args );
82    }
83}