Skip to content

Commit 60fda82

Browse files
authored
Add support for modal checkout on woocommerce blocks (#39)
* Fix formatting. * Add modal checkout for woocommerce blocks.
1 parent 013d0e1 commit 60fda82

File tree

6 files changed

+335
-77
lines changed

6 files changed

+335
-77
lines changed

btcpay-greenfield-for-woocommerce.php

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Author URI: https://btcpayserver.org
88
* Text Domain: btcpay-greenfield-for-woocommerce
99
* Domain Path: /languages
10-
* Version: 2.4.1
10+
* Version: 2.5.0
1111
* Requires PHP: 7.4
1212
* Tested up to: 6.4
1313
* Requires at least: 5.2
@@ -26,7 +26,7 @@
2626

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

29-
define( 'BTCPAYSERVER_VERSION', '2.4.1' );
29+
define( 'BTCPAYSERVER_VERSION', '2.5.0' );
3030
define( 'BTCPAYSERVER_VERSION_KEY', 'btcpay_gf_version' );
3131
define( 'BTCPAYSERVER_PLUGIN_FILE_PATH', plugin_dir_path( __FILE__ ) );
3232
define( 'BTCPAYSERVER_PLUGIN_URL', plugin_dir_url(__FILE__ ) );
@@ -44,6 +44,8 @@ public function __construct() {
4444
add_action( 'wp_ajax_btcpaygf_notifications', [$this, 'processAjaxNotification'] );
4545
add_action( 'wp_ajax_nopriv_btcpaygf_modal_checkout', [$this, 'processAjaxModalCheckout'] );
4646
add_action( 'admin_enqueue_scripts', [$this, 'enqueueAdminScripts'] );
47+
add_action( 'wp_ajax_btcpaygf_modal_blocks_checkout', [$this, 'processAjaxModalBlocksCheckout'] );
48+
add_action( 'wp_ajax_nopriv_btcpaygf_modal_blocks_checkout', [$this, 'processAjaxModalBlocksCheckout'] );
4749

4850
// Run the updates.
4951
\BTCPayServer\WC\Helper\UpdateManager::processUpdates();
@@ -237,13 +239,14 @@ public function processAjaxApiUrl() {
237239
}
238240

239241
/**
240-
* Handles the AJAX callback from the Payment Request on the checkout page.
242+
* Handles the modal AJAX callback from the checkout page.
241243
*/
242244
public function processAjaxModalCheckout() {
243245

244246
Logger::debug('Entering ' . __METHOD__);
247+
Logger::debug('$_POST: ' . print_r($_POST, true));
245248

246-
$nonce = $_POST['apiNonce'];
249+
$nonce = sanitize_text_field($_POST['apiNonce']);
247250
if ( ! wp_verify_nonce( $nonce, 'btcpay-nonce' ) ) {
248251
wp_die('Unauthorized!', '', ['response' => 401]);
249252
}
@@ -261,13 +264,71 @@ public function processAjaxModalCheckout() {
261264
}
262265
}
263266

267+
/**
268+
* Handles the modal AJAX callback on the blocks checkout page.
269+
*/
270+
public function processAjaxModalBlocksCheckout() {
271+
272+
Logger::debug('Entering ' . __METHOD__);
273+
Logger::debug('$_POST: ' . print_r($_POST, true));
274+
275+
$nonce = sanitize_text_field($_POST['apiNonce']);
276+
if ( ! wp_verify_nonce( $nonce, 'btcpay-nonce' ) ) {
277+
wp_die('Unauthorized!', '', ['response' => 401]);
278+
}
279+
280+
if ( get_option('btcpay_gf_modal_checkout') !== 'yes' ) {
281+
wp_die('Modal checkout mode not enabled.', '', ['response' => 400]);
282+
}
283+
284+
$selectedPaymentGateway = sanitize_text_field($_POST['paymentGateway']);
285+
$orderId = sanitize_text_field($_POST['orderId']);
286+
$order = wc_get_order($orderId);
287+
288+
if ($order) {
289+
290+
$orderPaymentMethod = $order->get_payment_method();
291+
if (empty($orderPaymentMethod) || $orderPaymentMethod !== $selectedPaymentGateway) {
292+
$order->set_payment_method($selectedPaymentGateway);
293+
$order->save();
294+
}
295+
296+
$payment_gateways = \WC_Payment_Gateways::instance();
297+
298+
if ($payment_gateway = $payment_gateways->payment_gateways()[$selectedPaymentGateway]) {
299+
300+
// Run the process_payment() method.
301+
$result = $payment_gateway->process_payment($order->get_id());
302+
303+
if (isset($result['result']) && $result['result'] === 'success') {
304+
wp_send_json_success($result);
305+
} else {
306+
wp_send_json_error($result);
307+
}
308+
309+
} else {
310+
wp_send_json_error('Payment gateway not found.');
311+
}
312+
} else {
313+
wp_send_json_error('Order not found, stopped processing.');
314+
}
315+
316+
wp_die();
317+
}
318+
319+
/**
320+
* Handles the AJAX callback to dismiss review notification.
321+
*/
264322
public function processAjaxNotification() {
265323
check_ajax_referer('btcpaygf-notifications-nonce', 'nonce');
266324
// Dismiss review notice for 30 days.
267325
set_transient('btcpaygf_review_dismissed', true, DAY_IN_SECONDS * 30);
268326
wp_send_json_success();
269327
}
270328

329+
/**
330+
* Displays the payment status on the thank you page.
331+
*/
271332
public static function orderStatusThankYouPage($order_id)
272333
{
273334
if (!$order = wc_get_order($order_id)) {

languages/btcpay-greenfield-for-woocommerce.pot

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ msgstr ""
99
"MIME-Version: 1.0\n"
1010
"Content-Type: text/plain; charset=UTF-8\n"
1111
"Content-Transfer-Encoding: 8bit\n"
12-
"POT-Creation-Date: 2024-01-22T21:36:35+00:00\n"
12+
"POT-Creation-Date: 2024-01-31T10:40:12+00:00\n"
1313
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1414
"X-Generator: WP-CLI 2.9.0\n"
1515
"language-team: LANGUAGE <EMAIL@ADDRESS>\n"
@@ -35,84 +35,72 @@ msgstr ""
3535
msgid "https://btcpayserver.org"
3636
msgstr ""
3737

38-
#: btcpay-greenfield-for-woocommerce.php:135
38+
#: btcpay-greenfield-for-woocommerce.php:137
3939
msgid "Plugin not configured yet, please %1$sconfigure the plugin here%2$s"
4040
msgstr ""
4141

42-
#: btcpay-greenfield-for-woocommerce.php:153
42+
#: btcpay-greenfield-for-woocommerce.php:155
4343
msgid "Your PHP version is %s but BTCPay Greenfield Payment plugin requires version 7.4+."
4444
msgstr ""
4545

46-
#: btcpay-greenfield-for-woocommerce.php:159
46+
#: btcpay-greenfield-for-woocommerce.php:161
4747
msgid "WooCommerce seems to be not installed. Make sure you do before you activate BTCPayServer Payment Gateway."
4848
msgstr ""
4949

50-
#: btcpay-greenfield-for-woocommerce.php:165
50+
#: btcpay-greenfield-for-woocommerce.php:167
5151
msgid "The PHP cURL extension is not installed. Make sure it is available otherwise this plugin will not work."
5252
msgstr ""
5353

54-
#: btcpay-greenfield-for-woocommerce.php:175
54+
#: btcpay-greenfield-for-woocommerce.php:177
5555
msgid "Seems you have the old BTCPay for WooCommerce plugin installed. While it should work it is strongly recommended to not run both versions but rely on the maintained version (BTCPay Greenfield for WooCommerce)."
5656
msgstr ""
5757

58-
#: btcpay-greenfield-for-woocommerce.php:186
58+
#: btcpay-greenfield-for-woocommerce.php:188
5959
msgid "Thank you for using BTCPay for WooCommerce! If you like the plugin, we would love if you %1$sleave us a review%2$s."
6060
msgstr ""
6161

62-
#: btcpay-greenfield-for-woocommerce.php:370
62+
#: btcpay-greenfield-for-woocommerce.php:431
6363
msgid "Settings"
6464
msgstr ""
6565

66-
#: btcpay-greenfield-for-woocommerce.php:372
66+
#: btcpay-greenfield-for-woocommerce.php:433
6767
msgid "Debug log"
6868
msgstr ""
6969

70-
#: btcpay-greenfield-for-woocommerce.php:374
70+
#: btcpay-greenfield-for-woocommerce.php:435
7171
msgid "Docs"
7272
msgstr ""
7373

74-
#: btcpay-greenfield-for-woocommerce.php:376
74+
#: btcpay-greenfield-for-woocommerce.php:437
7575
msgid "Support Chat"
7676
msgstr ""
7777

78-
#: btcpay-greenfield-for-woocommerce.php:416
78+
#: btcpay-greenfield-for-woocommerce.php:477
7979
msgid "Error on verifiying redirected API wey with stored BTCPay Server url. Aborting API wizard. Please try again or do a manual setup."
8080
msgstr ""
8181

82-
#: btcpay-greenfield-for-woocommerce.php:438
82+
#: btcpay-greenfield-for-woocommerce.php:499
8383
msgid "Successfully received api key and store id from BTCPay Server API. Please finish setup by saving this settings form."
8484
msgstr ""
8585

86-
#: btcpay-greenfield-for-woocommerce.php:442
86+
#: btcpay-greenfield-for-woocommerce.php:503
8787
#: src/Admin/GlobalSettings.php:369
8888
msgid "Successfully registered a new webhook on BTCPay Server."
8989
msgstr ""
9090

91-
#: btcpay-greenfield-for-woocommerce.php:446
91+
#: btcpay-greenfield-for-woocommerce.php:507
9292
#: src/Admin/GlobalSettings.php:373
9393
msgid "Could not register a new webhook on the store."
9494
msgstr ""
9595

96-
#: btcpay-greenfield-for-woocommerce.php:455
96+
#: btcpay-greenfield-for-woocommerce.php:516
9797
msgid "Please make sure you only select one store on the BTCPay API authorization page."
9898
msgstr ""
9999

100-
#: btcpay-greenfield-for-woocommerce.php:460
100+
#: btcpay-greenfield-for-woocommerce.php:521
101101
msgid "Error processing the data from BTCPay. Please try again."
102102
msgstr ""
103103

104-
#: generated/BTCPay_GF_BTC.php:25
105-
#: generated/BTCPay_GF_BTC_LightningNetwork.php:25
106-
#: generated/BTCPay_GF_BTC_LNURLPAY.php:25
107-
msgid "Token type"
108-
msgstr ""
109-
110-
#: generated/BTCPay_GF_BTC.php:32
111-
#: generated/BTCPay_GF_BTC_LightningNetwork.php:32
112-
#: generated/BTCPay_GF_BTC_LNURLPAY.php:32
113-
msgid "Tokens of type promotion will not have a FIAT (USD, EUR, ..) exchange rate but counted as 1 per item quantity. See <a target=\"_blank\" href=\"https://docs.btcpayserver.org/FAQ/Integrations/#token-types\">here</a> for more details."
114-
msgstr ""
115-
116104
#: src/Admin/GlobalSettings.php:24
117105
msgid "BTCPay Settings"
118106
msgstr ""
@@ -403,97 +391,102 @@ msgstr ""
403391
msgid "Can't process order. Please contact us if the problem persists."
404392
msgstr ""
405393

406-
#: src/Gateway/AbstractGateway.php:204
394+
#: src/Gateway/AbstractGateway.php:219
407395
msgid "Refund of order "
408396
msgstr ""
409397

410-
#: src/Gateway/AbstractGateway.php:282
398+
#: src/Gateway/AbstractGateway.php:297
411399
msgid "Gateway Icon:"
412400
msgstr ""
413401

414-
#: src/Gateway/AbstractGateway.php:287
402+
#: src/Gateway/AbstractGateway.php:302
415403
msgid "Upload or select icon"
416404
msgstr ""
417405

418-
#: src/Gateway/AbstractGateway.php:293
406+
#: src/Gateway/AbstractGateway.php:308
419407
msgid "Remove image"
420408
msgstr ""
421409

422-
#: src/Gateway/AbstractGateway.php:343
410+
#: src/Gateway/AbstractGateway.php:358
423411
msgid "Use this image"
424412
msgstr ""
425413

426-
#: src/Gateway/AbstractGateway.php:344
414+
#: src/Gateway/AbstractGateway.php:359
427415
msgid "Insert image"
428416
msgstr ""
429417

430-
#: src/Gateway/AbstractGateway.php:382
418+
#: src/Gateway/AbstractGateway.php:397
431419
msgctxt "js"
432420
msgid "The invoice expired. Please try again, choose a different payment method or contact us if you paid but the payment did not confirm in time."
433421
msgstr ""
434422

435-
#: src/Gateway/AbstractGateway.php:383
423+
#: src/Gateway/AbstractGateway.php:398
436424
msgctxt "js"
437425
msgid "Payment aborted by you. Please try again or choose a different payment method."
438426
msgstr ""
439427

440-
#: src/Gateway/AbstractGateway.php:466
428+
#: src/Gateway/AbstractGateway.php:399
429+
msgctxt "js"
430+
msgid "Error processing checkout. Please try again or choose another payment option."
431+
msgstr ""
432+
433+
#: src/Gateway/AbstractGateway.php:495
441434
msgid "Webhook (%s) received from BTCPay, but the order is already processing or completed, skipping to update order status. Please manually check if everything is alright."
442435
msgstr ""
443436

444-
#: src/Gateway/AbstractGateway.php:478
437+
#: src/Gateway/AbstractGateway.php:507
445438
msgid "Invoice (partial) payment incoming (unconfirmed) after invoice was already expired."
446439
msgstr ""
447440

448-
#: src/Gateway/AbstractGateway.php:481
441+
#: src/Gateway/AbstractGateway.php:510
449442
msgid "Invoice (partial) payment incoming (unconfirmed). Waiting for settlement."
450443
msgstr ""
451444

452-
#: src/Gateway/AbstractGateway.php:500
445+
#: src/Gateway/AbstractGateway.php:529
453446
msgid "Invoice fully settled after invoice was already expired. Needs manual checking."
454447
msgstr ""
455448

456-
#: src/Gateway/AbstractGateway.php:505
449+
#: src/Gateway/AbstractGateway.php:534
457450
msgid "(Partial) payment settled but invoice not settled yet (could be more transactions incoming). Needs manual checking."
458451
msgstr ""
459452

460-
#: src/Gateway/AbstractGateway.php:509
453+
#: src/Gateway/AbstractGateway.php:538
461454
msgid "Invoice (partial) payment settled."
462455
msgstr ""
463456

464-
#: src/Gateway/AbstractGateway.php:519
457+
#: src/Gateway/AbstractGateway.php:548
465458
msgid "Invoice payment received fully with overpayment, waiting for settlement."
466459
msgstr ""
467460

468-
#: src/Gateway/AbstractGateway.php:521
461+
#: src/Gateway/AbstractGateway.php:550
469462
msgid "Invoice payment received fully, waiting for settlement."
470463
msgstr ""
471464

472-
#: src/Gateway/AbstractGateway.php:527
465+
#: src/Gateway/AbstractGateway.php:556
473466
msgid "Invoice manually marked invalid."
474467
msgstr ""
475468

476-
#: src/Gateway/AbstractGateway.php:529
469+
#: src/Gateway/AbstractGateway.php:558
477470
msgid "Invoice became invalid."
478471
msgstr ""
479472

480-
#: src/Gateway/AbstractGateway.php:535
473+
#: src/Gateway/AbstractGateway.php:564
481474
msgid "Invoice expired but was paid partially, please check."
482475
msgstr ""
483476

484-
#: src/Gateway/AbstractGateway.php:538
477+
#: src/Gateway/AbstractGateway.php:567
485478
msgid "Invoice expired."
486479
msgstr ""
487480

488-
#: src/Gateway/AbstractGateway.php:544
481+
#: src/Gateway/AbstractGateway.php:573
489482
msgid "Invoice payment settled but was overpaid."
490483
msgstr ""
491484

492-
#: src/Gateway/AbstractGateway.php:547
485+
#: src/Gateway/AbstractGateway.php:576
493486
msgid "Invoice payment settled."
494487
msgstr ""
495488

496-
#: src/Gateway/AbstractGateway.php:590
489+
#: src/Gateway/AbstractGateway.php:619
497490
msgid "BTCPay invoice manually set to invalid because customer went back to checkout and changed payment gateway."
498491
msgstr ""
499492

readme.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tags: Bitcoin, Lightning Network, BTCPay Server, WooCommerce, payment gateway, a
55
Requires at least: 5.2
66
Tested up to: 6.4
77
Requires PHP: 7.4
8-
Stable tag: 2.4.1
8+
Stable tag: 2.5.0
99
License: MIT
1010
License URI: https://github.com/btcpayserver/woocommerce-greenfield-plugin/blob/master/license.txt
1111

@@ -15,7 +15,7 @@ BTCPay Server is a free and open-source bitcoin payment processor which allows y
1515

1616
= Accept Bitcoin payments in your WooCommerce powered WordPress site with BTCPay Server =
1717

18-
BTCPay Server for WooCommerce is a revolutionary, self-hosted, open-source payment gateway to accept Bitcoin payments. Our** seamless integration** with WooCommerce allows you to connect your self-hosted [BTCPay Server](https://btcpayserver.org) and start accepting Bitcoin payments in **[just a few simple steps](https://docs.btcpayserver.org/WooCommerce)**.
18+
BTCPay Server for WooCommerce is a revolutionary, self-hosted, open-source payment gateway to accept Bitcoin payments. Our **seamless integration** with WooCommerce allows you to connect your self-hosted [BTCPay Server](https://btcpayserver.org) and start accepting Bitcoin payments in **[just a few simple steps](https://docs.btcpayserver.org/WooCommerce)**.
1919

2020
= Features: =
2121

@@ -111,10 +111,13 @@ You'll find extensive documentation and answers to many of your questions on [BT
111111
6. On BTCPay Server you have extensive reporting and accounting features.
112112

113113
== Upgrade Notice ==
114-
= 2.4.0 =
115-
* New feature: Add basic support for [WooCommerce cart and checkout blocks](https://woo.com/document/cart-checkout-blocks-status/).
114+
= 2.5.0 =
115+
* New feature: Added support for modal checkout with [WooCommerce checkout blocks](https://woo.com/document/cart-checkout-blocks-status/).
116116

117117
== Changelog ==
118+
= 2.5.0 :: 2024-01-31 =
119+
* Fix: Formatting in readme.txt
120+
* Add support for modal overlay for checkout blocks.
118121

119122
= 2.4.1 :: 2024-01-22 =
120123
* Fix: Ensure order status is not cancelled if paid by other payment gateway.

0 commit comments

Comments
 (0)