Поддержка платежей и 3D Secure через Braintree
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.
Install the plugin in your Medusa project:
Set the following environment variables in your file:
In Braintree sandbox, transactions often remain in or status until they are settled. The provider routes refunds differently by status:
To test the refund path locally without waiting for settlement, set and in provider options (optionally via env):
When both are set, calls Braintree's sandbox on the transaction, re-fetches it, then proceeds with . If is but the provider environment is not , the settle step is skipped and a warning is logged.
Add the following configuration to the section of your or file:
Enable plugin debug logs in :
Then in :
BRAINTREE_LOGGING=trueWhat enables:
Logs are written through Medusa's and appear in the Medusa server output. Ensure Medusa's is not set to if you want to see them (the default level includes messages).
Earlier README examples used (auto-enabled in development). Current examples use explicit / . If you relied on implicit dev logging, set or pass in provider options.
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.
Note:
- Sequential partial refunds keep the original sale on . Credit/void results are recorded only on . If you were reading the latest credit from after a refund, use instead.
- Refund rejection errors now include the Braintree transaction status in the message.
Note:
- : Late additional requirement so future partial order refunds and order edits can be supported. When , only / may be refunded; / fail with (“cannot be refunded right now because it's in status …”); other statuses fail with (“cannot be refunded because it's in status …”). may still void.
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:
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.
npm install @lambdacurry/medusa-payment-braintreenpm install @lambdacurry/medusa-payment-braintree1BRAINTREE_PUBLIC_KEY=<your_public_key>2BRAINTREE_MERCHANT_ID=<your_merchant_id>3BRAINTREE_PRIVATE_KEY=<your_private_key>4BRAINTREE_WEBHOOK_SECRET=<your_webhook_secret>5BRAINTREE_ENVIRONMENT=sandbox|development|production|qa6BRAINTREE_ENABLE_3D_SECURE=true|false7BRAINTREE_LOGGING=true|false8TEST_FORCE_SETTLED=true|false1BRAINTREE_ENVIRONMENT=sandbox2TEST_FORCE_SETTLED=true1options: {2 environment: process.env.BRAINTREE_ENVIRONMENT || 'sandbox',3 testForceSettled: process.env.TEST_FORCE_SETTLED === 'true',4 // ...5}1dependencies:[Modules.CACHE]2{3 resolve: '@lambdacurry/medusa-payment-braintree/providers/payment-braintree',4 id: 'braintree',5 options: {6 environment: process.env.BRAINTREE_ENVIRONMENT || (process.env.NODE_ENV !== 'production' ? 'sandbox' : 'production'),7 defaultCurrencyCode: "USD",8 merchantId: process.env.BRAINTREE_MERCHANT_ID,9 publicKey: process.env.BRAINTREE_PUBLIC_KEY,10 privateKey: process.env.BRAINTREE_PRIVATE_KEY,11 webhookSecret: process.env.BRAINTREE_WEBHOOK_SECRET,12 enable3DSecure: process.env.BRAINTREE_ENABLE_3D_SECURE === 'true',13 savePaymentMethod: true, // Save payment methods for future use14 autoCapture: true, // Automatically capture payments15 allowRefundOnRefunded: false,16 disableVoidTransactions: false,17 logging: process.env.BRAINTREE_LOGGING === 'true', // Enable plugin debug logs18 testForceSettled: process.env.TEST_FORCE_SETTLED === 'true', // Sandbox: settle before refund19 }20}1options: {2 // ...3 logging: process.env.BRAINTREE_LOGGING === 'true',4}1// Example shape; Medusa calls the provider under the hood.2await braintreeProvider.authorizePayment({3 data: {4 amount: 10, // standard currency units; converted to "10.00"5 currency_code: 'USD',6 payment_method_nonce: '<client-side-nonce>',7 },8 context: {9 idempotency_key: 'sess_123',10 customer: { id: 'cust_123', email: 'c@example.com' },11 custom_fields: {12 medusa_payment_session_id: 'sess_123',13 cart_id: 'cart_123',14 customer_id: 'cust_123',15 },16 // Optional: shipping_address, billing_address, totals, items17 },18});