Instant Braintree Transaction Settlement

I’m working on an e-commerce project using WooCommerce and the Braintree payment gateway. One of the areas I’m testing involves issuing partial refunds on orders (e.g., one item out of a larger order is canceled). Part of the credit card transaction process involves submitting an order for settlement, then waiting for that order to settle with the credit card processor. You cannot issue a partial refund until the transaction is settled. This process can take anywhere from 30 minutes to 24 hours, an inconvenient bottleneck to development and testing.

Fortunately for us, there is a route to circumvent this delay. The Braintree API provides a method to immediately settle sandbox transactions.

In the context of WooCommerce, we can take advantage of this method while still using the API connection established by the Braintree plugin. This can be run in the context of a WP-CLI shell session, built into your own custom CLI command, or called from an automated testing suite.

// get the ID of the transaction you want to settle
$transaction_id = 'your-id-here';

// get the instance of the WooCommerce payment gateway for braintree
$gateway = wc_braintree()->get_gateway( 'braintree_credit_card' );

// use the WooCommerce gateway to authenticate with Braintree's SDK
$sdk = new \Braintree\Gateway( [ 'accessToken' => $gateway->get_auth_access_token() ] );

// call the testing API to immediately settle your transaction
$transaction = $sdk->testing()->settle( $transaction_id );

Your transaction’s status should now be “settled”, and your order is ready for issuing partial refunds.

One thought on “Instant Braintree Transaction Settlement”

  1. This is an absolutely nuts limitation, which I’ve just discovered in testing our new Braintree account. We often get customers who want partial refunds before we dispatch because they made a mistake or changed their mind. And with Braintree, we can’t do what they want. We either have to cancel and void, or faff around with dispatching a partial order, and keeping records so the refund and stock level is fixed the following day.

    Did you ever get this into a format that could be used as a fix for a production site? I mean (a) does it work out of sandbox, and (b) can it be triggered by a non-developer?


Comments are closed.