1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_Redirect_Handler {
public static function init() {
add_action( 'wp', array( __CLASS__, 'local_payment_redirect' ) );
add_action( 'get_header', array( __CLASS__, 'maybe_restore_cart' ), 100 );
}
public static function local_payment_redirect() {
if ( isset( $_GET['_stripe_local_payment'] ) ) {
self::process_redirect();
} elseif ( isset( $_GET['_stripe_voucher_payment'], $_GET['order-id'] ) ) {
self::process_voucher_redirect();
}
}
public static function process_redirect() {
if ( isset( $_GET['source'] ) ) {
$result = WC_Stripe_Gateway::load()->sources->retrieve( wc_clean( $_GET['source'] ) );
$client_secret = isset( $_GET['client_secret'] ) ? $_GET['client_secret'] : '';
} else {
$result = WC_Stripe_Gateway::load()->paymentIntents->retrieve( wc_clean( $_GET['payment_intent'] ) );
$client_secret = isset( $_GET['payment_intent_client_secret'] ) ? $_GET['payment_intent_client_secret'] : '';
}
if ( is_wp_error( $result ) ) {
wc_add_notice( sprintf( __( 'Error retrieving payment source. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ), 'error' );
} elseif ( ! hash_equals( $client_secret, $result->client_secret ) ) {
wc_add_notice( __( 'This request is invalid. Please try again.', 'woo-stripe-payment' ), 'error' );
} else {
define( WC_Stripe_Constants::REDIRECT_HANDLER, true );
$order_id = $result->metadata['order_id'];
$order = wc_get_order( wc_stripe_filter_order_id( $order_id, $result ) );
$payment_method = WC()->payment_gateways()->payment_gateways()[ $order->get_payment_method() ];
$redirect = $payment_method->get_return_url( $order );
if ( in_array( $result->status, array( 'requires_action', 'pending' ) ) ) {
if ( $result->status === 'pending' ) {
$order->update_status( 'on-hold' );
} else {
return;
}
} elseif ( in_array( $result->status, array( 'requires_payment_method', 'failed' ) ) ) {
wc_add_notice( __( 'Payment authorization failed. Please select another payment method.', 'woo-stripe-payment' ), 'error' );
if ( $result instanceof \Stripe\PaymentIntent ) {
$order->update_meta_data( WC_Stripe_Constants::PAYMENT_INTENT, $result->jsonSerialize() );
} else {
$order->delete_meta_data( WC_Stripe_Constants::SOURCE_ID );
}
$order->update_status( 'failed', __( 'Payment authorization failed.', 'woo-stripe-payment' ) );
return;
} elseif ( 'chargeable' === $result->status ) {
if ( ! $payment_method->has_order_lock( $order ) && ! $order->get_transaction_id() ) {
$payment_method->set_order_lock( $order );
$payment_method->set_new_source_token( $result->id );
$result = $payment_method->process_payment( $order_id );
if ( $result['result'] === 'success' ) {
$redirect = $result['redirect'];
}
}
} elseif ( in_array( $result->status, array( 'succeeded', 'requires_capture' ) ) ) {
if ( ! $payment_method->has_order_lock( $order ) ) {
$payment_method->set_order_lock( $order );
$result = $payment_method->process_payment( $order_id );
if ( $result['result'] === 'success' ) {
$redirect = $result['redirect'];
}
}
}
wp_safe_redirect( $redirect );
exit();
}
}
public static function maybe_restore_cart() {
global $wp;
if ( isset( $wp->query_vars['order-received'] ) && isset( $_GET['wc_stripe_product_checkout'] ) ) {
add_action( 'woocommerce_cart_emptied', 'wc_stripe_restore_cart_after_product_checkout' );
}
}
private static function process_voucher_redirect() {
$payment_method = wc_clean( $_GET['_stripe_voucher_payment'] );
$payment_method = WC()->payment_gateways()->payment_gateways()[ $payment_method ];
$order = wc_get_order( absint( wc_clean( $_GET['order-id'] ) ) );
$order_key = isset( $_GET['order-key'] ) ? wc_clean( wp_unslash( $_GET['order-key'] ) ) : '';
if ( $order && hash_equals( $order->get_order_key(), $order_key ) ) {
if ( method_exists( $payment_method, 'process_voucher_order_status' ) ) {
$payment_method->process_voucher_order_status( $order );
wp_safe_redirect( $payment_method->get_return_url( $order ) );
exit();
}
}
}
}
WC_Stripe_Redirect_Handler::init();