Braintree plugin for Medusa
This plugin integrates Braintree as a payment provider for your Medusa store. It allows you to process payments, handle 3D Secure authentication, and manage payment methods seamlessly.
npm install @lambdacurry/medusa-payment-braintree
Install the plugin in your Medusa project:
npm install @lambdacurry/medusa-payment-braintree
Set the following environment variables in your file:
BRAINTREE_PUBLIC_KEY=<your_public_key>BRAINTREE_MERCHANT_ID=<your_merchant_id>BRAINTREE_PRIVATE_KEY=<your_private_key>BRAINTREE_WEBHOOK_SECRET=<your_webhook_secret>BRAINTREE_ENVIRONMENT=sandbox|development|production|qaBRAINTREE_ENABLE_3D_SECURE=true|false
Add the following configuration to the section of your or file:
dependencies:[Modules.CACHE]{resolve: '@lambdacurry/medusa-payment-braintree/providers/payment-braintree',id: 'braintree',options: {environment: process.env.BRAINTREE_ENVIRONMENT || (process.env.NODE_ENV !== 'production' ? 'sandbox' : 'production'),defaultCurrencyCode: "USD",merchantId: process.env.BRAINTREE_MERCHANT_ID,publicKey: process.env.BRAINTREE_PUBLIC_KEY,privateKey: process.env.BRAINTREE_PRIVATE_KEY,webhookSecret: process.env.BRAINTREE_WEBHOOK_SECRET,enable3DSecure: process.env.BRAINTREE_ENABLE_3D_SECURE === 'true',savePaymentMethod: true, // Save payment methods for future useautoCapture: true, // Automatically capture payments}}
Note:
- : If set to , payments are captured automatically after authorization.
- : If set to , customer payment methods are saved for future use.
- : If set to , the imported payment provider will gracefully handle refund attempts on transactions that have already been refunded in Braintree. Instead of throwing an error, it will log a warning and record the refund locally only. This is useful when orders are imported and later refunded directly in Braintree.
If you enable 3D Secure (), you may need to make additional changes on your storefront to support 3D Secure flows. Refer to the Braintree 3D Secure documentation for more details.
To handle payment updates from Braintree, you need to configure webhooks:
For more information, see the Braintree Webhooks documentation.
To use custom fields, create them in your Braintree dashboard (API names must be lowercase). You will provide their values when calling via .
Navigate to:
→ →
Add each custom field:
| Field Name (example) | API Name (example) | Description | Options |
|---|---|---|---|
| Medusa Payment Session Id | Medusa Session Id | Store and Pass back | |
| Cart Id | Cart Id | Store and Pass back | |
| Customer Id | Customer Id | Store and Pass back |
Note
- Braintree only accepts values for custom fields that exist in your dashboard and match the field API names (lowercase).
- If you rely on webhooks that read , include that key in when you call .
Custom fields are forwarded to Braintree when the provider creates the transaction during . Provide them on the as .
Example:
// Example shape; Medusa calls the provider under the hood.await braintreeProvider.authorizePayment({data: {amount: 10, // standard currency units; converted to "10.00"currency_code: 'USD',payment_method_nonce: '<client-side-nonce>',},context: {idempotency_key: 'sess_123',customer: { id: 'cust_123', email: 'c@example.com' },custom_fields: {medusa_payment_session_id: 'sess_123',cart_id: 'cart_123',customer_id: 'cust_123',},// Optional: shipping_address, billing_address, totals, items},});
Requirements and tips:
Implementation detail: the provider passes directly to Braintree’s in the sale request ().
This plugin is licensed under the MIT License.
For more information, visit the Braintree Documentation.