Skip to content

Commit

Permalink
Adding HPOS/COT support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeet committed Aug 28, 2023
1 parent 2c2c270 commit 0fe6ea2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
7 changes: 7 additions & 0 deletions btcpay-greenfield-for-woocommerce.php
Original file line number Diff line number Diff line change
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 );
}
} );
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 0fe6ea2

Please sign in to comment.