Skip to content

Commit

Permalink
Use order id as a reference consistently
Browse files Browse the repository at this point in the history
Builds on [this proposal](btcpayserver#45 (comment)) and removes the ambiguity of order ID and number:

- Replaces references to the order number with the actual order ID
- Removes the `woocommerce_order_id_from_number `, because it is obsolete then
- Adds order number as well as other custom data for reference to the `posData` field of the invoice
- Changes logging output to mention order ID as well as number
  • Loading branch information
dennisreimann committed Dec 11, 2020
1 parent f6fe197 commit ad2c25c
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/class-wc-gateway-btcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,9 @@ public function process_payment($order_id)
throw new \Exception('The BTCPay payment plugin was called to process a payment but could not retrieve the order details for order_id ' . $order_id . '. Cannot continue!');
}

$order_number = $order->get_order_number();
$notification_url = $this->get_option('notification_url', WC()->api_request_url('WC_Gateway_BtcPay'));
$this->log(' [Info] Generating payment form for order ' . $order->get_order_number() . '. Notify URL: ' . $notification_url);
$this->log(' [Info] Generating payment form for order ' . $order_id . ' (Order number ' . $order_number . '). Notify URL: ' . $notification_url);

// Mark new order according to user settings (we're awaiting the payment)
$new_order_states = $this->get_option('order_states');
Expand Down Expand Up @@ -735,8 +736,18 @@ public function process_payment($order_id)
$this->log(' [Info] Invoice object created successfully...');
}

$order_number = $order->get_order_number();
$invoice->setOrderId((string)$order_number);
$order_url = $order->get_edit_order_url();

$pos_data = array(
'Woocommerce Order ID' => $order_id,
'Woocommerce Order Number' => $order_number,
'Woocommerce Order URL' => $order_url,
'Woocommerce Thanks URL' => $thanks_link,
'Woocommerce Redirect URL' => $redirect_url
);

$invoice->setOrderId((string)$order_id);
$invoice->setPosData(json_encode($pos_data));
$invoice->setCurrency($currency);
$invoice->setFullNotifications(true);
$invoice->setExtendedNotifications(true);
Expand Down Expand Up @@ -777,7 +788,7 @@ public function process_payment($order_id)
$invoice->setTransactionSpeed($this->transaction_speed);

try {
$this->log(' [Info] Attempting to generate invoice for ' . $order->get_order_number() . '...');
$this->log(' [Info] Attempting to generate invoice for ' . $order_id . ' (Order number ' . $order_number . ') ...');

$invoice = $client->createInvoice($invoice);

Expand All @@ -788,7 +799,7 @@ public function process_payment($order_id)
$this->log(' [Info] Call to generate invoice was successful: ' . $client->getResponse()->getBody());
}
} catch (\Exception $e) {
$this->log(' [Error] Error generating invoice for ' . $order->get_order_number() . ', "' . $e->getMessage() . '"');
$this->log(' [Error] Error generating invoice for ' . $order_id . ' (Order number ' . $order_number . '), "' . $e->getMessage() . '"');
error_log($e->getMessage());

return array(
Expand Down Expand Up @@ -956,7 +967,7 @@ public function ipn_callback()
wp_die('Invalid IPN');
}
} catch (\Exception $e) {
$error_string = 'IPN Check: Can\'t find invoice ' . $json['id'];
$error_string = 'IPN Check: Can\'t find invoice ' . $json['id'] . ' (Order ID: ' . $json['orderId'] . ')';
$this->log(" [Error] $error_string");
$this->log(" [Error] " . $e->getMessage());

Expand All @@ -973,14 +984,9 @@ public function ipn_callback()
$this->log(' [Info] Order ID is: ' . $order_id);
}

//this is for the basic and advanced woocommerce order numbering plugins
//if we need to apply other filters, just add them in place of the this one
$order_id = apply_filters('woocommerce_order_id_from_number', $order_id);

$order = wc_get_order($order_id);
$this->log('$order = ' . $order. 'and order class = ' . get_class($order));


if (false === $order) {
$this->log(' [Error] The BTCPay payment plugin was called to process an IPN message but could not retrieve the order details for order_id: "' . $order_id . '". If you use an alternative order numbering system, please see class-wc-gateway-btcpay.php to apply a search filter.');
throw new \Exception('The BTCPay payment plugin was called to process an IPN message but could not retrieve the order details for order_id ' . $order_id . '. Cannot continue!');
Expand Down

0 comments on commit ad2c25c

Please sign in to comment.