swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * WC_Stripe_Intent_Controller class. |
| 8 | * |
| 9 | * Handles in-checkout AJAX calls, related to Payment Intents. |
| 10 | */ |
| 11 | class WC_Stripe_Intent_Controller { |
| 12 | /** |
| 13 | * Holds an instance of the gateway class. |
| 14 | * |
| 15 | * @since 4.2.0 |
| 16 | * @var WC_Gateway_Stripe |
| 17 | */ |
| 18 | protected $gateway; |
| 19 | |
| 20 | /** |
| 21 | * Class constructor, adds the necessary hooks. |
| 22 | * |
| 23 | * @since 4.2.0 |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | add_action( 'wc_ajax_wc_stripe_verify_intent', [ $this, 'verify_intent' ] ); |
| 27 | add_action( 'wc_ajax_wc_stripe_create_setup_intent', [ $this, 'create_setup_intent' ] ); |
| 28 | |
| 29 | add_action( 'wc_ajax_wc_stripe_create_payment_intent', [ $this, 'create_payment_intent_ajax' ] ); |
| 30 | add_action( 'wc_ajax_wc_stripe_update_payment_intent', [ $this, 'update_payment_intent_ajax' ] ); |
| 31 | add_action( 'wc_ajax_wc_stripe_init_setup_intent', [ $this, 'init_setup_intent_ajax' ] ); |
| 32 | |
| 33 | add_action( 'wc_ajax_wc_stripe_save_upe_appearance', [ $this, 'save_upe_appearance_ajax' ] ); |
| 34 | add_action( 'wc_ajax_nopriv_wc_stripe_save_upe_appearance', [ $this, 'save_upe_appearance_ajax' ] ); |
| 35 | |
| 36 | add_action( 'wc_ajax_wc_stripe_update_order_status', [ $this, 'update_order_status_ajax' ] ); |
| 37 | add_action( 'wc_ajax_wc_stripe_update_failed_order', [ $this, 'update_failed_order_ajax' ] ); |
| 38 | |
| 39 | add_action( 'switch_theme', [ $this, 'clear_upe_appearance_transient' ] ); |
| 40 | |
| 41 | add_action( 'wp', [ $this, 'maybe_process_upe_redirect' ] ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns an instantiated gateway. |
| 46 | * |
| 47 | * @since 4.2.0 |
| 48 | * @return WC_Stripe_Payment_Gateway |
| 49 | */ |
| 50 | protected function get_gateway() { |
| 51 | if ( ! isset( $this->gateway ) ) { |
| 52 | $gateways = WC()->payment_gateways()->payment_gateways(); |
| 53 | $this->gateway = $gateways[ WC_Gateway_Stripe::ID ]; |
| 54 | } |
| 55 | |
| 56 | return $this->gateway; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns an instantiated UPE gateway |
| 61 | * |
| 62 | * @since 5.6.0 |
| 63 | * @throws WC_Stripe_Exception if UPE is not enabled. |
| 64 | * @return WC_Stripe_UPE_Payment_Gateway |
| 65 | */ |
| 66 | protected function get_upe_gateway() { |
| 67 | $gateway = $this->get_gateway(); |
| 68 | if ( ! $gateway instanceof WC_Stripe_UPE_Payment_Gateway ) { |
| 69 | WC_Stripe_Logger::log( 'Error instantiating the UPE Payment Gateway, UPE is not enabled.' ); |
| 70 | throw new WC_Stripe_Exception( __( "We're not able to process this payment.", 'woocommerce-gateway-stripe' ) ); |
| 71 | } |
| 72 | return $gateway; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Loads the order from the current request. |
| 77 | * |
| 78 | * @since 4.2.0 |
| 79 | * @throws WC_Stripe_Exception An exception if there is no order ID or the order does not exist. |
| 80 | * @return WC_Order |
| 81 | */ |
| 82 | protected function get_order_from_request() { |
| 83 | if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['nonce'] ), 'wc_stripe_confirm_pi' ) ) { |
| 84 | throw new WC_Stripe_Exception( 'missing-nonce', __( 'CSRF verification failed.', 'woocommerce-gateway-stripe' ) ); |
| 85 | } |
| 86 | |
| 87 | // Load the order ID. |
| 88 | $order_id = null; |
| 89 | if ( isset( $_GET['order'] ) && absint( $_GET['order'] ) ) { |
| 90 | $order_id = absint( $_GET['order'] ); |
| 91 | } |
| 92 | |
| 93 | // Retrieve the order. |
| 94 | $order = wc_get_order( $order_id ); |
| 95 | |
| 96 | if ( ! $order ) { |
| 97 | throw new WC_Stripe_Exception( 'missing-order', __( 'Missing order ID for payment confirmation', 'woocommerce-gateway-stripe' ) ); |
| 98 | } |
| 99 | |
| 100 | return $order; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Handles successful PaymentIntent authentications. |
| 105 | * |
| 106 | * @since 4.2.0 |
| 107 | */ |
| 108 | public function verify_intent() { |
| 109 | global $woocommerce; |
| 110 | |
| 111 | $gateway = $this->get_gateway(); |
| 112 | |
| 113 | try { |
| 114 | $order = $this->get_order_from_request(); |
| 115 | } catch ( WC_Stripe_Exception $e ) { |
| 116 | /* translators: Error message text */ |
| 117 | $message = sprintf( __( 'Payment verification error: %s', 'woocommerce-gateway-stripe' ), $e->getLocalizedMessage() ); |
| 118 | wc_add_notice( esc_html( $message ), 'error' ); |
| 119 | |
| 120 | $redirect_url = $woocommerce->cart->is_empty() |
| 121 | ? get_permalink( wc_get_page_id( 'shop' ) ) |
| 122 | : wc_get_checkout_url(); |
| 123 | |
| 124 | $this->handle_error( $e, $redirect_url ); |
| 125 | } |
| 126 | |
| 127 | try { |
| 128 | $gateway->verify_intent_after_checkout( $order ); |
| 129 | |
| 130 | if ( isset( $_GET['save_payment_method'] ) && ! empty( $_GET['save_payment_method'] ) ) { |
| 131 | $intent = $gateway->get_intent_from_order( $order ); |
| 132 | if ( isset( $intent->last_payment_error ) ) { |
| 133 | // Currently, Stripe saves the payment method even if the authentication fails for 3DS cards. |
| 134 | // Although, the card is not stored in DB we need to remove the source from the customer on Stripe |
| 135 | // in order to keep the sources in sync with the data in DB. |
| 136 | $customer = new WC_Stripe_Customer( wp_get_current_user()->ID ); |
| 137 | $customer->delete_source( $intent->last_payment_error->source->id ); |
| 138 | } else { |
| 139 | $metadata = $intent->metadata; |
| 140 | if ( isset( $metadata->save_payment_method ) && 'true' === $metadata->save_payment_method ) { |
| 141 | $source_object = WC_Stripe_API::retrieve( 'sources/' . $intent->source ); |
| 142 | $gateway->save_payment_method( $source_object ); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( ! isset( $_GET['is_ajax'] ) ) { |
| 148 | $redirect_url = isset( $_GET['redirect_to'] ) // wpcs: csrf ok. |
| 149 | ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) // wpcs: csrf ok. |
| 150 | : $gateway->get_return_url( $order ); |
| 151 | |
| 152 | wp_safe_redirect( $redirect_url ); |
| 153 | } |
| 154 | |
| 155 | exit; |
| 156 | } catch ( WC_Stripe_Exception $e ) { |
| 157 | $this->handle_error( $e, $gateway->get_return_url( $order ) ); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Handles exceptions during intent verification. |
| 163 | * |
| 164 | * @since 4.2.0 |
| 165 | * @param WC_Stripe_Exception $e The exception that was thrown. |
| 166 | * @param string $redirect_url An URL to use if a redirect is needed. |
| 167 | */ |
| 168 | protected function handle_error( $e, $redirect_url ) { |
| 169 | // Log the exception before redirecting. |
| 170 | $message = sprintf( 'PaymentIntent verification exception: %s', $e->getLocalizedMessage() ); |
| 171 | WC_Stripe_Logger::log( $message ); |
| 172 | |
| 173 | // `is_ajax` is only used for PI error reporting, a response is not expected. |
| 174 | if ( isset( $_GET['is_ajax'] ) ) { |
| 175 | exit; |
| 176 | } |
| 177 | |
| 178 | wp_safe_redirect( $redirect_url ); |
| 179 | exit; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Creates a Setup Intent through AJAX while adding cards. |
| 184 | */ |
| 185 | public function create_setup_intent() { |
| 186 | if ( |
| 187 | ! is_user_logged_in() |
| 188 | || ! isset( $_POST['stripe_source_id'] ) |
| 189 | || ! isset( $_POST['nonce'] ) |
| 190 | ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | try { |
| 195 | $source_id = wc_clean( wp_unslash( $_POST['stripe_source_id'] ) ); |
| 196 | |
| 197 | // 1. Verify. |
| 198 | if ( |
| 199 | ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'wc_stripe_create_si' ) |
| 200 | || ! preg_match( '/^src_.*$/', $source_id ) |
| 201 | ) { |
| 202 | throw new Exception( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); |
| 203 | } |
| 204 | |
| 205 | // 2. Load the customer ID (and create a customer eventually). |
| 206 | $customer = new WC_Stripe_Customer( wp_get_current_user()->ID ); |
| 207 | |
| 208 | // 3. Attach the source to the customer (Setup Intents require that). |
| 209 | $source_object = $customer->attach_source( $source_id ); |
| 210 | |
| 211 | if ( ! empty( $source_object->error ) ) { |
| 212 | throw new Exception( $source_object->error->message ); |
| 213 | } |
| 214 | if ( is_wp_error( $source_object ) ) { |
| 215 | throw new Exception( $source_object->get_error_message() ); |
| 216 | } |
| 217 | |
| 218 | // SEPA Direct Debit payments do not require any customer action after the source has been created. |
| 219 | // Once the customer has provided their IBAN details and accepted the mandate, no further action is needed and the resulting source is directly chargeable. |
| 220 | if ( 'sepa_debit' === $source_object->type ) { |
| 221 | $response = [ |
| 222 | 'status' => 'success', |
| 223 | ]; |
| 224 | echo wp_json_encode( $response ); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | // 4. Generate the setup intent |
| 229 | $setup_intent = WC_Stripe_API::request( |
| 230 | [ |
| 231 | 'customer' => $customer->get_id(), |
| 232 | 'confirm' => 'true', |
| 233 | 'payment_method' => $source_id, |
| 234 | ], |
| 235 | 'setup_intents' |
| 236 | ); |
| 237 | |
| 238 | if ( ! empty( $setup_intent->error ) ) { |
| 239 | $error_response_message = print_r( $setup_intent, true ); |
| 240 | WC_Stripe_Logger::log( 'Failed create Setup Intent while saving a card.' ); |
| 241 | WC_Stripe_Logger::log( "Response: $error_response_message" ); |
| 242 | throw new Exception( __( 'Your card could not be set up for future usage.', 'woocommerce-gateway-stripe' ) ); |
| 243 | } |
| 244 | |
| 245 | // 5. Respond. |
| 246 | if ( 'requires_action' === $setup_intent->status ) { |
| 247 | $response = [ |
| 248 | 'status' => 'requires_action', |
| 249 | 'client_secret' => $setup_intent->client_secret, |
| 250 | ]; |
| 251 | } elseif ( 'requires_payment_method' === $setup_intent->status |
| 252 | || 'requires_confirmation' === $setup_intent->status |
| 253 | || 'canceled' === $setup_intent->status ) { |
| 254 | // These statuses should not be possible, as such we return an error. |
| 255 | $response = [ |
| 256 | 'status' => 'error', |
| 257 | 'error' => [ |
| 258 | 'type' => 'setup_intent_error', |
| 259 | 'message' => __( 'Failed to save payment method.', 'woocommerce-gateway-stripe' ), |
| 260 | ], |
| 261 | ]; |
| 262 | } else { |
| 263 | // This should only be reached when status is `processing` or `succeeded`, which are |
| 264 | // the only statuses that we haven't explicitly handled. |
| 265 | $response = [ |
| 266 | 'status' => 'success', |
| 267 | ]; |
| 268 | } |
| 269 | } catch ( Exception $e ) { |
| 270 | $response = [ |
| 271 | 'status' => 'error', |
| 272 | 'error' => [ |
| 273 | 'type' => 'setup_intent_error', |
| 274 | 'message' => $e->getMessage(), |
| 275 | ], |
| 276 | ]; |
| 277 | } |
| 278 | |
| 279 | echo wp_json_encode( $response ); |
| 280 | exit; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Handle AJAX requests for creating a payment intent for Stripe UPE. |
| 285 | */ |
| 286 | public function create_payment_intent_ajax() { |
| 287 | try { |
| 288 | $is_nonce_valid = check_ajax_referer( 'wc_stripe_create_payment_intent_nonce', false, false ); |
| 289 | if ( ! $is_nonce_valid ) { |
| 290 | throw new Exception( __( "We're not able to process this payment. Please refresh the page and try again.", 'woocommerce-gateway-stripe' ) ); |
| 291 | } |
| 292 | |
| 293 | // If paying from order, we need to get the total from the order instead of the cart. |
| 294 | $order_id = isset( $_POST['stripe_order_id'] ) ? absint( $_POST['stripe_order_id'] ) : null; |
| 295 | |
| 296 | wp_send_json_success( $this->create_payment_intent( $order_id ), 200 ); |
| 297 | } catch ( Exception $e ) { |
| 298 | WC_Stripe_Logger::log( 'Create payment intent error: ' . $e->getMessage() ); |
| 299 | // Send back error so it can be displayed to the customer. |
| 300 | wp_send_json_error( |
| 301 | [ |
| 302 | 'error' => [ |
| 303 | 'message' => $e->getMessage(), |
| 304 | ], |
| 305 | ] |
| 306 | ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Creates payment intent using current cart or order and store details. |
| 312 | * |
| 313 | * @param {int} $order_id The id of the order if intent created from Order. |
| 314 | * @throws Exception - If the create intent call returns with an error. |
| 315 | * @return array |
| 316 | */ |
| 317 | public function create_payment_intent( $order_id = null ) { |
| 318 | $amount = WC()->cart->get_total( false ); |
| 319 | $order = wc_get_order( $order_id ); |
| 320 | if ( is_a( $order, 'WC_Order' ) ) { |
| 321 | $amount = $order->get_total(); |
| 322 | } |
| 323 | |
| 324 | $gateway = $this->get_upe_gateway(); |
| 325 | $enabled_payment_methods = $gateway->get_upe_enabled_at_checkout_payment_method_ids( $order_id ); |
| 326 | |
| 327 | $currency = get_woocommerce_currency(); |
| 328 | $capture = empty( $gateway->get_option( 'capture' ) ) || $gateway->get_option( 'capture' ) === 'yes'; |
| 329 | $payment_intent = WC_Stripe_API::request( |
| 330 | [ |
| 331 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) ), |
| 332 | 'currency' => strtolower( $currency ), |
| 333 | 'payment_method_types' => $enabled_payment_methods, |
| 334 | 'capture_method' => $capture ? 'automatic' : 'manual', |
| 335 | ], |
| 336 | 'payment_intents' |
| 337 | ); |
| 338 | |
| 339 | if ( ! empty( $payment_intent->error ) ) { |
| 340 | throw new Exception( $payment_intent->error->message ); |
| 341 | } |
| 342 | |
| 343 | return [ |
| 344 | 'id' => $payment_intent->id, |
| 345 | 'client_secret' => $payment_intent->client_secret, |
| 346 | ]; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Handle AJAX request for updating a payment intent for Stripe UPE. |
| 351 | * |
| 352 | * @since 5.6.0 |
| 353 | */ |
| 354 | public function update_payment_intent_ajax() { |
| 355 | try { |
| 356 | $is_nonce_valid = check_ajax_referer( 'wc_stripe_update_payment_intent_nonce', false, false ); |
| 357 | if ( ! $is_nonce_valid ) { |
| 358 | throw new Exception( __( "We're not able to process this payment. Please refresh the page and try again.", 'woocommerce-gateway-stripe' ) ); |
| 359 | } |
| 360 | |
| 361 | $order_id = isset( $_POST['stripe_order_id'] ) ? absint( $_POST['stripe_order_id'] ) : null; |
| 362 | $payment_intent_id = isset( $_POST['wc_payment_intent_id'] ) ? wc_clean( wp_unslash( $_POST['wc_payment_intent_id'] ) ) : ''; |
| 363 | $save_payment_method = isset( $_POST['save_payment_method'] ) ? 'yes' === wc_clean( wp_unslash( $_POST['save_payment_method'] ) ) : false; |
| 364 | $selected_upe_payment_type = ! empty( $_POST['selected_upe_payment_type'] ) ? wc_clean( wp_unslash( $_POST['selected_upe_payment_type'] ) ) : ''; |
| 365 | |
| 366 | wp_send_json_success( $this->update_payment_intent( $payment_intent_id, $order_id, $save_payment_method, $selected_upe_payment_type ), 200 ); |
| 367 | } catch ( Exception $e ) { |
| 368 | // Send back error so it can be displayed to the customer. |
| 369 | wp_send_json_error( |
| 370 | [ |
| 371 | 'error' => [ |
| 372 | 'message' => $e->getMessage(), |
| 373 | ], |
| 374 | ] |
| 375 | ); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Updates payment intent to be able to save payment method. |
| 381 | * |
| 382 | * @since 5.6.0 |
| 383 | * |
| 384 | * @param {string} $payment_intent_id The id of the payment intent to update. |
| 385 | * @param {int} $order_id The id of the order if intent created from Order. |
| 386 | * @param {boolean} $save_payment_method True if saving the payment method. |
| 387 | * @param {string} $selected_upe_payment_type The name of the selected UPE payment type or empty string. |
| 388 | * |
| 389 | * @throws Exception If the update intent call returns with an error. |
| 390 | * @return array|null An array with result of the update, or nothing |
| 391 | */ |
| 392 | public function update_payment_intent( $payment_intent_id = '', $order_id = null, $save_payment_method = false, $selected_upe_payment_type = '' ) { |
| 393 | $order = wc_get_order( $order_id ); |
| 394 | |
| 395 | if ( ! is_a( $order, 'WC_Order' ) ) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | $gateway = $this->get_upe_gateway(); |
| 400 | $amount = $order->get_total(); |
| 401 | $currency = $order->get_currency(); |
| 402 | $customer = new WC_Stripe_Customer( wp_get_current_user()->ID ); |
| 403 | |
| 404 | if ( $payment_intent_id ) { |
| 405 | |
| 406 | $request = [ |
| 407 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) ), |
| 408 | 'currency' => strtolower( $currency ), |
| 409 | 'metadata' => $gateway->get_metadata_from_order( $order ), |
| 410 | /* translators: 1) blog name 2) order number */ |
| 411 | 'description' => sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() ), |
| 412 | ]; |
| 413 | |
| 414 | if ( '' !== $selected_upe_payment_type ) { |
| 415 | // Only update the payment_method_types if we have a reference to the payment type the customer selected. |
| 416 | $request['payment_method_types'] = [ $selected_upe_payment_type ]; |
| 417 | if ( |
| 418 | WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID === $selected_upe_payment_type && |
| 419 | in_array( |
| 420 | WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID, |
| 421 | $gateway->get_upe_enabled_payment_method_ids(), |
| 422 | true |
| 423 | ) |
| 424 | ) { |
| 425 | $request['payment_method_types'] = [ |
| 426 | WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, |
| 427 | WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID, |
| 428 | ]; |
| 429 | } |
| 430 | $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); |
| 431 | } |
| 432 | if ( ! empty( $customer ) && $customer->get_id() ) { |
| 433 | $request['customer'] = $customer->get_id(); |
| 434 | } |
| 435 | if ( $save_payment_method ) { |
| 436 | $request['setup_future_usage'] = 'off_session'; |
| 437 | } |
| 438 | |
| 439 | $level3_data = $gateway->get_level3_data_from_order( $order ); |
| 440 | |
| 441 | WC_Stripe_API::request_with_level3_data( |
| 442 | $request, |
| 443 | "payment_intents/{$payment_intent_id}", |
| 444 | $level3_data, |
| 445 | $order |
| 446 | ); |
| 447 | |
| 448 | $order->update_status( 'pending', __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); |
| 449 | $order->save(); |
| 450 | WC_Stripe_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); |
| 451 | } |
| 452 | |
| 453 | return [ |
| 454 | 'success' => true, |
| 455 | ]; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Handle AJAX requests for creating a setup intent without confirmation for Stripe UPE. |
| 460 | * |
| 461 | * @since 5.6.0 |
| 462 | * @version 5.6.0 |
| 463 | */ |
| 464 | public function init_setup_intent_ajax() { |
| 465 | try { |
| 466 | $is_nonce_valid = check_ajax_referer( 'wc_stripe_create_setup_intent_nonce', false, false ); |
| 467 | if ( ! $is_nonce_valid ) { |
| 468 | throw new Exception( __( "We're not able to add this payment method. Please refresh the page and try again.", 'woocommerce-gateway-stripe' ) ); |
| 469 | } |
| 470 | |
| 471 | wp_send_json_success( $this->init_setup_intent(), 200 ); |
| 472 | } catch ( Exception $e ) { |
| 473 | // Send back error, so it can be displayed to the customer. |
| 474 | wp_send_json_error( |
| 475 | [ |
| 476 | 'error' => [ |
| 477 | 'message' => $e->getMessage(), |
| 478 | ], |
| 479 | ] |
| 480 | ); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Creates a setup intent without confirmation. |
| 486 | * |
| 487 | * @since 5.6.0 |
| 488 | * @version 5.6.0 |
| 489 | * @return array |
| 490 | * @throws Exception If customer for the current user cannot be read/found. |
| 491 | */ |
| 492 | public function init_setup_intent() { |
| 493 | // Determine the customer managing the payment methods, create one if we don't have one already. |
| 494 | $user = wp_get_current_user(); |
| 495 | $customer = new WC_Stripe_Customer( $user->ID ); |
| 496 | $customer_id = $customer->get_id(); |
| 497 | if ( empty( $customer_id ) ) { |
| 498 | $customer_data = WC_Stripe_Customer::map_customer_data( null, new WC_Customer( $user->ID ) ); |
| 499 | $customer_id = $customer->create_customer( $customer_data ); |
| 500 | } |
| 501 | |
| 502 | $gateway = $this->get_upe_gateway(); |
| 503 | $payment_method_types = array_filter( $gateway->get_upe_enabled_payment_method_ids(), [ $gateway, 'is_enabled_for_saved_payments' ] ); |
| 504 | |
| 505 | $setup_intent = WC_Stripe_API::request( |
| 506 | [ |
| 507 | 'customer' => $customer_id, |
| 508 | 'confirm' => 'false', |
| 509 | 'payment_method_types' => array_values( $payment_method_types ), |
| 510 | ], |
| 511 | 'setup_intents' |
| 512 | ); |
| 513 | |
| 514 | if ( ! empty( $setup_intent->error ) ) { |
| 515 | throw new Exception( $setup_intent->error->message ); |
| 516 | } |
| 517 | |
| 518 | return [ |
| 519 | 'id' => $setup_intent->id, |
| 520 | 'client_secret' => $setup_intent->client_secret, |
| 521 | ]; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Handle AJAX request for saving UPE appearance value to transient. |
| 526 | * |
| 527 | * @throws Exception - If nonce or setup intent is invalid. |
| 528 | */ |
| 529 | public function save_upe_appearance_ajax() { |
| 530 | try { |
| 531 | $is_nonce_valid = check_ajax_referer( 'wc_stripe_save_upe_appearance_nonce', false, false ); |
| 532 | if ( ! $is_nonce_valid ) { |
| 533 | throw new Exception( |
| 534 | __( 'Unable to update UPE appearance values at this time.', 'woocommerce-gateway-stripe' ) |
| 535 | ); |
| 536 | } |
| 537 | |
| 538 | $is_blocks_checkout = isset( $_POST['is_blocks_checkout'] ) ? rest_sanitize_boolean( wc_clean( wp_unslash( $_POST['is_blocks_checkout'] ) ) ) : false; |
| 539 | $appearance = isset( $_POST['appearance'] ) ? json_decode( wc_clean( wp_unslash( $_POST['appearance'] ) ) ) : null; |
| 540 | |
| 541 | $appearance_transient = $is_blocks_checkout ? WC_Stripe_UPE_Payment_Gateway::WC_BLOCKS_UPE_APPEARANCE_TRANSIENT : WC_Stripe_UPE_Payment_Gateway::UPE_APPEARANCE_TRANSIENT; |
| 542 | |
| 543 | if ( null !== $appearance ) { |
| 544 | set_transient( $appearance_transient, $appearance, DAY_IN_SECONDS ); |
| 545 | } |
| 546 | wp_send_json_success( $appearance, 200 ); |
| 547 | } catch ( Exception $e ) { |
| 548 | // Send back error so it can be displayed to the customer. |
| 549 | wp_send_json_error( |
| 550 | [ |
| 551 | 'error' => [ |
| 552 | 'message' => $e->getMessage(), |
| 553 | ], |
| 554 | ] |
| 555 | ); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Clear the saved UPE appearance transient value. |
| 561 | */ |
| 562 | public function clear_upe_appearance_transient() { |
| 563 | delete_transient( WC_Stripe_UPE_Payment_Gateway::UPE_APPEARANCE_TRANSIENT ); |
| 564 | delete_transient( WC_Stripe_UPE_Payment_Gateway::WC_BLOCKS_UPE_APPEARANCE_TRANSIENT ); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Handle AJAX request after authenticating payment at checkout. |
| 569 | * |
| 570 | * This function is used to update the order status after the user has |
| 571 | * been asked to authenticate their payment. |
| 572 | * |
| 573 | * This function is used for both: |
| 574 | * - regular checkout |
| 575 | * - Pay for Order page (in theory). |
| 576 | * |
| 577 | * @throws WC_Stripe_Exception |
| 578 | */ |
| 579 | public function update_order_status_ajax() { |
| 580 | try { |
| 581 | $is_nonce_valid = check_ajax_referer( 'wc_stripe_update_order_status_nonce', false, false ); |
| 582 | if ( ! $is_nonce_valid ) { |
| 583 | throw new WC_Stripe_Exception( 'missing-nonce', __( 'CSRF verification failed.', 'woocommerce-gateway-stripe' ) ); |
| 584 | } |
| 585 | |
| 586 | $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : false; |
| 587 | $order = wc_get_order( $order_id ); |
| 588 | if ( ! $order ) { |
| 589 | throw new WC_Stripe_Exception( 'order_not_found', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); |
| 590 | } |
| 591 | |
| 592 | $intent_id = $order->get_meta( '_stripe_intent_id' ); |
| 593 | $intent_id_received = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : null; |
| 594 | if ( empty( $intent_id_received ) || $intent_id_received !== $intent_id ) { |
| 595 | $note = sprintf( |
| 596 | /* translators: %1: transaction ID of the payment or a translated string indicating an unknown ID. */ |
| 597 | __( 'A payment with ID %s was used in an attempt to pay for this order. This payment intent ID does not match any payments for this order, so it was ignored and the order was not updated.', 'woocommerce-gateway-stripe' ), |
| 598 | $intent_id_received |
| 599 | ); |
| 600 | $order->add_order_note( $note ); |
| 601 | throw new WC_Stripe_Exception( 'invalid_intent_id', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); |
| 602 | } |
| 603 | $save_payment_method = isset( $_POST['payment_method_id'] ) && ! empty( wc_clean( wp_unslash( $_POST['payment_method_id'] ) ) ); |
| 604 | |
| 605 | $gateway = $this->get_upe_gateway(); |
| 606 | $gateway->process_order_for_confirmed_intent( $order, $intent_id_received, $save_payment_method ); |
| 607 | wp_send_json_success( |
| 608 | [ |
| 609 | 'return_url' => $gateway->get_return_url( $order ), |
| 610 | ], |
| 611 | 200 |
| 612 | ); |
| 613 | } catch ( WC_Stripe_Exception $e ) { |
| 614 | wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
| 615 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
| 616 | |
| 617 | /* translators: error message */ |
| 618 | if ( $order ) { |
| 619 | $order->update_status( 'failed' ); |
| 620 | } |
| 621 | |
| 622 | // Send back error so it can be displayed to the customer. |
| 623 | wp_send_json_error( |
| 624 | [ |
| 625 | 'error' => [ |
| 626 | 'message' => $e->getMessage(), |
| 627 | ], |
| 628 | ] |
| 629 | ); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Handle AJAX request if error occurs while confirming intent. |
| 635 | * We will log the error and update the order. |
| 636 | * |
| 637 | * @throws WC_Stripe_Exception |
| 638 | */ |
| 639 | public function update_failed_order_ajax() { |
| 640 | try { |
| 641 | $is_nonce_valid = check_ajax_referer( 'wc_stripe_update_failed_order_nonce', false, false ); |
| 642 | if ( ! $is_nonce_valid ) { |
| 643 | throw new WC_Stripe_Exception( 'missing-nonce', __( 'CSRF verification failed.', 'woocommerce-gateway-stripe' ) ); |
| 644 | } |
| 645 | |
| 646 | $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : null; |
| 647 | $intent_id = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : ''; |
| 648 | $order = wc_get_order( $order_id ); |
| 649 | |
| 650 | if ( ! empty( $order_id ) && ! empty( $intent_id ) && is_object( $order ) ) { |
| 651 | $payment_needed = 0 < $order->get_total(); |
| 652 | if ( $payment_needed ) { |
| 653 | $intent = WC_Stripe_API::retrieve( "payment_intents/$intent_id" ); |
| 654 | } else { |
| 655 | $intent = WC_Stripe_API::retrieve( "setup_intents/$intent_id" ); |
| 656 | } |
| 657 | $error = $intent->last_payment_error; |
| 658 | |
| 659 | if ( ! empty( $error ) ) { |
| 660 | WC_Stripe_Logger::log( 'Error when processing payment: ' . $error->message ); |
| 661 | throw new WC_Stripe_Exception( __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); |
| 662 | } |
| 663 | |
| 664 | // Use the last charge within the intent to proceed. |
| 665 | $gateway = $this->get_gateway(); |
| 666 | if ( isset( $intent->charges ) && ! empty( $intent->charges->data ) ) { |
| 667 | $charge = end( $intent->charges->data ); |
| 668 | $gateway->process_response( $charge, $order ); |
| 669 | } else { |
| 670 | // TODO: Add implementation for setup intents. |
| 671 | $gateway->process_response( $intent, $order ); |
| 672 | } |
| 673 | $gateway->save_intent_to_order( $order, $intent ); |
| 674 | } |
| 675 | } catch ( WC_Stripe_Exception $e ) { |
| 676 | // We are expecting an exception to be thrown here. |
| 677 | wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
| 678 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
| 679 | |
| 680 | do_action( 'wc_gateway_stripe_process_payment_error', $e, $order ); |
| 681 | |
| 682 | /* translators: error message */ |
| 683 | $order->update_status( 'failed' ); |
| 684 | } |
| 685 | |
| 686 | wp_send_json_success(); |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Check for a UPE redirect payment method on order received page or setup intent on payment methods page. |
| 691 | * |
| 692 | * @since 5.6.0 |
| 693 | * @version 5.6.0 |
| 694 | */ |
| 695 | public function maybe_process_upe_redirect() { |
| 696 | $gateway = $this->get_gateway(); |
| 697 | if ( is_a( $gateway, 'WC_Stripe_UPE_Payment_Gateway' ) ) { |
| 698 | $gateway->maybe_process_upe_redirect(); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | new WC_Stripe_Intent_Controller(); |