From 0fe6ea241ca8acdf7feba6f70e59de13d9f53ea9 Mon Sep 17 00:00:00 2001 From: ndeet Date: Tue, 29 Aug 2023 00:35:34 +0200 Subject: [PATCH] Adding HPOS/COT support. --- btcpay-greenfield-for-woocommerce.php | 7 +++++ src/Gateway/AbstractGateway.php | 40 +++++++++++++++------------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/btcpay-greenfield-for-woocommerce.php b/btcpay-greenfield-for-woocommerce.php index 915d580..77e6179 100644 --- a/btcpay-greenfield-for-woocommerce.php +++ b/btcpay-greenfield-for-woocommerce.php @@ -421,3 +421,10 @@ function init_btcpay_greenfield() { // Initialize payment gateways and plugin. add_filter( 'woocommerce_payment_gateways', [ 'BTCPayServerWCPlugin', 'initPaymentGateways' ] ); add_action( 'plugins_loaded', 'init_btcpay_greenfield', 0 ); + +// Mark support for HPOS / COT. +add_action( 'before_woocommerce_init', function() { + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + } +} ); diff --git a/src/Gateway/AbstractGateway.php b/src/Gateway/AbstractGateway.php index 1067fcd..b715723 100644 --- a/src/Gateway/AbstractGateway.php +++ b/src/Gateway/AbstractGateway.php @@ -111,7 +111,7 @@ public function process_payment( $orderId ) { // Check for existing invoice and redirect instead. if ( $this->validInvoiceExists( $orderId ) ) { - $existingInvoiceId = get_post_meta( $orderId, 'BTCPay_id', true ); + $existingInvoiceId = $order->get_meta( 'BTCPay_id' ); Logger::debug( 'Found existing BTCPay Server invoice and redirecting to it. Invoice id: ' . $existingInvoiceId ); return [ @@ -546,12 +546,13 @@ protected function processOrderStatus(\WC_Order $order, \stdClass $webhookData) */ protected function validInvoiceExists( int $orderId ): bool { // Check order metadata for BTCPay_id. - if ( $invoiceId = get_post_meta( $orderId, 'BTCPay_id', true ) ) { + $order = wc_get_order($orderId); + if ( $invoiceId = $order->get_meta( 'BTCPay_id' ) ) { // Validate the order status on BTCPay server. $client = new Invoice( $this->apiHelper->url, $this->apiHelper->apiKey ); try { Logger::debug( 'Trying to fetch existing invoice from BTCPay Server.' ); - $invoice = $client->getInvoice( $this->apiHelper->storeId, $invoiceId ); + $invoice = $client->getInvoice( $this->apiHelper->storeId, $invoiceId ); $invalidStates = [ 'Expired', 'Invalid' ]; if ( in_array( $invoice->getData()['status'], $invalidStates ) ) { return false; @@ -613,25 +614,28 @@ public function updateWCOrderPayments(\WC_Order $order): void { if ((float) $payment->getPaymentMethodPaid() > 0.0) { $paymentMethodName = $payment->getPaymentMethod(); // Update order meta data with payment methods and transactions. - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_paid", $payment->getTotalPaid() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_amount", $payment->getAmount() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_due", $payment->getDue() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_fee", $payment->getNetworkFee() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_rate", $payment->getRate() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_total_paid", $payment->getTotalPaid() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_total_amount", $payment->getAmount() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_total_due", $payment->getDue() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_total_fee", $payment->getNetworkFee() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_rate", $payment->getRate() ?? '' ); if ((float) $payment->getRate() > 0.0) { $formattedRate = number_format((float) $payment->getRate(), wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator()); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_rateFormatted", $formattedRate ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_rateFormatted", $formattedRate ); } // For each actual payment make a separate entry to make sense of it. foreach ($payment->getPayments() as $index => $trx) { - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_id", $trx->getTransactionId() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_timestamp", $trx->getReceivedTimestamp() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_destination", $trx->getDestination() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_amount", $trx->getValue() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_status", $trx->getStatus() ?? '' ); - update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_networkFee", $trx->getFee() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_id", $trx->getTransactionId() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_timestamp", $trx->getReceivedTimestamp() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_destination", $trx->getDestination() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_amount", $trx->getValue() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_status", $trx->getStatus() ?? '' ); + $order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_networkFee", $trx->getFee() ?? '' ); } + + // Save the order. + $order->save(); } } } catch (\Throwable $e) { @@ -766,8 +770,10 @@ protected function preparePosMetadata( $order ): string { */ protected function updateOrderMetadata( int $orderId, \BTCPayServer\Result\Invoice $invoice ) { // Store relevant BTCPay invoice data. - update_post_meta( $orderId, 'BTCPay_redirect', $invoice->getData()['checkoutLink'] ); - update_post_meta( $orderId, 'BTCPay_id', $invoice->getData()['id'] ); + $order = wc_get_order($orderId); + $order->update_meta_data( 'BTCPay_redirect', $invoice->getData()['checkoutLink'] ); + $order->update_meta_data( 'BTCPay_id', $invoice->getData()['id'] ); + $order->save(); } /**