blob: e48bdb1180516ca6560202b80e20e4027db66356 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4}
5
6use Automattic\WooCommerce\Utilities\OrderUtil;
7
8/**
9 * Provides static methods as helpers.
10 *
11 * @since 4.0.0
12 */
13class WC_Stripe_Helper {
14 const LEGACY_META_NAME_FEE = 'Stripe Fee';
15 const LEGACY_META_NAME_NET = 'Net Revenue From Stripe';
16 const META_NAME_FEE = '_stripe_fee';
17 const META_NAME_NET = '_stripe_net';
18 const META_NAME_STRIPE_CURRENCY = '_stripe_currency';
19
20 /**
21 * Gets the Stripe currency for order.
22 *
23 * @since 4.1.0
24 * @param object $order
25 * @return string $currency
26 */
27 public static function get_stripe_currency( $order = null ) {
28 if ( is_null( $order ) ) {
29 return false;
30 }
31
32 return $order->get_meta( self::META_NAME_STRIPE_CURRENCY, true );
33 }
34
35 /**
36 * Updates the Stripe currency for order.
37 *
38 * @since 4.1.0
39 * @param object $order
40 * @param string $currency
41 */
42 public static function update_stripe_currency( $order, $currency ) {
43 if ( is_null( $order ) ) {
44 return false;
45 }
46
47 $order->update_meta_data( self::META_NAME_STRIPE_CURRENCY, $currency );
48 }
49
50 /**
51 * Gets the Stripe fee for order. With legacy check.
52 *
53 * @since 4.1.0
54 * @param object $order
55 * @return string $amount
56 */
57 public static function get_stripe_fee( $order = null ) {
58 if ( is_null( $order ) ) {
59 return false;
60 }
61
62 $amount = $order->get_meta( self::META_NAME_FEE, true );
63
64 // If not found let's check for legacy name.
65 if ( empty( $amount ) ) {
66 $amount = $order->get_meta( self::LEGACY_META_NAME_FEE, true );
67
68 // If found update to new name.
69 if ( $amount ) {
70 self::update_stripe_fee( $order, $amount );
71 }
72 }
73
74 return $amount;
75 }
76
77 /**
78 * Updates the Stripe fee for order.
79 *
80 * @since 4.1.0
81 * @param object $order
82 * @param float $amount
83 */
84 public static function update_stripe_fee( $order = null, $amount = 0.0 ) {
85 if ( is_null( $order ) ) {
86 return false;
87 }
88
89 $order->update_meta_data( self::META_NAME_FEE, $amount );
90 }
91
92 /**
93 * Deletes the Stripe fee for order.
94 *
95 * @since 4.1.0
96 * @param object $order
97 */
98 public static function delete_stripe_fee( $order = null ) {
99 if ( is_null( $order ) ) {
100 return false;
101 }
102
103 $order_id = $order->get_id();
104
105 delete_post_meta( $order_id, self::META_NAME_FEE );
106 delete_post_meta( $order_id, self::LEGACY_META_NAME_FEE );
107 }
108
109 /**
110 * Gets the Stripe net for order. With legacy check.
111 *
112 * @since 4.1.0
113 * @param object $order
114 * @return string $amount
115 */
116 public static function get_stripe_net( $order = null ) {
117 if ( is_null( $order ) ) {
118 return false;
119 }
120
121 $amount = $order->get_meta( self::META_NAME_NET, true );
122
123 // If not found let's check for legacy name.
124 if ( empty( $amount ) ) {
125 $amount = $order->get_meta( self::LEGACY_META_NAME_NET, true );
126
127 // If found update to new name.
128 if ( $amount ) {
129 self::update_stripe_net( $order, $amount );
130 }
131 }
132
133 return $amount;
134 }
135
136 /**
137 * Updates the Stripe net for order.
138 *
139 * @since 4.1.0
140 * @param object $order
141 * @param float $amount
142 */
143 public static function update_stripe_net( $order = null, $amount = 0.0 ) {
144 if ( is_null( $order ) ) {
145 return false;
146 }
147
148 $order->update_meta_data( self::META_NAME_NET, $amount );
149 }
150
151 /**
152 * Deletes the Stripe net for order.
153 *
154 * @since 4.1.0
155 * @param object $order
156 */
157 public static function delete_stripe_net( $order = null ) {
158 if ( is_null( $order ) ) {
159 return false;
160 }
161
162 $order_id = $order->get_id();
163
164 delete_post_meta( $order_id, self::META_NAME_NET );
165 delete_post_meta( $order_id, self::LEGACY_META_NAME_NET );
166 }
167
168 /**
169 * Get Stripe amount to pay
170 *
171 * @param float $total Amount due.
172 * @param string $currency Accepted currency.
173 *
174 * @return float|int
175 */
176 public static function get_stripe_amount( $total, $currency = '' ) {
177 if ( ! $currency ) {
178 $currency = get_woocommerce_currency();
179 }
180
181 if ( in_array( strtolower( $currency ), self::no_decimal_currencies() ) ) {
182 return absint( $total );
183 } else {
184 return absint( wc_format_decimal( ( (float) $total * 100 ), wc_get_price_decimals() ) ); // In cents.
185 }
186 }
187
188 /**
189 * Localize Stripe messages based on code
190 *
191 * @since 3.0.6
192 * @version 3.0.6
193 * @return array
194 */
195 public static function get_localized_messages() {
196 return apply_filters(
197 'wc_stripe_localized_messages',
198 [
199 'invalid_number' => __( 'The card number is not a valid credit card number.', 'woocommerce-gateway-stripe' ),
200 'invalid_expiry_month' => __( 'The card\'s expiration month is invalid.', 'woocommerce-gateway-stripe' ),
201 'invalid_expiry_year' => __( 'The card\'s expiration year is invalid.', 'woocommerce-gateway-stripe' ),
202 'invalid_cvc' => __( 'The card\'s security code is invalid.', 'woocommerce-gateway-stripe' ),
203 'incorrect_number' => __( 'The card number is incorrect.', 'woocommerce-gateway-stripe' ),
204 'incomplete_number' => __( 'The card number is incomplete.', 'woocommerce-gateway-stripe' ),
205 'incomplete_cvc' => __( 'The card\'s security code is incomplete.', 'woocommerce-gateway-stripe' ),
206 'incomplete_expiry' => __( 'The card\'s expiration date is incomplete.', 'woocommerce-gateway-stripe' ),
207 'expired_card' => __( 'The card has expired.', 'woocommerce-gateway-stripe' ),
208 'incorrect_cvc' => __( 'The card\'s security code is incorrect.', 'woocommerce-gateway-stripe' ),
209 'incorrect_zip' => __( 'The card\'s zip code failed validation.', 'woocommerce-gateway-stripe' ),
210 'postal_code_invalid' => __( 'Invalid zip code, please correct and try again', 'woocommerce-gateway-stripe' ),
211 'invalid_expiry_year_past' => __( 'The card\'s expiration year is in the past', 'woocommerce-gateway-stripe' ),
212 'card_declined' => __( 'The card was declined.', 'woocommerce-gateway-stripe' ),
213 'missing' => __( 'There is no card on a customer that is being charged.', 'woocommerce-gateway-stripe' ),
214 'processing_error' => __( 'An error occurred while processing the card.', 'woocommerce-gateway-stripe' ),
215 'invalid_sofort_country' => __( 'The billing country is not accepted by Sofort. Please try another country.', 'woocommerce-gateway-stripe' ),
216 'email_invalid' => __( 'Invalid email address, please correct and try again.', 'woocommerce-gateway-stripe' ),
217 'invalid_request_error' => is_add_payment_method_page()
218 ? __( 'Unable to save this payment method, please try again or use alternative method.', 'woocommerce-gateway-stripe' )
219 : __( 'Unable to process this payment, please try again or use alternative method.', 'woocommerce-gateway-stripe' ),
220 'amount_too_large' => __( 'The order total is too high for this payment method', 'woocommerce-gateway-stripe' ),
221 'amount_too_small' => __( 'The order total is too low for this payment method', 'woocommerce-gateway-stripe' ),
222 'country_code_invalid' => __( 'Invalid country code, please try again with a valid country code', 'woocommerce-gateway-stripe' ),
223 'tax_id_invalid' => __( 'Invalid Tax Id, please try again with a valid tax id', 'woocommerce-gateway-stripe' ),
224 ]
225 );
226 }
227
228 /**
229 * List of currencies supported by Stripe that has no decimals
230 * https://stripe.com/docs/currencies#zero-decimal from https://stripe.com/docs/currencies#presentment-currencies
231 *
232 * @return array $currencies
233 */
234 public static function no_decimal_currencies() {
235 return [
236 'bif', // Burundian Franc
237 'clp', // Chilean Peso
238 'djf', // Djiboutian Franc
239 'gnf', // Guinean Franc
240 'jpy', // Japanese Yen
241 'kmf', // Comorian Franc
242 'krw', // South Korean Won
243 'mga', // Malagasy Ariary
244 'pyg', // Paraguayan Guaraní
245 'rwf', // Rwandan Franc
246 'ugx', // Ugandan Shilling
247 'vnd', // Vietnamese Đồng
248 'vuv', // Vanuatu Vatu
249 'xaf', // Central African Cfa Franc
250 'xof', // West African Cfa Franc
251 'xpf', // Cfp Franc
252 ];
253 }
254
255 /**
256 * Stripe uses smallest denomination in currencies such as cents.
257 * We need to format the returned currency from Stripe into human readable form.
258 * The amount is not used in any calculations so returning string is sufficient.
259 *
260 * @param object $balance_transaction
261 * @param string $type Type of number to format
262 * @return string
263 */
264 public static function format_balance_fee( $balance_transaction, $type = 'fee' ) {
265 if ( ! is_object( $balance_transaction ) ) {
266 return;
267 }
268
269 if ( in_array( strtolower( $balance_transaction->currency ), self::no_decimal_currencies() ) ) {
270 if ( 'fee' === $type ) {
271 return $balance_transaction->fee;
272 }
273
274 return $balance_transaction->net;
275 }
276
277 if ( 'fee' === $type ) {
278 return number_format( $balance_transaction->fee / 100, 2, '.', '' );
279 }
280
281 return number_format( $balance_transaction->net / 100, 2, '.', '' );
282 }
283
284 /**
285 * Checks Stripe minimum order value authorized per currency
286 */
287 public static function get_minimum_amount() {
288 // Check order amount
289 switch ( get_woocommerce_currency() ) {
290 case 'USD':
291 case 'CAD':
292 case 'EUR':
293 case 'CHF':
294 case 'AUD':
295 case 'SGD':
296 $minimum_amount = 50;
297 break;
298 case 'GBP':
299 $minimum_amount = 30;
300 break;
301 case 'DKK':
302 $minimum_amount = 250;
303 break;
304 case 'NOK':
305 case 'SEK':
306 $minimum_amount = 300;
307 break;
308 case 'JPY':
309 $minimum_amount = 5000;
310 break;
311 case 'MXN':
312 $minimum_amount = 1000;
313 break;
314 case 'HKD':
315 $minimum_amount = 400;
316 break;
317 default:
318 $minimum_amount = 50;
319 break;
320 }
321
322 return $minimum_amount;
323 }
324
325 /**
326 * Gets all the saved setting options from a specific method.
327 * If specific setting is passed, only return that.
328 *
329 * @since 4.0.0
330 * @version 4.0.0
331 * @param string $method The payment method to get the settings from.
332 * @param string $setting The name of the setting to get.
333 */
334 public static function get_settings( $method = null, $setting = null ) {
335 $all_settings = null === $method ? get_option( 'woocommerce_stripe_settings', [] ) : get_option( 'woocommerce_stripe_' . $method . '_settings', [] );
336
337 if ( null === $setting ) {
338 return $all_settings;
339 }
340
341 return isset( $all_settings[ $setting ] ) ? $all_settings[ $setting ] : '';
342 }
343
344 /**
345 * Checks if WC version is less than passed in version.
346 *
347 * @since 4.1.11
348 * @param string $version Version to check against.
349 * @return bool
350 */
351 public static function is_wc_lt( $version ) {
352 return version_compare( WC_VERSION, $version, '<' );
353 }
354
355 /**
356 * Gets the webhook URL for Stripe triggers. Used mainly for
357 * asyncronous redirect payment methods in which statuses are
358 * not immediately chargeable.
359 *
360 * @since 4.0.0
361 * @version 4.0.0
362 * @return string
363 */
364 public static function get_webhook_url() {
365 return add_query_arg( 'wc-api', 'wc_stripe', trailingslashit( get_home_url() ) );
366 }
367
368 /**
369 * Gets the order by Stripe source ID.
370 *
371 * @since 4.0.0
372 * @version 4.0.0
373 * @param string $source_id
374 */
375 public static function get_order_by_source_id( $source_id ) {
376 global $wpdb;
377
378 if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
379 $orders = wc_get_orders(
380 [
381 'limit' => 1,
382 'meta_query' => [
383 'key' => '_stripe_source_id',
384 'value' => $source_id,
385 ],
386 ]
387 );
388 $order_id = current( $orders ) ? current( $orders )->get_id() : false;
389 } else {
390 $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $source_id, '_stripe_source_id' ) );
391 }
392
393 if ( ! empty( $order_id ) ) {
394 return wc_get_order( $order_id );
395 }
396
397 return false;
398 }
399
400 /**
401 * Gets the order by Stripe charge ID.
402 *
403 * @since 4.0.0
404 * @since 4.1.16 Return false if charge_id is empty.
405 * @param string $charge_id
406 */
407 public static function get_order_by_charge_id( $charge_id ) {
408 global $wpdb;
409
410 if ( empty( $charge_id ) ) {
411 return false;
412 }
413
414 if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
415 $orders = wc_get_orders(
416 [
417 'transaction_id' => $charge_id,
418 'limit' => 1,
419 ]
420 );
421 $order_id = current( $orders ) ? current( $orders )->get_id() : false;
422 } else {
423 $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $charge_id, '_transaction_id' ) );
424 }
425
426 if ( ! empty( $order_id ) ) {
427 return wc_get_order( $order_id );
428 }
429
430 return false;
431 }
432
433 /**
434 * Gets the order by Stripe PaymentIntent ID.
435 *
436 * @since 4.2
437 * @param string $intent_id The ID of the intent.
438 * @return WC_Order|bool Either an order or false when not found.
439 */
440 public static function get_order_by_intent_id( $intent_id ) {
441 global $wpdb;
442
443 if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
444 $orders = wc_get_orders(
445 [
446 'limit' => 1,
447 'meta_query' => [
448 'key' => '_stripe_intent_id',
449 'value' => $intent_id,
450 ],
451 ]
452 );
453 $order_id = current( $orders ) ? current( $orders )->get_id() : false;
454 } else {
455 $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_intent_id' ) );
456 }
457
458 if ( ! empty( $order_id ) ) {
459 return wc_get_order( $order_id );
460 }
461
462 return false;
463 }
464
465 /**
466 * Gets the order by Stripe SetupIntent ID.
467 *
468 * @since 4.3
469 * @param string $intent_id The ID of the intent.
470 * @return WC_Order|bool Either an order or false when not found.
471 */
472 public static function get_order_by_setup_intent_id( $intent_id ) {
473 global $wpdb;
474
475 if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
476 $orders = wc_get_orders(
477 [
478 'limit' => 1,
479 'meta_query' => [
480 'key' => '_stripe_setup_intent',
481 'value' => $intent_id,
482 ],
483 ]
484 );
485 $order_id = current( $orders ) ? current( $orders )->get_id() : false;
486 } else {
487 $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $intent_id, '_stripe_setup_intent' ) );
488 }
489
490 if ( ! empty( $order_id ) ) {
491 return wc_get_order( $order_id );
492 }
493
494 return false;
495 }
496
497 /**
498 * Sanitize and retrieve the shortened statement descriptor concatenated with the order number.
499 *
500 * @param string $statement_descriptor Shortened statement descriptor.
501 * @param WC_Order $order Order.
502 * @param string $fallback_descriptor (optional) Fallback of the shortened statement descriptor in case it's blank.
503 * @return string $statement_descriptor Final shortened statement descriptor.
504 */
505 public static function get_dynamic_statement_descriptor( $statement_descriptor = '', $order = null, $fallback_descriptor = '' ) {
506 $actual_descriptor = ! empty( $statement_descriptor ) ? $statement_descriptor : $fallback_descriptor;
507 $prefix = self::clean_statement_descriptor( $actual_descriptor );
508 $suffix = '';
509
510 if ( empty( $prefix ) ) {
511 return '';
512 }
513
514 if ( method_exists( $order, 'get_order_number' ) && ! empty( $order->get_order_number() ) ) {
515 $suffix = '* #' . $order->get_order_number();
516 }
517
518 // Make sure it is limited at 22 characters.
519 $statement_descriptor = substr( $prefix . $suffix, 0, 22 );
520
521 return $statement_descriptor;
522 }
523
524 /**
525 * Sanitize statement descriptor text.
526 *
527 * Stripe requires max of 22 characters and no special characters.
528 *
529 * @since 4.0.0
530 * @param string $statement_descriptor Statement descriptor.
531 * @return string $statement_descriptor Sanitized statement descriptor.
532 */
533 public static function clean_statement_descriptor( $statement_descriptor = '' ) {
534 $disallowed_characters = [ '<', '>', '\\', '*', '"', "'", '/', '(', ')', '{', '}' ];
535
536 // Strip any tags.
537 $statement_descriptor = strip_tags( $statement_descriptor );
538
539 // Strip any HTML entities.
540 // Props https://stackoverflow.com/questions/657643/how-to-remove-html-special-chars .
541 $statement_descriptor = preg_replace( '/&#?[a-z0-9]{2,8};/i', '', $statement_descriptor );
542
543 // Next, remove any remaining disallowed characters.
544 $statement_descriptor = str_replace( $disallowed_characters, '', $statement_descriptor );
545
546 // Trim any whitespace at the ends and limit to 22 characters.
547 $statement_descriptor = substr( trim( $statement_descriptor ), 0, 22 );
548
549 return $statement_descriptor;
550 }
551
552 /**
553 * Converts a WooCommerce locale to the closest supported by Stripe.js.
554 *
555 * Stripe.js supports only a subset of IETF language tags, if a country specific locale is not supported we use
556 * the default for that language (https://stripe.com/docs/js/appendix/supported_locales).
557 * If no match is found we return 'auto' so Stripe.js uses the browser locale.
558 *
559 * @param string $wc_locale The locale to convert.
560 *
561 * @return string Closest locale supported by Stripe ('auto' if NONE).
562 */
563 public static function convert_wc_locale_to_stripe_locale( $wc_locale ) {
564 // List copied from: https://stripe.com/docs/js/appendix/supported_locales.
565 $supported = [
566 'ar', // Arabic.
567 'bg', // Bulgarian (Bulgaria).
568 'cs', // Czech (Czech Republic).
569 'da', // Danish.
570 'de', // German (Germany).
571 'el', // Greek (Greece).
572 'en', // English.
573 'en-GB', // English (United Kingdom).
574 'es', // Spanish (Spain).
575 'es-419', // Spanish (Latin America).
576 'et', // Estonian (Estonia).
577 'fi', // Finnish (Finland).
578 'fr', // French (France).
579 'fr-CA', // French (Canada).
580 'he', // Hebrew (Israel).
581 'hu', // Hungarian (Hungary).
582 'id', // Indonesian (Indonesia).
583 'it', // Italian (Italy).
584 'ja', // Japanese.
585 'lt', // Lithuanian (Lithuania).
586 'lv', // Latvian (Latvia).
587 'ms', // Malay (Malaysia).
588 'mt', // Maltese (Malta).
589 'nb', // Norwegian Bokmål.
590 'nl', // Dutch (Netherlands).
591 'pl', // Polish (Poland).
592 'pt-BR', // Portuguese (Brazil).
593 'pt', // Portuguese (Brazil).
594 'ro', // Romanian (Romania).
595 'ru', // Russian (Russia).
596 'sk', // Slovak (Slovakia).
597 'sl', // Slovenian (Slovenia).
598 'sv', // Swedish (Sweden).
599 'th', // Thai.
600 'tr', // Turkish (Turkey).
601 'zh', // Chinese Simplified (China).
602 'zh-HK', // Chinese Traditional (Hong Kong).
603 'zh-TW', // Chinese Traditional (Taiwan).
604 ];
605
606 // Stripe uses '-' instead of '_' (used in WordPress).
607 $locale = str_replace( '_', '-', $wc_locale );
608
609 if ( in_array( $locale, $supported, true ) ) {
610 return $locale;
611 }
612
613 // The plugin has been fully translated for Spanish (Ecuador), Spanish (Mexico), and
614 // Spanish(Venezuela), and partially (88% at 2021-05-14) for Spanish (Colombia).
615 // We need to map these locales to Stripe's Spanish (Latin America) 'es-419' locale.
616 // This list should be updated if more localized versions of Latin American Spanish are
617 // made available.
618 $lowercase_locale = strtolower( $wc_locale );
619 $translated_latin_american_locales = [
620 'es_co', // Spanish (Colombia).
621 'es_ec', // Spanish (Ecuador).
622 'es_mx', // Spanish (Mexico).
623 'es_ve', // Spanish (Venezuela).
624 ];
625 if ( in_array( $lowercase_locale, $translated_latin_american_locales, true ) ) {
626 return 'es-419';
627 }
628
629 // Finally, we check if the "base locale" is available.
630 $base_locale = substr( $wc_locale, 0, 2 );
631 if ( in_array( $base_locale, $supported, true ) ) {
632 return $base_locale;
633 }
634
635 // Default to 'auto' so Stripe.js uses the browser locale.
636 return 'auto';
637 }
638
639 /**
640 * Checks if this page is a cart or checkout page.
641 *
642 * @since 5.2.3
643 * @return boolean
644 */
645 public static function has_cart_or_checkout_on_current_page() {
646 return is_cart() || is_checkout();
647 }
648
649 /**
650 * Return true if the current_tab and current_section match the ones we want to check against.
651 *
652 * @param string $tab
653 * @param string $section
654 * @return boolean
655 */
656 public static function should_enqueue_in_current_tab_section( $tab, $section ) {
657 global $current_tab, $current_section;
658
659 if ( ! isset( $current_tab ) || $tab !== $current_tab ) {
660 return false;
661 }
662
663 if ( ! isset( $current_section ) || $section !== $current_section ) {
664 return false;
665 }
666
667 return true;
668 }
669
670 /**
671 * Returns true if the Stripe JS should be loaded on product pages.
672 *
673 * The critical part here is running the filter to allow merchants to disable Stripe's JS to
674 * improve their store's performance when PRBs are disabled.
675 *
676 * @since 5.8.0
677 * @return boolean True if Stripe's JS should be loaded, false otherwise.
678 */
679 public static function should_load_scripts_on_product_page() {
680 if ( self::should_load_scripts_for_prb_location( 'product' ) ) {
681 return true;
682 }
683
684 return apply_filters( 'wc_stripe_load_scripts_on_product_page_when_prbs_disabled', true );
685 }
686
687 /**
688 * Returns true if the Stripe JS should be loaded on the cart page.
689 *
690 * The critical part here is running the filter to allow merchants to disable Stripe's JS to
691 * improve their store's performance when PRBs are disabled.
692 *
693 * @since 5.8.0
694 * @return boolean True if Stripe's JS should be loaded, false otherwise.
695 */
696 public static function should_load_scripts_on_cart_page() {
697 if ( self::should_load_scripts_for_prb_location( 'cart' ) ) {
698 return true;
699 }
700
701 return apply_filters( 'wc_stripe_load_scripts_on_cart_page_when_prbs_disabled', true );
702 }
703
704 /**
705 * Returns true if the Stripe JS should be loaded for the provided location.
706 *
707 * @since 5.8.1
708 * @param string $location Either 'product' or 'cart'. Used to specify which location to check.
709 * @return boolean True if Stripe's JS should be loaded for the provided location, false otherwise.
710 */
711 private static function should_load_scripts_for_prb_location( $location ) {
712 // Make sure location parameter is sanitized.
713 $location = in_array( $location, [ 'product', 'cart' ], true ) ? $location : '';
714 $are_prbs_enabled = self::get_settings( null, 'payment_request' ) ?? 'yes';
715 $prb_locations = self::get_settings( null, 'payment_request_button_locations' ) ?? [ 'product', 'cart' ];
716
717 // The scripts should be loaded when all of the following are true:
718 // 1. The PRBs are enabled; and
719 // 2. The PRB location settings have an array value (saving an empty option in the GUI results in non-array value); and
720 // 3. The PRBs are enabled at $location.
721 return 'yes' === $are_prbs_enabled && is_array( $prb_locations ) && in_array( $location, $prb_locations, true );
722 }
723
724 /**
725 * Adds payment intent id and order note to order if payment intent is not already saved
726 *
727 * @param $payment_intent_id
728 * @param $order
729 */
730 public static function add_payment_intent_to_order( $payment_intent_id, $order ) {
731
732 $old_intent_id = $order->get_meta( '_stripe_intent_id' );
733
734 if ( $old_intent_id === $payment_intent_id ) {
735 return;
736 }
737
738 $order->add_order_note(
739 sprintf(
740 /* translators: $1%s payment intent ID */
741 __( 'Stripe payment intent created (Payment Intent ID: %1$s)', 'woocommerce-gateway-stripe' ),
742 $payment_intent_id
743 )
744 );
745
746 $order->update_meta_data( '_stripe_intent_id', $payment_intent_id );
747 $order->save();
748 }
749}