Server-side
Step 1: Generate client token
To get started,
generate a client token. If you use Braintree's GraphQL API on your server, refer to the
GraphQL Documentation.
Using Braintree SDK
You must include your root domain in the client token request. Omitting the root domain will cause Fastlane to malfunction and will prevent it from working entirely.
- python
import braintree
braintreeInstance = braintree.BraintreeGateway(
braintree.Configuration(
environment=braintree.Environment.Sandbox,
merchant_id="<BRAINTREE_MERCHANT_ID>",
public_key="<BRAINTREE_PUBLIC_KEY>",
private_key="<BRAINTREE_PRIVATE_KEY>",
)
)
client_token = braintreeInstance.client_token.generate({
"domains": ['example.com', 'example2.com']
})
# return the clientToken to your front-end
return jsonify(clientToken=client_token)
- Graphql
mutation ($input: CreateClientTokenInput) {
createClientToken(input: $input) {
clientToken
}
}
- Graphql
{
"input": {
"clientToken": {
"domains": [
"example.com"
]
}
}
}
- Restrictions on Domain Names:
- No Subdomains: Do not specify subdomains (for example, "sub.example.com").
- No Wildcards: Do not use wildcard characters (for example, "*.example.com").
- No Protocols: Do not include HTTP or HTTPS protocols in the domain name (for example, "https://example.com/")
- Error Handling: If you specify subdomains, wildcards, or protocols, the system will generate an error.
Step 2: Update Fastlane with Consumer's shipping address
After generating a Fastlane token, if you're processing a payment, make sure to include the customer's shipping address in the request.This ensures the address is saved to their Fastlane profile for future use.
Field name | Description | Link |
---|---|---|
shipping | The shipping address collected for a customer's order. | Link |
- python
try:
deviceData = transactionData.get('deviceData')
email = transactionData.get('email')
name = transactionData.get('name')
billingAddress = transactionData['paymentToken']['paymentSource']['card']['billingAddress']
shippingAddress = transactionData['shippingAddress']
# Convert information to snake_case for Braintree python gateway.
saleRequest = {
"amount": "10.00",
"payment_method_nonce": transactionData['paymentToken']['id'],
"device_data": deviceData,
"customer": {
"email": email,
**({"first_name": name.get('firstName'), "last_name": name.get('lastName')} if name else {})
},
"billing": {
"street_address": billingAddress.get('streetAddress'),
"extended_address": billingAddress.get('extendedAddress'),
"locality": billingAddress.get('locality'),
"region": billingAddress.get('region'),
"postal_code": billingAddress.get('postalCode'),
"country_code_alpha2": billingAddress.get('countryCodeAlpha2')
},
"shipping": {
"street_address": shippingAddress.get('streetAddress'),
"extended_address": shippingAddress.get('extendedAddress'),
"locality": shippingAddress.get('locality'),
"region": shippingAddress.get('region'),
"postal_code": shippingAddress.get('postalCode'),
"country_code_alpha2": shippingAddress.get('countryCodeAlpha2'),
"shipping_method": "ground"
} if shippingAddress else None
}
result = braintree_gateway().transaction.sale(saleRequest)
if not result.is_success:
raise Exception(result.message)
return jsonify(result={
"success": True,
"transaction": {
"id": <a href="http://result.transaction.id">result.transaction.id</a>,
"status": result.transaction.status,
"type": result.transaction.type,
},
})
except Exception as error:
print(error)
return make_response(jsonify(error=str(error)), 500)
- Graphql
mutation ($input: ChargeCreditCardInput!) {
chargeCreditCard(input: $input) {
transaction {
id
legacyId
createdAt
amount {
value
currencyCode
}
status
}
}
}
- Graphql
{
"input": {
"paymentMethodId": payment_token["id"],
"transaction": {
"amount": "1.00",
"riskData": {
"deviceData": transactionData["deviceData"]
},
"shipping": {
"shippingAddress": (
{
"streetAddress": shippingAddress.get("streetAddress"),
"extendedAddress": shippingAddress.get("extendedAddress"),
"locality": shippingAddress.get("locality"),
"region": shippingAddress.get("region"),
"postalCode": shippingAddress.get("postalCode"),
"countryCodeAlpha2": shippingAddress.get("countryCodeAlpha2")
}
if shippingAddress
else {}
),
"shippingMethod": "GROUND",
},
"customerDetails": {
"email": transactionData["email"],
},
"vaultPaymentMethodAfterTransacting": {
"when": "ON_SUCCESSFUL_TRANSACTION"
},
},
"options": {
"billingAddress": payment_token["paymentSource"]["card"][
"billingAddress"
]
},
}
}