---
title: "Braintree"
space: "BWH Payments"
url: "https://docs.bwh.tech/bwh-payments/gateways/braintree"
updated: "2026-11-07"
---

Take card payments worldwide through [Braintree](https://www.braintreepayments.com) with 3-D Secure. Braintree has no hosted payment page, so BWH Payments serves its own checkout page and shows Braintree's Drop-in card form on it. The card details go from the shopper's browser to Braintree and never reach your site.

Braintree works differently from a hosted-page gateway in three ways:

- **The checkout page is yours.** `order_url` points at a BWH Payments page, not at Braintree.
- **Nothing exists at Braintree until the shopper pays.** The session id is a random token that BWH Payments makes, and the sale carries it as its `order_id`.
- **Each currency needs its own merchant account.** Braintree settles a currency through the merchant account that holds it.

## Before you start

- A Braintree account. A sandbox account is fine.
- Your **Merchant ID**, **Public Key** and **Private Key** from the Braintree Control Panel. Sandbox and Production have separate credentials.
- A **Merchant Account ID** for each currency you charge in. In the Control Panel, go to the gear icon, then **Business**, then **Merchant Accounts**. It is not the Merchant ID.

## Set up Braintree

**1. Fill in Braintree Gateway Settings**

| Field | Value |
|---|---|
| Mode | `Sandbox` or `Production`. It selects the Braintree environment that the credentials go to. Sandbox credentials don't work in Production. |
| Merchant ID | Your Braintree merchant id |
| Public Key | Your public key |
| Private Key | Your private key |
| Merchant Accounts | One row for each currency: the **Currency** and the **Merchant Account ID**. A currency can appear in one row only. |
| 3-D Secure | On by default. Asks the card issuer to authenticate the shopper. See [3-D Secure](#3-d-secure). |
| Success URL | Your [confirmation page](/bwh-payments/get-started/return-pages). `?reference_id=…` is added to it. Required, unless every payment sets `redirect_to`. |
| Cancelled URL | Your [payment-failed page](/bwh-payments/get-started/return-pages). The **Cancel** link on the checkout page goes here, with `?reference_id=…` added. Required, unless every payment sets `redirect_to`. |
| Failure URL | Optional. A declined card stays on the checkout page so the shopper can try again. BWH Payments stores this URL on the request, so your app has a page to send a shopper whose payment did not complete. |

**2. Create a Payment Gateway Profile** named `Braintree`, set **Gateway Settings** to `Braintree Gateway Settings`, and tick **Enabled**.

When you save the settings with **Enabled** ticked, BWH Payments asks Braintree for a client token for each merchant account. The save fails if the keys are wrong, a merchant account does not exist, or the table is empty.

There's no webhook to set up. BWH Payments settles the payment when the shopper pays, so closing the tab afterwards still places the order.

## How checkout works

1. `create_payment` makes no call to Braintree. It checks that the currency has a merchant account, generates a 32-character token, stores it as `order_ref`, and sets `order_url` to BWH Payments' own `start_checkout` page.
2. You redirect the shopper to `order_url`. The page shows the amount and loads Drop-in. If the request is already Paid, it redirects to the Success URL instead.
3. The shopper enters a card. Drop-in turns it into a nonce, and the page posts the nonce to the `pay` endpoint.
4. `pay` charges the request's own amount once, with the merchant account for its currency. The amount never comes from the browser. It then marks the request Paid from the sale itself and returns the Success URL.
5. If Braintree declines the card, the page shows the reason and the shopper can try another card on the same request.

:::caution
`order_url` works for anyone who has it, as it does for every gateway. The page holds the shopper's email address, so share the link only with the shopper. It's never cached. It opens only while the payment is Pending and the Braintree profile is enabled. `start_checkout` allows 60 requests a minute and `pay` allows 20, for each IP address.
:::

### One charge for each request

`pay` takes a database lock named after the session, so two attempts at the same time can't both charge. It never charges a request that has a `gateway_transaction_ref`, or one that Braintree already shows a paid sale for. Braintree's search can trail a fresh sale, so BWH Payments keeps the sale's id as `gateway_transaction_ref` the moment it is made.

:::note
The lock is a MariaDB `GET_LOCK`. Braintree checkout does not run on Postgres or SQLite.
:::

### 3-D Secure

With **3-D Secure** on, Drop-in asks the issuer to authenticate the card, and the page refuses a card whose liability did not shift. The sale is also sent with `three_d_secure.required`. `pay` is open to guests, so someone could post a nonce to it without using the page. Braintree refuses that nonce.

## How it behaves

| Braintree sale status | Payment status |
|---|---|
| `submitted_for_settlement`, `settling`, `settled`, `settlement_pending`, `settlement_confirmed` | Paid |
| `settlement_declined`, `settlement_failed` | Not Paid |
| `voided`, `authorization_expired` | Cancelled |
| Anything else, including a declined sale | Pending |

BWH Payments searches the sales that carry the request's token. If any sale is in a paid status, the request is Paid. Otherwise the latest sale decides. A declined card stays Pending, so the shopper can try another one.

- **Amounts** are sent as a decimal string in major units, for example `"49.99"`. A currency with no decimals gets no decimal point.
- **Sales** are submitted for settlement at once. They are never authorised only.
- **Cancelling** an open checkout (`release_if_unpaid()`) works. It returns `False` when a sale is already paid, authorising or authorised. While it works, it holds the session lock until your change is saved, so `pay` can't charge in between.
- **Refunds** go against the sale, whose id is stored as `gateway_transaction_ref`:
  - A `settling` or `settled` sale gets a credit. Partial refunds are supported. A credit is `Processed` when it is `submitted_for_settlement`, `settling` or `settled`, and `Pending` when it is `settlement_pending`.
  - A sale that is `authorized` or `submitted_for_settlement` can only be voided. BWH Payments refuses a partial refund then, and a void is a full refund reported as `Processed`. Retry a partial refund after settlement, usually the next day.
  - `sync_refunds` can read a refund back from Braintree.
- **Refunds raised in the Braintree Control Panel** are not recorded. Braintree can't list them here, and BWH Payments ignores its webhooks.
- **Webhooks** are checked with the Braintree SDK, using your keys, and then ignored. A delivery with a bad signature gets a 400.

## Good to know

- Gateway settings are a single document, so a site can hold **one** Braintree account.
- Braintree is offered at checkout only for the currencies in the **Merchant Accounts** table. `create_payment` refuses any other currency.
- The checkout link stays payable until the sale is made, so `get_gateways` reports Braintree with `reusable_checkout` set. You can send a shopper who left the page back to `order_url`.

## Test mode

Set **Mode** to `Sandbox`, and use the sandbox keys and merchant accounts. Pay with one of Braintree's sandbox test cards. With **3-D Secure** on, use a test card that supports 3-D Secure authentication.

## Next steps

- [Return pages](/bwh-payments/get-started/return-pages): the confirmation and payment-failed pages
- [Webhooks and security](/bwh-payments/build/webhooks-and-security): how BWH Payments checks a delivery
