chargebee 2.22.0 → 2.22.1
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/LICENSE +1 -1
- package/README.md +182 -36
- package/lib/chargebee.js +9 -9
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -3,61 +3,207 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/chargebee)
|
|
4
4
|
[](https://www.npmjs.com/package/chargebee)
|
|
5
5
|
|
|
6
|
-
This is the [node.js](http://nodejs.org/)
|
|
6
|
+
This is the [node.js](http://nodejs.org/) library for integrating with Chargebee. Sign up for a Chargebee account [here](https://www.chargebee.com).
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
> **Note**
|
|
9
|
+
> Chargebee now supports two API versions - [V1](https://apidocs.chargebee.com/docs/api/v1) and [V2](https://apidocs.chargebee.com/docs/api), of which V2 is the latest release and all future developments will happen in V2. This library is for <b>API version V2</b>. If you’re looking for V1, head to [chargebee-v1 branch](https://github.com/chargebee/chargebee-node/tree/chargebee-v1).
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
## Requirements
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
Node 0.6 or higher.
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
15
16
|
|
|
16
|
-
Install the latest version
|
|
17
|
+
Install the latest version of the library with:
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
```sh
|
|
20
|
+
npm install chargebee
|
|
21
|
+
# or
|
|
22
|
+
yarn add chargebee
|
|
23
|
+
# or
|
|
24
|
+
pnpm install chargebee
|
|
25
|
+
```
|
|
19
26
|
|
|
20
|
-
|
|
27
|
+
## Usage
|
|
21
28
|
|
|
22
|
-
|
|
29
|
+
The package needs to be configured with your site's API key, which is available under Configure Chargebee Section. Refer [here](https://www.chargebee.com/docs/2.0/api_keys.html) for more details.
|
|
23
30
|
|
|
24
|
-
|
|
31
|
+
```js
|
|
32
|
+
var chargebee = require('chargebee');
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```node
|
|
30
|
-
var chargebee = require("chargebee");
|
|
31
|
-
chargebee.configure({site : "{site}", api_key : "{site_api_key}"})
|
|
32
|
-
chargebee.customer.create({
|
|
33
|
-
first_name : "John",
|
|
34
|
-
last_name : "Doe",
|
|
35
|
-
email : "john@test.com"
|
|
36
|
-
})
|
|
37
|
-
.setIdempotencyKey("<<UUID>>") // Replace <<UUID>> with a unique string
|
|
38
|
-
.request(function(error,result, headers) {
|
|
39
|
-
if(error){
|
|
40
|
-
//handle error
|
|
41
|
-
console.log(error);
|
|
42
|
-
}else{
|
|
43
|
-
console.log(result);
|
|
44
|
-
console.log(headers); // Retrieves response headers
|
|
45
|
-
console.log(isIdempotencyReplayed); // Retrieves idempotency replayed header value
|
|
46
|
-
var customer = result.customer;
|
|
47
|
-
var card = result.card;
|
|
48
|
-
}
|
|
34
|
+
chargebee.configure({
|
|
35
|
+
site: 'YOUR_SITE_NAME',
|
|
36
|
+
api_key: 'YOUR_API_KEY',
|
|
49
37
|
});
|
|
50
38
|
```
|
|
39
|
+
|
|
40
|
+
### Using Async / Await
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
try {
|
|
44
|
+
const result = await chargebee.customer
|
|
45
|
+
.create({
|
|
46
|
+
email: 'john@test.com',
|
|
47
|
+
// other params
|
|
48
|
+
})
|
|
49
|
+
.request();
|
|
50
|
+
// access customer as result.response.customer;
|
|
51
|
+
} catch (err) {
|
|
52
|
+
// handle error
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Using Promises
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
chargebee.customer
|
|
60
|
+
.create({
|
|
61
|
+
email: 'john@test.com',
|
|
62
|
+
// other params
|
|
63
|
+
})
|
|
64
|
+
.request()
|
|
65
|
+
.then((result) => {
|
|
66
|
+
// handle result
|
|
67
|
+
// access customer as result.response.customer;
|
|
68
|
+
})
|
|
69
|
+
.catch((err) => {
|
|
70
|
+
// handle error
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Using callbacks
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
chargebee.customer
|
|
78
|
+
.create({
|
|
79
|
+
email: 'john@test.com',
|
|
80
|
+
// other params
|
|
81
|
+
})
|
|
82
|
+
.request(function (error, result) {
|
|
83
|
+
if (error) {
|
|
84
|
+
// handle error
|
|
85
|
+
} else {
|
|
86
|
+
// handle result
|
|
87
|
+
// access customer as result.customer;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Accessing the response object
|
|
93
|
+
|
|
94
|
+
The response object returned by the `request()` method is generic response wrapper. You need to access the resource from it. For example,
|
|
95
|
+
|
|
96
|
+
- To access customer object.
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
const result = await chargebee.customer.create({ email: 'john@test.com' }).request();
|
|
100
|
+
console.log(result.response.customer);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Other resources can be accessed by the same approach. For subscription, it will be `result.subscription`
|
|
104
|
+
|
|
105
|
+
- To access list response.
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
const result = await chargebee.subscription
|
|
109
|
+
.list({
|
|
110
|
+
/* params */
|
|
111
|
+
})
|
|
112
|
+
.request();
|
|
113
|
+
|
|
114
|
+
// A list of Subscription objects
|
|
115
|
+
console.log(result.response.list.map((obj) => obj.subscription));
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Note**
|
|
119
|
+
|
|
120
|
+
If you have a `result` (or children further down the line) and are unsure what properties are available, you can use `Object.keys` to get a list of available accessor properties. Using `Object.keys` in the previous example would yield
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
// ['list', 'next_offset']
|
|
124
|
+
console.log(Object.keys(result.response));
|
|
125
|
+
// ['1', '2', '3'], e.g. `result.list` is an array with 3 entries
|
|
126
|
+
console.log(Object.keys(result.response.list));
|
|
127
|
+
// ['activated_at', 'base_currency_code', ...]
|
|
128
|
+
// ['activated_at', 'base_currency_code', ...]
|
|
129
|
+
// ['activated_at', 'base_currency_code', ...]
|
|
130
|
+
// Which means we've reached the bottom and should have all the information available from this request
|
|
131
|
+
console.log(result.response.list.map((obj) => obj.subscription));
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
#### Using filters in the List API
|
|
135
|
+
|
|
136
|
+
For pagination: `offset` is the parameter that is being used. The value used for this parameter must be the value returned for `next_offset` parameter in the previous API call.
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
const fetchCustomers = async (offset) => {
|
|
140
|
+
const result = await chargebee.customer.list({
|
|
141
|
+
limit: 2,
|
|
142
|
+
offset: offset,
|
|
143
|
+
first_name: { is: 'John' },
|
|
144
|
+
}).request();
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
customers: result.response.list.map((obj) => obj.customer),
|
|
148
|
+
next_offset: result.response.next_offset,
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const getCustomers = async () => {
|
|
153
|
+
const { customers, next_offset } = await fetchCustomers();
|
|
154
|
+
console.log('Offset:', next_offset); // Print the offset value
|
|
155
|
+
|
|
156
|
+
// Fetching next set of customers
|
|
157
|
+
await fetchCustomers(next_offset);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
getCustomers().catch((err) => {
|
|
161
|
+
console.log(err);
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### Using custom headers and custom fields:
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
const result = await chargebee.customer
|
|
169
|
+
.create({ email: 'john@test.com', cf_host_url: 'http://xyz.com' }) //Add custom field in payload
|
|
170
|
+
.headers({
|
|
171
|
+
'chargebee-event-email': 'all-disabled', // To disable webhooks
|
|
172
|
+
'chargebee-request-origin-ip': '192.168.1.2',
|
|
173
|
+
})
|
|
174
|
+
.setIdempotencyKey("safeKey")
|
|
175
|
+
.request();
|
|
176
|
+
|
|
177
|
+
const customer = result.response.customer;
|
|
178
|
+
console.log(customer.cf_host_url);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Create an idempotent request
|
|
182
|
+
|
|
183
|
+
[Idempotency keys](https://apidocs.chargebee.com/docs/api/idempotency?prod_cat_ver=2) are passed along with request headers to allow a safe retry of POST requests.
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
const result = await chargebee.customer
|
|
187
|
+
.create({ email: 'john@test.com' })
|
|
188
|
+
.setIdempotencyKey("safeKey")
|
|
189
|
+
.request();
|
|
190
|
+
const customer = result.response.customer;
|
|
191
|
+
console.log(result.headers); // Retrieves response headers
|
|
192
|
+
console.log(result.isIdempotencyReplayed); // Retrieves idempotency replayed header
|
|
193
|
+
```
|
|
194
|
+
|
|
51
195
|
`isIdempotencyReplayed()` method can be accessed to differentiate between original and replayed requests.
|
|
52
196
|
|
|
197
|
+
### Processing Webhooks - API Version Check
|
|
198
|
+
|
|
199
|
+
An attribute, <b>api_version</b>, is added to the [Event](https://apidocs.chargebee.com/docs/api/events) resource, which indicates the API version based on which the event content is structured. In your webhook servers, ensure this \_api_version* is the same as the [API version](https://apidocs.chargebee.com/docs/api#versions) used by your webhook server's client library.
|
|
200
|
+
|
|
53
201
|
## Documentation
|
|
54
202
|
|
|
55
|
-
The full documentation can be found on the
|
|
203
|
+
The full documentation can be found on the Chargebee API Docs:
|
|
56
204
|
|
|
57
205
|
[https://apidocs.chargebee.com/docs/api?lang=node](https://apidocs.chargebee.com/docs/api?lang=node)
|
|
58
206
|
|
|
59
|
-
|
|
60
207
|
## License
|
|
61
208
|
|
|
62
|
-
See the LICENSE file.
|
|
63
|
-
|
|
209
|
+
See the [LICENSE](./LICENSE) file.
|
package/lib/chargebee.js
CHANGED
|
@@ -11,7 +11,7 @@ ChargeBee._env = {
|
|
|
11
11
|
hostSuffix: '.chargebee.com',
|
|
12
12
|
apiPath: '/api/v2',
|
|
13
13
|
timeout: 80000,
|
|
14
|
-
clientVersion: 'v2.22.
|
|
14
|
+
clientVersion: 'v2.22.1',
|
|
15
15
|
port: 443,
|
|
16
16
|
timemachineWaitInMillis: 3000,
|
|
17
17
|
exportWaitInMillis: 3000
|
|
@@ -157,11 +157,11 @@ RequestWrapper.prototype.request = function(callBack, envOptions) {
|
|
|
157
157
|
if (typeof callBack !== 'undefined' && !ChargeBee._util.isFunction(callBack)) {
|
|
158
158
|
throw new Error('The callback parameter passed is incorrect.');
|
|
159
159
|
}
|
|
160
|
-
function callBackWrapper(err, response, headers) {
|
|
160
|
+
function callBackWrapper(err, response, headers, isIdempotencyReplayed) {
|
|
161
161
|
if (err) {
|
|
162
162
|
deferred.reject(err);
|
|
163
163
|
} else {
|
|
164
|
-
deferred.resolve({'response': response, 'headers': headers});
|
|
164
|
+
deferred.resolve({'response': response, 'headers': headers, 'isIdempotencyReplayed' : isIdempotencyReplayed});
|
|
165
165
|
}
|
|
166
166
|
};
|
|
167
167
|
ChargeBee._core.makeApiRequest(env, callBackWrapper, this.apiCall.httpMethod, this.apiCall.urlPrefix, this.apiCall.urlSuffix, urlIdParam, params, this.httpHeaders, this.apiCall.isListReq);
|
|
@@ -236,7 +236,11 @@ ChargeBee._core = (function() {
|
|
|
236
236
|
response.http_status_code = res.statusCode;
|
|
237
237
|
callBack(response, null, res.headers);
|
|
238
238
|
} else {
|
|
239
|
-
|
|
239
|
+
isIdempotencyReplayed = false
|
|
240
|
+
if(typeof res.headers[IDEMPOTENCY_REPLAY_HEADER] !== 'undefined' && res.headers[IDEMPOTENCY_REPLAY_HEADER] !== ''){
|
|
241
|
+
isIdempotencyReplayed = res.headers[IDEMPOTENCY_REPLAY_HEADER];
|
|
242
|
+
}
|
|
243
|
+
callBack(null, response, res.headers, isIdempotencyReplayed);
|
|
240
244
|
}
|
|
241
245
|
});
|
|
242
246
|
};
|
|
@@ -470,11 +474,7 @@ ChargeBee._util = (function() {
|
|
|
470
474
|
if (callback) {
|
|
471
475
|
deferred.promise.then(function(res) {
|
|
472
476
|
setTimeout(function() {
|
|
473
|
-
|
|
474
|
-
if(typeof res.headers[IDEMPOTENCY_REPLAY_HEADER] !== 'undefined' && res.headers[IDEMPOTENCY_REPLAY_HEADER] !== ''){
|
|
475
|
-
isIdempotencyReplayed = res.headers[IDEMPOTENCY_REPLAY_HEADER];
|
|
476
|
-
}
|
|
477
|
-
callback(null, res.response, res.headers);
|
|
477
|
+
callback(null, res.response, res.headers, isIdempotencyReplayed);
|
|
478
478
|
}, 0);
|
|
479
479
|
}, function(err) {
|
|
480
480
|
setTimeout(function() {
|