| @@ 112-157 (lines=46) @@ | ||
| 109 | // Make the request. |
|
| 110 | $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) ); |
|
| 111 | ||
| 112 | if ( ! empty( $response->error ) ) { |
|
| 113 | // If it is an API error such connection or server, let's retry. |
|
| 114 | if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) { |
|
| 115 | if ( $retry ) { |
|
| 116 | sleep( 5 ); |
|
| 117 | return $this->process_redirect_payment( $order_id, false ); |
|
| 118 | } else { |
|
| 119 | $message = 'API connection error and retries exhausted.'; |
|
| 120 | $order->add_order_note( $message ); |
|
| 121 | throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
| 122 | } |
|
| 123 | } |
|
| 124 | ||
| 125 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
|
| 126 | if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) { |
|
| 127 | if ( WC_Stripe_Helper::is_pre_30() ) { |
|
| 128 | delete_user_meta( $order->customer_user, '_stripe_customer_id' ); |
|
| 129 | delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
| 130 | } else { |
|
| 131 | delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' ); |
|
| 132 | $order->delete_meta_data( '_stripe_customer_id' ); |
|
| 133 | $order->save(); |
|
| 134 | } |
|
| 135 | ||
| 136 | return $this->process_redirect_payment( $order_id, false ); |
|
| 137 | ||
| 138 | } elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) { |
|
| 139 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
|
| 140 | ||
| 141 | $wc_token = WC_Payment_Tokens::get( $source_object->token_id ); |
|
| 142 | $wc_token->delete(); |
|
| 143 | $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
|
| 144 | $order->add_order_note( $message ); |
|
| 145 | throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
| 146 | } |
|
| 147 | ||
| 148 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
|
| 149 | ||
| 150 | if ( 'card_error' === $response->error->type ) { |
|
| 151 | $message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
| 152 | } else { |
|
| 153 | $message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
| 154 | } |
|
| 155 | ||
| 156 | throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
| 157 | } |
|
| 158 | ||
| 159 | do_action( 'wc_gateway_stripe_process_redirect_payment', $response, $order ); |
|
| 160 | ||
| @@ 150-196 (lines=47) @@ | ||
| 147 | // Make the request. |
|
| 148 | $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) ); |
|
| 149 | ||
| 150 | if ( ! empty( $response->error ) ) { |
|
| 151 | // If it is an API error such connection or server, let's retry. |
|
| 152 | if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) { |
|
| 153 | if ( $retry ) { |
|
| 154 | sleep( 5 ); |
|
| 155 | return $this->process_payment( $order_id, false ); |
|
| 156 | } else { |
|
| 157 | $localized_message = 'API connection error and retries exhausted.'; |
|
| 158 | $order->add_order_note( $localized_message ); |
|
| 159 | throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
| 160 | } |
|
| 161 | } |
|
| 162 | ||
| 163 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
|
| 164 | if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) { |
|
| 165 | if ( WC_Stripe_Helper::is_pre_30() ) { |
|
| 166 | delete_user_meta( $order->customer_user, '_stripe_customer_id' ); |
|
| 167 | delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
| 168 | } else { |
|
| 169 | delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' ); |
|
| 170 | $order->delete_meta_data( '_stripe_customer_id' ); |
|
| 171 | $order->save(); |
|
| 172 | } |
|
| 173 | ||
| 174 | return $this->process_payment( $order_id, false ); |
|
| 175 | ||
| 176 | } elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) { |
|
| 177 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
|
| 178 | $wc_token = WC_Payment_Tokens::get( $source_object->token_id ); |
|
| 179 | $wc_token->delete(); |
|
| 180 | $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
|
| 181 | $order->add_order_note( $message ); |
|
| 182 | throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
| 183 | } |
|
| 184 | ||
| 185 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
|
| 186 | ||
| 187 | if ( 'card_error' === $response->error->type ) { |
|
| 188 | $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
| 189 | } else { |
|
| 190 | $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
| 191 | } |
|
| 192 | ||
| 193 | $order->add_order_note( $localized_message ); |
|
| 194 | ||
| 195 | throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
| 196 | } |
|
| 197 | ||
| 198 | do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order ); |
|
| 199 | ||