@@ -466,6 +466,10 @@ function ( $field ) {
466
466
* @param WC_Order $order The order that needs payment.
467
467
*/
468
468
public function localize_pay_for_order_page_scripts ( $ order ) {
469
+ // Ensure the script is registered before localizing
470
+ if ( ! wp_script_is ( 'wc_stripe_express_checkout ' , 'registered ' ) ) {
471
+ $ this ->register_express_checkout_script ();
472
+ }
469
473
$ currency = get_woocommerce_currency ();
470
474
$ data = [];
471
475
$ items = [];
@@ -553,21 +557,15 @@ public function localize_pay_for_order_page_scripts( $order ) {
553
557
}
554
558
555
559
/**
556
- * Load scripts and styles.
560
+ * Get asset file data (version and dependencies).
561
+ *
562
+ * @return array Array containing 'version' and 'dependencies' keys.
557
563
*/
558
- public function scripts () {
559
- // If page is not supported, bail.
560
- if ( ! $ this ->express_checkout_helper ->is_page_supported () ) {
561
- return ;
562
- }
563
-
564
- if ( ! $ this ->express_checkout_helper ->should_show_express_checkout_button () ) {
565
- return ;
566
- }
567
-
564
+ private function get_asset_data () {
568
565
$ asset_path = WC_STRIPE_PLUGIN_PATH . '/build/express-checkout.asset.php ' ;
569
566
$ version = WC_STRIPE_VERSION ;
570
567
$ dependencies = [];
568
+
571
569
if ( file_exists ( $ asset_path ) ) {
572
570
$ asset = require $ asset_path ;
573
571
$ version = is_array ( $ asset ) && isset ( $ asset ['version ' ] )
@@ -578,20 +576,53 @@ public function scripts() {
578
576
: $ dependencies ;
579
577
}
580
578
579
+ return [
580
+ 'version ' => $ version ,
581
+ 'dependencies ' => $ dependencies ,
582
+ ];
583
+ }
584
+
585
+ /**
586
+ * Register the express checkout script without enqueuing it.
587
+ */
588
+ private function register_express_checkout_script () {
589
+ $ asset_data = $ this ->get_asset_data ();
590
+
581
591
wp_register_script ( 'stripe ' , 'https://js.stripe.com/v3/ ' , '' , '3.0 ' , true );
582
592
wp_register_script (
583
593
'wc_stripe_express_checkout ' ,
584
594
WC_STRIPE_PLUGIN_URL . '/build/express-checkout.js ' ,
585
- array_merge ( [ 'jquery ' , 'stripe ' ], $ dependencies ),
586
- $ version ,
595
+ array_merge ( [ 'jquery ' , 'stripe ' ], $ asset_data [ ' dependencies ' ] ),
596
+ $ asset_data [ ' version ' ] ,
587
597
true
588
598
);
599
+ }
600
+
601
+ /**
602
+ * Load scripts and styles.
603
+ */
604
+ public function scripts () {
605
+ // If page is not supported, bail.
606
+ if ( ! $ this ->express_checkout_helper ->is_page_supported () ) {
607
+ return ;
608
+ }
609
+
610
+ if ( ! $ this ->express_checkout_helper ->should_show_express_checkout_button () ) {
611
+ return ;
612
+ }
613
+
614
+ // Register the script if not already registered
615
+ if ( ! wp_script_is ( 'wc_stripe_express_checkout ' , 'registered ' ) ) {
616
+ $ this ->register_express_checkout_script ();
617
+ }
618
+
619
+ $ asset_data = $ this ->get_asset_data ();
589
620
590
621
wp_enqueue_style (
591
622
'wc_stripe_express_checkout_style ' ,
592
623
WC_STRIPE_PLUGIN_URL . '/build/express-checkout.css ' ,
593
624
[],
594
- $ version
625
+ $ asset_data [ ' version ' ]
595
626
);
596
627
597
628
wp_localize_script (
0 commit comments