Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Order | |
100.00% |
21 / 21 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_wc_order | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| new_transactions_seen | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| payment_received | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Constants for order meta-keys. |
| 4 | * |
| 5 | * Handles new transactions and payment actions. |
| 6 | * |
| 7 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 8 | */ |
| 9 | |
| 10 | namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce; |
| 11 | |
| 12 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Address; |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Services\Results\Check_Address_For_Payment_Service_Result; |
| 14 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money; |
| 15 | use Psr\Log\LoggerAwareInterface; |
| 16 | use Psr\Log\LoggerAwareTrait; |
| 17 | use Psr\Log\LoggerInterface; |
| 18 | use WC_Order; |
| 19 | |
| 20 | /** |
| 21 | * Defines constants for meta-keys. |
| 22 | * |
| 23 | * @see WooCommerce_Integration::define_order_hooks() |
| 24 | */ |
| 25 | class Order implements LoggerAwareInterface { |
| 26 | use LoggerAwareTrait; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | * |
| 31 | * @param API_WooCommerce_Interface $api_woocommerce Functions for querying/fetching crypto payment details for WooCommerce orders. |
| 32 | * @param LoggerInterface $logger PSR logger. |
| 33 | */ |
| 34 | public function __construct( |
| 35 | protected API_WooCommerce_Interface $api_woocommerce, |
| 36 | LoggerInterface $logger |
| 37 | ) { |
| 38 | $this->setLogger( $logger ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check hooked data is relevant / can return a WC_Order. |
| 43 | * |
| 44 | * @param ?string $integration_id The plugin integration. |
| 45 | * @param ?int $order_post_id The order post_id. |
| 46 | */ |
| 47 | protected function get_wc_order( |
| 48 | ?string $integration_id, |
| 49 | ?int $order_post_id, |
| 50 | ): ?WC_Order { |
| 51 | |
| 52 | if ( WooCommerce_Integration::class !== $integration_id ) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | $order = wc_get_order( $order_post_id ); |
| 57 | |
| 58 | if ( ! ( $order instanceof WC_Order ) ) { |
| 59 | // TODO: log... trashed? |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | return $order; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Record new transactions as order notes. |
| 68 | * |
| 69 | * @hooked bh_wp_bitcoin_gateway_new_transactions_seen |
| 70 | * |
| 71 | * @param string|class-string|null $integration_id Identifier for the integration the payment address was used by. |
| 72 | * @param ?int $order_post_id Identifier for the order the payment address was assigned to. |
| 73 | * @param Bitcoin_Address $payment_address The address the transactions were found for. |
| 74 | * @param Check_Address_For_Payment_Service_Result $check_address_for_payment_service_result The detail of existing and new transactions. |
| 75 | */ |
| 76 | public function new_transactions_seen( |
| 77 | ?string $integration_id, |
| 78 | ?int $order_post_id, |
| 79 | Bitcoin_Address $payment_address, |
| 80 | Check_Address_For_Payment_Service_Result $check_address_for_payment_service_result, |
| 81 | ): void { |
| 82 | |
| 83 | $wc_order = $this->get_wc_order( $integration_id, $order_post_id ); |
| 84 | |
| 85 | if ( ! $wc_order ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // TODO: Should also update the confirmed_received amount on the order meta. |
| 90 | |
| 91 | $this->api_woocommerce->add_order_note_for_transactions( |
| 92 | $wc_order, |
| 93 | $check_address_for_payment_service_result->get_new_transactions() |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Mark the order as paid. |
| 99 | * |
| 100 | * @hooked bh_wp_bitcoin_gateway_payment_received |
| 101 | * |
| 102 | * @param string|class-string|null $integration_id Identifier for the integration the payment address was used by. |
| 103 | * @param ?int $order_post_id Identifier for the order the payment address was assigned to. |
| 104 | * @param Bitcoin_Address $payment_address The address the transactions were found for. |
| 105 | * @param Check_Address_For_Payment_Service_Result $check_address_for_payment_service_result The detail of existing and new transactions, amounts. |
| 106 | */ |
| 107 | public function payment_received( |
| 108 | ?string $integration_id, |
| 109 | ?int $order_post_id, |
| 110 | Bitcoin_Address $payment_address, |
| 111 | Check_Address_For_Payment_Service_Result $check_address_for_payment_service_result, |
| 112 | ): void { |
| 113 | |
| 114 | $wc_order = $this->get_wc_order( $integration_id, $order_post_id ); |
| 115 | |
| 116 | if ( ! $wc_order ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $this->api_woocommerce->mark_order_paid( |
| 121 | $wc_order, |
| 122 | $check_address_for_payment_service_result |
| 123 | ); |
| 124 | } |
| 125 | } |