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.
- Ruby
require 'braintree'
@braintree_gateway ||= Braintree::Gateway.new(
environment: :sandbox,
merchant_id: ENV['BRAINTREE_MERCHANT_ID'],
public_key: ENV['BRAINTREE_PUBLIC_KEY'],
private_key: ENV['BRAINTREE_PRIVATE_KEY']
)
client_token = braintree_gateway.client_token.generate
- 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 |
- Ruby
require 'braintree'
def get_braintree_gateway
@braintree_gateway ||= Braintree::Gateway.new(
environment: :sandbox,
merchant_id: ENV['BRAINTREE_MERCHANT_ID'],
public_key: ENV['BRAINTREE_PUBLIC_KEY'],
private_key: ENV['BRAINTREE_PRIVATE_KEY']
)
end
def do_POST(request, response)
data = JSON.parse(request.body)
email = data['email']
name = data['name']
shipping_address = data['shippingAddress']
payment_token = data['paymentToken']
device_data = data['deviceData']
result = get_braintree_gateway.transaction.sale(
amount: '10.00',
payment_method_nonce: payment_token['id'],
device_data: device_data,
options: {
submit_for_settlement: true
}
)
response.status = 201
response.content_type = 'application/json'
response.body = {
result: {
success: result.success?,
transaction: { id: result.transaction.id }
}
}.to_json
end
Using Braintree GraphQL
- Graphql
mutation ($input: ChargeCreditCardInput!) {
chargeCreditCard(input: $input) {
transaction {
id
status
}
}
}
- Graphql
mutation ($input: ChargeCreditCardInput!) {
chargeCreditCard(input: $input) {
transaction {
id
status
}
}
}
Next step: Test your integration