Set Up Your Server
Install and configure
The SSL certificates for all Braintree SDKs are set to expire by June 31, 2025. This will impact existing versions of the SDK in published versions of your app. To reduce the impact, upgrade the Python SDK to version 4.31.0+ for the new SSL certificates.
If you do not decommission your app versions that include the older SDK versions or force upgrade your app with the updated certificates by the expiration date, 100% of your customer traffic will fail.
The Braintree Python SDK is tested against Python versions 3.5.3 and 3.8.0.
Install with pip:
pip install braintree
Manual Installation:
# 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://files.pythonhosted.org/packages/67/df/f408e3b3088fcdeff42ce0fb00a6bda37912e5c7726e47e01dde310b11e7/braintree-4.34.0.tar.gz
tar zxf braintree-4.34.0.tar.gz
cd braintree-4.34.0
python setup.py install
In your code, configure the environment and API credentials:
- Python
import braintree
gateway = braintree.BraintreeGateway(
braintree.Configuration(
braintree.Environment.Sandbox,
merchant_id="use_your_merchant_id",
public_key="use_your_public_key",
private_key="use_your_private_key"
)
)
See the Braintree Python Version Changelog.
Generate a client token
customer_id
when generating the client token lets returning customers select from previously used payment method options, improving user experience over multiple checkouts.- Python
# pass client_token to your front-end
client_token = gateway.client_token.generate({
"customer_id": a_customer_id
})
If the customer can't be found, it will raise a ValueError
.
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
@app.route("/client_token", methods=["GET"])
def client_token():
return gateway.client_token.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
@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
- Python
result = gateway.transaction.sale({
"amount": "10.00",
"payment_method_nonce": nonce_from_the_client,
"device_data": device_data_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
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.