mirror of
https://github.com/aljazceru/breez-woocommerce.git
synced 2025-12-18 22:44:22 +01:00
fixed checkout status
This commit is contained in:
@@ -114,6 +114,10 @@ function breez_wc_init() {
|
||||
require_once $full_path;
|
||||
}
|
||||
|
||||
// Add hooks for payment instructions on order received page
|
||||
add_action('woocommerce_thankyou_breez', 'breez_wc_thankyou_payment_instructions', 10);
|
||||
add_action('woocommerce_order_details_after_order_table', 'breez_wc_order_received_payment_instructions', 10);
|
||||
|
||||
// Verify WooCommerce settings
|
||||
if (!get_option('woocommerce_currency')) {
|
||||
throw new Exception('WooCommerce currency not configured');
|
||||
@@ -682,31 +686,67 @@ function breez_wc_filter_blocks_checkout_order_data($order, $request) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Display payment instructions on the payment page
|
||||
* Display payment instructions on the thank you page
|
||||
*/
|
||||
function breez_wc_display_payment_instructions() {
|
||||
if (!isset($_GET['show_payment']) || !isset($_GET['order_id'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$order_id = absint($_GET['order_id']);
|
||||
function breez_wc_thankyou_payment_instructions($order_id) {
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
if (!$order || $order->get_payment_method() !== 'breez') {
|
||||
if (!$order || $order->get_payment_method() !== 'breez' || $order->has_status('processing')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify order key
|
||||
if (!isset($_GET['key']) || $_GET['key'] !== $order->get_order_key()) {
|
||||
// Get payment details from database
|
||||
$db_manager = new Breez_DB_Manager();
|
||||
$payment = $db_manager->get_payment_by_order($order_id);
|
||||
|
||||
if (!$payment) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get gateway instance
|
||||
$gateways = WC()->payment_gateways->payment_gateways();
|
||||
$gateway = isset($gateways['breez']) ? $gateways['breez'] : null;
|
||||
// Load the payment instructions template
|
||||
wc_get_template(
|
||||
'payment-instructions.php',
|
||||
array(
|
||||
'order' => $order,
|
||||
'invoice_id' => $payment['invoice_id'],
|
||||
'payment_method' => $payment['metadata']['payment_method'],
|
||||
'expiry' => $payment['metadata']['expires_at'],
|
||||
'current_time' => time(),
|
||||
'payment_status' => $payment['status']
|
||||
),
|
||||
'',
|
||||
BREEZ_WC_PLUGIN_DIR . 'templates/'
|
||||
);
|
||||
}
|
||||
|
||||
if ($gateway) {
|
||||
$gateway->payment_page($order_id);
|
||||
/**
|
||||
* Display payment instructions on the order received page
|
||||
*/
|
||||
function breez_wc_order_received_payment_instructions($order) {
|
||||
if (!$order || $order->get_payment_method() !== 'breez' || $order->has_status('processing')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get payment details from database
|
||||
$db_manager = new Breez_DB_Manager();
|
||||
$payment = $db_manager->get_payment_by_order($order->get_id());
|
||||
|
||||
if (!$payment) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Load the payment instructions template
|
||||
wc_get_template(
|
||||
'payment-instructions.php',
|
||||
array(
|
||||
'order' => $order,
|
||||
'invoice_id' => $payment['invoice_id'],
|
||||
'payment_method' => $payment['metadata']['payment_method'],
|
||||
'expiry' => $payment['metadata']['expires_at'],
|
||||
'current_time' => time(),
|
||||
'payment_status' => $payment['status']
|
||||
),
|
||||
'',
|
||||
BREEZ_WC_PLUGIN_DIR . 'templates/'
|
||||
);
|
||||
}
|
||||
add_action('woocommerce_receipt_breez', 'breez_wc_display_payment_instructions');
|
||||
|
||||
@@ -254,100 +254,79 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
*/
|
||||
protected function get_available_payment_methods() {
|
||||
if (empty($this->payment_methods)) {
|
||||
return ['lightning']; // Default to lightning if nothing is set
|
||||
return ['LIGHTNING']; // Default to LIGHTNING if nothing is set
|
||||
}
|
||||
|
||||
if (is_array($this->payment_methods)) {
|
||||
return array_filter(array_map('trim', $this->payment_methods));
|
||||
return array_map(function($method) {
|
||||
return strtoupper(trim($method));
|
||||
}, $this->payment_methods);
|
||||
}
|
||||
|
||||
return array_filter(array_map('trim', explode(',', $this->payment_methods)));
|
||||
return array_map(function($method) {
|
||||
return strtoupper(trim($method));
|
||||
}, explode(',', $this->payment_methods));
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the payment and return the result
|
||||
* Process the payment
|
||||
*
|
||||
* @param int $order_id
|
||||
* @return array
|
||||
*/
|
||||
public function process_payment($order_id) {
|
||||
try {
|
||||
$this->logger->log("Processing payment for order #$order_id", 'debug');
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
if (!$order) {
|
||||
throw new Exception(__('Order not found', 'breez-woocommerce'));
|
||||
}
|
||||
|
||||
// Check for existing payment in database
|
||||
$existing_payment = $this->db_manager->get_payment_by_order($order_id);
|
||||
$current_time = time();
|
||||
$this->logger->log("Processing payment for order #$order_id", 'debug');
|
||||
|
||||
if ($existing_payment && $existing_payment['status'] === 'pending') {
|
||||
$payment_created = strtotime($existing_payment['created_at']);
|
||||
$expiry_time = $payment_created + ($this->expiry_minutes * 60);
|
||||
|
||||
if ($current_time < $expiry_time) {
|
||||
$this->logger->log("Using existing valid payment for order #$order_id", 'debug');
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $this->get_return_url($order)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the selected payment method
|
||||
// Get payment method from request
|
||||
$payment_method = $this->get_payment_method_from_request();
|
||||
|
||||
// Check for blocks payment data in order meta
|
||||
$blocks_payment_data = $order->get_meta('_breez_blocks_payment_data');
|
||||
if (!$payment_method && $blocks_payment_data) {
|
||||
$this->logger->log("Found blocks payment data in order meta", 'debug');
|
||||
$data = is_string($blocks_payment_data) ? json_decode($blocks_payment_data, true) : $blocks_payment_data;
|
||||
if (is_array($data) && isset($data['breez_payment_method'])) {
|
||||
$payment_method = sanitize_text_field($data['breez_payment_method']);
|
||||
$this->logger->log("Using payment method from blocks data: $payment_method", 'debug');
|
||||
}
|
||||
}
|
||||
|
||||
if (!$payment_method) {
|
||||
throw new Exception(__('No payment method selected', 'breez-woocommerce'));
|
||||
}
|
||||
$this->logger->log("Selected payment method: $payment_method", 'debug');
|
||||
|
||||
// Validate payment method
|
||||
$available_methods = $this->get_available_payment_methods();
|
||||
if (!in_array($payment_method, $available_methods)) {
|
||||
throw new Exception(__('Invalid payment method', 'breez-woocommerce'));
|
||||
}
|
||||
|
||||
// Get the order total and convert to satoshis
|
||||
// Get order details
|
||||
$order_total = $order->get_total();
|
||||
$currency = $order->get_currency();
|
||||
|
||||
// Get exchange rate and convert to satoshis
|
||||
// Get exchange rate
|
||||
$exchange_rate_response = $this->client->request('GET', "/exchange_rates/{$currency}");
|
||||
$this->logger->log("Exchange rate response: " . print_r($exchange_rate_response, true), 'debug');
|
||||
|
||||
if (!$exchange_rate_response || !isset($exchange_rate_response['rate'])) {
|
||||
throw new Exception(__('Failed to get exchange rate', 'breez-woocommerce'));
|
||||
}
|
||||
|
||||
// Calculate amount in satoshis
|
||||
// The exchange rate is in fiat/BTC, so we need to:
|
||||
// 1. Convert order total to BTC by dividing by the rate
|
||||
// 2. Convert BTC to satoshis
|
||||
$btc_amount = $order_total / $exchange_rate_response['rate'];
|
||||
$amount_sat = (int)($btc_amount * 100000000); // Convert BTC to sats
|
||||
$amount_sat = (int)round($btc_amount * 100000000);
|
||||
|
||||
$this->logger->log("Order total: {$order_total} {$currency}, Amount in sats: {$amount_sat}", 'debug');
|
||||
|
||||
if ($amount_sat <= 0) {
|
||||
throw new Exception(__('Invalid amount conversion', 'breez-woocommerce'));
|
||||
// Validate amount is within reasonable range (1000 to 100000000 sats)
|
||||
if ($amount_sat < 1000 || $amount_sat > 100000000) {
|
||||
throw new Exception(sprintf(
|
||||
__('Invalid payment amount calculated: %d sats. Please check exchange rate calculation.', 'breez-woocommerce'),
|
||||
$amount_sat
|
||||
));
|
||||
}
|
||||
|
||||
// Create payment request
|
||||
$this->logger->log("Exchange rate calculation:", 'debug');
|
||||
$this->logger->log("Order total ({$currency}): {$order_total}", 'debug');
|
||||
$this->logger->log("Exchange rate ({$currency}/BTC): {$exchange_rate_response['rate']}", 'debug');
|
||||
$this->logger->log("BTC amount: {$btc_amount}", 'debug');
|
||||
$this->logger->log("Final amount (sats): {$amount_sat}", 'debug');
|
||||
|
||||
// Prepare payment data
|
||||
$payment_data = array(
|
||||
'amount' => $amount_sat,
|
||||
'method' => $payment_method === 'onchain' ? 'BITCOIN_ADDRESS' : 'LIGHTNING',
|
||||
'description' => sprintf(__('Payment for order #%s', 'breez-woocommerce'), $order->get_order_number())
|
||||
'method' => $payment_method,
|
||||
'description' => sprintf(__('Order #%s', 'breez-woocommerce'), $order->get_order_number()),
|
||||
'source' => 'woocommerce'
|
||||
);
|
||||
|
||||
// Log the payment data being sent
|
||||
$this->logger->log("Creating payment with data: " . print_r($payment_data, true), 'debug');
|
||||
|
||||
// Create the payment
|
||||
@@ -402,21 +381,14 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
// Empty cart
|
||||
WC()->cart->empty_cart();
|
||||
|
||||
// Return success with redirect to payment instructions page
|
||||
$redirect_url = add_query_arg(
|
||||
array(
|
||||
'order_id' => $order->get_id(),
|
||||
'key' => $order->get_order_key(),
|
||||
'show_payment' => true
|
||||
),
|
||||
$order->get_checkout_payment_url(true)
|
||||
);
|
||||
|
||||
$this->logger->log("Redirecting to payment page: $redirect_url", 'debug');
|
||||
// Store payment URL in order meta
|
||||
$order->update_meta_data('_breez_payment_url', $response['destination']);
|
||||
$order->save();
|
||||
|
||||
// Return success and redirect to order received page
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $redirect_url
|
||||
'redirect' => $order->get_checkout_order_received_url()
|
||||
);
|
||||
|
||||
} catch (Exception $e) {
|
||||
@@ -438,7 +410,7 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
public function get_payment_method_from_request() {
|
||||
// Priority 1: Direct POST parameter for breez_payment_method (from blocks)
|
||||
if (isset($_POST['breez_payment_method'])) {
|
||||
$method = sanitize_text_field($_POST['breez_payment_method']);
|
||||
$method = strtoupper(sanitize_text_field($_POST['breez_payment_method']));
|
||||
$this->logger->log("Payment method found in direct POST: $method", 'debug');
|
||||
return $method;
|
||||
}
|
||||
@@ -462,7 +434,7 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
|
||||
// Extract payment method
|
||||
if (is_array($raw_data) && isset($raw_data['breez_payment_method'])) {
|
||||
$method = sanitize_text_field($raw_data['breez_payment_method']);
|
||||
$method = strtoupper(sanitize_text_field($raw_data['breez_payment_method']));
|
||||
$this->logger->log("Payment method found in payment_data: $method", 'debug');
|
||||
return $method;
|
||||
}
|
||||
@@ -473,7 +445,7 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
if (isset($_POST['wc-breez-payment-data'])) {
|
||||
$blocks_data = json_decode(wp_unslash($_POST['wc-breez-payment-data']), true);
|
||||
if (is_array($blocks_data) && isset($blocks_data['breez_payment_method'])) {
|
||||
$method = sanitize_text_field($blocks_data['breez_payment_method']);
|
||||
$method = strtoupper(sanitize_text_field($blocks_data['breez_payment_method']));
|
||||
$this->logger->log("Payment method found in blocks data: $method", 'debug');
|
||||
return $method;
|
||||
}
|
||||
@@ -481,7 +453,7 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
|
||||
// Default fallback
|
||||
$available_methods = $this->get_available_payment_methods();
|
||||
$default_method = reset($available_methods); // Get first available method
|
||||
$default_method = strtoupper(reset($available_methods)); // Get first available method
|
||||
$this->logger->log("Using default payment method: $default_method", 'debug');
|
||||
return $default_method;
|
||||
}
|
||||
@@ -531,17 +503,18 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check current payment status
|
||||
$payment_status = 'pending';
|
||||
// Check current payment status (API status is in UPPERCASE)
|
||||
$api_payment_status = 'PENDING';
|
||||
try {
|
||||
$payment_status = $this->client->check_payment_status($invoice_id);
|
||||
$this->logger->log("Current payment status for order #$order_id: $payment_status", 'debug');
|
||||
$status_response = $this->client->check_payment_status($invoice_id);
|
||||
$api_payment_status = $status_response['status'];
|
||||
$this->logger->log("Current API payment status for order #$order_id: $api_payment_status", 'debug');
|
||||
} catch (Exception $e) {
|
||||
$this->logger->log("Error checking payment status: " . $e->getMessage(), 'error');
|
||||
}
|
||||
|
||||
// Update order status if needed
|
||||
if ($payment_status === 'SUCCEEDED' && $order->get_status() === 'pending') {
|
||||
// Update order status if needed (WooCommerce status is in lowercase)
|
||||
if (($api_payment_status === 'SUCCEEDED' || $api_payment_status === 'WAITING_CONFIRMATION') && $order->get_status() === 'pending') {
|
||||
$order->payment_complete();
|
||||
$order->add_order_note(__('Payment confirmed via Breez API.', 'breez-woocommerce'));
|
||||
$this->logger->log("Payment for order #$order_id confirmed via API check", 'debug');
|
||||
@@ -565,7 +538,7 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
'payment_method' => $payment_method,
|
||||
'expiry' => $expiry,
|
||||
'current_time' => $current_time,
|
||||
'payment_status' => $payment_status
|
||||
'payment_status' => $api_payment_status
|
||||
),
|
||||
'',
|
||||
BREEZ_WC_PLUGIN_DIR . 'templates/'
|
||||
@@ -662,12 +635,12 @@ class WC_Gateway_Breez extends WC_Payment_Gateway {
|
||||
|
||||
// Check exchange rate
|
||||
try {
|
||||
$rate = $this->payment_handler->get_btc_rate();
|
||||
if (!$rate) {
|
||||
$rate_response = $this->client->request('GET', '/exchange_rates/USD');
|
||||
if (!$rate_response || !isset($rate_response['rate'])) {
|
||||
$this->logger->log("Unable to get exchange rate", 'error');
|
||||
return false;
|
||||
}
|
||||
$this->logger->log("Exchange rate: $rate", 'debug');
|
||||
$this->logger->log("Exchange rate: " . $rate_response['rate'], 'debug');
|
||||
} catch (Exception $e) {
|
||||
$this->logger->log("Exchange rate error: " . $e->getMessage(), 'error');
|
||||
return false;
|
||||
|
||||
@@ -128,19 +128,16 @@ class Breez_API_Client {
|
||||
// If the payment is not found, return pending instead of throwing an error
|
||||
if ($response['status'] === 'UNKNOWN') {
|
||||
return array(
|
||||
'status' => 'pending',
|
||||
'status' => 'PENDING',
|
||||
'destination' => $invoice_id,
|
||||
'sdk_status' => 'UNKNOWN'
|
||||
);
|
||||
}
|
||||
|
||||
// Map SDK payment states to WooCommerce states
|
||||
$status = $this->map_payment_status($response['status']);
|
||||
|
||||
// Build response with all available details
|
||||
// Build response with all available details - keep original SDK status
|
||||
$result = array(
|
||||
'status' => $status,
|
||||
'sdk_status' => $response['status'], // Include original SDK status
|
||||
'status' => $response['status'], // Keep original SDK status (SUCCEEDED, WAITING_CONFIRMATION, etc)
|
||||
'sdk_status' => $response['status'],
|
||||
'destination' => $invoice_id,
|
||||
'amount_sat' => $response['amount_sat'] ?? null,
|
||||
'fees_sat' => $response['fees_sat'] ?? null,
|
||||
@@ -162,7 +159,7 @@ class Breez_API_Client {
|
||||
$this->logger->log('Payment status check error: ' . $e->getMessage(), 'error');
|
||||
// Return pending status instead of throwing an error
|
||||
return array(
|
||||
'status' => 'pending',
|
||||
'status' => 'PENDING',
|
||||
'sdk_status' => 'UNKNOWN',
|
||||
'destination' => $invoice_id,
|
||||
'error' => $e->getMessage()
|
||||
|
||||
@@ -39,19 +39,21 @@ if ($payment_method === 'LIGHTNING' && strpos($qr_data, 'lightning:') !== 0 && s
|
||||
?>
|
||||
|
||||
<div class="breez-payment-box">
|
||||
<h3><?php _e('Bitcoin/Lightning Payment', 'breez-woocommerce'); ?></h3>
|
||||
|
||||
<?php if ($payment_status === 'completed'): ?>
|
||||
<?php if ($payment_status === 'SUCCEEDED' || $payment_status === 'WAITING_CONFIRMATION'): ?>
|
||||
<div class="breez-payment-status breez-payment-completed">
|
||||
<p><?php _e('Payment received! Thank you for your payment.', 'breez-woocommerce'); ?></p>
|
||||
<p><?php _e('Your order is now being processed.', 'breez-woocommerce'); ?></p>
|
||||
</div>
|
||||
<?php elseif ($payment_status === 'failed'): ?>
|
||||
<?php elseif ($payment_status === 'FAILED'): ?>
|
||||
<div class="breez-payment-status breez-payment-failed">
|
||||
<p><?php _e('Payment failed or expired.', 'breez-woocommerce'); ?></p>
|
||||
<p><?php _e('Please contact us for assistance.', 'breez-woocommerce'); ?></p>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="breez-payment-instructions">
|
||||
<h3><?php _e('Complete Your Payment', 'breez-woocommerce'); ?></h3>
|
||||
<p><?php echo esc_html(get_option('woocommerce_breez_instructions')); ?></p>
|
||||
|
||||
<?php if ($time_left > 0): ?>
|
||||
<div class="breez-payment-countdown" data-expiry="<?php echo esc_attr($expiry); ?>">
|
||||
<p><?php _e('Time remaining: ', 'breez-woocommerce'); ?><span class="breez-countdown"><?php printf('%02d:%02d', $minutes_left, $seconds_left); ?></span></p>
|
||||
@@ -72,39 +74,44 @@ if ($payment_method === 'LIGHTNING' && strpos($qr_data, 'lightning:') !== 0 && s
|
||||
<?php _e('Copy', 'breez-woocommerce'); ?>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="breez-payment-info">
|
||||
<?php echo esc_html(get_option('woocommerce_breez_instructions')); ?>
|
||||
</p>
|
||||
<div class="breez-payment-status" id="breez-payment-status">
|
||||
<div class="breez-payment-pending">
|
||||
<p><?php _e('Waiting for payment...', 'breez-woocommerce'); ?></p>
|
||||
<div class="breez-spinner"></div>
|
||||
</div>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="breez-payment-expired">
|
||||
<p><?php _e('Payment time expired. Please contact support or place a new order.', 'breez-woocommerce'); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// QR Code generation
|
||||
var qrContainer = document.getElementById('breez-qr-container');
|
||||
if (qrContainer) {
|
||||
var qrData = qrContainer.getAttribute('data-qr-data');
|
||||
generateQRCode(qrData);
|
||||
if (qrData) {
|
||||
var qrScript = document.createElement('script');
|
||||
qrScript.src = 'https://cdn.jsdelivr.net/npm/qrcode-generator@1.4.4/qrcode.min.js';
|
||||
qrScript.onload = function() {
|
||||
var qr = qrcode(0, 'M');
|
||||
qr.addData(qrData);
|
||||
qr.make();
|
||||
qrContainer.innerHTML = qr.createImgTag(5);
|
||||
};
|
||||
document.head.appendChild(qrScript);
|
||||
}
|
||||
}
|
||||
|
||||
// Get API configuration
|
||||
var apiUrl = '<?php echo esc_js(get_option('woocommerce_breez_api_url')); ?>';
|
||||
var apiKey = '<?php echo esc_js(get_option('woocommerce_breez_api_key')); ?>';
|
||||
var invoiceId = '<?php echo esc_js($invoice_id); ?>';
|
||||
|
||||
// Ensure API URL ends with a slash
|
||||
apiUrl = apiUrl.replace(/\/?$/, '/');
|
||||
|
||||
// Countdown functionality
|
||||
// Countdown timer
|
||||
var countdownEl = document.querySelector('.breez-countdown');
|
||||
var expiryTime = parseInt(document.querySelector('.breez-payment-countdown')?.dataset.expiry, 10);
|
||||
var expiryTime = document.querySelector('.breez-payment-countdown')?.getAttribute('data-expiry');
|
||||
|
||||
if (countdownEl && expiryTime) {
|
||||
var countdownInterval = setInterval(function() {
|
||||
@@ -113,7 +120,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
if (timeLeft <= 0) {
|
||||
clearInterval(countdownInterval);
|
||||
clearInterval(statusCheckInterval); // Also clear status check
|
||||
clearInterval(statusCheckInterval);
|
||||
document.querySelector('.breez-payment-countdown').innerHTML = '<p><?php _e('Payment time expired.', 'breez-woocommerce'); ?></p>';
|
||||
document.querySelector('.breez-payment-qr').style.opacity = '0.3';
|
||||
|
||||
@@ -121,7 +128,6 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
var paymentBox = document.querySelector('.breez-payment-box');
|
||||
if (paymentBox) {
|
||||
paymentBox.innerHTML = `
|
||||
<h3><?php _e('Bitcoin/Lightning Payment', 'breez-woocommerce'); ?></h3>
|
||||
<div class="breez-payment-expired">
|
||||
<p><?php _e('Payment time expired. Please contact support or place a new order.', 'breez-woocommerce'); ?></p>
|
||||
</div>
|
||||
@@ -136,151 +142,61 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
// Payment status check functionality
|
||||
// Payment status check
|
||||
var invoiceId = '<?php echo esc_js($invoice_id); ?>';
|
||||
var orderId = '<?php echo esc_js($order->get_id()); ?>';
|
||||
|
||||
function checkPaymentStatus() {
|
||||
var paymentBox = document.querySelector('.breez-payment-box');
|
||||
|
||||
// Stop checking if payment box doesn't exist
|
||||
if (!paymentBox) {
|
||||
clearInterval(statusCheckInterval);
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop checking if payment is already completed or failed
|
||||
if (paymentBox.querySelector('.breez-payment-completed') ||
|
||||
paymentBox.querySelector('.breez-payment-failed') ||
|
||||
paymentBox.querySelector('.breez-payment-expired')) {
|
||||
clearInterval(statusCheckInterval);
|
||||
return;
|
||||
}
|
||||
|
||||
// Show loading indicator
|
||||
var statusIndicator = paymentBox.querySelector('.breez-payment-status');
|
||||
if (!statusIndicator) {
|
||||
statusIndicator = document.createElement('div');
|
||||
statusIndicator.className = 'breez-payment-status';
|
||||
paymentBox.appendChild(statusIndicator);
|
||||
}
|
||||
|
||||
// Use WordPress REST API endpoint instead of direct Breez API call
|
||||
fetch('/wp-json/breez-wc/v1/check-payment-status/' + invoiceId, {
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response error: ' + response.status);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
// Use the 'data' property from our API wrapper
|
||||
const paymentData = data.data || data;
|
||||
var paymentData = data.data || data;
|
||||
var statusContainer = document.getElementById('breez-payment-status');
|
||||
|
||||
// Clear any existing error message
|
||||
statusIndicator.classList.remove('breez-payment-error');
|
||||
if (!statusContainer) return;
|
||||
|
||||
// Update status message based on SDK status
|
||||
if (paymentData.sdk_status === 'PENDING' || paymentData.sdk_status === 'WAITING_FEE_ACCEPTANCE') {
|
||||
statusIndicator.innerHTML = `
|
||||
<div class="breez-payment-pending">
|
||||
<p>${paymentData.status_description || '<?php _e('Payment is being processed...', 'breez-woocommerce'); ?>'}</p>
|
||||
<div class="breez-spinner"></div>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
if (paymentData.sdk_status === 'WAITING_CONFIRMATION') {
|
||||
statusIndicator.innerHTML = `
|
||||
<div class="breez-payment-confirming">
|
||||
<p>${paymentData.status_description || '<?php _e('Payment received! Waiting for confirmation...', 'breez-woocommerce'); ?>'}</p>
|
||||
<div class="breez-spinner"></div>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
if (paymentData.sdk_status === 'SUCCEEDED') {
|
||||
// Stop checking status immediately
|
||||
if (paymentData.status === 'SUCCEEDED' || paymentData.status === 'WAITING_CONFIRMATION') {
|
||||
clearInterval(statusCheckInterval);
|
||||
|
||||
// Format amounts with commas for better readability
|
||||
const amountSats = paymentData.amount_sat ? Number(paymentData.amount_sat).toLocaleString() : '0';
|
||||
const feesSats = paymentData.fees_sat ? Number(paymentData.fees_sat).toLocaleString() : '0';
|
||||
|
||||
// Update UI to show payment completed with amount details
|
||||
paymentBox.innerHTML = `
|
||||
<h3><?php _e('Bitcoin/Lightning Payment', 'breez-woocommerce'); ?></h3>
|
||||
<div class="breez-payment-status breez-payment-completed">
|
||||
<p>${paymentData.status_description || '<?php _e('Payment confirmed! Thank you for your payment.', 'breez-woocommerce'); ?>'}</p>
|
||||
statusContainer.innerHTML = `
|
||||
<div class="breez-payment-completed">
|
||||
<p><?php _e('Payment received! Thank you for your payment.', 'breez-woocommerce'); ?></p>
|
||||
<p><?php _e('Your order is now being processed.', 'breez-woocommerce'); ?></p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Notify WordPress about the completed payment
|
||||
notifyServer(paymentData);
|
||||
|
||||
// Double check interval is cleared
|
||||
if (statusCheckInterval) {
|
||||
// Reload page after successful payment
|
||||
setTimeout(() => window.location.reload(), 2000);
|
||||
} else if (paymentData.status === 'FAILED') {
|
||||
clearInterval(statusCheckInterval);
|
||||
statusCheckInterval = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (paymentData.sdk_status === 'FAILED') {
|
||||
// Stop checking status immediately
|
||||
clearInterval(statusCheckInterval);
|
||||
|
||||
// Show error message
|
||||
paymentBox.innerHTML = `
|
||||
<h3><?php _e('Bitcoin/Lightning Payment', 'breez-woocommerce'); ?></h3>
|
||||
<div class="breez-payment-status breez-payment-failed">
|
||||
<p>${paymentData.status_description || '<?php _e('Payment failed or expired.', 'breez-woocommerce'); ?>'}</p>
|
||||
<p>${paymentData.error || ''}</p>
|
||||
statusContainer.innerHTML = `
|
||||
<div class="breez-payment-failed">
|
||||
<p><?php _e('Payment failed or expired.', 'breez-woocommerce'); ?></p>
|
||||
<p><?php _e('Please try again or contact support if the problem persists.', 'breez-woocommerce'); ?></p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
// For unknown status, show generic message
|
||||
statusIndicator.innerHTML = `
|
||||
<div class="breez-payment-unknown">
|
||||
<p>${paymentData.status_description || '<?php _e('Checking payment status...', 'breez-woocommerce'); ?>'}</p>
|
||||
} else {
|
||||
// For PENDING or any other status
|
||||
statusContainer.innerHTML = `
|
||||
<div class="breez-payment-pending">
|
||||
<p><?php _e('Waiting for payment...', 'breez-woocommerce'); ?></p>
|
||||
<div class="breez-spinner"></div>
|
||||
</div>
|
||||
`;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error checking payment status:', error);
|
||||
// Don't show error message for status check failures
|
||||
// Just log to console and continue checking
|
||||
});
|
||||
}
|
||||
|
||||
// Function to notify WordPress about payment status changes
|
||||
function notifyServer(paymentData) {
|
||||
fetch('/wp-json/breez-wc/v1/check-payment-status?order_id=<?php echo esc_js($order->get_id()); ?>', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': '<?php echo esc_js(wp_create_nonce('wp_rest')); ?>'
|
||||
},
|
||||
body: JSON.stringify(paymentData)
|
||||
})
|
||||
.catch(error => console.error('Error notifying server:', error));
|
||||
.catch(error => console.error('Error checking payment status:', error));
|
||||
}
|
||||
|
||||
// Check payment status every 5 seconds
|
||||
var statusCheckInterval = setInterval(checkPaymentStatus, 5000);
|
||||
|
||||
// Do an initial check immediately
|
||||
// Initial check
|
||||
checkPaymentStatus();
|
||||
|
||||
// Copy invoice button functionality
|
||||
// Copy invoice functionality
|
||||
var copyButton = document.querySelector('.breez-copy-button');
|
||||
if (copyButton) {
|
||||
copyButton.addEventListener('click', function() {
|
||||
@@ -293,8 +209,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
try {
|
||||
document.execCommand('copy');
|
||||
this.textContent = '<?php _e('Copied!', 'breez-woocommerce'); ?>';
|
||||
setTimeout(function() {
|
||||
copyButton.textContent = '<?php _e('Copy', 'breez-woocommerce'); ?>';
|
||||
setTimeout(() => {
|
||||
this.textContent = '<?php _e('Copy', 'breez-woocommerce'); ?>';
|
||||
}, 2000);
|
||||
} catch (err) {
|
||||
console.error('Failed to copy:', err);
|
||||
@@ -303,110 +219,114 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
document.body.removeChild(textarea);
|
||||
});
|
||||
}
|
||||
|
||||
// QR Code generation function
|
||||
function generateQRCode(data) {
|
||||
// Try QRServer.com first
|
||||
var primaryUrl = 'https://api.qrserver.com/v1/create-qr-code/?' + new URLSearchParams({
|
||||
data: data,
|
||||
size: '300x300',
|
||||
margin: '10',
|
||||
format: 'svg',
|
||||
qzone: '1',
|
||||
color: '000000',
|
||||
bgcolor: 'FFFFFF'
|
||||
}).toString();
|
||||
|
||||
// Fallback URL (Google Charts)
|
||||
var fallbackUrl = 'https://chart.googleapis.com/chart?' + new URLSearchParams({
|
||||
chs: '300x300',
|
||||
cht: 'qr',
|
||||
chl: data,
|
||||
choe: 'UTF-8',
|
||||
chld: 'M|4'
|
||||
}).toString();
|
||||
|
||||
// Create image element
|
||||
var img = new Image();
|
||||
img.style.cssText = 'display:block; max-width:300px; height:auto;';
|
||||
img.alt = 'QR Code';
|
||||
|
||||
// Try primary service first
|
||||
img.onerror = function() {
|
||||
// If primary fails, try fallback
|
||||
img.src = fallbackUrl;
|
||||
};
|
||||
|
||||
img.onload = function() {
|
||||
qrContainer.innerHTML = '';
|
||||
var wrapper = document.createElement('div');
|
||||
wrapper.className = 'breez-qr-wrapper';
|
||||
wrapper.style.cssText = 'background:white; padding:15px; border-radius:8px; box-shadow:0 2px 4px rgba(0,0,0,0.1); display:inline-block;';
|
||||
wrapper.appendChild(img);
|
||||
qrContainer.appendChild(wrapper);
|
||||
};
|
||||
|
||||
// Start loading primary QR code
|
||||
img.src = primaryUrl;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.breez-payment-box {
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
margin: 2em auto;
|
||||
padding: 20px;
|
||||
background: #f8f8f8;
|
||||
border-radius: 5px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.breez-payment-instructions {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.breez-payment-instructions h3 {
|
||||
margin-bottom: 1em;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.breez-payment-status {
|
||||
padding: 15px;
|
||||
border-radius: 5px;
|
||||
margin-bottom: 20px;
|
||||
margin: 10px 0;
|
||||
border-radius: 4px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.breez-payment-completed {
|
||||
background: #d4edda;
|
||||
background-color: #d4edda;
|
||||
border: 1px solid #c3e6cb;
|
||||
color: #155724;
|
||||
}
|
||||
|
||||
.breez-payment-failed {
|
||||
background: #f8d7da;
|
||||
background-color: #f8d7da;
|
||||
border: 1px solid #f5c6cb;
|
||||
color: #721c24;
|
||||
}
|
||||
|
||||
.breez-payment-countdown {
|
||||
font-size: 18px;
|
||||
margin-bottom: 20px;
|
||||
margin: 1em 0;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.breez-countdown {
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.breez-payment-qr {
|
||||
margin-bottom: 20px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
margin: 2em auto;
|
||||
max-width: 300px;
|
||||
}
|
||||
|
||||
.breez-qr-loading {
|
||||
text-align: center;
|
||||
padding: 20px;
|
||||
.breez-payment-qr img {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.breez-invoice-container {
|
||||
position: relative;
|
||||
margin: 1em auto;
|
||||
max-width: 500px;
|
||||
}
|
||||
|
||||
.breez-invoice-text {
|
||||
width: 100%;
|
||||
padding: 10px 40px 10px 10px;
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
resize: none;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.breez-copy-button {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
padding: 5px 10px;
|
||||
background: #0073aa;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 3px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.breez-copy-button:hover {
|
||||
background: #005177;
|
||||
}
|
||||
|
||||
.breez-spinner {
|
||||
display: inline-block;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 4px solid #f3f3f3;
|
||||
border-top: 4px solid #3498db;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border: 2px solid #f3f3f3;
|
||||
border-top: 2px solid #3498db;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin-bottom: 10px;
|
||||
margin: 0 10px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
@@ -414,69 +334,22 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.breez-invoice-container {
|
||||
position: relative;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.breez-invoice-text {
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
font-family: monospace;
|
||||
font-size: 14px;
|
||||
resize: none;
|
||||
border: 1px solid #ddd;
|
||||
.breez-payment-pending,
|
||||
.breez-payment-confirming {
|
||||
background-color: #fff3cd;
|
||||
border: 1px solid #ffeeba;
|
||||
color: #856404;
|
||||
padding: 15px;
|
||||
margin: 1em 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.breez-copy-button {
|
||||
position: absolute;
|
||||
right: 10px;
|
||||
top: 10px;
|
||||
padding: 5px 10px;
|
||||
background: #0073aa;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 3px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.breez-copy-button:hover {
|
||||
background: #005177;
|
||||
}
|
||||
|
||||
.breez-payment-info {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.breez-payment-expired {
|
||||
background: #f8d7da;
|
||||
background-color: #f8d7da;
|
||||
border: 1px solid #f5c6cb;
|
||||
color: #721c24;
|
||||
padding: 15px;
|
||||
border-radius: 5px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.breez-payment-error {
|
||||
background: #fff3cd;
|
||||
color: #856404;
|
||||
padding: 10px;
|
||||
margin: 10px 0;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #ffeeba;
|
||||
}
|
||||
|
||||
.breez-error-details {
|
||||
font-size: 0.9em;
|
||||
margin-top: 5px;
|
||||
color: #721c24;
|
||||
}
|
||||
|
||||
.breez-payment-details {
|
||||
font-size: 0.9em;
|
||||
margin-top: 15px;
|
||||
padding: 10px;
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
margin: 1em 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user