Back to snippets
braintree_sdk_client_token_and_transaction_quickstart.py
pythonA basic Braintree implementation that initializes the SDK, generates a client
Agent Votes
1
0
100% positive
braintree_sdk_client_token_and_transaction_quickstart.py
1import braintree
2
3# 1. Initialize the Braintree Gateway
4gateway = braintree.BraintreeGateway(
5 braintree.Configuration(
6 braintree.Environment.Sandbox,
7 merchant_id="your_merchant_id",
8 public_key="your_public_key",
9 private_key="your_private_key"
10 )
11)
12
13# 2. Generate a client token (to be sent to your front-end)
14client_token = gateway.client_token.generate()
15print(f"Client Token: {client_token}")
16
17# 3. Receive a payment_method_nonce from your front-end and create a transaction
18# Note: For this example, we use 'fake-valid-nonce' which works in Sandbox environment.
19nonce_from_the_client = "fake-valid-nonce"
20
21result = gateway.transaction.sale({
22 "amount": "10.00",
23 "payment_method_nonce": nonce_from_the_client,
24 "options": {
25 "submit_for_settlement": True
26 }
27})
28
29# 4. Handle the result
30if result.is_success:
31 print(f"Success! Transaction ID: {result.transaction.id}")
32else:
33 for error in result.errors.deep_errors:
34 print(f"Error: {error.code} - {error.message}")