This is an API module to enable using the Braintree API in your custom module. Setup:

  1. Install using composer require drupal/braintree_api in order to pick up the Braintree SDK dependency.
  2. Follow the links given on the settings form at /admin/config/services/braintree_api to find your API credentials.
  3. Enter them in the settings form, and save the form.
  4. Confirm that the "Sandbox" environment is selected during initial development
  5. Click the "Test Braintree Connection" to confirm things are working.
  6. Inject the braintree_api.braintree_api service into your custom class.
  7. Get the gateway instance object with $gateway = $this->braintreeApiService->getGateway() assuming you assigned the braintree_api.braintree_api service to the $braintreeApiService property of your custom class.
  8. Use the Braintree SDK! Example
    $gateway->customer()->create([
          'firstName' => $user->getAccountName(),
          'email' => $user->getEmail(),
          'paymentMethodNonce' => $nonce,
          'creditCard' => [
            'options' => [
              'verifyCard' => TRUE,
            ],
          ],
        ]);

When entering your private key using the Key module, if you're using a file provider to store your private key be sure to select the option to strip away line endings, otherwise Webhooks received from Braintree will not validate.

Webhooks

If you are using webhooks, add a webhook in the Braintree console by navigating to Settings -> Webhooks. Add a new webhook pointing to https://[your-domain]/braintree/webhooks, and select the events for which Braintree should send webhooks. During local development you may wish to tunnel Braintree webhooks to your local machine using ngrok, by entering a webhook URL of the form http://[subdomain-provided-by-ngrok].ngrok.io/braintree/webhooks?XDEBUG_SESSION_START=PHPSTORM into the sandbox Braintree console, where the query parameter starts xdebug with PHPStorm if you have that configured on your local system.
Subscribe to the BraintreeApiWebhookEvent with an event subscriber. For an example, see the BraintreeApiWebhookEvent event subscriber in Braintree Cashier.

Braintree API is inspired by the Stripe API module, but uses the Braintree payment gateway instead of Stripe.

Settings.php

Consider adding $config['braintree_api.settings']['environment'] = 'sandbox'; to your local settings.local.php to ensure you're always using the sandbox environment locally.

Dependencies

See the Braintree Cashier module for an example of how to use Braintree API.

Supporting organizations: 
Sponsored development

Project information

Releases