Set Up Your Server
Install and configure
The SSL certificates for all Braintree SDKs are set to expire by June 31, 2025. This will impact existing versions of the SDK in published versions of your app. To reduce the impact, upgrade the PHP SDK to version 6.21.0+ for the new SSL certificates.
If you do not decommission your app versions that include the older SDK versions or force upgrade your app with the updated certificates by the expiration date, 100% of your customer traffic will fail.
The Braintree PHP SDK requires PHP version 7.3.0 or higher and the PHP cURL extension.
Get started with our PHP library by hitting the download link below.
Download PHP library
- Version: 6.24.0
- SHA256: cf7df1761b0167229b05e7515e28151baa214c1835d60b75bcbe3ea40080ab54
Or use composer
Composer is a package manager for PHP. In the composer.json
file in your project add:
- JSON
{
"require" : {
"braintree/braintree_php" : "6.24.0"
}
}
And then run:
- bash
php composer.phar install
In your code, configure the environment and API credentials:
- PHP
$gateway = new Braintree\\Gateway([
'environment' => 'sandbox',
'merchantId' => 'use_your_merchant_id',
'publicKey' => 'use_your_public_key',
'privateKey' => 'use_your_private_key'
]);
Generate a client token
customerId
when generating the client token lets returning customers select from previously used payment method options, improving user experience over multiple checkouts.- PHP
// pass $clientToken to your front-end
$clientToken = $gateway->clientToken()->generate([
"customerId" => $aCustomerId
]);
If the customer can't be found, it will throw an InvalidArgumentException
.
Set Up Your Client covers the client side of the exchange.
Send a client token to your client
Here is an example of how your server would generate and expose a client token:
- PHP
echo($clientToken = $gateway->clientToken()->generate());
How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, while in mobile apps the client token must be requested. These methods are discussed in the client token setup section.
Receive a payment method nonce from your client
Once your client successfully obtains a customer payment method, it receives a payment_method_nonce
representing customer payment authorization, which it then sends to your server.
Your server implementation is then responsible for receiving the payment_method_nonce
and using it appropriately.
- PHP
$nonceFromTheClient = $_POST["payment_method_nonce"]
/* Use payment method nonce here */
Create a transaction
- PHP
$result = $gateway->transaction()->sale([
'amount' => '10.00',
'paymentMethodNonce' => $nonceFromTheClient,
'deviceData' => $deviceDataFromTheClient,
'options' => [
'submitForSettlement' => True
]
]);
The sale call returns a Transaction Result Object which contains the transaction and information about the request.
Test your integration
Always develop and test your code against your sandbox account before processing live transactions against a production account.
Transition to production
At this point, you should be able to accept a payment method nonce and create a transaction in our sandbox. When you're ready to start charging real money, transition over to our production environment. We'll explain that process next.