Skip to content

Commit 4564879

Browse files
authored
Merge pull request #26 from ndeet/hpos
Adding HPOS/COT support.
2 parents 2c2c270 + cd8648e commit 4564879

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

btcpay-greenfield-for-woocommerce.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Author URI: https://btcpayserver.org
88
* Text Domain: btcpay-greenfield-for-woocommerce
99
* Domain Path: /languages
10-
* Version: 2.2.3
10+
* Version: 2.3.0
1111
* Requires PHP: 7.4
1212
* Tested up to: 6.3
1313
* Requires at least: 5.2
@@ -26,7 +26,7 @@
2626

2727
defined( 'ABSPATH' ) || exit();
2828

29-
define( 'BTCPAYSERVER_VERSION', '2.2.3' );
29+
define( 'BTCPAYSERVER_VERSION', '2.3.0' );
3030
define( 'BTCPAYSERVER_VERSION_KEY', 'btcpay_gf_version' );
3131
define( 'BTCPAYSERVER_PLUGIN_FILE_PATH', plugin_dir_path( __FILE__ ) );
3232
define( 'BTCPAYSERVER_PLUGIN_URL', plugin_dir_url(__FILE__ ) );
@@ -421,3 +421,10 @@ function init_btcpay_greenfield() {
421421
// Initialize payment gateways and plugin.
422422
add_filter( 'woocommerce_payment_gateways', [ 'BTCPayServerWCPlugin', 'initPaymentGateways' ] );
423423
add_action( 'plugins_loaded', 'init_btcpay_greenfield', 0 );
424+
425+
// Mark support for HPOS / COT.
426+
add_action( 'before_woocommerce_init', function() {
427+
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
428+
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
429+
}
430+
} );

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tags: bitcoin, btcpay, BTCPay Server, btcpayserver, WooCommerce, payment gateway
55
Requires at least: 5.2
66
Tested up to: 6.3
77
Requires PHP: 7.4
8-
Stable tag: 2.2.3
8+
Stable tag: 2.3.0
99
License: MIT
1010
License URI: https://github.com/btcpayserver/woocommerce-greenfield-plugin/blob/master/license.txt
1111

src/Gateway/AbstractGateway.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function process_payment( $orderId ) {
111111

112112
// Check for existing invoice and redirect instead.
113113
if ( $this->validInvoiceExists( $orderId ) ) {
114-
$existingInvoiceId = get_post_meta( $orderId, 'BTCPay_id', true );
114+
$existingInvoiceId = $order->get_meta( 'BTCPay_id' );
115115
Logger::debug( 'Found existing BTCPay Server invoice and redirecting to it. Invoice id: ' . $existingInvoiceId );
116116

117117
return [
@@ -546,12 +546,13 @@ protected function processOrderStatus(\WC_Order $order, \stdClass $webhookData)
546546
*/
547547
protected function validInvoiceExists( int $orderId ): bool {
548548
// Check order metadata for BTCPay_id.
549-
if ( $invoiceId = get_post_meta( $orderId, 'BTCPay_id', true ) ) {
549+
$order = wc_get_order($orderId);
550+
if ( $invoiceId = $order->get_meta( 'BTCPay_id' ) ) {
550551
// Validate the order status on BTCPay server.
551552
$client = new Invoice( $this->apiHelper->url, $this->apiHelper->apiKey );
552553
try {
553554
Logger::debug( 'Trying to fetch existing invoice from BTCPay Server.' );
554-
$invoice = $client->getInvoice( $this->apiHelper->storeId, $invoiceId );
555+
$invoice = $client->getInvoice( $this->apiHelper->storeId, $invoiceId );
555556
$invalidStates = [ 'Expired', 'Invalid' ];
556557
if ( in_array( $invoice->getData()['status'], $invalidStates ) ) {
557558
return false;
@@ -613,25 +614,28 @@ public function updateWCOrderPayments(\WC_Order $order): void {
613614
if ((float) $payment->getPaymentMethodPaid() > 0.0) {
614615
$paymentMethodName = $payment->getPaymentMethod();
615616
// Update order meta data with payment methods and transactions.
616-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_paid", $payment->getTotalPaid() ?? '' );
617-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_amount", $payment->getAmount() ?? '' );
618-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_due", $payment->getDue() ?? '' );
619-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_total_fee", $payment->getNetworkFee() ?? '' );
620-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_rate", $payment->getRate() ?? '' );
617+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_total_paid", $payment->getTotalPaid() ?? '' );
618+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_total_amount", $payment->getAmount() ?? '' );
619+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_total_due", $payment->getDue() ?? '' );
620+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_total_fee", $payment->getNetworkFee() ?? '' );
621+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_rate", $payment->getRate() ?? '' );
621622
if ((float) $payment->getRate() > 0.0) {
622623
$formattedRate = number_format((float) $payment->getRate(), wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator());
623-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_rateFormatted", $formattedRate );
624+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_rateFormatted", $formattedRate );
624625
}
625626

626627
// For each actual payment make a separate entry to make sense of it.
627628
foreach ($payment->getPayments() as $index => $trx) {
628-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_id", $trx->getTransactionId() ?? '' );
629-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_timestamp", $trx->getReceivedTimestamp() ?? '' );
630-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_destination", $trx->getDestination() ?? '' );
631-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_amount", $trx->getValue() ?? '' );
632-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_status", $trx->getStatus() ?? '' );
633-
update_post_meta( $order->get_id(), "BTCPay_{$paymentMethodName}_{$index}_networkFee", $trx->getFee() ?? '' );
629+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_id", $trx->getTransactionId() ?? '' );
630+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_timestamp", $trx->getReceivedTimestamp() ?? '' );
631+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_destination", $trx->getDestination() ?? '' );
632+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_amount", $trx->getValue() ?? '' );
633+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_status", $trx->getStatus() ?? '' );
634+
$order->update_meta_data( "BTCPay_{$paymentMethodName}_{$index}_networkFee", $trx->getFee() ?? '' );
634635
}
636+
637+
// Save the order.
638+
$order->save();
635639
}
636640
}
637641
} catch (\Throwable $e) {
@@ -766,8 +770,10 @@ protected function preparePosMetadata( $order ): string {
766770
*/
767771
protected function updateOrderMetadata( int $orderId, \BTCPayServer\Result\Invoice $invoice ) {
768772
// Store relevant BTCPay invoice data.
769-
update_post_meta( $orderId, 'BTCPay_redirect', $invoice->getData()['checkoutLink'] );
770-
update_post_meta( $orderId, 'BTCPay_id', $invoice->getData()['id'] );
773+
$order = wc_get_order($orderId);
774+
$order->update_meta_data( 'BTCPay_redirect', $invoice->getData()['checkoutLink'] );
775+
$order->update_meta_data( 'BTCPay_id', $invoice->getData()['id'] );
776+
$order->save();
771777
}
772778

773779
/**

0 commit comments

Comments
 (0)