woocommerce /
woocommerce-gateway-stripe
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 3 | exit; |
||
| 4 | } |
||
| 5 | |||
| 6 | /** |
||
| 7 | * WC_Stripe_Apple_Pay class. |
||
| 8 | * |
||
| 9 | * @extends WC_Gateway_Stripe |
||
| 10 | */ |
||
| 11 | class WC_Stripe_Apple_Pay extends WC_Gateway_Stripe { |
||
| 12 | /** |
||
| 13 | * This Instance. |
||
| 14 | * |
||
| 15 | * @var |
||
| 16 | */ |
||
| 17 | private static $_this; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Gateway. |
||
| 21 | * |
||
| 22 | * @var |
||
| 23 | */ |
||
| 24 | private $_gateway; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Statement Description |
||
| 28 | * |
||
| 29 | * @var |
||
| 30 | */ |
||
| 31 | public $statement_descriptor; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Gateway settings. |
||
| 35 | * |
||
| 36 | * @var |
||
| 37 | */ |
||
| 38 | private $_gateway_settings; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * @access public |
||
| 44 | * @since 3.1.0 |
||
| 45 | * @version 3.1.0 |
||
| 46 | */ |
||
| 47 | public function __construct() { |
||
| 48 | self::$_this = $this; |
||
| 49 | |||
| 50 | $this->_gateway_settings = get_option( 'woocommerce_stripe_settings', '' ); |
||
| 51 | |||
| 52 | $this->statement_descriptor = ! empty( $this->_gateway_settings['statement_descriptor'] ) ? $this->_gateway_settings['statement_descriptor'] : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); |
||
| 53 | |||
| 54 | $this->init(); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function instance() { |
||
| 58 | return self::$_this; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initialize. |
||
| 63 | * |
||
| 64 | * @access public |
||
| 65 | * @since 3.1.0 |
||
| 66 | * @version 3.1.0 |
||
| 67 | */ |
||
| 68 | public function init() { |
||
| 69 | add_action( 'wp_enqueue_scripts', array( $this, 'cart_scripts' ) ); |
||
| 70 | add_action( 'wp_enqueue_scripts', array( $this, 'single_scripts' ) ); |
||
| 71 | add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'display_apple_pay_button' ), 1 ); |
||
| 72 | add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_button' ), 1 ); |
||
| 73 | add_action( 'woocommerce_proceed_to_checkout', array( $this, 'display_apple_pay_separator_html' ), 2 ); |
||
| 74 | add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_button' ), 1 ); |
||
| 75 | add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'display_apple_pay_separator_html' ), 2 ); |
||
| 76 | add_action( 'wc_ajax_wc_stripe_apple_pay', array( $this, 'process_apple_pay' ) ); |
||
| 77 | add_action( 'wc_ajax_wc_stripe_generate_apple_pay_cart', array( $this, 'generate_apple_pay_cart' ) ); |
||
| 78 | add_action( 'wc_ajax_wc_stripe_generate_apple_pay_single', array( $this, 'generate_apple_pay_single' ) ); |
||
| 79 | add_action( 'wc_ajax_wc_stripe_apple_pay_get_shipping_methods', array( $this, 'get_shipping_methods' ) ); |
||
| 80 | add_action( 'wc_ajax_wc_stripe_apple_pay_update_shipping_method', array( $this, 'update_shipping_method' ) ); |
||
| 81 | add_filter( 'woocommerce_gateway_title', array( $this, 'filter_gateway_title' ), 10, 2 ); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Filters the gateway title to reflect Apple Pay. |
||
| 86 | * |
||
| 87 | */ |
||
| 88 | public function filter_gateway_title( $title, $id ) { |
||
| 89 | global $post; |
||
| 90 | |||
| 91 | if ( ! is_object( $post ) ) { |
||
| 92 | return $title; |
||
| 93 | } |
||
| 94 | |||
| 95 | $method_title = get_post_meta( $post->ID, '_payment_method_title', true ); |
||
| 96 | |||
| 97 | if ( 'stripe' === $id && ! empty( $method_title ) ) { |
||
| 98 | return $method_title; |
||
| 99 | } |
||
| 100 | |||
| 101 | return $title; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Enqueue JS scripts and styles for single product page. |
||
| 106 | * |
||
| 107 | * @since 3.1.0 |
||
| 108 | * @version 3.1.0 |
||
| 109 | */ |
||
| 110 | public function single_scripts() { |
||
| 111 | if ( ! is_single() ) { |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | global $post; |
||
| 116 | |||
| 117 | $product = wc_get_product( $post->ID ); |
||
| 118 | |||
| 119 | if ( ! in_array( ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 124 | |||
| 125 | wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
||
| 126 | |||
| 127 | wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
||
| 128 | wp_enqueue_script( 'woocommerce_stripe_apple_pay_single', plugins_url( 'assets/js/stripe-apple-pay-single' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
||
| 129 | |||
| 130 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
||
| 131 | |||
| 132 | $stripe_params = array( |
||
| 133 | 'key' => $publishable_key, |
||
| 134 | 'currency_code' => get_woocommerce_currency(), |
||
| 135 | 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
||
| 136 | 'label' => $this->statement_descriptor, |
||
| 137 | 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
||
| 138 | 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
||
| 139 | 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
||
| 140 | 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
||
| 141 | 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
||
| 142 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
||
| 143 | ); |
||
| 144 | |||
| 145 | wp_localize_script( 'woocommerce_stripe_apple_pay_single', 'wc_stripe_apple_pay_single_params', apply_filters( 'wc_stripe_apple_pay_single_params', $stripe_params ) ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Enqueue JS scripts and styles for the cart/checkout. |
||
| 150 | * |
||
| 151 | * @since 3.1.0 |
||
| 152 | * @version 3.1.0 |
||
| 153 | */ |
||
| 154 | public function cart_scripts() { |
||
| 155 | if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) ) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 160 | |||
| 161 | wp_enqueue_style( 'stripe_apple_pay', plugins_url( 'assets/css/stripe-apple-pay.css', WC_STRIPE_MAIN_FILE ), array(), WC_STRIPE_VERSION ); |
||
| 162 | |||
| 163 | wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
||
| 164 | wp_enqueue_script( 'woocommerce_stripe_apple_pay', plugins_url( 'assets/js/stripe-apple-pay' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'stripe' ), WC_STRIPE_VERSION, true ); |
||
| 165 | |||
| 166 | $publishable_key = 'yes' === $this->_gateway_settings['testmode'] ? $this->_gateway_settings['test_publishable_key'] : $this->_gateway_settings['publishable_key']; |
||
| 167 | |||
| 168 | $stripe_params = array( |
||
| 169 | 'key' => $publishable_key, |
||
| 170 | 'currency_code' => get_woocommerce_currency(), |
||
| 171 | 'country_code' => substr( get_option( 'woocommerce_default_country' ), 0, 2 ), |
||
| 172 | 'label' => $this->statement_descriptor, |
||
| 173 | 'ajaxurl' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
||
| 174 | 'stripe_apple_pay_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_nonce' ), |
||
| 175 | 'stripe_apple_pay_cart_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_cart_nonce' ), |
||
| 176 | 'stripe_apple_pay_get_shipping_methods_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_get_shipping_methods_nonce' ), |
||
| 177 | 'stripe_apple_pay_update_shipping_method_nonce' => wp_create_nonce( '_wc_stripe_apple_pay_update_shipping_method_nonce' ), |
||
| 178 | 'needs_shipping' => WC()->cart->needs_shipping() ? 'yes' : 'no', |
||
| 179 | 'is_cart_page' => is_cart() ? 'yes' : 'no', |
||
| 180 | ); |
||
| 181 | |||
| 182 | wp_localize_script( 'woocommerce_stripe_apple_pay', 'wc_stripe_apple_pay_params', apply_filters( 'wc_stripe_apple_pay_params', $stripe_params ) ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Checks to make sure product type is supported by Apple Pay. |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | public function supported_product_types() { |
||
| 190 | return array( |
||
| 191 | 'simple', |
||
| 192 | 'variable', |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Display Apple Pay button on the cart page |
||
| 198 | * |
||
| 199 | * @since 3.1.0 |
||
| 200 | * @version 3.1.0 |
||
| 201 | */ |
||
| 202 | public function display_apple_pay_button() { |
||
| 203 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * In order for the Apple Pay button to show on cart page, |
||
| 207 | * Apple Pay must be enabled and Stripe gateway must be enabled. |
||
| 208 | */ |
||
| 209 | if ( |
||
| 210 | 'yes' !== $this->_gateway_settings['apple_pay'] |
||
| 211 | || ! isset( $gateways['stripe'] ) |
||
| 212 | ) { |
||
| 213 | return; |
||
| 214 | } |
||
| 215 | |||
| 216 | View Code Duplication | if ( is_single() ) { |
|
| 217 | global $post; |
||
| 218 | |||
| 219 | $product = wc_get_product( $post->ID ); |
||
| 220 | |||
| 221 | if ( ! in_array( ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | $apple_pay_button = ! empty( $this->_gateway_settings['apple_pay_button'] ) ? $this->_gateway_settings['apple_pay_button'] : 'black'; |
||
| 227 | $button_lang = ! empty( $this->_gateway_settings['apple_pay_button_lang'] ) ? strtolower( $this->_gateway_settings['apple_pay_button_lang'] ) : 'en'; |
||
| 228 | ?> |
||
| 229 | <button class="apple-pay-button" lang="<?php echo esc_attr( $button_lang ); ?>" style="-webkit-appearance: -apple-pay-button; -apple-pay-button-type: buy; -apple-pay-button-style: <?php echo esc_attr( $apple_pay_button ); ?>;"></button> |
||
| 230 | <?php |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Display Apple Pay button on the cart page |
||
| 235 | * |
||
| 236 | * @since 3.1.0 |
||
| 237 | * @version 3.1.0 |
||
| 238 | */ |
||
| 239 | public function display_apple_pay_separator_html() { |
||
| 240 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 241 | |||
| 242 | /** |
||
| 243 | * In order for the Apple Pay button to show on cart page, |
||
| 244 | * Apple Pay must be enabled and Stripe gateway must be enabled. |
||
| 245 | */ |
||
| 246 | if ( |
||
| 247 | 'yes' !== $this->_gateway_settings['apple_pay'] |
||
| 248 | || ! isset( $gateways['stripe'] ) |
||
| 249 | ) { |
||
| 250 | return; |
||
| 251 | } |
||
| 252 | |||
| 253 | View Code Duplication | if ( is_single() ) { |
|
| 254 | global $post; |
||
| 255 | |||
| 256 | $product = wc_get_product( $post->ID ); |
||
| 257 | |||
| 258 | if ( ! in_array( ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ), $this->supported_product_types() ) ) { |
||
| 259 | return; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | ?> |
||
| 263 | <p class="apple-pay-button-checkout-separator">- <?php esc_html_e( 'Or', 'woocommerce-gateway-stripe' ); ?> -</p> |
||
| 264 | <?php |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Generates the Apple Pay single cart. |
||
| 269 | * |
||
| 270 | * @since 3.1.0 |
||
| 271 | * @version 3.1.0 |
||
| 272 | */ |
||
| 273 | public function generate_apple_pay_single() { |
||
| 274 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
||
| 275 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 276 | } |
||
| 277 | |||
| 278 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
||
| 279 | define( 'WOOCOMMERCE_CART', true ); |
||
| 280 | } |
||
| 281 | |||
| 282 | global $post; |
||
| 283 | |||
| 284 | $product = wc_get_product( $post->ID ); |
||
| 285 | $qty = absint( $_POST['qty'] ); |
||
| 286 | |||
| 287 | /** |
||
| 288 | * If this page is single product page, we need to simulate |
||
| 289 | * adding the product to the cart taken account if it is a |
||
| 290 | * simple or variable product. |
||
| 291 | */ |
||
| 292 | if ( is_single() ) { |
||
| 293 | // First empty the cart to prevent wrong calculation. |
||
| 294 | WC()->cart->empty_cart(); |
||
| 295 | |||
| 296 | if ( 'variable' === ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ) && isset( $_POST['attributes'] ) ) { |
||
| 297 | $attributes = array_map( 'wc_clean', $_POST['attributes'] ); |
||
| 298 | |||
| 299 | $variation_id = $product->get_matching_variation( $attributes ); |
||
| 300 | |||
| 301 | WC()->cart->add_to_cart( $product->get_id(), $qty, $variation_id, $attributes ); |
||
| 302 | } |
||
| 303 | |||
| 304 | if ( 'simple' === ( version_compare( WC_VERSION, '2.7.0', '<' ) ? $product->product_type : $product->get_type() ) ) { |
||
| 305 | WC()->cart->add_to_cart( $product->get_id(), $qty ); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | WC()->cart->calculate_totals(); |
||
| 310 | |||
| 311 | wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Generates the Apple Pay cart. |
||
| 316 | * |
||
| 317 | * @since 3.1.0 |
||
| 318 | * @version 3.1.0 |
||
| 319 | */ |
||
| 320 | public function generate_apple_pay_cart() { |
||
| 321 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_cart_nonce' ) ) { |
||
| 322 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 323 | } |
||
| 324 | |||
| 325 | wp_send_json( array( 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Calculate and set shipping method. |
||
| 330 | * |
||
| 331 | * @since 3.1.0 |
||
| 332 | * @version 3.1.0 |
||
| 333 | * @param array $address |
||
| 334 | */ |
||
| 335 | public function calculate_shipping( $address = array() ) { |
||
| 336 | $country = strtoupper( $address['countryCode'] ); |
||
| 337 | $state = strtoupper( $address['administrativeArea'] ); |
||
| 338 | $postcode = $address['postalCode']; |
||
| 339 | $city = $address['locality']; |
||
| 340 | |||
| 341 | WC()->shipping->reset_shipping(); |
||
| 342 | |||
| 343 | if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
||
| 344 | throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce-gateway-stripe' ) ); |
||
| 345 | } elseif ( $postcode ) { |
||
| 346 | $postcode = wc_format_postcode( $postcode, $country ); |
||
| 347 | } |
||
| 348 | |||
| 349 | View Code Duplication | if ( $country ) { |
|
| 350 | WC()->customer->set_location( $country, $state, $postcode, $city ); |
||
| 351 | WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
||
| 352 | } else { |
||
| 353 | WC()->customer->set_to_base(); |
||
| 354 | WC()->customer->set_shipping_to_base(); |
||
| 355 | } |
||
| 356 | |||
| 357 | WC()->customer->calculated_shipping( true ); |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set the shipping package. |
||
| 361 | * |
||
| 362 | * Note that address lines are not provided at this point |
||
| 363 | * because Apple Pay does not supply that until after |
||
| 364 | * authentication via passcode or Touch ID. We will need to |
||
| 365 | * capture this information when we process the payment. |
||
| 366 | */ |
||
| 367 | |||
| 368 | $packages = array(); |
||
| 369 | |||
| 370 | $packages[0]['contents'] = WC()->cart->get_cart(); |
||
| 371 | $packages[0]['contents_cost'] = 0; |
||
| 372 | $packages[0]['applied_coupons'] = WC()->cart->applied_coupons; |
||
| 373 | $packages[0]['user']['ID'] = get_current_user_id(); |
||
| 374 | $packages[0]['destination']['country'] = $country; |
||
| 375 | $packages[0]['destination']['state'] = $state; |
||
| 376 | $packages[0]['destination']['postcode'] = $postcode; |
||
| 377 | $packages[0]['destination']['city'] = $city; |
||
| 378 | |||
| 379 | View Code Duplication | foreach ( WC()->cart->get_cart() as $item ) { |
|
| 380 | if ( $item['data']->needs_shipping() ) { |
||
| 381 | if ( isset( $item['line_total'] ) ) { |
||
| 382 | $packages[0]['contents_cost'] += $item['line_total']; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | $packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
||
| 388 | |||
| 389 | WC()->shipping->calculate_shipping( $packages ); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Gets shipping for Apple Pay Payment sheet. |
||
| 394 | * |
||
| 395 | * @since 3.1.0 |
||
| 396 | * @version 3.1.0 |
||
| 397 | */ |
||
| 398 | public function get_shipping_methods() { |
||
| 399 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_get_shipping_methods_nonce' ) ) { |
||
| 400 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 401 | } |
||
| 402 | |||
| 403 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
||
| 404 | define( 'WOOCOMMERCE_CART', true ); |
||
| 405 | } |
||
| 406 | |||
| 407 | try { |
||
| 408 | $address = array_map( 'wc_clean', $_POST['address'] ); |
||
| 409 | |||
| 410 | $this->calculate_shipping( $address ); |
||
| 411 | |||
| 412 | // Set the shipping options. |
||
| 413 | $currency = get_woocommerce_currency(); |
||
| 414 | $data = array(); |
||
| 415 | |||
| 416 | $packages = WC()->shipping->get_packages(); |
||
| 417 | |||
| 418 | if ( ! empty( $packages ) && WC()->customer->has_calculated_shipping() ) { |
||
| 419 | foreach ( $packages as $package_key => $package ) { |
||
| 420 | if ( empty( $package['rates'] ) ) { |
||
| 421 | throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
||
| 422 | } |
||
| 423 | |||
| 424 | foreach ( $package['rates'] as $key => $rate ) { |
||
| 425 | $data[] = array( |
||
| 426 | 'id' => $rate->id, |
||
| 427 | 'label' => $rate->label, |
||
| 428 | 'amount' => array( |
||
| 429 | 'currency' => $currency, |
||
| 430 | 'value' => $rate->cost, |
||
| 431 | ), |
||
| 432 | 'selected' => false, |
||
| 433 | ); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | // Auto select the first shipping method. |
||
| 438 | WC()->session->set( 'chosen_shipping_methods', array( $data[0]['id'] ) ); |
||
| 439 | |||
| 440 | WC()->cart->calculate_totals(); |
||
| 441 | |||
| 442 | wp_send_json( array( 'success' => 'true', 'shipping_methods' => $this->build_shipping_methods( $data ), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
||
| 443 | } else { |
||
| 444 | throw new Exception( __( 'Unable to find shipping method for address.', 'woocommerce-gateway-stripe' ) ); |
||
| 445 | } |
||
| 446 | } catch ( Exception $e ) { |
||
| 447 | wp_send_json( array( 'success' => 'false', 'shipping_methods' => array(), 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Updates shipping method on cart session. |
||
| 453 | * |
||
| 454 | * @since 3.1.0 |
||
| 455 | * @version 3.1.0 |
||
| 456 | */ |
||
| 457 | public function update_shipping_method() { |
||
| 458 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
||
| 459 | define( 'WOOCOMMERCE_CART', true ); |
||
| 460 | } |
||
| 461 | |||
| 462 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_update_shipping_method_nonce' ) ) { |
||
| 463 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 464 | } |
||
| 465 | |||
| 466 | $selected_shipping_method = array_map( 'wc_clean', $_POST['selected_shipping_method'] ); |
||
| 467 | |||
| 468 | WC()->session->set( 'chosen_shipping_methods', array( $selected_shipping_method['identifier'] ) ); |
||
| 469 | |||
| 470 | WC()->cart->calculate_totals(); |
||
| 471 | |||
| 472 | // Send back the new cart total. |
||
| 473 | $currency = get_woocommerce_currency(); |
||
| 474 | $tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) ); |
||
| 475 | $data = array( |
||
| 476 | 'total' => WC()->cart->total, |
||
| 477 | ); |
||
| 478 | |||
| 479 | // Include fees and taxes as displayItems. |
||
| 480 | View Code Duplication | foreach ( WC()->cart->fees as $key => $fee ) { |
|
| 481 | $data['items'][] = array( |
||
| 482 | 'label' => $fee->name, |
||
| 483 | 'amount' => array( |
||
| 484 | 'currency' => $currency, |
||
| 485 | 'value' => $fee->amount, |
||
| 486 | ), |
||
| 487 | ); |
||
| 488 | } |
||
| 489 | View Code Duplication | if ( 0 < $tax_total ) { |
|
| 490 | $data['items'][] = array( |
||
| 491 | 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
||
| 492 | 'amount' => array( |
||
| 493 | 'currency' => $currency, |
||
| 494 | 'value' => $tax_total, |
||
| 495 | ), |
||
| 496 | ); |
||
| 497 | } |
||
| 498 | |||
| 499 | wp_send_json( array( 'success' => 'true', 'line_items' => $this->build_line_items(), 'total' => WC()->cart->total ) ); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Handles the Apple Pay processing via AJAX |
||
| 504 | * |
||
| 505 | * @access public |
||
| 506 | * @since 3.1.0 |
||
| 507 | * @version 3.1.0 |
||
| 508 | */ |
||
| 509 | public function process_apple_pay() { |
||
| 510 | if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_apple_pay_nonce' ) ) { |
||
| 511 | wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
||
| 512 | } |
||
| 513 | |||
| 514 | try { |
||
| 515 | $result = array_map( 'wc_clean', $_POST['result'] ); |
||
| 516 | |||
| 517 | $order = $this->create_order( $result ); |
||
| 518 | |||
| 519 | $order_id = version_compare( WC_VERSION, '2.7.0', '<' ) ? $order->id : $order->get_id(); |
||
| 520 | |||
| 521 | // Handle payment. |
||
| 522 | if ( $order->get_total() > 0 ) { |
||
| 523 | |||
| 524 | View Code Duplication | if ( $order->get_total() * 100 < WC_Stripe::get_minimum_amount() ) { |
|
| 525 | return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe::get_minimum_amount() / 100 ) ) ); |
||
| 526 | } |
||
| 527 | |||
| 528 | WC_Stripe::log( "Info: Begin processing payment for order {$order_id} for the amount of {$order->get_total()}" ); |
||
| 529 | |||
| 530 | // Make the request. |
||
| 531 | $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $result['token']['id'] ) ); |
||
| 532 | |||
| 533 | if ( is_wp_error( $response ) ) { |
||
| 534 | $localized_messages = $this->get_localized_messages(); |
||
| 535 | |||
| 536 | throw new Exception( ( isset( $localized_messages[ $response->get_error_code() ] ) ? $localized_messages[ $response->get_error_code() ] : $response->get_error_message() ) ); |
||
| 537 | } |
||
| 538 | |||
| 539 | // Process valid response. |
||
| 540 | $this->process_response( $response, $order ); |
||
| 541 | } else { |
||
| 542 | $order->payment_complete(); |
||
| 543 | } |
||
| 544 | |||
| 545 | // Remove cart. |
||
| 546 | WC()->cart->empty_cart(); |
||
| 547 | |||
| 548 | update_post_meta( $order_id, '_customer_user', get_current_user_id() ); |
||
| 549 | update_post_meta( $order_id, '_payment_method_title', __( 'Apple Pay (Stripe)', 'woocommerce-gateway-stripe' ) ); |
||
| 550 | |||
| 551 | // Return thank you page redirect. |
||
| 552 | wp_send_json( array( |
||
| 553 | 'success' => 'true', |
||
| 554 | 'redirect' => $this->get_return_url( $order ), |
||
| 555 | ) ); |
||
| 556 | |||
| 557 | } catch ( Exception $e ) { |
||
| 558 | WC()->session->set( 'refresh_totals', true ); |
||
| 559 | WC_Stripe::log( sprintf( __( 'Error: %s', 'woocommerce-gateway-stripe' ), $e->getMessage() ) ); |
||
| 560 | |||
| 561 | if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
||
|
0 ignored issues
–
show
|
|||
| 562 | $this->send_failed_order_email( $order_id ); |
||
|
0 ignored issues
–
show
The variable
$order_id does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 563 | } |
||
| 564 | |||
| 565 | wp_send_json( array( 'success' => 'false', 'msg' => $e->getMessage() ) ); |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Generate the request for the payment. |
||
| 571 | * @param WC_Order $order |
||
| 572 | * @param string $source token |
||
| 573 | * @return array() |
||
| 574 | */ |
||
| 575 | protected function generate_payment_request( $order, $source ) { |
||
| 576 | $post_data = array(); |
||
| 577 | $post_data['currency'] = strtolower( version_compare( WC_VERSION, '2.7.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ); |
||
| 578 | $post_data['amount'] = $this->get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
||
| 579 | $post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), $this->statement_descriptor, $order->get_order_number() ); |
||
| 580 | $post_data['capture'] = 'yes' === $this->_gateway_settings['capture'] ? 'true' : 'false'; |
||
| 581 | |||
| 582 | $billing_email = version_compare( WC_VERSION, '2.7.0', '<' ) ? $order->billing_email : $order->get_billing_email(); |
||
| 583 | |||
| 584 | if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
||
| 585 | $post_data['receipt_email'] = $billing_email; |
||
| 586 | } |
||
| 587 | |||
| 588 | $post_data['expand[]'] = 'balance_transaction'; |
||
| 589 | $post_data['source'] = $source; |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Filter the return value of the WC_Payment_Gateway_CC::generate_payment_request. |
||
| 593 | * |
||
| 594 | * @since 3.1.0 |
||
| 595 | * @param array $post_data |
||
| 596 | * @param WC_Order $order |
||
| 597 | * @param object $source |
||
| 598 | */ |
||
| 599 | return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order ); |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Builds the shippings methods to pass to Apple Pay. |
||
| 604 | * |
||
| 605 | * @since 3.1.0 |
||
| 606 | * @version 3.1.0 |
||
| 607 | */ |
||
| 608 | public function build_shipping_methods( $shipping_methods ) { |
||
| 609 | if ( empty( $shipping_methods ) ) { |
||
| 610 | return array(); |
||
| 611 | } |
||
| 612 | |||
| 613 | $shipping = array(); |
||
| 614 | |||
| 615 | foreach ( $shipping_methods as $method ) { |
||
| 616 | $shipping[] = array( |
||
| 617 | 'label' => $method['label'], |
||
| 618 | 'detail' => '', |
||
| 619 | 'amount' => $method['amount']['value'], |
||
| 620 | 'identifier' => $method['id'], |
||
| 621 | ); |
||
| 622 | } |
||
| 623 | |||
| 624 | return $shipping; |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Builds the line items to pass to Apple Pay. |
||
| 629 | * |
||
| 630 | * @since 3.1.0 |
||
| 631 | * @version 3.1.0 |
||
| 632 | */ |
||
| 633 | public function build_line_items() { |
||
| 634 | if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
||
| 635 | define( 'WOOCOMMERCE_CART', true ); |
||
| 636 | } |
||
| 637 | |||
| 638 | $decimals = apply_filters( 'wc_stripe_apple_pay_decimals', 2 ); |
||
| 639 | |||
| 640 | $items = array(); |
||
| 641 | |||
| 642 | foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
||
| 643 | $amount = wc_format_decimal( $values['line_subtotal'], $decimals ); |
||
| 644 | $quantity_label = 1 < $values['quantity'] ? ' (x' . $values['quantity'] . ')' : ''; |
||
| 645 | |||
| 646 | $item = array( |
||
| 647 | 'type' => 'final', |
||
| 648 | 'label' => $values['data']->post->post_title . $quantity_label, |
||
| 649 | 'amount' => wc_format_decimal( $amount, $decimals ), |
||
| 650 | ); |
||
| 651 | |||
| 652 | $items[] = $item; |
||
| 653 | } |
||
| 654 | |||
| 655 | $discounts = wc_format_decimal( WC()->cart->get_cart_discount_total(), $decimals ); |
||
| 656 | $tax = wc_format_decimal( WC()->cart->tax_total + WC()->cart->shipping_tax_total, $decimals ); |
||
| 657 | $shipping = wc_format_decimal( WC()->cart->shipping_total, $decimals ); |
||
| 658 | $item_total = wc_format_decimal( WC()->cart->cart_contents_total, $decimals ) + $discounts; |
||
| 659 | $order_total = wc_format_decimal( $item_total + $tax + $shipping, $decimals ); |
||
| 660 | |||
| 661 | if ( wc_tax_enabled() ) { |
||
| 662 | $items[] = array( |
||
| 663 | 'type' => 'final', |
||
| 664 | 'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
||
| 665 | 'amount' => $tax, |
||
| 666 | ); |
||
| 667 | } |
||
| 668 | |||
| 669 | View Code Duplication | if ( WC()->cart->needs_shipping() ) { |
|
| 670 | $items[] = array( |
||
| 671 | 'type' => 'final', |
||
| 672 | 'label' => __( 'Shipping', 'woocommerce-gateway-stripe' ), |
||
| 673 | 'amount' => $shipping, |
||
| 674 | ); |
||
| 675 | } |
||
| 676 | |||
| 677 | View Code Duplication | if ( WC()->cart->has_discount() ) { |
|
| 678 | $items[] = array( |
||
| 679 | 'type' => 'final', |
||
| 680 | 'label' => __( 'Discount', 'woocommerce-gateway-stripe' ), |
||
| 681 | 'amount' => $discounts, |
||
| 682 | ); |
||
| 683 | } |
||
| 684 | |||
| 685 | return $items; |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Create order programatically. |
||
| 690 | * |
||
| 691 | * @since 3.1.0 |
||
| 692 | * @version 3.1.0 |
||
| 693 | * @param array $data |
||
| 694 | * @return object $order |
||
| 695 | */ |
||
| 696 | public function create_order( $data = array() ) { |
||
| 697 | if ( empty( $data ) ) { |
||
| 698 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
||
| 699 | } |
||
| 700 | |||
| 701 | $order = wc_create_order(); |
||
| 702 | $order_id = version_compare( WC_VERSION, '2.7.0', '<' ) ? $order->id : $order->get_id(); |
||
| 703 | |||
| 704 | if ( is_wp_error( $order ) ) { |
||
| 705 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
||
| 706 | } elseif ( false === $order ) { |
||
| 707 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) ); |
||
| 708 | } else { |
||
| 709 | do_action( 'woocommerce_new_order', $order_id ); |
||
| 710 | } |
||
| 711 | |||
| 712 | // Store the line items to the new/resumed order |
||
| 713 | foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
||
| 714 | $item_id = $order->add_product( |
||
| 715 | $values['data'], |
||
| 716 | $values['quantity'], |
||
| 717 | array( |
||
| 718 | 'variation' => $values['variation'], |
||
| 719 | 'totals' => array( |
||
| 720 | 'subtotal' => $values['line_subtotal'], |
||
| 721 | 'subtotal_tax' => $values['line_subtotal_tax'], |
||
| 722 | 'total' => $values['line_total'], |
||
| 723 | 'tax' => $values['line_tax'], |
||
| 724 | 'tax_data' => $values['line_tax_data'], // Since 2.2 |
||
| 725 | ), |
||
| 726 | ) |
||
| 727 | ); |
||
| 728 | |||
| 729 | if ( ! $item_id ) { |
||
| 730 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) ); |
||
| 731 | } |
||
| 732 | |||
| 733 | // Allow plugins to add order item meta |
||
| 734 | do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key ); |
||
| 735 | } |
||
| 736 | |||
| 737 | // Store fees |
||
| 738 | foreach ( WC()->cart->get_fees() as $fee_key => $fee ) { |
||
| 739 | $item_id = $order->add_fee( $fee ); |
||
| 740 | |||
| 741 | if ( ! $item_id ) { |
||
| 742 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) ); |
||
| 743 | } |
||
| 744 | |||
| 745 | // Allow plugins to add order item meta to fees |
||
| 746 | do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key ); |
||
| 747 | } |
||
| 748 | |||
| 749 | // Store tax rows |
||
| 750 | foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) { |
||
| 751 | if ( $tax_rate_id && ! $order->add_tax( $tax_rate_id, WC()->cart->get_tax_amount( $tax_rate_id ), WC()->cart->get_shipping_tax_amount( $tax_rate_id ) ) && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { |
||
| 752 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) ); |
||
| 753 | } |
||
| 754 | } |
||
| 755 | |||
| 756 | // Store coupons |
||
| 757 | foreach ( WC()->cart->get_coupons() as $code => $coupon ) { |
||
| 758 | if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) { |
||
| 759 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) ); |
||
| 760 | } |
||
| 761 | } |
||
| 762 | |||
| 763 | // Billing address |
||
| 764 | $billing_address = array(); |
||
| 765 | if ( ! empty( $data['token']['card'] ) ) { |
||
| 766 | // Name from Stripe is a full name string. |
||
| 767 | $name = explode( ' ', $data['token']['card']['name'] ); |
||
| 768 | $lastname = array_pop( $name ); |
||
| 769 | $firstname = implode( ' ', $name ); |
||
| 770 | $billing_address['first_name'] = $firstname; |
||
| 771 | $billing_address['last_name'] = $lastname; |
||
| 772 | $billing_address['email'] = $data['shippingContact']['emailAddress']; |
||
| 773 | $billing_address['phone'] = $data['shippingContact']['phoneNumber']; |
||
| 774 | $billing_address['country'] = $data['token']['card']['country']; |
||
| 775 | $billing_address['address_1'] = $data['token']['card']['address_line1']; |
||
| 776 | $billing_address['address_2'] = $data['token']['card']['address_line2']; |
||
| 777 | $billing_address['city'] = $data['token']['card']['address_city']; |
||
| 778 | $billing_address['state'] = $data['token']['card']['address_state']; |
||
| 779 | $billing_address['postcode'] = $data['token']['card']['address_zip']; |
||
| 780 | } |
||
| 781 | |||
| 782 | // Shipping address. |
||
| 783 | $shipping_address = array(); |
||
| 784 | if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) { |
||
| 785 | $shipping_address['first_name'] = $data['shippingContact']['givenName']; |
||
| 786 | $shipping_address['last_name'] = $data['shippingContact']['familyName']; |
||
| 787 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
||
| 788 | $shipping_address['phone'] = $data['shippingContact']['phoneNumber']; |
||
| 789 | $shipping_address['country'] = $data['shippingContact']['countryCode']; |
||
| 790 | $shipping_address['address_1'] = $data['shippingContact']['addressLines'][0]; |
||
| 791 | $shipping_address['address_2'] = $data['shippingContact']['addressLines'][1]; |
||
| 792 | $shipping_address['city'] = $data['shippingContact']['locality']; |
||
| 793 | $shipping_address['state'] = $data['shippingContact']['administrativeArea']; |
||
| 794 | $shipping_address['postcode'] = $data['shippingContact']['postalCode']; |
||
| 795 | } elseif ( ! empty( $data['shippingContact'] ) ) { |
||
| 796 | $shipping_address['first_name'] = $firstname; |
||
| 797 | $shipping_address['last_name'] = $lastname; |
||
| 798 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
||
| 799 | $shipping_address['phone'] = $data['shippingContact']['phoneNumber']; |
||
| 800 | $shipping_address['country'] = $data['token']['card']['country']; |
||
| 801 | $shipping_address['address_1'] = $data['token']['card']['address_line1']; |
||
| 802 | $shipping_address['address_2'] = $data['token']['card']['address_line2']; |
||
| 803 | $shipping_address['city'] = $data['token']['card']['address_city']; |
||
| 804 | $shipping_address['state'] = $data['token']['card']['address_state']; |
||
| 805 | $shipping_address['postcode'] = $data['token']['card']['address_zip']; |
||
| 806 | } |
||
| 807 | |||
| 808 | $order->set_address( $billing_address, 'billing' ); |
||
| 809 | $order->set_address( $shipping_address, 'shipping' ); |
||
| 810 | |||
| 811 | WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() ); |
||
| 812 | |||
| 813 | // Get the rate object selected by user. |
||
| 814 | foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
||
| 815 | foreach ( $package['rates'] as $key => $rate ) { |
||
| 816 | // Loop through user chosen shipping methods. |
||
| 817 | foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $method ) { |
||
| 818 | if ( $method === $key ) { |
||
| 819 | $order->add_shipping( $rate ); |
||
| 820 | } |
||
| 821 | } |
||
| 822 | } |
||
| 823 | } |
||
| 824 | |||
| 825 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 826 | $order->set_payment_method( $available_gateways['stripe'] ); |
||
| 827 | $order->set_total( WC()->cart->shipping_total, 'shipping' ); |
||
| 828 | $order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' ); |
||
| 829 | $order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' ); |
||
| 830 | $order->set_total( WC()->cart->tax_total, 'tax' ); |
||
| 831 | $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); |
||
| 832 | $order->set_total( WC()->cart->total ); |
||
| 833 | |||
| 834 | // If we got here, the order was created without problems! |
||
| 835 | wc_transaction_query( 'commit' ); |
||
| 836 | |||
| 837 | return $order; |
||
| 838 | } |
||
| 839 | } |
||
| 840 | |||
| 841 | new WC_Stripe_Apple_Pay(); |
||
| 842 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: