Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AJAX
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_order_details
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/**
3 * AJAX endpoint for fetching order information.
4 *
5 * Used on Thank You and my-account screens to query for transaction updates.
6 *
7 * @package    brianhenryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\Frontend;
11
12use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface;
13use Psr\Log\LoggerAwareTrait;
14use Psr\Log\LoggerInterface;
15
16/**
17 * Action `bh_wp_bitcoin_gateway_refresh_order_details` hooked to `wp_ajax` and `wp_ajax_nopriv`.
18 */
19class AJAX {
20    use LoggerAwareTrait;
21
22    /**
23     * Main class to get order information.
24     *
25     * @uses API_Interface::get_formatted_order_details()
26     */
27    protected API_Interface $api;
28
29    /**
30     * Constructor
31     *
32     * @param API_Interface   $api The main plugin functions.
33     * @param LoggerInterface $logger A PSR logger.
34     */
35    public function __construct( API_Interface $api, LoggerInterface $logger ) {
36        $this->setLogger( $logger );
37        $this->api = $api;
38    }
39
40    /**
41     * Return data for number of confirmations,
42     * is the order paid.
43     * does more need to be sent
44     *
45     * @hooked wp_ajax_bh_wp_bitcoin_gateway_refresh_order_details
46     *
47     * @return void
48     */
49    public function get_order_details() {
50
51        if ( ! check_ajax_referer( Frontend_Assets::class, false, false ) ) {
52            wp_send_json_error( array( 'message' => 'Bad/no nonce.' ), 400 );
53        }
54
55        if ( ! isset( $_POST['order_id'] ) ) {
56            wp_send_json_error( array( 'message' => 'No order id provided.' ), 400 );
57        }
58
59        $order_id = intval( wp_unslash( $_POST['order_id'] ) );
60
61        $order = wc_get_order( $order_id );
62
63        if ( ! ( $order instanceof \WC_Order ) ) {
64            wp_send_json_error( array( 'message' => 'Invalid order id' ), 400 );
65        }
66
67        // TODO: Include the order key in the AJAX request.
68        // if( $order->get_customer_id() !== get_current_user_id() && ! $order->key_is_valid( $key ) ) {
69        // wp_send_json_error( 'Not permitted', 401 );
70        // }
71
72        $result = $this->api->get_formatted_order_details( $order, true );
73
74        // These are the only keys used by the JavaScript.
75        $allowed_keys = array(
76            'btc_address',
77            'btc_total',
78            'order_id',
79            'btc_amount_received',
80            'status',
81            'amount_received',
82            'order_status_formatted',
83            'last_checked_time_formatted',
84        );
85
86        foreach ( array_keys( $result ) as $key ) {
87            if ( ! in_array( $key, $allowed_keys, true ) ) {
88                unset( $result[ $key ] );
89            }
90        }
91
92        wp_send_json_success( $result );
93    }
94}