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