Order wrongly cancelled due to Exception after correct payment
-
I’ve debugged one problem we are experiencing in our site.
Eventually, we have several transactions per week that get cancelled even after the payment has been correctly charged and accounted for in the Braintree side.
The order gets the following message: “Error processing payment. Reason: Invalid or missing payment token fields.”.
Trying to debug the problem, I’ve observed that the problem happens if there’s an Exception in the code block
if ( isset( $args['options']['storeInVaultOnSuccess'] ) && $args['options']['storeInVaultOnSuccess'] == true ) { $token = $this->get_payment_token( $this->get_payment_method_from_transaction( $response->transaction ) ); $token->set_user_id( $order->get_customer_id() ); $token->save(); // set token in case downstream processes need access to it. $this->payment_method_token = $token->get_token(); WC_Payment_Tokens::set_users_default( $order->get_customer_id(), $token->get_id() ); }
in the process_payment() method at abstract-class-wc-braintree-gateway.php.
IMHO, an exception in this block shouldn’t cause the whole payment transaction to fail, as we already have charged the user and saved the payment data.
I propose embracing this block into its own try/catch block area, this way:
if ( isset( $args['options']['storeInVaultOnSuccess'] ) && $args['options']['storeInVaultOnSuccess'] == true ) { try { $token = $this->get_payment_token( $this->get_payment_method_from_transaction( $response->transaction ) ); $token->set_user_id( $order->get_customer_id() ); $token->save(); // set token in case downstream processes need access to it. $this->payment_method_token = $token->get_token(); WC_Payment_Tokens::set_users_default( $order->get_customer_id(), $token->get_id() ); } catch ( Exception $e ) { if ( $e instanceof \Braintree\Exception ) { wc_braintree_log_error( sprintf( __( 'Error saving token into vault for order %1$s. Exception: %2$s. Transaction args: %3$s', 'woo-payment-gateway' ), $order->get_id(), get_class( $e ), print_r( $args, true ) ) ); $msg = wc_braintree_errors_from_object( $e ); } else { $msg = $e->getMessage(); } $order->add_order_note( sprintf( __( 'Error saving Braintree token into vault. Reason: %1$s', 'woo-payment-gateway' ), $msg ) ); } }
- The topic ‘Order wrongly cancelled due to Exception after correct payment’ is closed to new replies.