chub-dev 0.1.0 → 0.1.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -0
- package/bin/chub-mcp +2 -0
- package/dist/airtable/docs/database/javascript/DOC.md +1437 -0
- package/dist/airtable/docs/database/python/DOC.md +1735 -0
- package/dist/amplitude/docs/analytics/javascript/DOC.md +1282 -0
- package/dist/amplitude/docs/analytics/python/DOC.md +1199 -0
- package/dist/anthropic/docs/claude-api/javascript/DOC.md +503 -0
- package/dist/anthropic/docs/claude-api/python/DOC.md +389 -0
- package/dist/asana/docs/tasks/DOC.md +1396 -0
- package/dist/assemblyai/docs/transcription/DOC.md +1043 -0
- package/dist/atlassian/docs/confluence/javascript/DOC.md +1347 -0
- package/dist/atlassian/docs/confluence/python/DOC.md +1604 -0
- package/dist/auth0/docs/identity/javascript/DOC.md +968 -0
- package/dist/auth0/docs/identity/python/DOC.md +1199 -0
- package/dist/aws/docs/s3/javascript/DOC.md +1773 -0
- package/dist/aws/docs/s3/python/DOC.md +1807 -0
- package/dist/binance/docs/trading/javascript/DOC.md +1315 -0
- package/dist/binance/docs/trading/python/DOC.md +1454 -0
- package/dist/braintree/docs/gateway/javascript/DOC.md +1278 -0
- package/dist/braintree/docs/gateway/python/DOC.md +1179 -0
- package/dist/chromadb/docs/embeddings-db/javascript/DOC.md +1263 -0
- package/dist/chromadb/docs/embeddings-db/python/DOC.md +1707 -0
- package/dist/clerk/docs/auth/javascript/DOC.md +1220 -0
- package/dist/clerk/docs/auth/python/DOC.md +274 -0
- package/dist/cloudflare/docs/workers/javascript/DOC.md +918 -0
- package/dist/cloudflare/docs/workers/python/DOC.md +994 -0
- package/dist/cockroachdb/docs/distributed-db/DOC.md +1500 -0
- package/dist/cohere/docs/llm/DOC.md +1335 -0
- package/dist/datadog/docs/monitoring/javascript/DOC.md +1740 -0
- package/dist/datadog/docs/monitoring/python/DOC.md +1815 -0
- package/dist/deepgram/docs/speech/javascript/DOC.md +885 -0
- package/dist/deepgram/docs/speech/python/DOC.md +685 -0
- package/dist/deepl/docs/translation/javascript/DOC.md +887 -0
- package/dist/deepl/docs/translation/python/DOC.md +944 -0
- package/dist/deepseek/docs/llm/DOC.md +1220 -0
- package/dist/directus/docs/headless-cms/javascript/DOC.md +1128 -0
- package/dist/directus/docs/headless-cms/python/DOC.md +1276 -0
- package/dist/discord/docs/bot/javascript/DOC.md +1090 -0
- package/dist/discord/docs/bot/python/DOC.md +1130 -0
- package/dist/elasticsearch/docs/search/DOC.md +1634 -0
- package/dist/elevenlabs/docs/text-to-speech/javascript/DOC.md +336 -0
- package/dist/elevenlabs/docs/text-to-speech/python/DOC.md +552 -0
- package/dist/firebase/docs/auth/DOC.md +1015 -0
- package/dist/gemini/docs/genai/javascript/DOC.md +691 -0
- package/dist/gemini/docs/genai/python/DOC.md +555 -0
- package/dist/github/docs/octokit/DOC.md +1560 -0
- package/dist/google/docs/bigquery/javascript/DOC.md +1688 -0
- package/dist/google/docs/bigquery/python/DOC.md +1503 -0
- package/dist/hubspot/docs/crm/javascript/DOC.md +1805 -0
- package/dist/hubspot/docs/crm/python/DOC.md +2033 -0
- package/dist/huggingface/docs/transformers/DOC.md +948 -0
- package/dist/intercom/docs/messaging/javascript/DOC.md +1844 -0
- package/dist/intercom/docs/messaging/python/DOC.md +1797 -0
- package/dist/jira/docs/issues/javascript/DOC.md +1420 -0
- package/dist/jira/docs/issues/python/DOC.md +1492 -0
- package/dist/kafka/docs/streaming/javascript/DOC.md +1671 -0
- package/dist/kafka/docs/streaming/python/DOC.md +1464 -0
- package/dist/landingai-ade/docs/api/DOC.md +620 -0
- package/dist/landingai-ade/docs/sdk/python/DOC.md +489 -0
- package/dist/landingai-ade/docs/sdk/typescript/DOC.md +542 -0
- package/dist/landingai-ade/skills/SKILL.md +489 -0
- package/dist/launchdarkly/docs/feature-flags/javascript/DOC.md +1191 -0
- package/dist/launchdarkly/docs/feature-flags/python/DOC.md +1671 -0
- package/dist/linear/docs/tracker/DOC.md +1554 -0
- package/dist/livekit/docs/realtime/javascript/DOC.md +303 -0
- package/dist/livekit/docs/realtime/python/DOC.md +163 -0
- package/dist/mailchimp/docs/marketing/DOC.md +1420 -0
- package/dist/meilisearch/docs/search/DOC.md +1241 -0
- package/dist/microsoft/docs/onedrive/javascript/DOC.md +1421 -0
- package/dist/microsoft/docs/onedrive/python/DOC.md +1549 -0
- package/dist/mongodb/docs/atlas/DOC.md +2041 -0
- package/dist/notion/docs/workspace-api/javascript/DOC.md +1435 -0
- package/dist/notion/docs/workspace-api/python/DOC.md +1400 -0
- package/dist/okta/docs/identity/javascript/DOC.md +1171 -0
- package/dist/okta/docs/identity/python/DOC.md +1401 -0
- package/dist/openai/docs/chat/javascript/DOC.md +407 -0
- package/dist/openai/docs/chat/python/DOC.md +568 -0
- package/dist/paypal/docs/checkout/DOC.md +278 -0
- package/dist/pinecone/docs/sdk/javascript/DOC.md +984 -0
- package/dist/pinecone/docs/sdk/python/DOC.md +1395 -0
- package/dist/plaid/docs/banking/javascript/DOC.md +1163 -0
- package/dist/plaid/docs/banking/python/DOC.md +1203 -0
- package/dist/playwright-community/skills/login-flows/SKILL.md +108 -0
- package/dist/postmark/docs/transactional-email/DOC.md +1168 -0
- package/dist/prisma/docs/orm/javascript/DOC.md +1419 -0
- package/dist/prisma/docs/orm/python/DOC.md +1317 -0
- package/dist/qdrant/docs/vector-search/javascript/DOC.md +1221 -0
- package/dist/qdrant/docs/vector-search/python/DOC.md +1653 -0
- package/dist/rabbitmq/docs/message-queue/javascript/DOC.md +1193 -0
- package/dist/rabbitmq/docs/message-queue/python/DOC.md +1243 -0
- package/dist/razorpay/docs/payments/javascript/DOC.md +1219 -0
- package/dist/razorpay/docs/payments/python/DOC.md +1330 -0
- package/dist/redis/docs/key-value/javascript/DOC.md +1851 -0
- package/dist/redis/docs/key-value/python/DOC.md +2054 -0
- package/dist/registry.json +2817 -0
- package/dist/replicate/docs/model-hosting/DOC.md +1318 -0
- package/dist/resend/docs/email/DOC.md +1271 -0
- package/dist/salesforce/docs/crm/javascript/DOC.md +1241 -0
- package/dist/salesforce/docs/crm/python/DOC.md +1183 -0
- package/dist/search-index.json +1 -0
- package/dist/sendgrid/docs/email-api/javascript/DOC.md +371 -0
- package/dist/sendgrid/docs/email-api/python/DOC.md +656 -0
- package/dist/sentry/docs/error-tracking/javascript/DOC.md +1073 -0
- package/dist/sentry/docs/error-tracking/python/DOC.md +1309 -0
- package/dist/shopify/docs/storefront/DOC.md +457 -0
- package/dist/slack/docs/workspace/javascript/DOC.md +933 -0
- package/dist/slack/docs/workspace/python/DOC.md +271 -0
- package/dist/square/docs/payments/javascript/DOC.md +1855 -0
- package/dist/square/docs/payments/python/DOC.md +1728 -0
- package/dist/stripe/docs/api/DOC.md +1727 -0
- package/dist/stripe/docs/payments/DOC.md +1726 -0
- package/dist/stytch/docs/auth/javascript/DOC.md +1813 -0
- package/dist/stytch/docs/auth/python/DOC.md +1962 -0
- package/dist/supabase/docs/client/DOC.md +1606 -0
- package/dist/twilio/docs/messaging/python/DOC.md +469 -0
- package/dist/twilio/docs/messaging/typescript/DOC.md +946 -0
- package/dist/vercel/docs/platform/DOC.md +1940 -0
- package/dist/weaviate/docs/vector-db/javascript/DOC.md +1268 -0
- package/dist/weaviate/docs/vector-db/python/DOC.md +1388 -0
- package/dist/zendesk/docs/support/javascript/DOC.md +2150 -0
- package/dist/zendesk/docs/support/python/DOC.md +2297 -0
- package/package.json +22 -6
- package/skills/get-api-docs/SKILL.md +84 -0
- package/src/commands/annotate.js +83 -0
- package/src/commands/build.js +12 -1
- package/src/commands/feedback.js +150 -0
- package/src/commands/get.js +83 -42
- package/src/commands/search.js +7 -0
- package/src/index.js +43 -17
- package/src/lib/analytics.js +90 -0
- package/src/lib/annotations.js +57 -0
- package/src/lib/bm25.js +170 -0
- package/src/lib/cache.js +69 -6
- package/src/lib/config.js +8 -3
- package/src/lib/identity.js +99 -0
- package/src/lib/registry.js +103 -20
- package/src/lib/telemetry.js +86 -0
- package/src/mcp/server.js +177 -0
- package/src/mcp/tools.js +251 -0
|
@@ -0,0 +1,1278 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: gateway
|
|
3
|
+
description: "Braintree Node.js SDK for payment gateway, PayPal, and subscriptions"
|
|
4
|
+
metadata:
|
|
5
|
+
languages: "javascript"
|
|
6
|
+
versions: "3.34.0"
|
|
7
|
+
updated-on: "2026-03-02"
|
|
8
|
+
source: maintainer
|
|
9
|
+
tags: "braintree,gateway,payments,paypal,subscriptions"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Braintree Node.js SDK Context
|
|
13
|
+
|
|
14
|
+
## Golden Rule
|
|
15
|
+
|
|
16
|
+
**ALWAYS use the official Braintree Node.js SDK package: `braintree`**
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install braintree
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**DO NOT use:**
|
|
23
|
+
- Deprecated or unofficial packages
|
|
24
|
+
- Direct API calls without the SDK
|
|
25
|
+
- Outdated versions (version 3.40.0+ required for SSL certificate support)
|
|
26
|
+
|
|
27
|
+
The official package is `braintree` and is maintained by Braintree (a PayPal service).
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install braintree
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
For Express.js applications, you'll also need:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install express body-parser
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Environment Configuration
|
|
42
|
+
|
|
43
|
+
Create a `.env` file with your Braintree credentials:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
BRAINTREE_ENVIRONMENT=Sandbox
|
|
47
|
+
BRAINTREE_MERCHANT_ID=your_merchant_id
|
|
48
|
+
BRAINTREE_PUBLIC_KEY=your_public_key
|
|
49
|
+
BRAINTREE_PRIVATE_KEY=your_private_key
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Load environment variables:
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
require('dotenv').config();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Initialization
|
|
59
|
+
|
|
60
|
+
### Basic Gateway Setup
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
const braintree = require("braintree");
|
|
64
|
+
|
|
65
|
+
const gateway = new braintree.BraintreeGateway({
|
|
66
|
+
environment: braintree.Environment.Sandbox,
|
|
67
|
+
merchantId: "your_merchant_id",
|
|
68
|
+
publicKey: "your_public_key",
|
|
69
|
+
privateKey: "your_private_key"
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Using Environment Variables
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
const braintree = require("braintree");
|
|
77
|
+
|
|
78
|
+
const gateway = new braintree.BraintreeGateway({
|
|
79
|
+
environment: process.env.BRAINTREE_ENVIRONMENT === 'Production'
|
|
80
|
+
? braintree.Environment.Production
|
|
81
|
+
: braintree.Environment.Sandbox,
|
|
82
|
+
merchantId: process.env.BRAINTREE_MERCHANT_ID,
|
|
83
|
+
publicKey: process.env.BRAINTREE_PUBLIC_KEY,
|
|
84
|
+
privateKey: process.env.BRAINTREE_PRIVATE_KEY
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Environment Options
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
// Sandbox environment (for testing)
|
|
92
|
+
braintree.Environment.Sandbox
|
|
93
|
+
|
|
94
|
+
// Production environment (for live transactions)
|
|
95
|
+
braintree.Environment.Production
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Client Token Generation
|
|
99
|
+
|
|
100
|
+
Client tokens contain authorization and configuration information for the client SDK.
|
|
101
|
+
|
|
102
|
+
### Basic Client Token
|
|
103
|
+
|
|
104
|
+
**Callback:**
|
|
105
|
+
```javascript
|
|
106
|
+
gateway.clientToken.generate({}, (err, response) => {
|
|
107
|
+
const clientToken = response.clientToken;
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Promise:**
|
|
112
|
+
```javascript
|
|
113
|
+
gateway.clientToken.generate({})
|
|
114
|
+
.then(response => {
|
|
115
|
+
const clientToken = response.clientToken;
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Client Token with Customer ID
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
gateway.clientToken.generate({
|
|
123
|
+
customerId: "customer_123"
|
|
124
|
+
}, (err, response) => {
|
|
125
|
+
const clientToken = response.clientToken;
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Express.js Endpoint for Client Token
|
|
130
|
+
|
|
131
|
+
```javascript
|
|
132
|
+
const express = require('express');
|
|
133
|
+
const app = express();
|
|
134
|
+
|
|
135
|
+
app.get("/client_token", (req, res) => {
|
|
136
|
+
gateway.clientToken.generate({}, (err, response) => {
|
|
137
|
+
if (err) {
|
|
138
|
+
res.status(500).send(err);
|
|
139
|
+
} else {
|
|
140
|
+
res.send(response.clientToken);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Transactions
|
|
147
|
+
|
|
148
|
+
### Creating a Transaction
|
|
149
|
+
|
|
150
|
+
**Basic Sale (Callback):**
|
|
151
|
+
```javascript
|
|
152
|
+
gateway.transaction.sale({
|
|
153
|
+
amount: "10.00",
|
|
154
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
155
|
+
options: {
|
|
156
|
+
submitForSettlement: true
|
|
157
|
+
}
|
|
158
|
+
}, (err, result) => {
|
|
159
|
+
if (result.success) {
|
|
160
|
+
console.log("Transaction ID: " + result.transaction.id);
|
|
161
|
+
} else {
|
|
162
|
+
console.log(result.message);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Basic Sale (Promise):**
|
|
168
|
+
```javascript
|
|
169
|
+
gateway.transaction.sale({
|
|
170
|
+
amount: "10.00",
|
|
171
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
172
|
+
options: {
|
|
173
|
+
submitForSettlement: true
|
|
174
|
+
}
|
|
175
|
+
}).then(result => {
|
|
176
|
+
if (result.success) {
|
|
177
|
+
console.log("Transaction ID: " + result.transaction.id);
|
|
178
|
+
} else {
|
|
179
|
+
console.log(result.message);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Transaction with Device Data
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
gateway.transaction.sale({
|
|
188
|
+
amount: "10.00",
|
|
189
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
190
|
+
deviceData: deviceDataFromTheClient,
|
|
191
|
+
options: {
|
|
192
|
+
submitForSettlement: true
|
|
193
|
+
}
|
|
194
|
+
}, (err, result) => {
|
|
195
|
+
// Handle result
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Transaction with Customer Information
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
gateway.transaction.sale({
|
|
203
|
+
amount: "100.00",
|
|
204
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
205
|
+
customer: {
|
|
206
|
+
firstName: "John",
|
|
207
|
+
lastName: "Doe",
|
|
208
|
+
email: "john.doe@example.com",
|
|
209
|
+
phone: "312-555-1234"
|
|
210
|
+
},
|
|
211
|
+
billing: {
|
|
212
|
+
firstName: "John",
|
|
213
|
+
lastName: "Doe",
|
|
214
|
+
streetAddress: "123 Main St",
|
|
215
|
+
locality: "Chicago",
|
|
216
|
+
region: "IL",
|
|
217
|
+
postalCode: "60614",
|
|
218
|
+
countryCodeAlpha2: "US"
|
|
219
|
+
},
|
|
220
|
+
shipping: {
|
|
221
|
+
firstName: "John",
|
|
222
|
+
lastName: "Doe",
|
|
223
|
+
streetAddress: "123 Main St",
|
|
224
|
+
locality: "Chicago",
|
|
225
|
+
region: "IL",
|
|
226
|
+
postalCode: "60614",
|
|
227
|
+
countryCodeAlpha2: "US"
|
|
228
|
+
},
|
|
229
|
+
options: {
|
|
230
|
+
submitForSettlement: true
|
|
231
|
+
}
|
|
232
|
+
}, (err, result) => {
|
|
233
|
+
// Handle result
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Transaction with Stored Payment Method
|
|
238
|
+
|
|
239
|
+
```javascript
|
|
240
|
+
gateway.transaction.sale({
|
|
241
|
+
amount: "10.00",
|
|
242
|
+
paymentMethodToken: "the_token",
|
|
243
|
+
options: {
|
|
244
|
+
submitForSettlement: true
|
|
245
|
+
}
|
|
246
|
+
}, (err, result) => {
|
|
247
|
+
// Handle result
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Express.js Checkout Endpoint
|
|
252
|
+
|
|
253
|
+
```javascript
|
|
254
|
+
app.post("/checkout", (req, res) => {
|
|
255
|
+
const nonceFromTheClient = req.body.payment_method_nonce;
|
|
256
|
+
const amount = req.body.amount;
|
|
257
|
+
|
|
258
|
+
gateway.transaction.sale({
|
|
259
|
+
amount: amount,
|
|
260
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
261
|
+
options: {
|
|
262
|
+
submitForSettlement: true
|
|
263
|
+
}
|
|
264
|
+
}, (err, result) => {
|
|
265
|
+
if (result.success) {
|
|
266
|
+
res.send({
|
|
267
|
+
success: true,
|
|
268
|
+
transactionId: result.transaction.id
|
|
269
|
+
});
|
|
270
|
+
} else {
|
|
271
|
+
res.status(500).send({
|
|
272
|
+
success: false,
|
|
273
|
+
message: result.message
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### Finding a Transaction
|
|
281
|
+
|
|
282
|
+
**Callback:**
|
|
283
|
+
```javascript
|
|
284
|
+
gateway.transaction.find("theTransactionId", (err, transaction) => {
|
|
285
|
+
console.log(transaction.amount);
|
|
286
|
+
console.log(transaction.status);
|
|
287
|
+
});
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
**Promise:**
|
|
291
|
+
```javascript
|
|
292
|
+
gateway.transaction.find("theTransactionId")
|
|
293
|
+
.then(transaction => {
|
|
294
|
+
console.log(transaction.amount);
|
|
295
|
+
console.log(transaction.status);
|
|
296
|
+
});
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Refunding a Transaction
|
|
300
|
+
|
|
301
|
+
**Full Refund (Callback):**
|
|
302
|
+
```javascript
|
|
303
|
+
gateway.transaction.refund("theTransactionId", (err, result) => {
|
|
304
|
+
if (result.success) {
|
|
305
|
+
console.log("Refund successful");
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**Partial Refund (Callback):**
|
|
311
|
+
```javascript
|
|
312
|
+
gateway.transaction.refund("theTransactionId", "10.00", (err, result) => {
|
|
313
|
+
if (result.success) {
|
|
314
|
+
console.log("Partial refund successful");
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**Promise:**
|
|
320
|
+
```javascript
|
|
321
|
+
gateway.transaction.refund("theTransactionId", "10.00")
|
|
322
|
+
.then(result => {
|
|
323
|
+
if (result.success) {
|
|
324
|
+
console.log("Refund successful");
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Voiding a Transaction
|
|
330
|
+
|
|
331
|
+
**Callback:**
|
|
332
|
+
```javascript
|
|
333
|
+
gateway.transaction.void("theTransactionId", (err, result) => {
|
|
334
|
+
if (result.success) {
|
|
335
|
+
console.log("Transaction voided");
|
|
336
|
+
} else {
|
|
337
|
+
console.log(result.message);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
**Promise:**
|
|
343
|
+
```javascript
|
|
344
|
+
gateway.transaction.void("theTransactionId")
|
|
345
|
+
.then(result => {
|
|
346
|
+
if (result.success) {
|
|
347
|
+
console.log("Transaction voided");
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Customer Management
|
|
353
|
+
|
|
354
|
+
### Creating a Customer
|
|
355
|
+
|
|
356
|
+
**Basic Customer (Callback):**
|
|
357
|
+
```javascript
|
|
358
|
+
gateway.customer.create({
|
|
359
|
+
firstName: "John",
|
|
360
|
+
lastName: "Doe",
|
|
361
|
+
email: "john.doe@example.com",
|
|
362
|
+
phone: "312-555-1234"
|
|
363
|
+
}, (err, result) => {
|
|
364
|
+
if (result.success) {
|
|
365
|
+
console.log("Customer ID: " + result.customer.id);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
**Promise:**
|
|
371
|
+
```javascript
|
|
372
|
+
gateway.customer.create({
|
|
373
|
+
firstName: "John",
|
|
374
|
+
lastName: "Doe",
|
|
375
|
+
email: "john.doe@example.com"
|
|
376
|
+
}).then(result => {
|
|
377
|
+
if (result.success) {
|
|
378
|
+
console.log("Customer ID: " + result.customer.id);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Creating Customer with Company Information
|
|
384
|
+
|
|
385
|
+
```javascript
|
|
386
|
+
gateway.customer.create({
|
|
387
|
+
firstName: "John",
|
|
388
|
+
lastName: "Doe",
|
|
389
|
+
company: "Acme Corp",
|
|
390
|
+
email: "john.doe@example.com",
|
|
391
|
+
phone: "312-555-1234",
|
|
392
|
+
fax: "614-555-5678",
|
|
393
|
+
website: "www.example.com"
|
|
394
|
+
}, (err, result) => {
|
|
395
|
+
// Handle result
|
|
396
|
+
});
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### Creating Customer with Payment Method
|
|
400
|
+
|
|
401
|
+
```javascript
|
|
402
|
+
gateway.customer.create({
|
|
403
|
+
firstName: "John",
|
|
404
|
+
lastName: "Doe",
|
|
405
|
+
email: "john.doe@example.com",
|
|
406
|
+
paymentMethodNonce: nonceFromTheClient
|
|
407
|
+
}, (err, result) => {
|
|
408
|
+
if (result.success) {
|
|
409
|
+
console.log("Customer ID: " + result.customer.id);
|
|
410
|
+
console.log("Payment Method Token: " + result.customer.paymentMethods[0].token);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### Finding a Customer
|
|
416
|
+
|
|
417
|
+
**Callback:**
|
|
418
|
+
```javascript
|
|
419
|
+
gateway.customer.find("theCustomerId", (err, customer) => {
|
|
420
|
+
console.log(customer.firstName);
|
|
421
|
+
console.log(customer.lastName);
|
|
422
|
+
console.log(customer.email);
|
|
423
|
+
});
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
**Promise:**
|
|
427
|
+
```javascript
|
|
428
|
+
gateway.customer.find("theCustomerId")
|
|
429
|
+
.then(customer => {
|
|
430
|
+
console.log(customer.firstName);
|
|
431
|
+
console.log(customer.paymentMethods);
|
|
432
|
+
});
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
### Updating a Customer
|
|
436
|
+
|
|
437
|
+
**Callback:**
|
|
438
|
+
```javascript
|
|
439
|
+
gateway.customer.update("theCustomerId", {
|
|
440
|
+
firstName: "Jane",
|
|
441
|
+
lastName: "Smith",
|
|
442
|
+
email: "jane.smith@example.com"
|
|
443
|
+
}, (err, result) => {
|
|
444
|
+
if (result.success) {
|
|
445
|
+
console.log("Customer updated");
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
**Promise:**
|
|
451
|
+
```javascript
|
|
452
|
+
gateway.customer.update("theCustomerId", {
|
|
453
|
+
firstName: "Jane",
|
|
454
|
+
lastName: "Smith"
|
|
455
|
+
}).then(result => {
|
|
456
|
+
if (result.success) {
|
|
457
|
+
console.log("Customer updated");
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### Deleting a Customer
|
|
463
|
+
|
|
464
|
+
**Callback:**
|
|
465
|
+
```javascript
|
|
466
|
+
gateway.customer.delete("theCustomerId", (err) => {
|
|
467
|
+
if (!err) {
|
|
468
|
+
console.log("Customer deleted");
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
**Promise:**
|
|
474
|
+
```javascript
|
|
475
|
+
gateway.customer.delete("theCustomerId")
|
|
476
|
+
.then(() => {
|
|
477
|
+
console.log("Customer deleted");
|
|
478
|
+
});
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
### Searching for Customers
|
|
482
|
+
|
|
483
|
+
```javascript
|
|
484
|
+
const stream = gateway.customer.search((search) => {
|
|
485
|
+
search.firstName().is("John");
|
|
486
|
+
search.lastName().is("Doe");
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
stream.on("data", (customer) => {
|
|
490
|
+
console.log(customer.id);
|
|
491
|
+
console.log(customer.email);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
stream.on("end", () => {
|
|
495
|
+
console.log("Search complete");
|
|
496
|
+
});
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
## Payment Method Management
|
|
500
|
+
|
|
501
|
+
### Creating a Payment Method
|
|
502
|
+
|
|
503
|
+
**Callback:**
|
|
504
|
+
```javascript
|
|
505
|
+
gateway.paymentMethod.create({
|
|
506
|
+
customerId: "customer_123",
|
|
507
|
+
paymentMethodNonce: nonceFromTheClient
|
|
508
|
+
}, (err, result) => {
|
|
509
|
+
if (result.success) {
|
|
510
|
+
console.log("Payment Method Token: " + result.paymentMethod.token);
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
**Promise:**
|
|
516
|
+
```javascript
|
|
517
|
+
gateway.paymentMethod.create({
|
|
518
|
+
customerId: "customer_123",
|
|
519
|
+
paymentMethodNonce: nonceFromTheClient
|
|
520
|
+
}).then(result => {
|
|
521
|
+
if (result.success) {
|
|
522
|
+
console.log("Payment Method Token: " + result.paymentMethod.token);
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
### Creating Payment Method with Billing Address
|
|
528
|
+
|
|
529
|
+
```javascript
|
|
530
|
+
gateway.paymentMethod.create({
|
|
531
|
+
customerId: "customer_123",
|
|
532
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
533
|
+
billingAddress: {
|
|
534
|
+
streetAddress: "123 Main St",
|
|
535
|
+
locality: "Chicago",
|
|
536
|
+
region: "IL",
|
|
537
|
+
postalCode: "60614"
|
|
538
|
+
}
|
|
539
|
+
}, (err, result) => {
|
|
540
|
+
// Handle result
|
|
541
|
+
});
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### Finding a Payment Method
|
|
545
|
+
|
|
546
|
+
**Callback:**
|
|
547
|
+
```javascript
|
|
548
|
+
gateway.paymentMethod.find("the_token", (err, paymentMethod) => {
|
|
549
|
+
console.log(paymentMethod.token);
|
|
550
|
+
});
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
**Promise:**
|
|
554
|
+
```javascript
|
|
555
|
+
gateway.paymentMethod.find("the_token")
|
|
556
|
+
.then(paymentMethod => {
|
|
557
|
+
console.log(paymentMethod.token);
|
|
558
|
+
});
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
### Updating a Payment Method
|
|
562
|
+
|
|
563
|
+
**Set as Default (Callback):**
|
|
564
|
+
```javascript
|
|
565
|
+
gateway.paymentMethod.update("the_token", {
|
|
566
|
+
options: {
|
|
567
|
+
makeDefault: true
|
|
568
|
+
}
|
|
569
|
+
}, (err, result) => {
|
|
570
|
+
if (result.success) {
|
|
571
|
+
console.log("Payment method is now default");
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
**Update Billing Address (Callback):**
|
|
577
|
+
```javascript
|
|
578
|
+
gateway.paymentMethod.update("the_token", {
|
|
579
|
+
billingAddress: {
|
|
580
|
+
streetAddress: "456 Oak Ave",
|
|
581
|
+
locality: "Chicago",
|
|
582
|
+
region: "IL",
|
|
583
|
+
postalCode: "60614",
|
|
584
|
+
options: {
|
|
585
|
+
updateExisting: true
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}, (err, result) => {
|
|
589
|
+
// Handle result
|
|
590
|
+
});
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
**Promise:**
|
|
594
|
+
```javascript
|
|
595
|
+
gateway.paymentMethod.update("the_token", {
|
|
596
|
+
options: {
|
|
597
|
+
makeDefault: true
|
|
598
|
+
}
|
|
599
|
+
}).then(result => {
|
|
600
|
+
if (result.success) {
|
|
601
|
+
console.log("Payment method updated");
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
### Deleting a Payment Method
|
|
607
|
+
|
|
608
|
+
**Callback:**
|
|
609
|
+
```javascript
|
|
610
|
+
gateway.paymentMethod.delete("the_token", (err) => {
|
|
611
|
+
if (!err) {
|
|
612
|
+
console.log("Payment method deleted");
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
**Promise:**
|
|
618
|
+
```javascript
|
|
619
|
+
gateway.paymentMethod.delete("the_token")
|
|
620
|
+
.then(() => {
|
|
621
|
+
console.log("Payment method deleted");
|
|
622
|
+
})
|
|
623
|
+
.catch(err => {
|
|
624
|
+
console.log(err);
|
|
625
|
+
});
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
## Subscription Billing
|
|
629
|
+
|
|
630
|
+
### Creating a Subscription
|
|
631
|
+
|
|
632
|
+
**Basic Subscription (Callback):**
|
|
633
|
+
```javascript
|
|
634
|
+
gateway.subscription.create({
|
|
635
|
+
paymentMethodToken: "the_token",
|
|
636
|
+
planId: "plan_id"
|
|
637
|
+
}, (err, result) => {
|
|
638
|
+
if (result.success) {
|
|
639
|
+
console.log("Subscription ID: " + result.subscription.id);
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
**Promise:**
|
|
645
|
+
```javascript
|
|
646
|
+
gateway.subscription.create({
|
|
647
|
+
paymentMethodToken: "the_token",
|
|
648
|
+
planId: "plan_id"
|
|
649
|
+
}).then(result => {
|
|
650
|
+
if (result.success) {
|
|
651
|
+
console.log("Subscription ID: " + result.subscription.id);
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
### Creating Subscription with Add-ons and Discounts
|
|
657
|
+
|
|
658
|
+
```javascript
|
|
659
|
+
gateway.subscription.create({
|
|
660
|
+
paymentMethodToken: "the_token",
|
|
661
|
+
planId: "plan_id",
|
|
662
|
+
addOns: {
|
|
663
|
+
add: [
|
|
664
|
+
{
|
|
665
|
+
inheritedFromId: "add_on_id_1",
|
|
666
|
+
amount: "10.00"
|
|
667
|
+
}
|
|
668
|
+
]
|
|
669
|
+
},
|
|
670
|
+
discounts: {
|
|
671
|
+
add: [
|
|
672
|
+
{
|
|
673
|
+
inheritedFromId: "discount_id_1",
|
|
674
|
+
amount: "5.00"
|
|
675
|
+
}
|
|
676
|
+
]
|
|
677
|
+
}
|
|
678
|
+
}, (err, result) => {
|
|
679
|
+
// Handle result
|
|
680
|
+
});
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
### Creating Subscription with Trial Period
|
|
684
|
+
|
|
685
|
+
```javascript
|
|
686
|
+
gateway.subscription.create({
|
|
687
|
+
paymentMethodToken: "the_token",
|
|
688
|
+
planId: "plan_id",
|
|
689
|
+
trialDuration: 14,
|
|
690
|
+
trialDurationUnit: "day",
|
|
691
|
+
trialPeriod: true
|
|
692
|
+
}, (err, result) => {
|
|
693
|
+
// Handle result
|
|
694
|
+
});
|
|
695
|
+
```
|
|
696
|
+
|
|
697
|
+
### Finding a Subscription
|
|
698
|
+
|
|
699
|
+
**Callback:**
|
|
700
|
+
```javascript
|
|
701
|
+
gateway.subscription.find("subscription_id", (err, subscription) => {
|
|
702
|
+
console.log(subscription.status);
|
|
703
|
+
console.log(subscription.price);
|
|
704
|
+
});
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
**Promise:**
|
|
708
|
+
```javascript
|
|
709
|
+
gateway.subscription.find("subscription_id")
|
|
710
|
+
.then(subscription => {
|
|
711
|
+
console.log(subscription.status);
|
|
712
|
+
});
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
### Updating a Subscription
|
|
716
|
+
|
|
717
|
+
**Callback:**
|
|
718
|
+
```javascript
|
|
719
|
+
gateway.subscription.update("subscription_id", {
|
|
720
|
+
price: "15.00"
|
|
721
|
+
}, (err, result) => {
|
|
722
|
+
if (result.success) {
|
|
723
|
+
console.log("Subscription updated");
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
**Promise:**
|
|
729
|
+
```javascript
|
|
730
|
+
gateway.subscription.update("subscription_id", {
|
|
731
|
+
price: "15.00",
|
|
732
|
+
planId: "new_plan_id"
|
|
733
|
+
}).then(result => {
|
|
734
|
+
if (result.success) {
|
|
735
|
+
console.log("Subscription updated");
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
### Canceling a Subscription
|
|
741
|
+
|
|
742
|
+
**Callback:**
|
|
743
|
+
```javascript
|
|
744
|
+
gateway.subscription.cancel("subscription_id", (err, result) => {
|
|
745
|
+
if (result.success) {
|
|
746
|
+
console.log("Subscription canceled");
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
**Promise:**
|
|
752
|
+
```javascript
|
|
753
|
+
gateway.subscription.cancel("subscription_id")
|
|
754
|
+
.then(result => {
|
|
755
|
+
if (result.success) {
|
|
756
|
+
console.log("Subscription canceled");
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
```
|
|
760
|
+
|
|
761
|
+
### Retrieving All Plans
|
|
762
|
+
|
|
763
|
+
**Callback:**
|
|
764
|
+
```javascript
|
|
765
|
+
gateway.plan.all((err, result) => {
|
|
766
|
+
result.plans.forEach(plan => {
|
|
767
|
+
console.log(plan.id);
|
|
768
|
+
console.log(plan.name);
|
|
769
|
+
console.log(plan.price);
|
|
770
|
+
});
|
|
771
|
+
});
|
|
772
|
+
```
|
|
773
|
+
|
|
774
|
+
**Promise:**
|
|
775
|
+
```javascript
|
|
776
|
+
gateway.plan.all()
|
|
777
|
+
.then(result => {
|
|
778
|
+
result.plans.forEach(plan => {
|
|
779
|
+
console.log(plan.id);
|
|
780
|
+
});
|
|
781
|
+
});
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
## Webhooks
|
|
785
|
+
|
|
786
|
+
### Parsing Webhook Notifications
|
|
787
|
+
|
|
788
|
+
**Callback:**
|
|
789
|
+
```javascript
|
|
790
|
+
gateway.webhookNotification.parse(
|
|
791
|
+
req.body.bt_signature,
|
|
792
|
+
req.body.bt_payload,
|
|
793
|
+
(err, webhookNotification) => {
|
|
794
|
+
if (err) {
|
|
795
|
+
console.error(err);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
console.log(webhookNotification.kind);
|
|
800
|
+
console.log(webhookNotification.timestamp);
|
|
801
|
+
}
|
|
802
|
+
);
|
|
803
|
+
```
|
|
804
|
+
|
|
805
|
+
**Promise:**
|
|
806
|
+
```javascript
|
|
807
|
+
gateway.webhookNotification.parse(
|
|
808
|
+
req.body.bt_signature,
|
|
809
|
+
req.body.bt_payload
|
|
810
|
+
).then(webhookNotification => {
|
|
811
|
+
console.log(webhookNotification.kind);
|
|
812
|
+
console.log(webhookNotification.timestamp);
|
|
813
|
+
});
|
|
814
|
+
```
|
|
815
|
+
|
|
816
|
+
### Express.js Webhook Endpoint
|
|
817
|
+
|
|
818
|
+
```javascript
|
|
819
|
+
const express = require('express');
|
|
820
|
+
const bodyParser = require('body-parser');
|
|
821
|
+
const app = express();
|
|
822
|
+
|
|
823
|
+
app.use(bodyParser.urlencoded({ extended: false }));
|
|
824
|
+
|
|
825
|
+
app.post('/webhooks', (req, res) => {
|
|
826
|
+
gateway.webhookNotification.parse(
|
|
827
|
+
req.body.bt_signature,
|
|
828
|
+
req.body.bt_payload,
|
|
829
|
+
(err, webhookNotification) => {
|
|
830
|
+
if (err) {
|
|
831
|
+
console.error(err);
|
|
832
|
+
return res.sendStatus(400);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// Handle different webhook types
|
|
836
|
+
if (webhookNotification.kind === braintree.WebhookNotification.Kind.SubscriptionCanceled) {
|
|
837
|
+
console.log("Subscription " + webhookNotification.subscription.id + " canceled");
|
|
838
|
+
} else if (webhookNotification.kind === braintree.WebhookNotification.Kind.SubscriptionChargedSuccessfully) {
|
|
839
|
+
console.log("Subscription " + webhookNotification.subscription.id + " charged");
|
|
840
|
+
} else if (webhookNotification.kind === braintree.WebhookNotification.Kind.DisputeOpened) {
|
|
841
|
+
console.log("Dispute opened for transaction " + webhookNotification.dispute.transaction.id);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
res.sendStatus(200);
|
|
845
|
+
}
|
|
846
|
+
);
|
|
847
|
+
});
|
|
848
|
+
```
|
|
849
|
+
|
|
850
|
+
### Handling Subscription Webhooks
|
|
851
|
+
|
|
852
|
+
```javascript
|
|
853
|
+
gateway.webhookNotification.parse(
|
|
854
|
+
signature,
|
|
855
|
+
payload,
|
|
856
|
+
(err, webhookNotification) => {
|
|
857
|
+
if (webhookNotification.kind === braintree.WebhookNotification.Kind.SubscriptionWentPastDue) {
|
|
858
|
+
const subscriptionId = webhookNotification.subscription.id;
|
|
859
|
+
// Handle past due subscription
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
);
|
|
863
|
+
```
|
|
864
|
+
|
|
865
|
+
### Verifying Webhook Signatures
|
|
866
|
+
|
|
867
|
+
```javascript
|
|
868
|
+
gateway.webhookNotification.verify(
|
|
869
|
+
"challenge_string",
|
|
870
|
+
(err, response) => {
|
|
871
|
+
console.log(response);
|
|
872
|
+
}
|
|
873
|
+
);
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
### Testing Webhooks
|
|
877
|
+
|
|
878
|
+
```javascript
|
|
879
|
+
const sampleNotification = gateway.webhookTesting.sampleNotification(
|
|
880
|
+
braintree.WebhookNotification.Kind.SubscriptionWentPastDue,
|
|
881
|
+
"subscription_id"
|
|
882
|
+
);
|
|
883
|
+
|
|
884
|
+
gateway.webhookNotification.parse(
|
|
885
|
+
sampleNotification.bt_signature,
|
|
886
|
+
sampleNotification.bt_payload
|
|
887
|
+
).then(webhookNotification => {
|
|
888
|
+
console.log(webhookNotification.kind);
|
|
889
|
+
console.log(webhookNotification.subscription.id);
|
|
890
|
+
});
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
## Advanced Features
|
|
894
|
+
|
|
895
|
+
### Searching Transactions
|
|
896
|
+
|
|
897
|
+
```javascript
|
|
898
|
+
const stream = gateway.transaction.search((search) => {
|
|
899
|
+
search.amount().min("10.00");
|
|
900
|
+
search.amount().max("100.00");
|
|
901
|
+
search.status().is(braintree.Transaction.Status.Settled);
|
|
902
|
+
search.createdAt().min(new Date("2024-01-01"));
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
stream.on("data", (transaction) => {
|
|
906
|
+
console.log(transaction.id);
|
|
907
|
+
console.log(transaction.amount);
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
stream.on("end", () => {
|
|
911
|
+
console.log("Search complete");
|
|
912
|
+
});
|
|
913
|
+
```
|
|
914
|
+
|
|
915
|
+
### Creating Customer with Multiple Payment Methods
|
|
916
|
+
|
|
917
|
+
```javascript
|
|
918
|
+
gateway.customer.create({
|
|
919
|
+
firstName: "John",
|
|
920
|
+
lastName: "Doe",
|
|
921
|
+
email: "john.doe@example.com",
|
|
922
|
+
creditCard: {
|
|
923
|
+
number: "4111111111111111",
|
|
924
|
+
expirationDate: "05/2025",
|
|
925
|
+
cvv: "123",
|
|
926
|
+
billingAddress: {
|
|
927
|
+
postalCode: "60614"
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}, (err, result) => {
|
|
931
|
+
if (result.success) {
|
|
932
|
+
const token = result.customer.paymentMethods[0].token;
|
|
933
|
+
console.log("Payment method token: " + token);
|
|
934
|
+
}
|
|
935
|
+
});
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
### Transaction with Custom Fields
|
|
939
|
+
|
|
940
|
+
```javascript
|
|
941
|
+
gateway.transaction.sale({
|
|
942
|
+
amount: "10.00",
|
|
943
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
944
|
+
customFields: {
|
|
945
|
+
order_id: "12345",
|
|
946
|
+
user_type: "premium"
|
|
947
|
+
},
|
|
948
|
+
options: {
|
|
949
|
+
submitForSettlement: true
|
|
950
|
+
}
|
|
951
|
+
}, (err, result) => {
|
|
952
|
+
// Handle result
|
|
953
|
+
});
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
### Holding in Escrow
|
|
957
|
+
|
|
958
|
+
```javascript
|
|
959
|
+
gateway.transaction.sale({
|
|
960
|
+
amount: "10.00",
|
|
961
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
962
|
+
serviceFeeAmount: "1.00",
|
|
963
|
+
merchantAccountId: "submerchant_account_id",
|
|
964
|
+
options: {
|
|
965
|
+
submitForSettlement: true,
|
|
966
|
+
holdInEscrow: true
|
|
967
|
+
}
|
|
968
|
+
}, (err, result) => {
|
|
969
|
+
// Handle result
|
|
970
|
+
});
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
### Transaction with Descriptor
|
|
974
|
+
|
|
975
|
+
```javascript
|
|
976
|
+
gateway.transaction.sale({
|
|
977
|
+
amount: "10.00",
|
|
978
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
979
|
+
descriptor: {
|
|
980
|
+
name: "Company*Product",
|
|
981
|
+
phone: "8004567890",
|
|
982
|
+
url: "example.com"
|
|
983
|
+
},
|
|
984
|
+
options: {
|
|
985
|
+
submitForSettlement: true
|
|
986
|
+
}
|
|
987
|
+
}, (err, result) => {
|
|
988
|
+
// Handle result
|
|
989
|
+
});
|
|
990
|
+
```
|
|
991
|
+
|
|
992
|
+
### Cloning a Transaction
|
|
993
|
+
|
|
994
|
+
```javascript
|
|
995
|
+
gateway.transaction.cloneTransaction(
|
|
996
|
+
"original_transaction_id",
|
|
997
|
+
{
|
|
998
|
+
amount: "20.00",
|
|
999
|
+
options: {
|
|
1000
|
+
submitForSettlement: true
|
|
1001
|
+
}
|
|
1002
|
+
},
|
|
1003
|
+
(err, result) => {
|
|
1004
|
+
if (result.success) {
|
|
1005
|
+
console.log("Cloned transaction ID: " + result.transaction.id);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
);
|
|
1009
|
+
```
|
|
1010
|
+
|
|
1011
|
+
### Settlement
|
|
1012
|
+
|
|
1013
|
+
**Submit for Settlement:**
|
|
1014
|
+
```javascript
|
|
1015
|
+
gateway.transaction.submitForSettlement("transaction_id", (err, result) => {
|
|
1016
|
+
if (result.success) {
|
|
1017
|
+
console.log("Submitted for settlement");
|
|
1018
|
+
}
|
|
1019
|
+
});
|
|
1020
|
+
```
|
|
1021
|
+
|
|
1022
|
+
**Submit with Amount:**
|
|
1023
|
+
```javascript
|
|
1024
|
+
gateway.transaction.submitForSettlement("transaction_id", "50.00", (err, result) => {
|
|
1025
|
+
if (result.success) {
|
|
1026
|
+
console.log("Submitted for partial settlement");
|
|
1027
|
+
}
|
|
1028
|
+
});
|
|
1029
|
+
```
|
|
1030
|
+
|
|
1031
|
+
### Dispute Management
|
|
1032
|
+
|
|
1033
|
+
**Finding a Dispute:**
|
|
1034
|
+
```javascript
|
|
1035
|
+
gateway.dispute.find("dispute_id", (err, dispute) => {
|
|
1036
|
+
console.log(dispute.status);
|
|
1037
|
+
console.log(dispute.amount);
|
|
1038
|
+
console.log(dispute.reason);
|
|
1039
|
+
});
|
|
1040
|
+
```
|
|
1041
|
+
|
|
1042
|
+
**Accept Dispute:**
|
|
1043
|
+
```javascript
|
|
1044
|
+
gateway.dispute.accept("dispute_id", (err, result) => {
|
|
1045
|
+
// Handle result
|
|
1046
|
+
});
|
|
1047
|
+
```
|
|
1048
|
+
|
|
1049
|
+
**Add Text Evidence:**
|
|
1050
|
+
```javascript
|
|
1051
|
+
gateway.dispute.addTextEvidence("dispute_id", "evidence_text", (err, result) => {
|
|
1052
|
+
// Handle result
|
|
1053
|
+
});
|
|
1054
|
+
```
|
|
1055
|
+
|
|
1056
|
+
### Merchant Account Creation
|
|
1057
|
+
|
|
1058
|
+
```javascript
|
|
1059
|
+
gateway.merchantAccount.create({
|
|
1060
|
+
individual: {
|
|
1061
|
+
firstName: "John",
|
|
1062
|
+
lastName: "Doe",
|
|
1063
|
+
email: "john.doe@example.com",
|
|
1064
|
+
dateOfBirth: "1980-01-01",
|
|
1065
|
+
ssn: "123-45-6789",
|
|
1066
|
+
address: {
|
|
1067
|
+
streetAddress: "123 Main St",
|
|
1068
|
+
locality: "Chicago",
|
|
1069
|
+
region: "IL",
|
|
1070
|
+
postalCode: "60614"
|
|
1071
|
+
}
|
|
1072
|
+
},
|
|
1073
|
+
business: {
|
|
1074
|
+
legalName: "John Doe Inc",
|
|
1075
|
+
dbaName: "JD Services",
|
|
1076
|
+
taxId: "12-3456789",
|
|
1077
|
+
address: {
|
|
1078
|
+
streetAddress: "123 Main St",
|
|
1079
|
+
locality: "Chicago",
|
|
1080
|
+
region: "IL",
|
|
1081
|
+
postalCode: "60614"
|
|
1082
|
+
}
|
|
1083
|
+
},
|
|
1084
|
+
funding: {
|
|
1085
|
+
destination: braintree.MerchantAccount.FundingDestination.Bank,
|
|
1086
|
+
routingNumber: "021000021",
|
|
1087
|
+
accountNumber: "1234567890"
|
|
1088
|
+
},
|
|
1089
|
+
tosAccepted: true,
|
|
1090
|
+
masterMerchantAccountId: "master_merchant_account_id"
|
|
1091
|
+
}, (err, result) => {
|
|
1092
|
+
if (result.success) {
|
|
1093
|
+
console.log("Merchant Account ID: " + result.merchantAccount.id);
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
```
|
|
1097
|
+
|
|
1098
|
+
## Error Handling
|
|
1099
|
+
|
|
1100
|
+
### Checking Result Success
|
|
1101
|
+
|
|
1102
|
+
```javascript
|
|
1103
|
+
gateway.transaction.sale({
|
|
1104
|
+
amount: "10.00",
|
|
1105
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
1106
|
+
options: {
|
|
1107
|
+
submitForSettlement: true
|
|
1108
|
+
}
|
|
1109
|
+
}, (err, result) => {
|
|
1110
|
+
if (err) {
|
|
1111
|
+
console.error("Error:", err);
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
if (result.success) {
|
|
1116
|
+
console.log("Transaction ID:", result.transaction.id);
|
|
1117
|
+
} else {
|
|
1118
|
+
console.log("Error:", result.message);
|
|
1119
|
+
|
|
1120
|
+
// Check validation errors
|
|
1121
|
+
if (result.errors) {
|
|
1122
|
+
console.log("Validation errors:");
|
|
1123
|
+
for (let key in result.errors.deepErrors()) {
|
|
1124
|
+
console.log(result.errors.deepErrors()[key]);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
});
|
|
1129
|
+
```
|
|
1130
|
+
|
|
1131
|
+
### Handling Validation Errors
|
|
1132
|
+
|
|
1133
|
+
```javascript
|
|
1134
|
+
gateway.customer.create({
|
|
1135
|
+
email: "invalid-email"
|
|
1136
|
+
}, (err, result) => {
|
|
1137
|
+
if (!result.success) {
|
|
1138
|
+
result.errors.deepErrors().forEach((error) => {
|
|
1139
|
+
console.log("Error:", error.code);
|
|
1140
|
+
console.log("Message:", error.message);
|
|
1141
|
+
console.log("Attribute:", error.attribute);
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
});
|
|
1145
|
+
```
|
|
1146
|
+
|
|
1147
|
+
### Promise Error Handling
|
|
1148
|
+
|
|
1149
|
+
```javascript
|
|
1150
|
+
gateway.transaction.sale({
|
|
1151
|
+
amount: "10.00",
|
|
1152
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
1153
|
+
options: {
|
|
1154
|
+
submitForSettlement: true
|
|
1155
|
+
}
|
|
1156
|
+
})
|
|
1157
|
+
.then(result => {
|
|
1158
|
+
if (result.success) {
|
|
1159
|
+
console.log("Transaction ID:", result.transaction.id);
|
|
1160
|
+
} else {
|
|
1161
|
+
console.log("Error:", result.message);
|
|
1162
|
+
}
|
|
1163
|
+
})
|
|
1164
|
+
.catch(err => {
|
|
1165
|
+
console.error("Unexpected error:", err);
|
|
1166
|
+
});
|
|
1167
|
+
```
|
|
1168
|
+
|
|
1169
|
+
## Common Patterns
|
|
1170
|
+
|
|
1171
|
+
### Complete Express.js Server Example
|
|
1172
|
+
|
|
1173
|
+
```javascript
|
|
1174
|
+
const express = require('express');
|
|
1175
|
+
const bodyParser = require('body-parser');
|
|
1176
|
+
const braintree = require('braintree');
|
|
1177
|
+
|
|
1178
|
+
const app = express();
|
|
1179
|
+
app.use(bodyParser.json());
|
|
1180
|
+
app.use(bodyParser.urlencoded({ extended: false }));
|
|
1181
|
+
|
|
1182
|
+
const gateway = new braintree.BraintreeGateway({
|
|
1183
|
+
environment: braintree.Environment.Sandbox,
|
|
1184
|
+
merchantId: process.env.BRAINTREE_MERCHANT_ID,
|
|
1185
|
+
publicKey: process.env.BRAINTREE_PUBLIC_KEY,
|
|
1186
|
+
privateKey: process.env.BRAINTREE_PRIVATE_KEY
|
|
1187
|
+
});
|
|
1188
|
+
|
|
1189
|
+
// Generate client token
|
|
1190
|
+
app.get('/client_token', (req, res) => {
|
|
1191
|
+
gateway.clientToken.generate({}, (err, response) => {
|
|
1192
|
+
if (err) {
|
|
1193
|
+
res.status(500).send(err);
|
|
1194
|
+
} else {
|
|
1195
|
+
res.send(response.clientToken);
|
|
1196
|
+
}
|
|
1197
|
+
});
|
|
1198
|
+
});
|
|
1199
|
+
|
|
1200
|
+
// Process payment
|
|
1201
|
+
app.post('/checkout', (req, res) => {
|
|
1202
|
+
const nonceFromTheClient = req.body.payment_method_nonce;
|
|
1203
|
+
const amount = req.body.amount;
|
|
1204
|
+
|
|
1205
|
+
gateway.transaction.sale({
|
|
1206
|
+
amount: amount,
|
|
1207
|
+
paymentMethodNonce: nonceFromTheClient,
|
|
1208
|
+
options: {
|
|
1209
|
+
submitForSettlement: true
|
|
1210
|
+
}
|
|
1211
|
+
}, (err, result) => {
|
|
1212
|
+
if (result.success) {
|
|
1213
|
+
res.send({
|
|
1214
|
+
success: true,
|
|
1215
|
+
transactionId: result.transaction.id
|
|
1216
|
+
});
|
|
1217
|
+
} else {
|
|
1218
|
+
res.status(500).send({
|
|
1219
|
+
success: false,
|
|
1220
|
+
message: result.message
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
});
|
|
1224
|
+
});
|
|
1225
|
+
|
|
1226
|
+
app.listen(3000, () => {
|
|
1227
|
+
console.log('Server running on port 3000');
|
|
1228
|
+
});
|
|
1229
|
+
```
|
|
1230
|
+
|
|
1231
|
+
### Customer Creation with Payment Method Workflow
|
|
1232
|
+
|
|
1233
|
+
```javascript
|
|
1234
|
+
// Step 1: Create customer
|
|
1235
|
+
gateway.customer.create({
|
|
1236
|
+
firstName: "John",
|
|
1237
|
+
lastName: "Doe",
|
|
1238
|
+
email: "john.doe@example.com",
|
|
1239
|
+
paymentMethodNonce: nonceFromTheClient
|
|
1240
|
+
}, (err, result) => {
|
|
1241
|
+
if (result.success) {
|
|
1242
|
+
const customerId = result.customer.id;
|
|
1243
|
+
const paymentToken = result.customer.paymentMethods[0].token;
|
|
1244
|
+
|
|
1245
|
+
// Step 2: Create subscription
|
|
1246
|
+
gateway.subscription.create({
|
|
1247
|
+
paymentMethodToken: paymentToken,
|
|
1248
|
+
planId: "monthly_plan"
|
|
1249
|
+
}, (err, subResult) => {
|
|
1250
|
+
if (subResult.success) {
|
|
1251
|
+
console.log("Subscription ID:", subResult.subscription.id);
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
});
|
|
1256
|
+
```
|
|
1257
|
+
|
|
1258
|
+
### Stored Payment Method Workflow
|
|
1259
|
+
|
|
1260
|
+
```javascript
|
|
1261
|
+
// Step 1: Get customer
|
|
1262
|
+
gateway.customer.find("customer_id", (err, customer) => {
|
|
1263
|
+
// Step 2: Use default payment method for transaction
|
|
1264
|
+
const defaultPaymentMethod = customer.paymentMethods.find(pm => pm.default);
|
|
1265
|
+
|
|
1266
|
+
gateway.transaction.sale({
|
|
1267
|
+
amount: "25.00",
|
|
1268
|
+
paymentMethodToken: defaultPaymentMethod.token,
|
|
1269
|
+
options: {
|
|
1270
|
+
submitForSettlement: true
|
|
1271
|
+
}
|
|
1272
|
+
}, (err, result) => {
|
|
1273
|
+
if (result.success) {
|
|
1274
|
+
console.log("Transaction ID:", result.transaction.id);
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
});
|
|
1278
|
+
```
|