• 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 ) );
    	}
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Payment Plugins

    (@mrclayton)

    Hi @davefx

    That error message is coming from WooCommerce and is an internal validation that occurs when the $token->save() function is called. It’s indicating that the paymentMethodToken is empty which it should not be.

    What payment method are these transactions for?

    Kind Regards

    Thread Starter David Marín Carre?o

    (@davefx)

    We’ve seen this problem for both Credit Cards and Paypal, but in the last weeks it’s much more frequent with Paypal.

    Our main concern is the fact that a problem when saving the token causes the order to be moved to failed status, even after the payment has correctly been charged. Having problems saving the token in the vault should never make the already-charged order to be moved to failed status.

    Having problems retrieving the token is, at this moment, secondary for us.

    Plugin Author Payment Plugins

    (@mrclayton)

    You can implement your custom try catch for now and we’ll add something in the next release that prevents the empty token from causing the order to be set to failed.

    I’m not able to re-create that error so if you have custom code on your site I’d recommend taking a look to make sure it’s not related to that.

    Is there anything special about the products that are being purchased when you encounter this error?

    Do you have any other plugins enabled that are using the Braintree PHP SDK?

    Thread Starter David Marín Carre?o

    (@davefx)

    Thanks for your answer.

    I’m adding this try/catch block to our site, and I’ll keep working to determine why this is happening. Unfortunately, the problem doesn’t seem to be deterministic. In all our tests, and in the general case, everything works correctly. There’s something that causes this in 3 or 4 orders per week.

    I’ll keep you informed if I get something clear from my tests.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Order wrongly cancelled due to Exception after correct payment’ is closed to new replies.