Braintree provides a Python module that simplifies dealing with its API. The source code is located at https://github.com/braintree/braintree_python. We are going to integrate the payment gateway into our project using the braintree module.
Install the braintree module from the shell using the following command:
pip install braintree==3.45.0
Add the following settings to the settings.py file of your project:
# Braintree settings
BRAINTREE_MERCHANT_ID = 'XXX' # Merchant ID
BRAINTREE_PUBLIC_KEY = 'XXX' # Public Key
BRAINTREE_PRIVATE_KEY = 'XXX' # Private key
from braintree import Configuration, Environment
Configuration.configure(
Environment.Sandbox,
BRAINTREE_MERCHANT_ID,
BRAINTREE_PUBLIC_KEY,
BRAINTREE_PRIVATE_KEY
)
Replace BRAINTREE_MERCHANT_ID, BRAINTREE_PUBLIC_KEY, and BRAINTREE_PRIVATE_KEY values with the ones of your account.
Note that we use Environment.Sandbox for integrating the sandbox. Once you go live and create a real account, you will need to change this to Environment.Production. Braintree will provide you with a new merchant ID and private/public keys for the production environment.
Let's integrate the payment gateway into the checkout process.