Skip to content

Commit

Permalink
Merge pull request #26 from ndeet/hpos
Browse files Browse the repository at this point in the history
Adding HPOS/COT support.
  • Loading branch information
ndeet authored Sep 6, 2023
2 parents 2c2c270 + cd8648e commit 4564879
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
11 changes: 9 additions & 2 deletions btcpay-greenfield-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://btcpayserver.org
* Text Domain: btcpay-greenfield-for-woocommerce
* Domain Path: /languages
* Version: 2.2.3
* Version: 2.3.0
* Requires PHP: 7.4
* Tested up to: 6.3
* Requires at least: 5.2
Expand All @@ -26,7 +26,7 @@

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

define( 'BTCPAYSERVER_VERSION', '2.2.3' );
define( 'BTCPAYSERVER_VERSION', '2.3.0' );
define( 'BTCPAYSERVER_VERSION_KEY', 'btcpay_gf_version' );
define( 'BTCPAYSERVER_PLUGIN_FILE_PATH', plugin_dir_path( __FILE__ ) );
define( 'BTCPAYSERVER_PLUGIN_URL', plugin_dir_url(__FILE__ ) );
Expand Down Expand Up @@ -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 );
}
} );
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: bitcoin, btcpay, BTCPay Server, btcpayserver, WooCommerce, payment gateway
Requires at least: 5.2
Tested up to: 6.3
Requires PHP: 7.4
Stable tag: 2.2.3
Stable tag: 2.3.0
License: MIT
License URI: https://github.com/btcpayserver/woocommerce-greenfield-plugin/blob/master/license.txt

Expand Down
40 changes: 23 additions & 17 deletions src/Gateway/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit 4564879

Please sign in to comment.