Server side payment token flow Diagram demonstrating the required interaction between the client, our servers, and your server.

Install and configure

availability

Supports Python 2.6, 2.7, 3.3, 3.4, and 3.5.
The final version that supports Python 2.5 is 2.29.1

Install with pip:

Copy
Copied
pip install braintree

Manual Installation:

Copy
Copied
# we require requests version v0.11.0 or higher
wget pypi.python.org/packages/source/r/requests/requests-2.2.1.tar.gz
tar zxf requests-2.2.1.tar.gz
cd requests-2.2.1
python setup.py install

wget https://pypi.python.org/packages/68/39/b0dd12bcc730108b4bbac73a7cece4dd0af12b39c4323eea24a64af43b0f/braintree-3.38.0.tar.gz#md5=190dff2fec24de8eb4328eeb5b0933b4
tar zxf braintree-3.38.0.tar.gz
cd braintree-3.38.0
python setup.py install

In your code, configure the environment and API credentials:

Python
Copy
Copied
import braintree

braintree.Configuration.configure(braintree.Environment.Sandbox,
                                  merchant_id="use_your_merchant_id",
                                  public_key="use_your_public_key",
                                  private_key="use_your_private_key")

Generate a client token

Your server is responsible for generating a client token, which contains all authorization and configuration information your client needs to initialize the client SDK to communicate with Braintree. Including a customer_id when generating the client token lets returning customers select from previously used payment method options, improving user experience over multiple checkouts. Set Up Your Client covers the client side of the exchange.

Send a client token to your client

Here is an example of how your server would generate and expose a client token:

Python
Copy
Copied
@app.route("/client_token", methods=["GET"])
def client_token():
  return braintree.ClientToken.generate()

How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, while in mobile apps the client token must be requested. These methods are discussed in the client token setup section.

Receive a payment method nonce from your client

Once your client successfully obtains a customer payment method, it receives a payment_method_nonce representing customer payment authorization, which it then sends to your server.

Your server implementation is then responsible for receiving the payment_method_nonce and using it appropriately.

Python
Copy
Copied
@app.route("/checkout", methods=["POST"])
def create_purchase():
  nonce_from_the_client = request.form["payment_method_nonce"]
  # Use payment method nonce here...

Create a transaction

You can create a transaction using an amount and the nonce_from_the_client you received in the previous step:

Python
Copy
Copied
result = braintree.Transaction.sale({
    "amount": "10.00",
    "payment_method_nonce": nonce_from_the_client,
    "options": {
      "submit_for_settlement": True
    }
})

The sale call returns a Transaction Result Object which contains the transaction and information about the request.

Test your integration

See our Testing page for values you can use for nonce_from_the_client in your sandbox account. These nonces can be passed as strings through server-side calls to generate payment methods in the desired state. To verify your integration, you can check in the sandbox Control Panel, where transactions will immediately appear on success.

important

Always develop and test your code against your sandbox account before processing live transactions against a production account.

Transition to production

At this point, you should be able to accept a payment method nonce and create a transaction in our sandbox. When you're ready to start charging real money, transition over to our production environment. We'll explain that process next.

Next: Go Live →

Still have questions?

If you can’t find an answer, contact our Support team.