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.
- php
$gateway = new Braintree\Gateway([
"environment" => "sandbox",
"merchantId" => Env::get("BRAINTREE_MERCHANT_ID"),
"publicKey" => Env::get("BRAINTREE_PUBLIC_KEY"),
"privateKey" => Env::get("BRAINTREE_PRIVATE_KEY",
)]);
// domain is required for Fastlane to work
$clientToken = $gateway->clientToken()->generate([
'domain' => ['example.com','example2.com']
]);
- 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 |
- php
<?php
function getGateway() {
return new Braintree\Gateway([
"environment" => "sandbox",
"merchantId" => Env::get("BRAINTREE_MERCHANT_ID"),
"publicKey" => Env::get("BRAINTREE_PUBLIC_KEY"),
"privateKey" => Env::get("BRAINTREE_PRIVATE_KEY",
]);
}
function createTransaction(Request $request) {
$data = $request->toArray();
$gateway = getGateway();
$result = $gateway->transaction()->sale([
"amount" => "10.00",
"paymentMethodNonce" => $data["paymentToken"]["id"],
"deviceData" => $data["deviceData"],
"options" => [
"submitForSettlement" => true,
],
]);
return new JsonResponse(["result" => $result]);
}
- Graphql
mutation ($input: ChargeCreditCardInput!) {
chargeCreditCard(input: $input) {
transaction {
id
legacyId
createdAt
amount {
value
currencyCode
}
status
}
}
}
- Graphql
[
"input" => [
"paymentMethodId" => $data["paymentToken"]["id"] ?? null,
"transaction" => [
"amount" => "1.00",
"riskData" => [
"deviceData" => $data["deviceData"],
],
"shipping" => [
"shippingAddress" => $data["shippingAddress"] ?? null,
"shippingMethod" => "GROUND",
],
"customerDetails" => [
"email" => $data["email"],
],
"vaultPaymentMethodAfterTransacting" => [
"when" => "ON_SUCCESSFUL_TRANSACTION",
],
],
"options" => [
"billingAddress" =>
$data["paymentToken"]["paymentSource"]["card"][
"billingAddress"
] ?? [],
],
],
]