swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * Stripe Payment Request API |
| 4 | * Adds support for Apple Pay and Chrome Payment Request API buttons. |
| 5 | * Utilizes the Stripe Payment Request Button to support checkout from the product detail and cart pages. |
| 6 | * |
| 7 | * @package WooCommerce_Stripe/Classes/Payment_Request |
| 8 | * @since 4.0.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * WC_Stripe_Payment_Request class. |
| 17 | */ |
| 18 | class WC_Stripe_Payment_Request { |
| 19 | |
| 20 | use WC_Stripe_Pre_Orders_Trait; |
| 21 | |
| 22 | /** |
| 23 | * Enabled. |
| 24 | * |
| 25 | * @var |
| 26 | */ |
| 27 | public $stripe_settings; |
| 28 | |
| 29 | /** |
| 30 | * Total label |
| 31 | * |
| 32 | * @var |
| 33 | */ |
| 34 | public $total_label; |
| 35 | |
| 36 | /** |
| 37 | * Key |
| 38 | * |
| 39 | * @var |
| 40 | */ |
| 41 | public $publishable_key; |
| 42 | |
| 43 | /** |
| 44 | * Key |
| 45 | * |
| 46 | * @var |
| 47 | */ |
| 48 | public $secret_key; |
| 49 | |
| 50 | /** |
| 51 | * Is test mode active? |
| 52 | * |
| 53 | * @var bool |
| 54 | */ |
| 55 | public $testmode; |
| 56 | |
| 57 | /** |
| 58 | * This Instance. |
| 59 | * |
| 60 | * @var |
| 61 | */ |
| 62 | private static $_this; |
| 63 | |
| 64 | /** |
| 65 | * Initialize class actions. |
| 66 | * |
| 67 | * @since 3.0.0 |
| 68 | * @version 4.0.0 |
| 69 | */ |
| 70 | public function __construct() { |
| 71 | self::$_this = $this; |
| 72 | $this->stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); |
| 73 | $this->testmode = ( ! empty( $this->stripe_settings['testmode'] ) && 'yes' === $this->stripe_settings['testmode'] ) ? true : false; |
| 74 | $this->publishable_key = ! empty( $this->stripe_settings['publishable_key'] ) ? $this->stripe_settings['publishable_key'] : ''; |
| 75 | $this->secret_key = ! empty( $this->stripe_settings['secret_key'] ) ? $this->stripe_settings['secret_key'] : ''; |
| 76 | $this->total_label = ! empty( $this->stripe_settings['statement_descriptor'] ) ? WC_Stripe_Helper::clean_statement_descriptor( $this->stripe_settings['statement_descriptor'] ) : ''; |
| 77 | |
| 78 | if ( $this->testmode ) { |
| 79 | $this->publishable_key = ! empty( $this->stripe_settings['test_publishable_key'] ) ? $this->stripe_settings['test_publishable_key'] : ''; |
| 80 | $this->secret_key = ! empty( $this->stripe_settings['test_secret_key'] ) ? $this->stripe_settings['test_secret_key'] : ''; |
| 81 | } |
| 82 | |
| 83 | $this->total_label = str_replace( "'", '', $this->total_label ) . apply_filters( 'wc_stripe_payment_request_total_label_suffix', ' (via WooCommerce)' ); |
| 84 | |
| 85 | // Checks if Stripe Gateway is enabled. |
| 86 | if ( empty( $this->stripe_settings ) || ( isset( $this->stripe_settings['enabled'] ) && 'yes' !== $this->stripe_settings['enabled'] ) ) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Checks if Payment Request is enabled. |
| 91 | if ( ! isset( $this->stripe_settings['payment_request'] ) || 'yes' !== $this->stripe_settings['payment_request'] ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // Don't load for change payment method page. |
| 96 | if ( isset( $_GET['change_payment_method'] ) ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $this->init(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks whether authentication is required for checkout. |
| 105 | * |
| 106 | * @since 5.1.0 |
| 107 | * @version 5.3.0 |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | public function is_authentication_required() { |
| 112 | // If guest checkout is disabled and account creation upon checkout is not possible, authentication is required. |
| 113 | if ( 'no' === get_option( 'woocommerce_enable_guest_checkout', 'yes' ) && ! $this->is_account_creation_possible() ) { |
| 114 | return true; |
| 115 | } |
| 116 | // If cart contains subscription and account creation upon checkout is not posible, authentication is required. |
| 117 | if ( $this->has_subscription_product() && ! $this->is_account_creation_possible() ) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Checks whether account creation is possible upon checkout. |
| 126 | * |
| 127 | * @since 5.1.0 |
| 128 | * |
| 129 | * @return bool |
| 130 | */ |
| 131 | public function is_account_creation_possible() { |
| 132 | // If automatically generate username/password are disabled, the Payment Request API |
| 133 | // can't include any of those fields, so account creation is not possible. |
| 134 | return ( |
| 135 | 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' ) && |
| 136 | 'yes' === get_option( 'woocommerce_registration_generate_username', 'yes' ) && |
| 137 | 'yes' === get_option( 'woocommerce_registration_generate_password', 'yes' ) |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Checks if keys are set and valid. |
| 143 | * |
| 144 | * @since 4.0.6 |
| 145 | * @return boolean True if the keys are set *and* valid, false otherwise (for example, if keys are empty or the secret key was pasted as publishable key). |
| 146 | */ |
| 147 | public function are_keys_set() { |
| 148 | // NOTE: updates to this function should be added to are_keys_set() |
| 149 | // in includes/abstracts/abstract-wc-stripe-payment-gateway.php |
| 150 | if ( $this->testmode ) { |
| 151 | return preg_match( '/^pk_test_/', $this->publishable_key ) |
| 152 | && preg_match( '/^[rs]k_test_/', $this->secret_key ); |
| 153 | } else { |
| 154 | return preg_match( '/^pk_live_/', $this->publishable_key ) |
| 155 | && preg_match( '/^[rs]k_live_/', $this->secret_key ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get this instance. |
| 161 | * |
| 162 | * @since 4.0.6 |
| 163 | * @return class |
| 164 | */ |
| 165 | public static function instance() { |
| 166 | return self::$_this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the WC customer session if one is not set. |
| 171 | * This is needed so nonces can be verified by AJAX Request. |
| 172 | * |
| 173 | * @since 4.0.0 |
| 174 | * @version 5.2.0 |
| 175 | * @return void |
| 176 | */ |
| 177 | public function set_session() { |
| 178 | if ( ! $this->is_product() || ( isset( WC()->session ) && WC()->session->has_session() ) ) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | WC()->session->set_customer_session_cookie( true ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Handles payment request redirect when the redirect dialog "Continue" button is clicked. |
| 187 | * |
| 188 | * @since 5.3.0 |
| 189 | */ |
| 190 | public function handle_payment_request_redirect() { |
| 191 | if ( |
| 192 | ! empty( $_GET['wc_stripe_payment_request_redirect_url'] ) |
| 193 | && ! empty( $_GET['_wpnonce'] ) |
| 194 | && wp_verify_nonce( $_GET['_wpnonce'], 'wc-stripe-set-redirect-url' ) // @codingStandardsIgnoreLine |
| 195 | ) { |
| 196 | $url = rawurldecode( esc_url_raw( wp_unslash( $_GET['wc_stripe_payment_request_redirect_url'] ) ) ); |
| 197 | // Sets a redirect URL cookie for 10 minutes, which we will redirect to after authentication. |
| 198 | // Users will have a 10 minute timeout to login/create account, otherwise redirect URL expires. |
| 199 | wc_setcookie( 'wc_stripe_payment_request_redirect_url', $url, time() + MINUTE_IN_SECONDS * 10 ); |
| 200 | // Redirects to "my-account" page. |
| 201 | wp_safe_redirect( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) ); |
| 202 | exit; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Initialize hooks. |
| 208 | * |
| 209 | * @since 4.0.0 |
| 210 | * @version 5.3.0 |
| 211 | * @return void |
| 212 | */ |
| 213 | public function init() { |
| 214 | |
| 215 | add_action( 'template_redirect', [ $this, 'set_session' ] ); |
| 216 | add_action( 'template_redirect', [ $this, 'handle_payment_request_redirect' ] ); |
| 217 | |
| 218 | add_action( 'wp_enqueue_scripts', [ $this, 'scripts' ] ); |
| 219 | |
| 220 | add_action( 'woocommerce_after_add_to_cart_quantity', [ $this, 'display_payment_request_button_html' ], 1 ); |
| 221 | add_action( 'woocommerce_after_add_to_cart_quantity', [ $this, 'display_payment_request_button_separator_html' ], 2 ); |
| 222 | |
| 223 | add_action( 'woocommerce_proceed_to_checkout', [ $this, 'display_payment_request_button_html' ], 1 ); |
| 224 | add_action( 'woocommerce_proceed_to_checkout', [ $this, 'display_payment_request_button_separator_html' ], 2 ); |
| 225 | |
| 226 | add_action( 'woocommerce_checkout_before_customer_details', [ $this, 'display_payment_request_button_html' ], 1 ); |
| 227 | add_action( 'woocommerce_checkout_before_customer_details', [ $this, 'display_payment_request_button_separator_html' ], 2 ); |
| 228 | |
| 229 | add_action( 'wc_ajax_wc_stripe_get_cart_details', [ $this, 'ajax_get_cart_details' ] ); |
| 230 | add_action( 'wc_ajax_wc_stripe_get_shipping_options', [ $this, 'ajax_get_shipping_options' ] ); |
| 231 | add_action( 'wc_ajax_wc_stripe_update_shipping_method', [ $this, 'ajax_update_shipping_method' ] ); |
| 232 | add_action( 'wc_ajax_wc_stripe_create_order', [ $this, 'ajax_create_order' ] ); |
| 233 | add_action( 'wc_ajax_wc_stripe_add_to_cart', [ $this, 'ajax_add_to_cart' ] ); |
| 234 | add_action( 'wc_ajax_wc_stripe_get_selected_product_data', [ $this, 'ajax_get_selected_product_data' ] ); |
| 235 | add_action( 'wc_ajax_wc_stripe_clear_cart', [ $this, 'ajax_clear_cart' ] ); |
| 236 | add_action( 'wc_ajax_wc_stripe_log_errors', [ $this, 'ajax_log_errors' ] ); |
| 237 | |
| 238 | add_filter( 'woocommerce_gateway_title', [ $this, 'filter_gateway_title' ], 10, 2 ); |
| 239 | add_action( 'woocommerce_checkout_order_processed', [ $this, 'add_order_meta' ], 10, 2 ); |
| 240 | add_filter( 'woocommerce_login_redirect', [ $this, 'get_login_redirect_url' ], 10, 3 ); |
| 241 | add_filter( 'woocommerce_registration_redirect', [ $this, 'get_login_redirect_url' ], 10, 3 ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Gets the button type. |
| 246 | * |
| 247 | * @since 4.0.0 |
| 248 | * @version 4.0.0 |
| 249 | * @return string |
| 250 | */ |
| 251 | public function get_button_type() { |
| 252 | return isset( $this->stripe_settings['payment_request_button_type'] ) ? $this->stripe_settings['payment_request_button_type'] : 'default'; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Gets the button theme. |
| 257 | * |
| 258 | * @since 4.0.0 |
| 259 | * @version 4.0.0 |
| 260 | * @return string |
| 261 | */ |
| 262 | public function get_button_theme() { |
| 263 | return isset( $this->stripe_settings['payment_request_button_theme'] ) ? $this->stripe_settings['payment_request_button_theme'] : 'dark'; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Gets the button height. |
| 268 | * |
| 269 | * @since 4.0.0 |
| 270 | * @version 4.0.0 |
| 271 | * @return string |
| 272 | */ |
| 273 | public function get_button_height() { |
| 274 | if ( ! WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) { |
| 275 | return isset( $this->stripe_settings['payment_request_button_height'] ) ? str_replace( 'px', '', $this->stripe_settings['payment_request_button_height'] ) : '64'; |
| 276 | } |
| 277 | |
| 278 | $height = isset( $this->stripe_settings['payment_request_button_size'] ) ? $this->stripe_settings['payment_request_button_size'] : 'default'; |
| 279 | if ( 'medium' === $height ) { |
| 280 | return '48'; |
| 281 | } |
| 282 | |
| 283 | if ( 'large' === $height ) { |
| 284 | return '56'; |
| 285 | } |
| 286 | |
| 287 | // for the "default" and "catch-all" scenarios. |
| 288 | return '40'; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Checks if the button is branded. |
| 293 | * |
| 294 | * @since 4.4.0 |
| 295 | * @version 4.4.0 |
| 296 | * @return boolean |
| 297 | */ |
| 298 | public function is_branded_button() { |
| 299 | return 'branded' === $this->get_button_type(); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Gets the branded button type. |
| 304 | * |
| 305 | * @since 4.4.0 |
| 306 | * @version 4.4.0 |
| 307 | * @return string |
| 308 | */ |
| 309 | public function get_button_branded_type() { |
| 310 | return isset( $this->stripe_settings['payment_request_button_branded_type'] ) ? $this->stripe_settings['payment_request_button_branded_type'] : 'default'; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Checks if the button is custom. |
| 315 | * |
| 316 | * @since 4.4.0 |
| 317 | * @version 4.4.0 |
| 318 | * @return boolean |
| 319 | */ |
| 320 | public function is_custom_button() { |
| 321 | // no longer a valid option |
| 322 | if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | return 'custom' === $this->get_button_type(); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Returns custom button css selector. |
| 331 | * |
| 332 | * @since 4.4.0 |
| 333 | * @version 4.4.0 |
| 334 | * @return string |
| 335 | */ |
| 336 | public function custom_button_selector() { |
| 337 | return $this->is_custom_button() ? '#wc-stripe-custom-button' : ''; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Gets the custom button label. |
| 342 | * |
| 343 | * @since 4.4.0 |
| 344 | * @version 4.4.0 |
| 345 | * @return string |
| 346 | */ |
| 347 | public function get_button_label() { |
| 348 | // no longer a valid option |
| 349 | if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) { |
| 350 | return ''; |
| 351 | } |
| 352 | |
| 353 | return isset( $this->stripe_settings['payment_request_button_label'] ) ? $this->stripe_settings['payment_request_button_label'] : 'Buy now'; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Gets the product total price. |
| 358 | * |
| 359 | * @since 5.2.0 |
| 360 | * |
| 361 | * @param object $product WC_Product_* object. |
| 362 | * @return integer Total price. |
| 363 | */ |
| 364 | public function get_product_price( $product ) { |
| 365 | $product_price = $product->get_price(); |
| 366 | // Add subscription sign-up fees to product price. |
| 367 | if ( 'subscription' === $product->get_type() && class_exists( 'WC_Subscriptions_Product' ) ) { |
| 368 | $product_price = $product->get_price() + WC_Subscriptions_Product::get_sign_up_fee( $product ); |
| 369 | } |
| 370 | |
| 371 | return $product_price; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Gets the product data for the currently viewed page |
| 376 | * |
| 377 | * @since 4.0.0 |
| 378 | * @version 5.2.0 |
| 379 | * @return mixed Returns false if not on a product page, the product information otherwise. |
| 380 | */ |
| 381 | public function get_product_data() { |
| 382 | if ( ! $this->is_product() ) { |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | $product = $this->get_product(); |
| 387 | |
| 388 | if ( 'variable' === $product->get_type() ) { |
| 389 | $variation_attributes = $product->get_variation_attributes(); |
| 390 | $attributes = []; |
| 391 | |
| 392 | foreach ( $variation_attributes as $attribute_name => $attribute_values ) { |
| 393 | $attribute_key = 'attribute_' . sanitize_title( $attribute_name ); |
| 394 | |
| 395 | // Passed value via GET takes precedence. Otherwise get the default value for given attribute |
| 396 | $attributes[ $attribute_key ] = isset( $_GET[ $attribute_key ] ) |
| 397 | ? wc_clean( wp_unslash( $_GET[ $attribute_key ] ) ) |
| 398 | : $product->get_variation_default_attribute( $attribute_name ); |
| 399 | } |
| 400 | |
| 401 | $data_store = WC_Data_Store::load( 'product' ); |
| 402 | $variation_id = $data_store->find_matching_product_variation( $product, $attributes ); |
| 403 | |
| 404 | if ( ! empty( $variation_id ) ) { |
| 405 | $product = wc_get_product( $variation_id ); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | $data = []; |
| 410 | $items = []; |
| 411 | |
| 412 | $items[] = [ |
| 413 | 'label' => $product->get_name(), |
| 414 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $this->get_product_price( $product ) ), |
| 415 | ]; |
| 416 | |
| 417 | if ( wc_tax_enabled() ) { |
| 418 | $items[] = [ |
| 419 | 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
| 420 | 'amount' => 0, |
| 421 | 'pending' => true, |
| 422 | ]; |
| 423 | } |
| 424 | |
| 425 | if ( wc_shipping_enabled() && $product->needs_shipping() ) { |
| 426 | $items[] = [ |
| 427 | 'label' => __( 'Shipping', 'woocommerce-gateway-stripe' ), |
| 428 | 'amount' => 0, |
| 429 | 'pending' => true, |
| 430 | ]; |
| 431 | |
| 432 | $data['shippingOptions'] = [ |
| 433 | 'id' => 'pending', |
| 434 | 'label' => __( 'Pending', 'woocommerce-gateway-stripe' ), |
| 435 | 'detail' => '', |
| 436 | 'amount' => 0, |
| 437 | ]; |
| 438 | } |
| 439 | |
| 440 | $data['displayItems'] = $items; |
| 441 | $data['total'] = [ |
| 442 | 'label' => apply_filters( 'wc_stripe_payment_request_total_label', $this->total_label ), |
| 443 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $this->get_product_price( $product ) ), |
| 444 | ]; |
| 445 | |
| 446 | $data['requestShipping'] = ( wc_shipping_enabled() && $product->needs_shipping() && 0 !== wc_get_shipping_method_count( true ) ); |
| 447 | $data['currency'] = strtolower( get_woocommerce_currency() ); |
| 448 | $data['country_code'] = substr( get_option( 'woocommerce_default_country' ), 0, 2 ); |
| 449 | |
| 450 | return apply_filters( 'wc_stripe_payment_request_product_data', $data, $product ); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Filters the gateway title to reflect Payment Request type |
| 455 | */ |
| 456 | public function filter_gateway_title( $title, $id ) { |
| 457 | global $post; |
| 458 | |
| 459 | if ( ! is_object( $post ) ) { |
| 460 | return $title; |
| 461 | } |
| 462 | |
| 463 | $order = wc_get_order( $post->ID ); |
| 464 | $method_title = is_object( $order ) ? $order->get_payment_method_title() : ''; |
| 465 | |
| 466 | if ( 'stripe' === $id && ! empty( $method_title ) ) { |
| 467 | if ( 'Apple Pay (Stripe)' === $method_title |
| 468 | || 'Google Pay (Stripe)' === $method_title |
| 469 | || 'Payment Request (Stripe)' === $method_title |
| 470 | ) { |
| 471 | return $method_title; |
| 472 | } |
| 473 | |
| 474 | // We renamed 'Chrome Payment Request' to just 'Payment Request' since Payment Requests |
| 475 | // are supported by other browsers besides Chrome. As such, we need to check for the |
| 476 | // old title to make sure older orders still reflect that they were paid via Payment |
| 477 | // Request Buttons. |
| 478 | if ( 'Chrome Payment Request (Stripe)' === $method_title ) { |
| 479 | return 'Payment Request (Stripe)'; |
| 480 | } |
| 481 | |
| 482 | return $method_title; |
| 483 | } |
| 484 | |
| 485 | return $title; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Normalizes postal code in case of redacted data from Apple Pay. |
| 490 | * |
| 491 | * @since 5.2.0 |
| 492 | * |
| 493 | * @param string $postcode Postal code. |
| 494 | * @param string $country Country. |
| 495 | */ |
| 496 | public function get_normalized_postal_code( $postcode, $country ) { |
| 497 | /** |
| 498 | * Currently, Apple Pay truncates the UK and Canadian postal codes to the first 4 and 3 characters respectively |
| 499 | * when passing it back from the shippingcontactselected object. This causes WC to invalidate |
| 500 | * the postal code and not calculate shipping zones correctly. |
| 501 | */ |
| 502 | if ( 'GB' === $country ) { |
| 503 | // Replaces a redacted string with something like LN10***. |
| 504 | return str_pad( preg_replace( '/\s+/', '', $postcode ), 7, '*' ); |
| 505 | } |
| 506 | if ( 'CA' === $country ) { |
| 507 | // Replaces a redacted string with something like L4Y***. |
| 508 | return str_pad( preg_replace( '/\s+/', '', $postcode ), 6, '*' ); |
| 509 | } |
| 510 | |
| 511 | return $postcode; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Add needed order meta |
| 516 | * |
| 517 | * @param integer $order_id The order ID. |
| 518 | * @param array $posted_data The posted data from checkout form. |
| 519 | * |
| 520 | * @since 4.0.0 |
| 521 | * @version 4.0.0 |
| 522 | * @return void |
| 523 | */ |
| 524 | public function add_order_meta( $order_id, $posted_data ) { |
| 525 | if ( empty( $_POST['payment_request_type'] ) || ! isset( $_POST['payment_method'] ) || 'stripe' !== $_POST['payment_method'] ) { |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | $order = wc_get_order( $order_id ); |
| 530 | |
| 531 | $payment_request_type = wc_clean( wp_unslash( $_POST['payment_request_type'] ) ); |
| 532 | |
| 533 | if ( 'apple_pay' === $payment_request_type ) { |
| 534 | $order->set_payment_method_title( 'Apple Pay (Stripe)' ); |
| 535 | $order->save(); |
| 536 | } elseif ( 'google_pay' === $payment_request_type ) { |
| 537 | $order->set_payment_method_title( 'Google Pay (Stripe)' ); |
| 538 | $order->save(); |
| 539 | } elseif ( 'payment_request_api' === $payment_request_type ) { |
| 540 | $order->set_payment_method_title( 'Payment Request (Stripe)' ); |
| 541 | $order->save(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Checks to make sure product type is supported. |
| 547 | * |
| 548 | * @since 3.1.0 |
| 549 | * @version 4.0.0 |
| 550 | * @return array |
| 551 | */ |
| 552 | public function supported_product_types() { |
| 553 | return apply_filters( |
| 554 | 'wc_stripe_payment_request_supported_types', |
| 555 | [ |
| 556 | 'simple', |
| 557 | 'variable', |
| 558 | 'variation', |
| 559 | 'subscription', |
| 560 | 'variable-subscription', |
| 561 | 'subscription_variation', |
| 562 | 'booking', |
| 563 | 'bundle', |
| 564 | 'composite', |
| 565 | ] |
| 566 | ); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Checks the cart to see if all items are allowed to be used. |
| 571 | * |
| 572 | * @since 3.1.4 |
| 573 | * @version 4.0.0 |
| 574 | * @return boolean |
| 575 | */ |
| 576 | public function allowed_items_in_cart() { |
| 577 | // Pre Orders compatibility where we don't support charge upon release. |
| 578 | if ( $this->is_pre_order_item_in_cart() && $this->is_pre_order_product_charged_upon_release( $this->get_pre_order_product_from_cart() ) ) { |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | // If the cart is not available we don't have any unsupported products in the cart, so we |
| 583 | // return true. This can happen e.g. when loading the cart or checkout blocks in Gutenberg. |
| 584 | if ( is_null( WC()->cart ) ) { |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { |
| 589 | $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); |
| 590 | |
| 591 | if ( ! in_array( $_product->get_type(), $this->supported_product_types() ) ) { |
| 592 | return false; |
| 593 | } |
| 594 | |
| 595 | // Trial subscriptions with shipping are not supported. |
| 596 | if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $_product ) && $_product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $_product ) > 0 ) { |
| 597 | return false; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // We don't support multiple packages with Payment Request Buttons because we can't offer |
| 602 | // a good UX. |
| 603 | $packages = WC()->cart->get_shipping_packages(); |
| 604 | if ( 1 < count( $packages ) ) { |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Checks whether cart contains a subscription product or this is a subscription product page. |
| 613 | * |
| 614 | * @since 5.3.0 |
| 615 | * @version 5.3.0 |
| 616 | * @return boolean |
| 617 | */ |
| 618 | public function has_subscription_product() { |
| 619 | if ( ! class_exists( 'WC_Subscriptions_Product' ) ) { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | if ( $this->is_product() ) { |
| 624 | $product = $this->get_product(); |
| 625 | if ( WC_Subscriptions_Product::is_subscription( $product ) ) { |
| 626 | return true; |
| 627 | } |
| 628 | } elseif ( WC_Stripe_Helper::has_cart_or_checkout_on_current_page() ) { |
| 629 | foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { |
| 630 | $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); |
| 631 | if ( WC_Subscriptions_Product::is_subscription( $_product ) ) { |
| 632 | return true; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Checks if this is a product page or content contains a product_page shortcode. |
| 642 | * |
| 643 | * @since 5.2.0 |
| 644 | * @return boolean |
| 645 | */ |
| 646 | public function is_product() { |
| 647 | return is_product() || wc_post_content_has_shortcode( 'product_page' ); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Get product from product page or product_page shortcode. |
| 652 | * |
| 653 | * @since 5.2.0 |
| 654 | * @return WC_Product Product object. |
| 655 | */ |
| 656 | public function get_product() { |
| 657 | global $post; |
| 658 | |
| 659 | if ( is_product() ) { |
| 660 | return wc_get_product( $post->ID ); |
| 661 | } elseif ( wc_post_content_has_shortcode( 'product_page' ) ) { |
| 662 | // Get id from product_page shortcode. |
| 663 | preg_match( '/\[product_page id="(?<id>\d+)"\]/', $post->post_content, $shortcode_match ); |
| 664 | |
| 665 | if ( ! isset( $shortcode_match['id'] ) ) { |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | return wc_get_product( $shortcode_match['id'] ); |
| 670 | } |
| 671 | |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Returns the login redirect URL. |
| 677 | * |
| 678 | * @since 5.3.0 |
| 679 | * |
| 680 | * @param string $redirect Default redirect URL. |
| 681 | * @return string Redirect URL. |
| 682 | */ |
| 683 | public function get_login_redirect_url( $redirect ) { |
| 684 | $url = esc_url_raw( wp_unslash( isset( $_COOKIE['wc_stripe_payment_request_redirect_url'] ) ? $_COOKIE['wc_stripe_payment_request_redirect_url'] : '' ) ); |
| 685 | |
| 686 | if ( empty( $url ) ) { |
| 687 | return $redirect; |
| 688 | } |
| 689 | wc_setcookie( 'wc_stripe_payment_request_redirect_url', null ); |
| 690 | |
| 691 | return $url; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Returns the JavaScript configuration object used for any pages with a payment request button. |
| 696 | * |
| 697 | * @return array The settings used for the payment request button in JavaScript. |
| 698 | */ |
| 699 | public function javascript_params() { |
| 700 | $needs_shipping = 'no'; |
| 701 | if ( ! is_null( WC()->cart ) && WC()->cart->needs_shipping() ) { |
| 702 | $needs_shipping = 'yes'; |
| 703 | } |
| 704 | |
| 705 | return [ |
| 706 | 'ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 707 | 'stripe' => [ |
| 708 | 'key' => $this->publishable_key, |
| 709 | 'allow_prepaid_card' => apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no', |
| 710 | 'locale' => WC_Stripe_Helper::convert_wc_locale_to_stripe_locale( get_locale() ), |
| 711 | ], |
| 712 | 'nonce' => [ |
| 713 | 'payment' => wp_create_nonce( 'wc-stripe-payment-request' ), |
| 714 | 'shipping' => wp_create_nonce( 'wc-stripe-payment-request-shipping' ), |
| 715 | 'update_shipping' => wp_create_nonce( 'wc-stripe-update-shipping-method' ), |
| 716 | 'checkout' => wp_create_nonce( 'woocommerce-process_checkout' ), |
| 717 | 'add_to_cart' => wp_create_nonce( 'wc-stripe-add-to-cart' ), |
| 718 | 'get_selected_product_data' => wp_create_nonce( 'wc-stripe-get-selected-product-data' ), |
| 719 | 'log_errors' => wp_create_nonce( 'wc-stripe-log-errors' ), |
| 720 | 'clear_cart' => wp_create_nonce( 'wc-stripe-clear-cart' ), |
| 721 | ], |
| 722 | 'i18n' => [ |
| 723 | 'no_prepaid_card' => __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' ), |
| 724 | /* translators: Do not translate the [option] placeholder */ |
| 725 | 'unknown_shipping' => __( 'Unknown shipping option "[option]".', 'woocommerce-gateway-stripe' ), |
| 726 | ], |
| 727 | 'checkout' => [ |
| 728 | 'url' => wc_get_checkout_url(), |
| 729 | 'currency_code' => strtolower( get_woocommerce_currency() ), |
| 730 | 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
| 731 | 'needs_shipping' => $needs_shipping, |
| 732 | // Defaults to 'required' to match how core initializes this option. |
| 733 | 'needs_payer_phone' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ), |
| 734 | ], |
| 735 | 'button' => $this->get_button_settings(), |
| 736 | 'login_confirmation' => $this->get_login_confirmation_settings(), |
| 737 | 'is_product_page' => $this->is_product(), |
| 738 | 'product' => $this->get_product_data(), |
| 739 | ]; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Load public scripts and styles. |
| 744 | * |
| 745 | * @since 3.1.0 |
| 746 | * @version 5.2.0 |
| 747 | */ |
| 748 | public function scripts() { |
| 749 | // If page is not supported, bail. |
| 750 | // Note: This check is not in `should_show_payment_request_button()` because that function is |
| 751 | // also called by the blocks support class, and this check would fail *incorrectly* when |
| 752 | // called from there. |
| 753 | if ( ! $this->is_page_supported() ) { |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | if ( ! $this->should_show_payment_request_button() ) { |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 762 | |
| 763 | wp_register_script( 'stripe', 'https://js.stripe.com/v3/', '', '3.0', true ); |
| 764 | wp_register_script( 'wc_stripe_payment_request', plugins_url( 'assets/js/stripe-payment-request' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), [ 'jquery', 'stripe' ], WC_STRIPE_VERSION, true ); |
| 765 | |
| 766 | wp_localize_script( |
| 767 | 'wc_stripe_payment_request', |
| 768 | 'wc_stripe_payment_request_params', |
| 769 | apply_filters( |
| 770 | 'wc_stripe_payment_request_params', |
| 771 | $this->javascript_params() |
| 772 | ) |
| 773 | ); |
| 774 | |
| 775 | wp_enqueue_script( 'wc_stripe_payment_request' ); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Returns true if the current page supports Payment Request Buttons, false otherwise. |
| 780 | * |
| 781 | * @since 5.3.0 |
| 782 | * @version 5.3.0 |
| 783 | * @return boolean True if the current page is supported, false otherwise. |
| 784 | */ |
| 785 | private function is_page_supported() { |
| 786 | return $this->is_product() |
| 787 | || WC_Stripe_Helper::has_cart_or_checkout_on_current_page() |
| 788 | || isset( $_GET['pay_for_order'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Display the payment request button. |
| 793 | * |
| 794 | * @since 4.0.0 |
| 795 | * @version 5.2.0 |
| 796 | */ |
| 797 | public function display_payment_request_button_html() { |
| 798 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 799 | |
| 800 | if ( ! isset( $gateways['stripe'] ) ) { |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | if ( ! $this->is_page_supported() ) { |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | if ( ! $this->should_show_payment_request_button() ) { |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | ?> |
| 813 | <div id="wc-stripe-payment-request-wrapper" style="clear:both;padding-top:1.5em;display:none;"> |
| 814 | <div id="wc-stripe-payment-request-button"> |
| 815 | <?php |
| 816 | if ( $this->is_custom_button() ) { |
| 817 | $label = esc_html( $this->get_button_label() ); |
| 818 | $class_name = esc_attr( 'button ' . $this->get_button_theme() ); |
| 819 | $style = esc_attr( 'height:' . $this->get_button_height() . 'px;' ); |
| 820 | echo "<button id=\"wc-stripe-custom-button\" class=\"$class_name\" style=\"$style\"> $label </button>"; |
| 821 | } |
| 822 | ?> |
| 823 | <!-- A Stripe Element will be inserted here. --> |
| 824 | </div> |
| 825 | </div> |
| 826 | <?php |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Display payment request button separator. |
| 831 | * |
| 832 | * @since 4.0.0 |
| 833 | * @version 5.2.0 |
| 834 | */ |
| 835 | public function display_payment_request_button_separator_html() { |
| 836 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 837 | |
| 838 | if ( ! isset( $gateways['stripe'] ) ) { |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | if ( ! is_cart() && ! is_checkout() && ! $this->is_product() && ! isset( $_GET['pay_for_order'] ) ) { |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | if ( is_checkout() && ! in_array( 'checkout', $this->get_button_locations(), true ) ) { |
| 847 | return; |
| 848 | } |
| 849 | ?> |
| 850 | <p id="wc-stripe-payment-request-button-separator" style="margin-top:1.5em;text-align:center;display:none;">— <?php esc_html_e( 'OR', 'woocommerce-gateway-stripe' ); ?> —</p> |
| 851 | <?php |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Returns true if Payment Request Buttons are supported on the current page, false |
| 856 | * otherwise. |
| 857 | * |
| 858 | * @since 5.3.0 |
| 859 | * @version 5.3.0 |
| 860 | * @return boolean True if PRBs are supported on current page, false otherwise |
| 861 | */ |
| 862 | public function should_show_payment_request_button() { |
| 863 | // If keys are not set bail. |
| 864 | if ( ! $this->are_keys_set() ) { |
| 865 | WC_Stripe_Logger::log( 'Keys are not set correctly.' ); |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | // If no SSL bail. |
| 870 | if ( ! $this->testmode && ! is_ssl() ) { |
| 871 | WC_Stripe_Logger::log( 'Stripe Payment Request live mode requires SSL.' ); |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | // Don't show if on the cart or checkout page, or if page contains the cart or checkout |
| 876 | // shortcodes, with items in the cart that aren't supported. |
| 877 | if ( |
| 878 | WC_Stripe_Helper::has_cart_or_checkout_on_current_page() |
| 879 | && ! $this->allowed_items_in_cart() |
| 880 | ) { |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | // Don't show on cart if disabled. |
| 885 | if ( is_cart() && ! $this->should_show_prb_on_cart_page() ) { |
| 886 | return false; |
| 887 | } |
| 888 | |
| 889 | // Don't show on checkout if disabled. |
| 890 | if ( is_checkout() && ! $this->should_show_prb_on_checkout_page() ) { |
| 891 | return false; |
| 892 | } |
| 893 | |
| 894 | // Don't show if product page PRB is disabled. |
| 895 | if ( $this->is_product() && ! $this->should_show_prb_on_product_pages() ) { |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | // Don't show if product on current page is not supported. |
| 900 | if ( $this->is_product() && ! $this->is_product_supported( $this->get_product() ) ) { |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | if ( $this->is_product() && in_array( $this->get_product()->get_type(), [ 'variable', 'variable-subscription' ], true ) ) { |
| 905 | $stock_availability = array_column( $this->get_product()->get_available_variations(), 'is_in_stock' ); |
| 906 | // Don't show if all product variations are out-of-stock. |
| 907 | if ( ! in_array( true, $stock_availability, true ) ) { |
| 908 | return false; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | return true; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Returns true if Payment Request Buttons are enabled on the cart page, false |
| 917 | * otherwise. |
| 918 | * |
| 919 | * @since 5.5.0 |
| 920 | * @version 5.5.0 |
| 921 | * @return boolean True if PRBs are enabled on the cart page, false otherwise |
| 922 | */ |
| 923 | public function should_show_prb_on_cart_page() { |
| 924 | $should_show_on_cart_page = in_array( 'cart', $this->get_button_locations(), true ); |
| 925 | |
| 926 | return apply_filters( |
| 927 | 'wc_stripe_show_payment_request_on_cart', |
| 928 | $should_show_on_cart_page |
| 929 | ); |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Returns true if Payment Request Buttons are enabled on the checkout page, false |
| 934 | * otherwise. |
| 935 | * |
| 936 | * @since 5.5.0 |
| 937 | * @version 5.5.0 |
| 938 | * @return boolean True if PRBs are enabled on the checkout page, false otherwise |
| 939 | */ |
| 940 | public function should_show_prb_on_checkout_page() { |
| 941 | global $post; |
| 942 | |
| 943 | $should_show_on_checkout_page = in_array( 'checkout', $this->get_button_locations(), true ); |
| 944 | |
| 945 | return apply_filters( |
| 946 | 'wc_stripe_show_payment_request_on_checkout', |
| 947 | $should_show_on_checkout_page, |
| 948 | $post |
| 949 | ); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Returns true if Payment Request Buttons are enabled on product pages, false |
| 954 | * otherwise. |
| 955 | * |
| 956 | * @since 5.5.0 |
| 957 | * @version 5.5.0 |
| 958 | * @return boolean True if PRBs are enabled on product pages, false otherwise |
| 959 | */ |
| 960 | public function should_show_prb_on_product_pages() { |
| 961 | global $post; |
| 962 | |
| 963 | $should_show_on_product_page = in_array( 'product', $this->get_button_locations(), true ); |
| 964 | |
| 965 | // Note the negation because if the filter returns `true` that means we should hide the PRB. |
| 966 | return ! apply_filters( |
| 967 | 'wc_stripe_hide_payment_request_on_product_page', |
| 968 | ! $should_show_on_product_page, |
| 969 | $post |
| 970 | ); |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Returns true if a the provided product is supported, false otherwise. |
| 975 | * |
| 976 | * @param WC_Product $param The product that's being checked for support. |
| 977 | * |
| 978 | * @since 5.3.0 |
| 979 | * @version 5.3.0 |
| 980 | * @return boolean True if the provided product is supported, false otherwise. |
| 981 | */ |
| 982 | private function is_product_supported( $product ) { |
| 983 | if ( ! is_object( $product ) || ! in_array( $product->get_type(), $this->supported_product_types() ) ) { |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | // Trial subscriptions with shipping are not supported. |
| 988 | if ( class_exists( 'WC_Subscriptions_Product' ) && $product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) { |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | // Pre Orders charge upon release not supported. |
| 993 | if ( $this->is_pre_order_product_charged_upon_release( $product ) ) { |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | // Composite products are not supported on the product page. |
| 998 | if ( class_exists( 'WC_Composite_Products' ) && function_exists( 'is_composite_product' ) && is_composite_product() ) { |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | // File upload addon not supported |
| 1003 | if ( class_exists( 'WC_Product_Addons_Helper' ) ) { |
| 1004 | $product_addons = WC_Product_Addons_Helper::get_product_addons( $product->get_id() ); |
| 1005 | foreach ( $product_addons as $addon ) { |
| 1006 | if ( 'file_upload' === $addon['type'] ) { |
| 1007 | return false; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Log errors coming from Payment Request |
| 1017 | * |
| 1018 | * @since 3.1.4 |
| 1019 | * @version 4.0.0 |
| 1020 | */ |
| 1021 | public function ajax_log_errors() { |
| 1022 | check_ajax_referer( 'wc-stripe-log-errors', 'security' ); |
| 1023 | |
| 1024 | $errors = isset( $_POST['errors'] ) ? wc_clean( wp_unslash( $_POST['errors'] ) ) : ''; |
| 1025 | |
| 1026 | WC_Stripe_Logger::log( $errors ); |
| 1027 | |
| 1028 | exit; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
| 1032 | * Clears cart. |
| 1033 | * |
| 1034 | * @since 3.1.4 |
| 1035 | * @version 4.0.0 |
| 1036 | */ |
| 1037 | public function ajax_clear_cart() { |
| 1038 | check_ajax_referer( 'wc-stripe-clear-cart', 'security' ); |
| 1039 | |
| 1040 | WC()->cart->empty_cart(); |
| 1041 | exit; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Get cart details. |
| 1046 | */ |
| 1047 | public function ajax_get_cart_details() { |
| 1048 | check_ajax_referer( 'wc-stripe-payment-request', 'security' ); |
| 1049 | |
| 1050 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
| 1051 | define( 'WOOCOMMERCE_CART', true ); |
| 1052 | } |
| 1053 | |
| 1054 | WC()->cart->calculate_totals(); |
| 1055 | |
| 1056 | $currency = get_woocommerce_currency(); |
| 1057 | |
| 1058 | // Set mandatory payment details. |
| 1059 | $data = [ |
| 1060 | 'shipping_required' => WC()->cart->needs_shipping(), |
| 1061 | 'order_data' => [ |
| 1062 | 'currency' => strtolower( $currency ), |
| 1063 | 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
| 1064 | ], |
| 1065 | ]; |
| 1066 | |
| 1067 | $data['order_data'] += $this->build_display_items(); |
| 1068 | |
| 1069 | wp_send_json( $data ); |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * Get shipping options. |
| 1074 | * |
| 1075 | * @see WC_Cart::get_shipping_packages(). |
| 1076 | * @see WC_Shipping::calculate_shipping(). |
| 1077 | * @see WC_Shipping::get_packages(). |
| 1078 | */ |
| 1079 | public function ajax_get_shipping_options() { |
| 1080 | check_ajax_referer( 'wc-stripe-payment-request-shipping', 'security' ); |
| 1081 | |
| 1082 | $shipping_address = filter_input_array( |
| 1083 | INPUT_POST, |
| 1084 | [ |
| 1085 | 'country' => FILTER_SANITIZE_STRING, |
| 1086 | 'state' => FILTER_SANITIZE_STRING, |
| 1087 | 'postcode' => FILTER_SANITIZE_STRING, |
| 1088 | 'city' => FILTER_SANITIZE_STRING, |
| 1089 | 'address' => FILTER_SANITIZE_STRING, |
| 1090 | 'address_2' => FILTER_SANITIZE_STRING, |
| 1091 | ] |
| 1092 | ); |
| 1093 | $product_view_options = filter_input_array( INPUT_POST, [ 'is_product_page' => FILTER_SANITIZE_STRING ] ); |
| 1094 | $should_show_itemized_view = ! isset( $product_view_options['is_product_page'] ) ? true : filter_var( $product_view_options['is_product_page'], FILTER_VALIDATE_BOOLEAN ); |
| 1095 | |
| 1096 | $data = $this->get_shipping_options( $shipping_address, $should_show_itemized_view ); |
| 1097 | wp_send_json( $data ); |
| 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * Gets shipping options available for specified shipping address |
| 1102 | * |
| 1103 | * @param array $shipping_address Shipping address. |
| 1104 | * @param boolean $itemized_display_items Indicates whether to show subtotals or itemized views. |
| 1105 | * |
| 1106 | * @return array Shipping options data. |
| 1107 | * phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag |
| 1108 | */ |
| 1109 | public function get_shipping_options( $shipping_address, $itemized_display_items = false ) { |
| 1110 | try { |
| 1111 | // Set the shipping options. |
| 1112 | $data = []; |
| 1113 | |
| 1114 | // Remember current shipping method before resetting. |
| 1115 | $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
| 1116 | $this->calculate_shipping( apply_filters( 'wc_stripe_payment_request_shipping_posted_values', $shipping_address ) ); |
| 1117 | |
| 1118 | $packages = WC()->shipping->get_packages(); |
| 1119 | $shipping_rate_ids = []; |
| 1120 | |
| 1121 | if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) { |
| 1122 | foreach ( $packages as $package_key => $package ) { |
| 1123 | if ( empty( $package['rates'] ) ) { |
| 1124 | throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
| 1125 | } |
| 1126 | |
| 1127 | foreach ( $package['rates'] as $key => $rate ) { |
| 1128 | if ( in_array( $rate->id, $shipping_rate_ids, true ) ) { |
| 1129 | // The Payment Requests will try to load indefinitely if there are duplicate shipping |
| 1130 | // option IDs. |
| 1131 | throw new Exception( __( 'Unable to provide shipping options for Payment Requests.', 'woocommerce-gateway-stripe' ) ); |
| 1132 | } |
| 1133 | $shipping_rate_ids[] = $rate->id; |
| 1134 | $data['shipping_options'][] = [ |
| 1135 | 'id' => $rate->id, |
| 1136 | 'label' => $rate->label, |
| 1137 | 'detail' => '', |
| 1138 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $rate->cost ), |
| 1139 | ]; |
| 1140 | } |
| 1141 | } |
| 1142 | } else { |
| 1143 | throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
| 1144 | } |
| 1145 | |
| 1146 | // The first shipping option is automatically applied on the client. |
| 1147 | // Keep chosen shipping method by sorting shipping options if the method still available for new address. |
| 1148 | // Fallback to the first available shipping method. |
| 1149 | if ( isset( $data['shipping_options'][0] ) ) { |
| 1150 | if ( isset( $chosen_shipping_methods[0] ) ) { |
| 1151 | $chosen_method_id = $chosen_shipping_methods[0]; |
| 1152 | $compare_shipping_options = function ( $a, $b ) use ( $chosen_method_id ) { |
| 1153 | if ( $a['id'] === $chosen_method_id ) { |
| 1154 | return -1; |
| 1155 | } |
| 1156 | |
| 1157 | if ( $b['id'] === $chosen_method_id ) { |
| 1158 | return 1; |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | }; |
| 1163 | usort( $data['shipping_options'], $compare_shipping_options ); |
| 1164 | } |
| 1165 | |
| 1166 | $first_shipping_method_id = $data['shipping_options'][0]['id']; |
| 1167 | $this->update_shipping_method( [ $first_shipping_method_id ] ); |
| 1168 | } |
| 1169 | |
| 1170 | WC()->cart->calculate_totals(); |
| 1171 | |
| 1172 | $data += $this->build_display_items( $itemized_display_items ); |
| 1173 | $data['result'] = 'success'; |
| 1174 | } catch ( Exception $e ) { |
| 1175 | $data += $this->build_display_items( $itemized_display_items ); |
| 1176 | $data['result'] = 'invalid_shipping_address'; |
| 1177 | } |
| 1178 | |
| 1179 | return $data; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * Update shipping method. |
| 1184 | */ |
| 1185 | public function ajax_update_shipping_method() { |
| 1186 | check_ajax_referer( 'wc-stripe-update-shipping-method', 'security' ); |
| 1187 | |
| 1188 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
| 1189 | define( 'WOOCOMMERCE_CART', true ); |
| 1190 | } |
| 1191 | |
| 1192 | $shipping_methods = filter_input( INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 1193 | $this->update_shipping_method( $shipping_methods ); |
| 1194 | |
| 1195 | WC()->cart->calculate_totals(); |
| 1196 | |
| 1197 | $product_view_options = filter_input_array( INPUT_POST, [ 'is_product_page' => FILTER_SANITIZE_STRING ] ); |
| 1198 | $should_show_itemized_view = ! isset( $product_view_options['is_product_page'] ) ? true : filter_var( $product_view_options['is_product_page'], FILTER_VALIDATE_BOOLEAN ); |
| 1199 | |
| 1200 | $data = []; |
| 1201 | $data += $this->build_display_items( $should_show_itemized_view ); |
| 1202 | $data['result'] = 'success'; |
| 1203 | |
| 1204 | wp_send_json( $data ); |
| 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * Updates shipping method in WC session |
| 1209 | * |
| 1210 | * @param array $shipping_methods Array of selected shipping methods ids. |
| 1211 | */ |
| 1212 | public function update_shipping_method( $shipping_methods ) { |
| 1213 | $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
| 1214 | |
| 1215 | if ( is_array( $shipping_methods ) ) { |
| 1216 | foreach ( $shipping_methods as $i => $value ) { |
| 1217 | $chosen_shipping_methods[ $i ] = wc_clean( $value ); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * Gets the selected product data. |
| 1226 | * |
| 1227 | * @since 4.0.0 |
| 1228 | * @version 4.0.0 |
| 1229 | * @return array $data |
| 1230 | */ |
| 1231 | public function ajax_get_selected_product_data() { |
| 1232 | check_ajax_referer( 'wc-stripe-get-selected-product-data', 'security' ); |
| 1233 | |
| 1234 | try { |
| 1235 | $product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 1236 | $qty = ! isset( $_POST['qty'] ) ? 1 : apply_filters( 'woocommerce_add_to_cart_quantity', absint( $_POST['qty'] ), $product_id ); |
| 1237 | $addon_value = isset( $_POST['addon_value'] ) ? max( floatval( $_POST['addon_value'] ), 0 ) : 0; |
| 1238 | $product = wc_get_product( $product_id ); |
| 1239 | $variation_id = null; |
| 1240 | |
| 1241 | if ( ! is_a( $product, 'WC_Product' ) ) { |
| 1242 | /* translators: 1) The product Id */ |
| 1243 | throw new Exception( sprintf( __( 'Product with the ID (%1$s) cannot be found.', 'woocommerce-gateway-stripe' ), $product_id ) ); |
| 1244 | } |
| 1245 | |
| 1246 | if ( 'variable' === $product->get_type() && isset( $_POST['attributes'] ) ) { |
| 1247 | $attributes = wc_clean( wp_unslash( $_POST['attributes'] ) ); |
| 1248 | |
| 1249 | $data_store = WC_Data_Store::load( 'product' ); |
| 1250 | $variation_id = $data_store->find_matching_product_variation( $product, $attributes ); |
| 1251 | |
| 1252 | if ( ! empty( $variation_id ) ) { |
| 1253 | $product = wc_get_product( $variation_id ); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // Force quantity to 1 if sold individually and check for existing item in cart. |
| 1258 | if ( $product->is_sold_individually() ) { |
| 1259 | $qty = apply_filters( 'wc_stripe_payment_request_add_to_cart_sold_individually_quantity', 1, $qty, $product_id, $variation_id ); |
| 1260 | } |
| 1261 | |
| 1262 | if ( ! $product->has_enough_stock( $qty ) ) { |
| 1263 | /* translators: 1) product name 2) quantity in stock */ |
| 1264 | throw new Exception( sprintf( __( 'You cannot add that amount of "%1$s"; to the cart because there is not enough stock (%2$s remaining).', 'woocommerce-gateway-stripe' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity(), $product ) ) ); |
| 1265 | } |
| 1266 | |
| 1267 | $total = $qty * $this->get_product_price( $product ) + $addon_value; |
| 1268 | |
| 1269 | $quantity_label = 1 < $qty ? ' (x' . $qty . ')' : ''; |
| 1270 | |
| 1271 | $data = []; |
| 1272 | $items = []; |
| 1273 | |
| 1274 | $items[] = [ |
| 1275 | 'label' => $product->get_name() . $quantity_label, |
| 1276 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $total ), |
| 1277 | ]; |
| 1278 | |
| 1279 | if ( wc_tax_enabled() ) { |
| 1280 | $items[] = [ |
| 1281 | 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
| 1282 | 'amount' => 0, |
| 1283 | 'pending' => true, |
| 1284 | ]; |
| 1285 | } |
| 1286 | |
| 1287 | if ( wc_shipping_enabled() && $product->needs_shipping() ) { |
| 1288 | $items[] = [ |
| 1289 | 'label' => __( 'Shipping', 'woocommerce-gateway-stripe' ), |
| 1290 | 'amount' => 0, |
| 1291 | 'pending' => true, |
| 1292 | ]; |
| 1293 | |
| 1294 | $data['shippingOptions'] = [ |
| 1295 | 'id' => 'pending', |
| 1296 | 'label' => __( 'Pending', 'woocommerce-gateway-stripe' ), |
| 1297 | 'detail' => '', |
| 1298 | 'amount' => 0, |
| 1299 | ]; |
| 1300 | } |
| 1301 | |
| 1302 | $data['displayItems'] = $items; |
| 1303 | $data['total'] = [ |
| 1304 | 'label' => $this->total_label, |
| 1305 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $total ), |
| 1306 | ]; |
| 1307 | |
| 1308 | $data['requestShipping'] = ( wc_shipping_enabled() && $product->needs_shipping() ); |
| 1309 | $data['currency'] = strtolower( get_woocommerce_currency() ); |
| 1310 | $data['country_code'] = substr( get_option( 'woocommerce_default_country' ), 0, 2 ); |
| 1311 | |
| 1312 | wp_send_json( $data ); |
| 1313 | } catch ( Exception $e ) { |
| 1314 | wp_send_json( [ 'error' => wp_strip_all_tags( $e->getMessage() ) ] ); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | /** |
| 1319 | * Adds the current product to the cart. Used on product detail page. |
| 1320 | * |
| 1321 | * @since 4.0.0 |
| 1322 | * @version 4.0.0 |
| 1323 | * @return array $data |
| 1324 | */ |
| 1325 | public function ajax_add_to_cart() { |
| 1326 | check_ajax_referer( 'wc-stripe-add-to-cart', 'security' ); |
| 1327 | |
| 1328 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
| 1329 | define( 'WOOCOMMERCE_CART', true ); |
| 1330 | } |
| 1331 | |
| 1332 | WC()->shipping->reset_shipping(); |
| 1333 | |
| 1334 | $product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 1335 | $qty = ! isset( $_POST['qty'] ) ? 1 : absint( $_POST['qty'] ); |
| 1336 | $product = wc_get_product( $product_id ); |
| 1337 | $product_type = $product->get_type(); |
| 1338 | |
| 1339 | // First empty the cart to prevent wrong calculation. |
| 1340 | WC()->cart->empty_cart(); |
| 1341 | |
| 1342 | if ( ( 'variable' === $product_type || 'variable-subscription' === $product_type ) && isset( $_POST['attributes'] ) ) { |
| 1343 | $attributes = wc_clean( wp_unslash( $_POST['attributes'] ) ); |
| 1344 | |
| 1345 | $data_store = WC_Data_Store::load( 'product' ); |
| 1346 | $variation_id = $data_store->find_matching_product_variation( $product, $attributes ); |
| 1347 | |
| 1348 | WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes ); |
| 1349 | } |
| 1350 | |
| 1351 | if ( 'simple' === $product_type || 'subscription' === $product_type ) { |
| 1352 | WC()->cart->add_to_cart( $product->get_id(), $qty ); |
| 1353 | } |
| 1354 | |
| 1355 | WC()->cart->calculate_totals(); |
| 1356 | |
| 1357 | $data = []; |
| 1358 | $data += $this->build_display_items(); |
| 1359 | $data['result'] = 'success'; |
| 1360 | |
| 1361 | wp_send_json( $data ); |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Normalizes billing and shipping state fields. |
| 1366 | * |
| 1367 | * @since 4.0.0 |
| 1368 | * @version 5.1.0 |
| 1369 | */ |
| 1370 | public function normalize_state() { |
| 1371 | $billing_country = ! empty( $_POST['billing_country'] ) ? wc_clean( wp_unslash( $_POST['billing_country'] ) ) : ''; |
| 1372 | $shipping_country = ! empty( $_POST['shipping_country'] ) ? wc_clean( wp_unslash( $_POST['shipping_country'] ) ) : ''; |
| 1373 | $billing_state = ! empty( $_POST['billing_state'] ) ? wc_clean( wp_unslash( $_POST['billing_state'] ) ) : ''; |
| 1374 | $shipping_state = ! empty( $_POST['shipping_state'] ) ? wc_clean( wp_unslash( $_POST['shipping_state'] ) ) : ''; |
| 1375 | |
| 1376 | // Due to a bug in Apple Pay, the "Region" part of a Hong Kong address is delivered in |
| 1377 | // `shipping_postcode`, so we need some special case handling for that. According to |
| 1378 | // our sources at Apple Pay people will sometimes use the district or even sub-district |
| 1379 | // for this value. As such we check against all regions, districts, and sub-districts |
| 1380 | // with both English and Mandarin spelling. |
| 1381 | // |
| 1382 | // @reykjalin: The check here is quite elaborate in an attempt to make sure this doesn't break once |
| 1383 | // Apple Pay fixes the bug that causes address values to be in the wrong place. Because of that the |
| 1384 | // algorithm becomes: |
| 1385 | // 1. Use the supplied state if it's valid (in case Apple Pay bug is fixed) |
| 1386 | // 2. Use the value supplied in the postcode if it's a valid HK region (equivalent to a WC state). |
| 1387 | // 3. Fall back to the value supplied in the state. This will likely cause a validation error, in |
| 1388 | // which case a merchant can reach out to us so we can either: 1) add whatever the customer used |
| 1389 | // as a state to our list of valid states; or 2) let them know the customer must spell the state |
| 1390 | // in some way that matches our list of valid states. |
| 1391 | // |
| 1392 | // @reykjalin: This HK specific sanitazation *should be removed* once Apple Pay fix |
| 1393 | // the address bug. More info on that in pc4etw-bY-p2. |
| 1394 | if ( 'HK' === $billing_country ) { |
| 1395 | include_once WC_STRIPE_PLUGIN_PATH . '/includes/constants/class-wc-stripe-hong-kong-states.php'; |
| 1396 | |
| 1397 | if ( ! WC_Stripe_Hong_Kong_States::is_valid_state( strtolower( $billing_state ) ) ) { |
| 1398 | $billing_postcode = ! empty( $_POST['billing_postcode'] ) ? wc_clean( wp_unslash( $_POST['billing_postcode'] ) ) : ''; |
| 1399 | if ( WC_Stripe_Hong_Kong_States::is_valid_state( strtolower( $billing_postcode ) ) ) { |
| 1400 | $billing_state = $billing_postcode; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | if ( 'HK' === $shipping_country ) { |
| 1405 | include_once WC_STRIPE_PLUGIN_PATH . '/includes/constants/class-wc-stripe-hong-kong-states.php'; |
| 1406 | |
| 1407 | if ( ! WC_Stripe_Hong_Kong_States::is_valid_state( strtolower( $shipping_state ) ) ) { |
| 1408 | $shipping_postcode = ! empty( $_POST['shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['shipping_postcode'] ) ) : ''; |
| 1409 | if ( WC_Stripe_Hong_Kong_States::is_valid_state( strtolower( $shipping_postcode ) ) ) { |
| 1410 | $shipping_state = $shipping_postcode; |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | // Finally we normalize the state value we want to process. |
| 1416 | if ( $billing_state && $billing_country ) { |
| 1417 | $_POST['billing_state'] = $this->get_normalized_state( $billing_state, $billing_country ); |
| 1418 | } |
| 1419 | |
| 1420 | if ( $shipping_state && $shipping_country ) { |
| 1421 | $_POST['shipping_state'] = $this->get_normalized_state( $shipping_state, $shipping_country ); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /** |
| 1426 | * Checks if given state is normalized. |
| 1427 | * |
| 1428 | * @since 5.1.0 |
| 1429 | * |
| 1430 | * @param string $state State. |
| 1431 | * @param string $country Two-letter country code. |
| 1432 | * |
| 1433 | * @return bool Whether state is normalized or not. |
| 1434 | */ |
| 1435 | public function is_normalized_state( $state, $country ) { |
| 1436 | $wc_states = WC()->countries->get_states( $country ); |
| 1437 | return ( |
| 1438 | is_array( $wc_states ) && |
| 1439 | in_array( $state, array_keys( $wc_states ), true ) |
| 1440 | ); |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * Sanitize string for comparison. |
| 1445 | * |
| 1446 | * @since 5.1.0 |
| 1447 | * |
| 1448 | * @param string $string String to be sanitized. |
| 1449 | * |
| 1450 | * @return string The sanitized string. |
| 1451 | */ |
| 1452 | public function sanitize_string( $string ) { |
| 1453 | return trim( wc_strtolower( remove_accents( $string ) ) ); |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * Get normalized state from Payment Request API dropdown list of states. |
| 1458 | * |
| 1459 | * @since 5.1.0 |
| 1460 | * |
| 1461 | * @param string $state Full state name or state code. |
| 1462 | * @param string $country Two-letter country code. |
| 1463 | * |
| 1464 | * @return string Normalized state or original state input value. |
| 1465 | */ |
| 1466 | public function get_normalized_state_from_pr_states( $state, $country ) { |
| 1467 | // Include Payment Request API State list for compatibility with WC countries/states. |
| 1468 | include_once WC_STRIPE_PLUGIN_PATH . '/includes/constants/class-wc-stripe-payment-request-button-states.php'; |
| 1469 | $pr_states = WC_Stripe_Payment_Request_Button_States::STATES; |
| 1470 | |
| 1471 | if ( ! isset( $pr_states[ $country ] ) ) { |
| 1472 | return $state; |
| 1473 | } |
| 1474 | |
| 1475 | foreach ( $pr_states[ $country ] as $wc_state_abbr => $pr_state ) { |
| 1476 | $sanitized_state_string = $this->sanitize_string( $state ); |
| 1477 | // Checks if input state matches with Payment Request state code (0), name (1) or localName (2). |
| 1478 | if ( |
| 1479 | ( ! empty( $pr_state[0] ) && $sanitized_state_string === $this->sanitize_string( $pr_state[0] ) ) || |
| 1480 | ( ! empty( $pr_state[1] ) && $sanitized_state_string === $this->sanitize_string( $pr_state[1] ) ) || |
| 1481 | ( ! empty( $pr_state[2] ) && $sanitized_state_string === $this->sanitize_string( $pr_state[2] ) ) |
| 1482 | ) { |
| 1483 | return $wc_state_abbr; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | return $state; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * Get normalized state from WooCommerce list of translated states. |
| 1492 | * |
| 1493 | * @since 5.1.0 |
| 1494 | * |
| 1495 | * @param string $state Full state name or state code. |
| 1496 | * @param string $country Two-letter country code. |
| 1497 | * |
| 1498 | * @return string Normalized state or original state input value. |
| 1499 | */ |
| 1500 | public function get_normalized_state_from_wc_states( $state, $country ) { |
| 1501 | $wc_states = WC()->countries->get_states( $country ); |
| 1502 | |
| 1503 | if ( is_array( $wc_states ) ) { |
| 1504 | foreach ( $wc_states as $wc_state_abbr => $wc_state_value ) { |
| 1505 | if ( preg_match( '/' . preg_quote( $wc_state_value, '/' ) . '/i', $state ) ) { |
| 1506 | return $wc_state_abbr; |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | return $state; |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * Gets the normalized state/county field because in some |
| 1516 | * cases, the state/county field is formatted differently from |
| 1517 | * what WC is expecting and throws an error. An example |
| 1518 | * for Ireland, the county dropdown in Chrome shows "Co. Clare" format. |
| 1519 | * |
| 1520 | * @since 5.0.0 |
| 1521 | * @version 5.1.0 |
| 1522 | * |
| 1523 | * @param string $state Full state name or an already normalized abbreviation. |
| 1524 | * @param string $country Two-letter country code. |
| 1525 | * |
| 1526 | * @return string Normalized state abbreviation. |
| 1527 | */ |
| 1528 | public function get_normalized_state( $state, $country ) { |
| 1529 | // If it's empty or already normalized, skip. |
| 1530 | if ( ! $state || $this->is_normalized_state( $state, $country ) ) { |
| 1531 | return $state; |
| 1532 | } |
| 1533 | |
| 1534 | // Try to match state from the Payment Request API list of states. |
| 1535 | $state = $this->get_normalized_state_from_pr_states( $state, $country ); |
| 1536 | |
| 1537 | // If it's normalized, return. |
| 1538 | if ( $this->is_normalized_state( $state, $country ) ) { |
| 1539 | return $state; |
| 1540 | } |
| 1541 | |
| 1542 | // If the above doesn't work, fallback to matching against the list of translated |
| 1543 | // states from WooCommerce. |
| 1544 | return $this->get_normalized_state_from_wc_states( $state, $country ); |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * The Payment Request API provides its own validation for the address form. |
| 1549 | * For some countries, it might not provide a state field, so we need to return a more descriptive |
| 1550 | * error message, indicating that the Payment Request button is not supported for that country. |
| 1551 | * |
| 1552 | * @since 5.1.0 |
| 1553 | */ |
| 1554 | public function validate_state() { |
| 1555 | $wc_checkout = WC_Checkout::instance(); |
| 1556 | $posted_data = $wc_checkout->get_posted_data(); |
| 1557 | $checkout_fields = $wc_checkout->get_checkout_fields(); |
| 1558 | $countries = WC()->countries->get_countries(); |
| 1559 | |
| 1560 | $is_supported = true; |
| 1561 | // Checks if billing state is missing and is required. |
| 1562 | if ( ! empty( $checkout_fields['billing']['billing_state']['required'] ) && '' === $posted_data['billing_state'] ) { |
| 1563 | $is_supported = false; |
| 1564 | } |
| 1565 | |
| 1566 | // Checks if shipping state is missing and is required. |
| 1567 | if ( WC()->cart->needs_shipping_address() && ! empty( $checkout_fields['shipping']['shipping_state']['required'] ) && '' === $posted_data['shipping_state'] ) { |
| 1568 | $is_supported = false; |
| 1569 | } |
| 1570 | |
| 1571 | if ( ! $is_supported ) { |
| 1572 | wc_add_notice( |
| 1573 | sprintf( |
| 1574 | /* translators: 1) country. */ |
| 1575 | __( 'The Payment Request button is not supported in %1$s because some required fields couldn\'t be verified. Please proceed to the checkout page and try again.', 'woocommerce-gateway-stripe' ), |
| 1576 | isset( $countries[ $posted_data['billing_country'] ] ) ? $countries[ $posted_data['billing_country'] ] : $posted_data['billing_country'] |
| 1577 | ), |
| 1578 | 'error' |
| 1579 | ); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | /** |
| 1584 | * Create order. Security is handled by WC. |
| 1585 | * |
| 1586 | * @since 3.1.0 |
| 1587 | * @version 5.1.0 |
| 1588 | */ |
| 1589 | public function ajax_create_order() { |
| 1590 | if ( WC()->cart->is_empty() ) { |
| 1591 | wp_send_json_error( __( 'Empty cart', 'woocommerce-gateway-stripe' ) ); |
| 1592 | } |
| 1593 | |
| 1594 | if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
| 1595 | define( 'WOOCOMMERCE_CHECKOUT', true ); |
| 1596 | } |
| 1597 | |
| 1598 | // Normalizes billing and shipping state values. |
| 1599 | $this->normalize_state(); |
| 1600 | |
| 1601 | // In case the state is required, but is missing, add a more descriptive error notice. |
| 1602 | $this->validate_state(); |
| 1603 | |
| 1604 | WC()->checkout()->process_checkout(); |
| 1605 | |
| 1606 | die( 0 ); |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * Calculate and set shipping method. |
| 1611 | * |
| 1612 | * @param array $address Shipping address. |
| 1613 | * |
| 1614 | * @since 3.1.0 |
| 1615 | * @version 5.0.0 |
| 1616 | */ |
| 1617 | protected function calculate_shipping( $address = [] ) { |
| 1618 | $country = $address['country']; |
| 1619 | $state = $address['state']; |
| 1620 | $postcode = $address['postcode']; |
| 1621 | $city = $address['city']; |
| 1622 | $address_1 = $address['address']; |
| 1623 | $address_2 = $address['address_2']; |
| 1624 | |
| 1625 | // Normalizes state to calculate shipping zones. |
| 1626 | $state = $this->get_normalized_state( $state, $country ); |
| 1627 | |
| 1628 | // Normalizes postal code in case of redacted data from Apple Pay. |
| 1629 | $postcode = $this->get_normalized_postal_code( $postcode, $country ); |
| 1630 | |
| 1631 | WC()->shipping->reset_shipping(); |
| 1632 | |
| 1633 | if ( $postcode && WC_Validation::is_postcode( $postcode, $country ) ) { |
| 1634 | $postcode = wc_format_postcode( $postcode, $country ); |
| 1635 | } |
| 1636 | |
| 1637 | if ( $country ) { |
| 1638 | WC()->customer->set_location( $country, $state, $postcode, $city ); |
| 1639 | WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
| 1640 | } else { |
| 1641 | WC()->customer->set_billing_address_to_base(); |
| 1642 | WC()->customer->set_shipping_address_to_base(); |
| 1643 | } |
| 1644 | |
| 1645 | WC()->customer->set_calculated_shipping( true ); |
| 1646 | WC()->customer->save(); |
| 1647 | |
| 1648 | $packages = []; |
| 1649 | |
| 1650 | $packages[0]['contents'] = WC()->cart->get_cart(); |
| 1651 | $packages[0]['contents_cost'] = 0; |
| 1652 | $packages[0]['applied_coupons'] = WC()->cart->applied_coupons; |
| 1653 | $packages[0]['user']['ID'] = get_current_user_id(); |
| 1654 | $packages[0]['destination']['country'] = $country; |
| 1655 | $packages[0]['destination']['state'] = $state; |
| 1656 | $packages[0]['destination']['postcode'] = $postcode; |
| 1657 | $packages[0]['destination']['city'] = $city; |
| 1658 | $packages[0]['destination']['address'] = $address_1; |
| 1659 | $packages[0]['destination']['address_2'] = $address_2; |
| 1660 | |
| 1661 | foreach ( WC()->cart->get_cart() as $item ) { |
| 1662 | if ( $item['data']->needs_shipping() ) { |
| 1663 | if ( isset( $item['line_total'] ) ) { |
| 1664 | $packages[0]['contents_cost'] += $item['line_total']; |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
| 1670 | |
| 1671 | WC()->shipping->calculate_shipping( $packages ); |
| 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * The settings for the `button` attribute - they depend on the "settings redesign" flag value. |
| 1676 | * |
| 1677 | * @return array |
| 1678 | */ |
| 1679 | public function get_button_settings() { |
| 1680 | // it would be DRYer to use `array_merge`, |
| 1681 | // but I thought that this approach might be more straightforward to clean up when we remove the feature flag code. |
| 1682 | $button_type = $this->get_button_type(); |
| 1683 | if ( WC_Stripe_Feature_Flags::is_upe_preview_enabled() ) { |
| 1684 | return [ |
| 1685 | 'type' => $button_type, |
| 1686 | 'theme' => $this->get_button_theme(), |
| 1687 | 'height' => $this->get_button_height(), |
| 1688 | // Default format is en_US. |
| 1689 | 'locale' => apply_filters( 'wc_stripe_payment_request_button_locale', substr( get_locale(), 0, 2 ) ), |
| 1690 | 'branded_type' => 'default' === $button_type ? 'short' : 'long', |
| 1691 | // these values are no longer applicable - all the JS relying on them can be removed. |
| 1692 | 'css_selector' => '', |
| 1693 | 'label' => '', |
| 1694 | 'is_custom' => false, |
| 1695 | 'is_branded' => false, |
| 1696 | ]; |
| 1697 | } |
| 1698 | |
| 1699 | return [ |
| 1700 | 'type' => $button_type, |
| 1701 | 'theme' => $this->get_button_theme(), |
| 1702 | 'height' => $this->get_button_height(), |
| 1703 | 'locale' => apply_filters( 'wc_stripe_payment_request_button_locale', substr( get_locale(), 0, 2 ) ), |
| 1704 | // Default format is en_US. |
| 1705 | 'is_custom' => $this->is_custom_button(), |
| 1706 | 'is_branded' => $this->is_branded_button(), |
| 1707 | 'css_selector' => $this->custom_button_selector(), |
| 1708 | 'branded_type' => $this->get_button_branded_type(), |
| 1709 | ]; |
| 1710 | } |
| 1711 | |
| 1712 | /** |
| 1713 | * Builds the shippings methods to pass to Payment Request |
| 1714 | * |
| 1715 | * @since 3.1.0 |
| 1716 | * @version 4.0.0 |
| 1717 | */ |
| 1718 | protected function build_shipping_methods( $shipping_methods ) { |
| 1719 | if ( empty( $shipping_methods ) ) { |
| 1720 | return []; |
| 1721 | } |
| 1722 | |
| 1723 | $shipping = []; |
| 1724 | |
| 1725 | foreach ( $shipping_methods as $method ) { |
| 1726 | $shipping[] = [ |
| 1727 | 'id' => $method['id'], |
| 1728 | 'label' => $method['label'], |
| 1729 | 'detail' => '', |
| 1730 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $method['amount']['value'] ), |
| 1731 | ]; |
| 1732 | } |
| 1733 | |
| 1734 | return $shipping; |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * Builds the line items to pass to Payment Request |
| 1739 | * |
| 1740 | * @since 3.1.0 |
| 1741 | * @version 4.0.0 |
| 1742 | */ |
| 1743 | protected function build_display_items( $itemized_display_items = false ) { |
| 1744 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
| 1745 | define( 'WOOCOMMERCE_CART', true ); |
| 1746 | } |
| 1747 | |
| 1748 | $items = []; |
| 1749 | $lines = []; |
| 1750 | $subtotal = 0; |
| 1751 | $discounts = 0; |
| 1752 | $display_items = ! apply_filters( 'wc_stripe_payment_request_hide_itemization', true ) || $itemized_display_items; |
| 1753 | |
| 1754 | foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { |
| 1755 | $subtotal += $cart_item['line_subtotal']; |
| 1756 | $amount = $cart_item['line_subtotal']; |
| 1757 | $quantity_label = 1 < $cart_item['quantity'] ? ' (x' . $cart_item['quantity'] . ')' : ''; |
| 1758 | $product_name = $cart_item['data']->get_name(); |
| 1759 | |
| 1760 | $lines[] = [ |
| 1761 | 'label' => $product_name . $quantity_label, |
| 1762 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $amount ), |
| 1763 | ]; |
| 1764 | } |
| 1765 | |
| 1766 | if ( $display_items ) { |
| 1767 | $items = array_merge( $items, $lines ); |
| 1768 | } else { |
| 1769 | // Default show only subtotal instead of itemization. |
| 1770 | |
| 1771 | $items[] = [ |
| 1772 | 'label' => 'Subtotal', |
| 1773 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $subtotal ), |
| 1774 | ]; |
| 1775 | } |
| 1776 | |
| 1777 | if ( version_compare( WC_VERSION, '3.2', '<' ) ) { |
| 1778 | $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), WC()->cart->dp ); |
| 1779 | } else { |
| 1780 | $applied_coupons = array_values( WC()->cart->get_coupon_discount_totals() ); |
| 1781 | |
| 1782 | foreach ( $applied_coupons as $amount ) { |
| 1783 | $discounts += (float) $amount; |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | $discounts = wc_format_decimal( $discounts, WC()->cart->dp ); |
| 1788 | $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ); |
| 1789 | $shipping = wc_format_decimal( WC()->cart->shipping_total, WC()->cart->dp ); |
| 1790 | $items_total = wc_format_decimal( WC()->cart->cart_contents_total, WC()->cart->dp ) + $discounts; |
| 1791 | $order_total = version_compare( WC_VERSION, '3.2', '<' ) ? wc_format_decimal( $items_total + $tax + $shipping - $discounts, WC()->cart->dp ) : WC()->cart->get_total( false ); |
| 1792 | |
| 1793 | if ( wc_tax_enabled() ) { |
| 1794 | $items[] = [ |
| 1795 | 'label' => esc_html( __( 'Tax', 'woocommerce-gateway-stripe' ) ), |
| 1796 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $tax ), |
| 1797 | ]; |
| 1798 | } |
| 1799 | |
| 1800 | if ( WC()->cart->needs_shipping() ) { |
| 1801 | $items[] = [ |
| 1802 | 'label' => esc_html( __( 'Shipping', 'woocommerce-gateway-stripe' ) ), |
| 1803 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $shipping ), |
| 1804 | ]; |
| 1805 | } |
| 1806 | |
| 1807 | if ( WC()->cart->has_discount() ) { |
| 1808 | $items[] = [ |
| 1809 | 'label' => esc_html( __( 'Discount', 'woocommerce-gateway-stripe' ) ), |
| 1810 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $discounts ), |
| 1811 | ]; |
| 1812 | } |
| 1813 | |
| 1814 | if ( version_compare( WC_VERSION, '3.2', '<' ) ) { |
| 1815 | $cart_fees = WC()->cart->fees; |
| 1816 | } else { |
| 1817 | $cart_fees = WC()->cart->get_fees(); |
| 1818 | } |
| 1819 | |
| 1820 | // Include fees and taxes as display items. |
| 1821 | foreach ( $cart_fees as $key => $fee ) { |
| 1822 | $items[] = [ |
| 1823 | 'label' => $fee->name, |
| 1824 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $fee->amount ), |
| 1825 | ]; |
| 1826 | } |
| 1827 | |
| 1828 | return [ |
| 1829 | 'displayItems' => $items, |
| 1830 | 'total' => [ |
| 1831 | 'label' => $this->total_label, |
| 1832 | 'amount' => max( 0, apply_filters( 'woocommerce_stripe_calculated_total', WC_Stripe_Helper::get_stripe_amount( $order_total ), $order_total, WC()->cart ) ), |
| 1833 | 'pending' => false, |
| 1834 | ], |
| 1835 | ]; |
| 1836 | } |
| 1837 | |
| 1838 | /** |
| 1839 | * Settings array for the user authentication dialog and redirection. |
| 1840 | * |
| 1841 | * @since 5.3.0 |
| 1842 | * @version 5.3.0 |
| 1843 | * |
| 1844 | * @return array |
| 1845 | */ |
| 1846 | public function get_login_confirmation_settings() { |
| 1847 | if ( is_user_logged_in() || ! $this->is_authentication_required() ) { |
| 1848 | return false; |
| 1849 | } |
| 1850 | |
| 1851 | /* translators: The text encapsulated in `**` can be replaced with "Apple Pay" or "Google Pay". Please translate this text, but don't remove the `**`. */ |
| 1852 | $message = __( 'To complete your transaction with **the selected payment method**, you must log in or create an account with our site.', 'woocommerce-gateway-stripe' ); |
| 1853 | $redirect_url = add_query_arg( |
| 1854 | [ |
| 1855 | '_wpnonce' => wp_create_nonce( 'wc-stripe-set-redirect-url' ), |
| 1856 | 'wc_stripe_payment_request_redirect_url' => rawurlencode( home_url( add_query_arg( [] ) ) ), // Current URL to redirect to after login. |
| 1857 | ], |
| 1858 | home_url() |
| 1859 | ); |
| 1860 | |
| 1861 | return [ |
| 1862 | 'message' => $message, |
| 1863 | 'redirect_url' => $redirect_url, |
| 1864 | ]; |
| 1865 | } |
| 1866 | |
| 1867 | public function get_button_locations() { |
| 1868 | // If the locations have not been set return the default setting. |
| 1869 | if ( ! isset( $this->stripe_settings['payment_request_button_locations'] ) ) { |
| 1870 | return [ 'product', 'cart' ]; |
| 1871 | } |
| 1872 | |
| 1873 | // If all locations are removed through the settings UI the location config will be set to |
| 1874 | // an empty string "". If that's the case (and if the settings are not an array for any |
| 1875 | // other reason) we should return an empty array. |
| 1876 | if ( ! is_array( $this->stripe_settings['payment_request_button_locations'] ) ) { |
| 1877 | return []; |
| 1878 | } |
| 1879 | |
| 1880 | return $this->stripe_settings['payment_request_button_locations']; |
| 1881 | } |
| 1882 | } |