Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
modal checkout: add global variable for invoice id at checkout, use f…
Browse files Browse the repository at this point in the history
…or validation
  • Loading branch information
adiled committed Apr 12, 2019
1 parent 040507a commit d76a23d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 141 deletions.
135 changes: 0 additions & 135 deletions src/assets/js/btcpay.js

This file was deleted.

22 changes: 16 additions & 6 deletions src/class-wc-gateway-btcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,12 @@ public function get_btcpay_redirect($order_id, $client)
$redirect = get_post_meta($order_id, 'BTCPay_redirect', true);
if($redirect)
{
$invoice_id = get_post_meta($order_id, 'BTCPay_id', true);;
$invoice_id = get_post_meta($order_id, 'BTCPay_id', true);
$invoice = $client->getInvoice($invoice_id);
$status = $invoice->getStatus();

if(true === isset($this->modal) && 'yes' == $this->modal) {
$redirect = "javascript:btcpay.showInvoice('".$invoice->getId()."')";
$redirect = get_btcpay_redirect_modal($invoice_id);
}

if($status === 'invalid' || $status === 'expired')
Expand All @@ -617,6 +617,16 @@ public function get_btcpay_redirect($order_id, $client)
return $redirect;
}

/**
* Return javascript to be used as checkout redirect parameter
*/
public function get_btcpay_redirect_modal($invoice_id)
{
return "javascript:window.btcpay_currentInvoiceId = $invoice_id;
btcpay.showInvoice($invoice_id)
"
}

/**
* Process the payment and return the result
*
Expand Down Expand Up @@ -849,7 +859,7 @@ public function process_payment($order_id)
$this->log(' [Info] BTCPay invoice assigned ' . $invoice->getId());
$this->log(' [Info] Leaving process_payment()...');

$redirect_url = (true === isset($this->modal) && 'yes' == $this->modal) ? "javascript:btcpay.showInvoice('".$invoice->getId()."')" : $invoice->getUrl();
$redirect_url = (true === isset($this->modal) && 'yes' == $this->modal) ? get_btcpay_redirect_modal($invoice->getId()) : $invoice->getUrl();

// Redirect the customer to the BitPay invoice
return array(
Expand Down Expand Up @@ -1581,10 +1591,10 @@ function action_woocommerce_thankyou_btcpay($order_id)
function enqueue_modal_js()
{
$ajax_url = admin_url( 'admin-ajax.php' );
wp_enqueue_script( 'btcpay-modal', plugin_dir_url( __FILE__ ) . 'assets/js/btcpay.js', array( 'jquery' ), null, false );
wp_enqueue_script( 'btcpay-modal', get_option('woocommerce_btcpay_url') . '/modal/btcpay.js', array( 'jquery' ), null, false );

wp_add_inline_script( 'btcpay-modal',
"btcpay.setApiUrlPrefix('".get_option('woocommerce_btcpay_url')."')
"
var ajax_url = '$ajax_url'
btcpay.onModalWillLeave(function() {
window.setTimeout(function() {
Expand All @@ -1593,7 +1603,7 @@ function enqueue_modal_js()
url: ajax_url,
data: {
action: 'fetch_order_status',
invoice_id: window.btcpay.getInvoiceId()
invoice_id: window.btcpay_currentInvoiceId
},
success: function(res) {
var data = JSON.parse(res)
Expand Down

0 comments on commit d76a23d

Please sign in to comment.