Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
11.90% covered (danger)
11.90%
5 / 42
27.27% covered (danger)
27.27%
3 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Details_Formatter
11.90% covered (danger)
11.90%
5 / 42
27.27% covered (danger)
27.27%
3 / 11
191.02
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
 get_btc_total_formatted
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_btc_exchange_rate_formatted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_wc_order_status_formatted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_last_checked_time_formatted
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 get_btc_address_derivation_path_sequence_number
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_xpub_js_span
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get_exchange_rate_url
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 get_btc_amount_received_formatted
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_friendly_status
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 to_array
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BrianHenryIE\WP_Bitcoin_Gateway\API;
4
5use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Bitcoin_Order_Interface;
6
7// $currency    = $order_details['currency'];
8// $fiat_symbol = get_woocommerce_currency_symbol( $currency );
9//
10// TODO: Find a WooCommerce function which correctly places the currency symbol before/after.
11// $btc_price_at_at_order_time = $fiat_symbol . ' ' . $order_details['exchange_rate'];
12// $fiat_formatted_price       = $order->get_formatted_order_total();
13// $btc_price                  = $order_details['btc_price'];
14// $bitcoin_formatted_price    = $btc_symbol . wc_format_decimal( $btc_price, $round_btc );
15//
16// $btc_logo_url = $site_url . '/wp-content/plugins/bh-wp-bitcoin-gateway/assets/bitcoin.png';
17
18class Details_Formatter {
19    private Bitcoin_Order_Interface $order;
20
21    public function __construct( Bitcoin_Order_Interface $order ) {
22        $this->order = $order;
23    }
24
25    public function get_btc_total_formatted(): string {
26        // ฿ U+0E3F THAI CURRENCY SYMBOL BAHT, decimal: 3647, HTML: &#3647;, UTF-8: 0xE0 0xB8 0xBF, block: Thai.
27        $btc_symbol = '฿';
28        return $btc_symbol . ' ' . wc_trim_zeros( $this->order->get_btc_total_price() );
29    }
30
31    public function get_btc_exchange_rate_formatted(): string {
32        return wc_price( $this->order->get_btc_exchange_rate(), array( 'currency' => $this->order->get_currency() ) );
33    }
34
35    /**
36     * @param $order_status
37     *
38     * @return mixed
39     */
40    public function get_wc_order_status_formatted() {
41        return wc_get_order_statuses()[ 'wc-' . $this->order->get_status() ];
42    }
43
44    public function get_last_checked_time_formatted(): string {
45        if ( is_null( $this->order->get_last_checked_time() ) ) {
46            return __( 'Never', 'bh-wp-bitcoin-gateway' );
47        }
48        $date_format = get_option( 'date_format' );
49        $time_format = get_option( 'time_format' );
50        $timezone    = wp_timezone_string();
51        // $last_checked_time is in UTC... change it to local time.?
52        // The server time is not local time... maybe use their address?
53        // @see https://stackoverflow.com/tags/timezone/info
54        return $this->order->get_last_checked_time()->format( $date_format . ', ' . $time_format ) . ' ' . $timezone;
55    }
56
57    public function get_btc_address_derivation_path_sequence_number(): string {
58        return $this->order->get_address()->get_derivation_path_sequence_number();
59    }
60
61    public function get_xpub_js_span(): string {
62        $xpub                  = $this->order->get_address()->get_raw_address();
63        $xpub_friendly_display = substr( $xpub, 0, 7 ) . ' ... ' . substr( $xpub, - 3, 3 );
64        return "<span style=\"border-bottom: 1px dashed #999; word-wrap: break-word\" onclick=\"this.innerText = this.innerText === '{$xpub}' ? '{$xpub_friendly_display}' : '{$xpub}';\" title=\"{$xpub}\"'>{$xpub_friendly_display}</span>";
65    }
66
67    /**
68     *  Add a link showing the exchange rate around the time of the order ( -12 hours to +12 hours after payment).
69     */
70    public function get_exchange_rate_url(): string {
71        /**
72         * This supposedly could be null, but I can't imagine a scenario where WooCommerce returns an order object
73         * that doesn't have a DateTime for created.
74         *
75         * @var \DateTimeInterface $date_created
76         */
77        $date_created = $this->order->get_date_created();
78        $from         = $date_created->getTimestamp() - ( DAY_IN_SECONDS / 2 );
79        if ( ! is_null( $this->order->get_date_paid() ) ) {
80            $to = $this->order->get_date_paid()->getTimestamp() + ( DAY_IN_SECONDS / 2 );
81        } else {
82            $to = $from + DAY_IN_SECONDS;
83        }
84        return "https://www.blockchain.com/prices/BTC?from={$from}&to={$to}&timeSpan=custom&scale=0&style=line";
85    }
86
87    public function get_btc_amount_received_formatted(): string {
88        $btc_symbol = '฿';
89
90        // TODO: An address doesn't know how many confirmations an order wants.
91        // e.g. there could be dynamic number of confirmations based on order total
92
93        return $btc_symbol . ' ' . $this->order->get_address()->get_confirmed_balance();
94    }
95
96    public function get_friendly_status(): string {
97
98        // If the order is not marked paid, but has transactions, it is partly-paid.
99        switch ( true ) {
100            case $this->order->is_paid():
101                $result = __( 'Paid', 'bh-wp-bitcoin-gateway' );
102                break;
103            case ! empty( $this->order->get_address()->get_blockchain_transactions() ):
104                $result = __( 'Partly Paid', 'bh-wp-bitcoin-gateway' );
105                break;
106            default:
107                $result = __( 'Awaiting Payment', 'bh-wp-bitcoin-gateway' );
108        }
109
110        return $result;
111    }
112
113    /**
114     * @return array{btc_total_formatted:string, btc_exchange_rate_formatted:string, order_status_before_formatted:string, order_status_formatted:string, btc_amount_received_formatted:string, last_checked_time_formatted:string}
115     */
116    public function to_array(): array {
117
118        $result                                  = array();
119        $result['btc_total_formatted']           = $this->get_btc_total_formatted();
120        $result['btc_exchange_rate_formatted']   = $this->get_btc_exchange_rate_formatted();
121        $result['order_status_formatted']        = $this->get_wc_order_status_formatted();
122        $result['btc_amount_received_formatted'] = $this->get_btc_amount_received_formatted();
123        $result['last_checked_time_formatted']   = $this->get_last_checked_time_formatted();
124        $result['btc_address_derivation_path_sequence_number'] = $this->get_btc_address_derivation_path_sequence_number();
125        $result['parent_wallet_xpub_html']                     = $this->get_xpub_js_span();
126        $result['exchange_rate_url']                           = $this->get_exchange_rate_url();
127        $result['payment_status']                              = $this->get_friendly_status();
128        return $result;
129    }
130}