chargebee 2.21.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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ### v2.22.0 (2023-05-16)
2
+ * * *
3
+
4
+ #### New Feature:
5
+ * Added setIdempotencyKey("UUID") utility to pass **Idempotency key** along with request headers to allow a safe retry of POST requests.
6
+ * Added isIdempotencyReplayed utility to differentiate between original and replayed requests.
7
+ * Added headers utility to fetch the response headers.
8
+
9
+
1
10
  ### v2.21.0 (2023-04-28)
2
11
  * * *
3
12
 
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2011-2022 ChargeBee, Inc.
3
+ Copyright (c) 2011-2023 ChargeBee, Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person
6
6
  obtaining a copy of this software and associated documentation
package/README.md CHANGED
@@ -3,32 +3,207 @@
3
3
  [![npm](https://img.shields.io/npm/v/chargebee.svg?maxAge=2592000)](https://www.npmjs.com/package/chargebee)
4
4
  [![npm](https://img.shields.io/npm/dt/chargebee.svg?maxAge=2592000)](https://www.npmjs.com/package/chargebee)
5
5
 
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).
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
- 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).
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
- ### Processing Webhooks - API Version Check
11
+ ## Requirements
11
12
 
12
- 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.
13
+ Node 0.6 or higher.
13
14
 
14
15
  ## Installation
15
16
 
16
- Install the latest version 2.x.x of the library with the following commands:
17
-
18
- $ npm install chargebee@">=2 <3"
17
+ Install the latest version of the library with:
18
+
19
+ ```sh
20
+ npm install chargebee
21
+ # or
22
+ yarn add chargebee
23
+ # or
24
+ pnpm install chargebee
25
+ ```
26
+
27
+ ## Usage
28
+
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.
30
+
31
+ ```js
32
+ var chargebee = require('chargebee');
33
+
34
+ chargebee.configure({
35
+ site: 'YOUR_SITE_NAME',
36
+ api_key: 'YOUR_API_KEY',
37
+ });
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
+
195
+ `isIdempotencyReplayed()` method can be accessed to differentiate between original and replayed requests.
19
196
 
20
- Then require the library as:
197
+ ### Processing Webhooks - API Version Check
21
198
 
22
- var chargebee = require('chargebee');
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.
23
200
 
24
201
  ## Documentation
25
202
 
26
- The full documentation can be found on the chargebee site here:
203
+ The full documentation can be found on the Chargebee API Docs:
27
204
 
28
205
  [https://apidocs.chargebee.com/docs/api?lang=node](https://apidocs.chargebee.com/docs/api?lang=node)
29
206
 
30
-
31
207
  ## License
32
208
 
33
- See the LICENSE file.
34
-
209
+ See the [LICENSE](./LICENSE) file.
package/lib/chargebee.js CHANGED
@@ -3,12 +3,15 @@ var ChargeBee = {};
3
3
  var Q = require("q");
4
4
  var os = require("os");
5
5
  var Buffer = require("safer-buffer").Buffer;
6
+ const IDEMPOTENCY_HEADER = "chargebee-idempotency-key";
7
+ const IDEMPOTENCY_REPLAY_HEADER = 'chargebee-idempotency-replayed';
8
+
6
9
  ChargeBee._env = {
7
10
  protocol: 'https',
8
11
  hostSuffix: '.chargebee.com',
9
12
  apiPath: '/api/v2',
10
13
  timeout: 80000,
11
- clientVersion: 'v2.21.0',
14
+ clientVersion: 'v2.22.1',
12
15
  port: 443,
13
16
  timemachineWaitInMillis: 3000,
14
17
  exportWaitInMillis: 3000
@@ -24,6 +27,12 @@ ChargeBee.updateRequestTimeoutInMillis = function(timeout) {
24
27
 
25
28
  ChargeBee._endpoints = require('./resources/api_endpoints.js');
26
29
 
30
+ RequestWrapper.prototype.setIdempotencyKey = function(idempotencyKey) {
31
+ this.headers({[IDEMPOTENCY_HEADER]: idempotencyKey});
32
+ return this;
33
+ };
34
+
35
+
27
36
 
28
37
  ChargeBee._waitToProcessComplete = function(retrieveHandling){
29
38
  if(typeof retrieveHandling == 'undefined' || !ChargeBee._util.isFunction(retrieveHandling)){
@@ -131,6 +140,7 @@ RequestWrapper.prototype.headers = function(headers) {
131
140
  return this;
132
141
  };
133
142
 
143
+
134
144
  RequestWrapper.prototype.request = function(callBack, envOptions) {
135
145
  var env = {};
136
146
  var jsonConstructor = {}.constructor;
@@ -147,11 +157,11 @@ RequestWrapper.prototype.request = function(callBack, envOptions) {
147
157
  if (typeof callBack !== 'undefined' && !ChargeBee._util.isFunction(callBack)) {
148
158
  throw new Error('The callback parameter passed is incorrect.');
149
159
  }
150
- function callBackWrapper(err, response) {
160
+ function callBackWrapper(err, response, headers, isIdempotencyReplayed) {
151
161
  if (err) {
152
162
  deferred.reject(err);
153
163
  } else {
154
- deferred.resolve(response);
164
+ deferred.resolve({'response': response, 'headers': headers, 'isIdempotencyReplayed' : isIdempotencyReplayed});
155
165
  }
156
166
  };
157
167
  ChargeBee._core.makeApiRequest(env, callBackWrapper, this.apiCall.httpMethod, this.apiCall.urlPrefix, this.apiCall.urlSuffix, urlIdParam, params, this.httpHeaders, this.apiCall.isListReq);
@@ -224,9 +234,13 @@ ChargeBee._core = (function() {
224
234
  }
225
235
  if (res.statusCode < 200 || res.statusCode > 299) {
226
236
  response.http_status_code = res.statusCode;
227
- callBack(response, null);
237
+ callBack(response, null, res.headers);
228
238
  } else {
229
- callBack(null, response);
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);
230
244
  }
231
245
  });
232
246
  };
@@ -460,7 +474,7 @@ ChargeBee._util = (function() {
460
474
  if (callback) {
461
475
  deferred.promise.then(function(res) {
462
476
  setTimeout(function() {
463
- callback(null, res);
477
+ callback(null, res.response, res.headers, isIdempotencyReplayed);
464
478
  }, 0);
465
479
  }, function(err) {
466
480
  setTimeout(function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name":"chargebee",
3
- "version":"2.21.0",
3
+ "version":"2.22.1",
4
4
  "description":"A library for integrating with ChargeBee.",
5
5
  "keywords":[
6
6
  "payments",