stripe 16.12.0 → 17.0.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/CHANGELOG.md +46 -0
- package/README.md +34 -0
- package/VERSION +1 -1
- package/cjs/Error.js +31 -4
- package/cjs/RequestSender.js +164 -63
- package/cjs/StripeResource.js +8 -3
- package/cjs/apiVersion.js +1 -1
- package/cjs/autoPagination.js +51 -7
- package/cjs/crypto/CryptoProvider.js +6 -0
- package/cjs/crypto/NodeCryptoProvider.js +7 -0
- package/cjs/crypto/SubtleCryptoProvider.js +4 -0
- package/cjs/multipart.js +1 -1
- package/cjs/resources/Billing/CreditBalanceSummary.js +12 -0
- package/cjs/resources/Billing/CreditBalanceTransactions.js +17 -0
- package/cjs/resources/Billing/CreditGrants.js +30 -0
- package/cjs/resources/OAuth.js +1 -1
- package/cjs/resources/V2/Billing/MeterEventAdjustments.js +12 -0
- package/cjs/resources/V2/Billing/MeterEventSession.js +12 -0
- package/cjs/resources/V2/Billing/MeterEventStream.js +13 -0
- package/cjs/resources/V2/Billing/MeterEvents.js +9 -0
- package/cjs/resources/V2/Billing.js +18 -0
- package/cjs/resources/V2/Core/Events.js +14 -0
- package/cjs/resources/V2/Core.js +12 -0
- package/cjs/resources/V2.js +14 -0
- package/cjs/resources.js +9 -1
- package/cjs/stripe.core.js +25 -10
- package/cjs/utils.js +64 -6
- package/esm/Error.js +27 -2
- package/esm/RequestSender.js +165 -64
- package/esm/StripeResource.js +9 -4
- package/esm/apiVersion.js +1 -1
- package/esm/autoPagination.js +52 -8
- package/esm/crypto/CryptoProvider.js +6 -0
- package/esm/crypto/NodeCryptoProvider.js +7 -0
- package/esm/crypto/SubtleCryptoProvider.js +4 -0
- package/esm/multipart.js +2 -2
- package/esm/resources/Billing/CreditBalanceSummary.js +9 -0
- package/esm/resources/Billing/CreditBalanceTransactions.js +14 -0
- package/esm/resources/Billing/CreditGrants.js +27 -0
- package/esm/resources/OAuth.js +2 -2
- package/esm/resources/V2/Billing/MeterEventAdjustments.js +9 -0
- package/esm/resources/V2/Billing/MeterEventSession.js +9 -0
- package/esm/resources/V2/Billing/MeterEventStream.js +10 -0
- package/esm/resources/V2/Billing/MeterEvents.js +6 -0
- package/esm/resources/V2/Billing.js +15 -0
- package/esm/resources/V2/Core/Events.js +11 -0
- package/esm/resources/V2/Core.js +9 -0
- package/esm/resources/V2.js +11 -0
- package/esm/resources.js +7 -0
- package/esm/stripe.core.js +26 -11
- package/esm/utils.js +59 -4
- package/package.json +1 -1
- package/types/Billing/Alerts.d.ts +17 -13
- package/types/Billing/AlertsResource.d.ts +18 -21
- package/types/Billing/CreditBalanceSummary.d.ts +94 -0
- package/types/Billing/CreditBalanceSummaryResource.d.ts +64 -0
- package/types/Billing/CreditBalanceTransactions.d.ts +159 -0
- package/types/Billing/CreditBalanceTransactionsResource.d.ts +54 -0
- package/types/Billing/CreditGrants.d.ts +124 -0
- package/types/Billing/CreditGrantsResource.d.ts +219 -0
- package/types/BillingPortal/ConfigurationsResource.d.ts +2 -2
- package/types/Capabilities.d.ts +1 -1
- package/types/Checkout/SessionsResource.d.ts +1 -1
- package/types/CreditNoteLineItems.d.ts +30 -0
- package/types/CreditNotes.d.ts +30 -0
- package/types/Customers.d.ts +2 -3
- package/types/Errors.d.ts +32 -2
- package/types/EventTypes.d.ts +2 -0
- package/types/InvoiceLineItems.d.ts +36 -0
- package/types/Invoices.d.ts +38 -0
- package/types/Margins.d.ts +56 -0
- package/types/ProductsResource.d.ts +28 -1
- package/types/PromotionCodes.d.ts +1 -1
- package/types/PromotionCodesResource.d.ts +3 -1
- package/types/SubscriptionsResource.d.ts +3 -3
- package/types/Tax/Settings.d.ts +1 -1
- package/types/Terminal/ReadersResource.d.ts +13 -4
- package/types/ThinEvent.d.ts +36 -0
- package/types/Treasury/ReceivedCredits.d.ts +5 -1
- package/types/V2/Billing/MeterEventAdjustments.d.ts +65 -0
- package/types/V2/Billing/MeterEventAdjustmentsResource.d.ts +47 -0
- package/types/V2/Billing/MeterEventSessionResource.d.ts +26 -0
- package/types/V2/Billing/MeterEventSessions.d.ts +45 -0
- package/types/V2/Billing/MeterEventStreamResource.d.ts +62 -0
- package/types/V2/Billing/MeterEvents.d.ts +54 -0
- package/types/V2/Billing/MeterEventsResource.d.ts +52 -0
- package/types/V2/BillingResource.d.ts +14 -0
- package/types/V2/Core/EventsResource.d.ts +57 -0
- package/types/V2/CoreResource.d.ts +11 -0
- package/types/V2/EventTypes.d.ts +214 -0
- package/types/V2/Events.d.ts +75 -0
- package/types/V2Resource.d.ts +10 -0
- package/types/WebhookEndpointsResource.d.ts +2 -1
- package/types/index.d.ts +87 -0
- package/types/lib.d.ts +8 -1
- package/types/test/typescriptTest.ts +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 17.0.0 - 2024-10-01
|
|
4
|
+
* [#2192](https://github.com/stripe/stripe-node/pull/2192) Support for APIs in the new API version 2024-09-30.acacia
|
|
5
|
+
|
|
6
|
+
This release changes the pinned API version to `2024-09-30.acacia`. Please read the [API Upgrade Guide](https://stripe.com/docs/upgrades#2024-09-30.acacia) and carefully review the API changes before upgrading.
|
|
7
|
+
|
|
8
|
+
### ⚠️ Breaking changes due to changes in the Stripe API
|
|
9
|
+
- Rename `usage_threshold_config` to `usage_threshold` on `Billing.AlertCreateParams` and `Billing.Alert`
|
|
10
|
+
- Remove support for `filter` on `Billing.AlertCreateParams` and `Billing.Alert`. Use the filters on the `usage_threshold` instead
|
|
11
|
+
- Remove support for `customer_consent_collected` on `Terminal.ReaderProcessSetupIntentParams`.
|
|
12
|
+
|
|
13
|
+
### ⚠️ Other Breaking changes in the SDK
|
|
14
|
+
- Adjusted default values around reties for HTTP requests. You can use the old defaults by setting them explicitly. New values are:
|
|
15
|
+
- max retries: `1` -> `2`
|
|
16
|
+
- max timeout (seconds): `2` -> `5`
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Additions
|
|
20
|
+
* Add support for `custom_unit_amount` on `ProductCreateParams.default_price_data`
|
|
21
|
+
* Add support for `allow_redisplay` on `Terminal.ReaderProcessPaymentIntentParams.process_config` and `Terminal.ReaderProcessSetupIntentParams`
|
|
22
|
+
* Add support for new value `international_transaction` on enum `Treasury.ReceivedCredit.failure_code`
|
|
23
|
+
* Add support for new value `2024-09-30.acacia` on enum `WebhookEndpointCreateParams.api_version`
|
|
24
|
+
* Add support for new Usage Billing APIs `Billing.MeterEvent`, `Billing.MeterEventAdjustments`, `Billing.MeterEventSession`, `Billing.MeterEventStream` and the new Events API `Core.Events` in the [v2 namespace ](https://docs.corp.stripe.com/api-v2-overview)
|
|
25
|
+
* Add method `parseThinEvent()` on the `Stripe` class to parse [thin events](https://docs.corp.stripe.com/event-destinations#events-overview).
|
|
26
|
+
* Add method [rawRequest()](https://github.com/stripe/stripe-node/tree/master?tab=readme-ov-file#custom-requests) on the `Stripe` class that takes a HTTP method type, url and relevant parameters to make requests to the Stripe API that are not yet supported in the SDK.
|
|
27
|
+
|
|
28
|
+
### Changes
|
|
29
|
+
* Change `BillingPortal.ConfigurationCreateParams.features.subscription_update.default_allowed_updates` and `BillingPortal.ConfigurationCreateParams.features.subscription_update.products` to be optional
|
|
30
|
+
* [#2195](https://github.com/stripe/stripe-node/pull/2195) Remove parseSnapshotEvent
|
|
31
|
+
* [#2188](https://github.com/stripe/stripe-node/pull/2188) Revert "Add raw_request (#2185)"
|
|
32
|
+
* [#2185](https://github.com/stripe/stripe-node/pull/2185) Add raw_request
|
|
33
|
+
Adds the ability to make raw requests to the Stripe API, by providing an HTTP method and url.
|
|
34
|
+
|
|
35
|
+
Example:
|
|
36
|
+
```node
|
|
37
|
+
import Stripe from 'stripe';
|
|
38
|
+
const stripe = new Stripe('sk_test_...');
|
|
39
|
+
|
|
40
|
+
const response = await stripe.rawRequest(
|
|
41
|
+
'POST',
|
|
42
|
+
'/v1/beta_endpoint',
|
|
43
|
+
{ param: 123 },
|
|
44
|
+
{ apiVersion: '2022-11-15; feature_beta=v3' }
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
|
|
3
49
|
## 16.12.0 - 2024-09-18
|
|
4
50
|
* [#2177](https://github.com/stripe/stripe-node/pull/2177) Update generated code
|
|
5
51
|
* Add support for new value `international_transaction` on enum `Treasury.ReceivedDebit.failure_code`
|
package/README.md
CHANGED
|
@@ -517,6 +517,40 @@ const stripe = new Stripe('sk_test_...', {
|
|
|
517
517
|
});
|
|
518
518
|
```
|
|
519
519
|
|
|
520
|
+
### Custom requests
|
|
521
|
+
|
|
522
|
+
If you would like to send a request to an undocumented API (for example you are in a private beta), or if you prefer to bypass the method definitions in the library and specify your request details directly, you can use the `rawRequest` method on the StripeClient object.
|
|
523
|
+
|
|
524
|
+
```javascript
|
|
525
|
+
const client = new Stripe('sk_test_...');
|
|
526
|
+
|
|
527
|
+
client.rawRequest(
|
|
528
|
+
'POST',
|
|
529
|
+
'/v1/beta_endpoint',
|
|
530
|
+
{ param: 123 },
|
|
531
|
+
{ apiVersion: '2022-11-15; feature_beta=v3' }
|
|
532
|
+
)
|
|
533
|
+
.then((response) => /* handle response */ )
|
|
534
|
+
.catch((error) => console.error(error));
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
Or using ES modules and `async`/`await`:
|
|
538
|
+
|
|
539
|
+
```javascript
|
|
540
|
+
import Stripe from 'stripe';
|
|
541
|
+
const stripe = new Stripe('sk_test_...');
|
|
542
|
+
|
|
543
|
+
const response = await stripe.rawRequest(
|
|
544
|
+
'POST',
|
|
545
|
+
'/v1/beta_endpoint',
|
|
546
|
+
{ param: 123 },
|
|
547
|
+
{ apiVersion: '2022-11-15; feature_beta=v3' }
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
// handle response
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
|
|
520
554
|
## Support
|
|
521
555
|
|
|
522
556
|
New features and bug fixes are released on the latest major version of the `stripe` package. If you are on an older major version, we recommend that you upgrade to the latest in order to use the new features and bug fixes including those for security vulnerabilities. Older major versions of the package will continue to be available for use, but will not be receiving any updates.
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
17.0.0
|
package/cjs/Error.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* eslint-disable camelcase */
|
|
3
|
+
/* eslint-disable no-warning-comments */
|
|
3
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.StripeUnknownError = exports.StripeInvalidGrantError = exports.StripeIdempotencyError = exports.StripeSignatureVerificationError = exports.StripeConnectionError = exports.StripeRateLimitError = exports.StripePermissionError = exports.StripeAuthenticationError = exports.StripeAPIError = exports.StripeInvalidRequestError = exports.StripeCardError = exports.StripeError = exports.
|
|
5
|
-
const
|
|
5
|
+
exports.TemporarySessionExpiredError = exports.StripeUnknownError = exports.StripeInvalidGrantError = exports.StripeIdempotencyError = exports.StripeSignatureVerificationError = exports.StripeConnectionError = exports.StripeRateLimitError = exports.StripePermissionError = exports.StripeAuthenticationError = exports.StripeAPIError = exports.StripeInvalidRequestError = exports.StripeCardError = exports.StripeError = exports.generateV2Error = exports.generateV1Error = void 0;
|
|
6
|
+
const generateV1Error = (rawStripeError) => {
|
|
6
7
|
switch (rawStripeError.type) {
|
|
7
8
|
case 'card_error':
|
|
8
9
|
return new StripeCardError(rawStripeError);
|
|
@@ -22,7 +23,24 @@ const generate = (rawStripeError) => {
|
|
|
22
23
|
return new StripeUnknownError(rawStripeError);
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
|
-
exports.
|
|
26
|
+
exports.generateV1Error = generateV1Error;
|
|
27
|
+
// eslint-disable-next-line complexity
|
|
28
|
+
const generateV2Error = (rawStripeError) => {
|
|
29
|
+
switch (rawStripeError.type) {
|
|
30
|
+
// switchCases: The beginning of the section generated from our OpenAPI spec
|
|
31
|
+
case 'temporary_session_expired':
|
|
32
|
+
return new TemporarySessionExpiredError(rawStripeError);
|
|
33
|
+
// switchCases: The end of the section generated from our OpenAPI spec
|
|
34
|
+
}
|
|
35
|
+
// Special handling for requests with missing required fields in V2 APIs.
|
|
36
|
+
// invalid_field response in V2 APIs returns the field 'code' instead of 'type'.
|
|
37
|
+
switch (rawStripeError.code) {
|
|
38
|
+
case 'invalid_fields':
|
|
39
|
+
return new StripeInvalidRequestError(rawStripeError);
|
|
40
|
+
}
|
|
41
|
+
return (0, exports.generateV1Error)(rawStripeError);
|
|
42
|
+
};
|
|
43
|
+
exports.generateV2Error = generateV2Error;
|
|
26
44
|
/**
|
|
27
45
|
* StripeError is the base error from which all other more specific Stripe errors derive.
|
|
28
46
|
* Specifically for errors returned from Stripe's REST API.
|
|
@@ -42,6 +60,7 @@ class StripeError extends Error {
|
|
|
42
60
|
this.statusCode = raw.statusCode;
|
|
43
61
|
// @ts-ignore
|
|
44
62
|
this.message = raw.message;
|
|
63
|
+
this.userMessage = raw.user_message;
|
|
45
64
|
this.charge = raw.charge;
|
|
46
65
|
this.decline_code = raw.decline_code;
|
|
47
66
|
this.payment_intent = raw.payment_intent;
|
|
@@ -55,7 +74,7 @@ exports.StripeError = StripeError;
|
|
|
55
74
|
/**
|
|
56
75
|
* Helper factory which takes raw stripe errors and outputs wrapping instances
|
|
57
76
|
*/
|
|
58
|
-
StripeError.generate = exports.
|
|
77
|
+
StripeError.generate = exports.generateV1Error;
|
|
59
78
|
// Specific Stripe Error types:
|
|
60
79
|
/**
|
|
61
80
|
* CardError is raised when a user enters a card that can't be charged for
|
|
@@ -174,3 +193,11 @@ class StripeUnknownError extends StripeError {
|
|
|
174
193
|
}
|
|
175
194
|
}
|
|
176
195
|
exports.StripeUnknownError = StripeUnknownError;
|
|
196
|
+
// classDefinitions: The beginning of the section generated from our OpenAPI spec
|
|
197
|
+
class TemporarySessionExpiredError extends StripeError {
|
|
198
|
+
constructor(rawStripeError = {}) {
|
|
199
|
+
super(rawStripeError, 'TemporarySessionExpiredError');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
exports.TemporarySessionExpiredError = TemporarySessionExpiredError;
|
|
203
|
+
// classDefinitions: The end of the section generated from our OpenAPI spec
|
package/cjs/RequestSender.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RequestSender = void 0;
|
|
4
4
|
const Error_js_1 = require("./Error.js");
|
|
5
|
-
const utils_js_1 = require("./utils.js");
|
|
6
5
|
const HttpClient_js_1 = require("./net/HttpClient.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
7
|
const MAX_RETRY_AFTER_WAIT = 60;
|
|
8
8
|
class RequestSender {
|
|
9
9
|
constructor(stripe, maxBufferedRequestMetric) {
|
|
@@ -69,7 +69,7 @@ class RequestSender {
|
|
|
69
69
|
* parses the JSON and returns it (i.e. passes it to the callback) if there
|
|
70
70
|
* is no "error" field. Otherwise constructs/passes an appropriate Error.
|
|
71
71
|
*/
|
|
72
|
-
_jsonResponseHandler(requestEvent, usage, callback) {
|
|
72
|
+
_jsonResponseHandler(requestEvent, apiMode, usage, callback) {
|
|
73
73
|
return (res) => {
|
|
74
74
|
const headers = res.getHeaders();
|
|
75
75
|
const requestId = this._getRequestId(headers);
|
|
@@ -101,8 +101,11 @@ class RequestSender {
|
|
|
101
101
|
else if (statusCode === 429) {
|
|
102
102
|
err = new Error_js_1.StripeRateLimitError(jsonResponse.error);
|
|
103
103
|
}
|
|
104
|
+
else if (apiMode === 'v2') {
|
|
105
|
+
err = (0, Error_js_1.generateV2Error)(jsonResponse.error);
|
|
106
|
+
}
|
|
104
107
|
else {
|
|
105
|
-
err = Error_js_1.
|
|
108
|
+
err = (0, Error_js_1.generateV1Error)(jsonResponse.error);
|
|
106
109
|
}
|
|
107
110
|
throw err;
|
|
108
111
|
}
|
|
@@ -174,7 +177,7 @@ class RequestSender {
|
|
|
174
177
|
// Apply exponential backoff with initialNetworkRetryDelay on the
|
|
175
178
|
// number of numRetries so far as inputs. Do not allow the number to exceed
|
|
176
179
|
// maxNetworkRetryDelay.
|
|
177
|
-
let sleepSeconds = Math.min(initialNetworkRetryDelay * Math.pow(numRetries - 1
|
|
180
|
+
let sleepSeconds = Math.min(initialNetworkRetryDelay * Math.pow(2, numRetries - 1), maxNetworkRetryDelay);
|
|
178
181
|
// Apply some jitter by randomizing the value in the range of
|
|
179
182
|
// (sleepSeconds / 2) to (sleepSeconds).
|
|
180
183
|
sleepSeconds *= 0.5 * (1 + Math.random());
|
|
@@ -193,26 +196,34 @@ class RequestSender {
|
|
|
193
196
|
? settings.maxNetworkRetries
|
|
194
197
|
: this._stripe.getMaxNetworkRetries();
|
|
195
198
|
}
|
|
196
|
-
_defaultIdempotencyKey(method, settings) {
|
|
199
|
+
_defaultIdempotencyKey(method, settings, apiMode) {
|
|
197
200
|
// If this is a POST and we allow multiple retries, ensure an idempotency key.
|
|
198
201
|
const maxRetries = this._getMaxNetworkRetries(settings);
|
|
199
|
-
|
|
200
|
-
|
|
202
|
+
const genKey = () => `stripe-node-retry-${this._stripe._platformFunctions.uuid4()}`;
|
|
203
|
+
// more verbose than it needs to be, but gives clear separation between V1 and V2 behavior
|
|
204
|
+
if (apiMode === 'v2') {
|
|
205
|
+
if (method === 'POST' || method === 'DELETE') {
|
|
206
|
+
return genKey();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (apiMode === 'v1') {
|
|
210
|
+
if (method === 'POST' && maxRetries > 0) {
|
|
211
|
+
return genKey();
|
|
212
|
+
}
|
|
201
213
|
}
|
|
202
214
|
return null;
|
|
203
215
|
}
|
|
204
|
-
_makeHeaders(
|
|
216
|
+
_makeHeaders({ contentType, contentLength, apiVersion, clientUserAgent, method, userSuppliedHeaders, userSuppliedSettings, stripeAccount, stripeContext, apiMode, }) {
|
|
205
217
|
const defaultHeaders = {
|
|
206
|
-
// Use specified auth token or use default from this stripe instance:
|
|
207
|
-
Authorization: auth ? `Bearer ${auth}` : this._stripe.getApiField('auth'),
|
|
208
218
|
Accept: 'application/json',
|
|
209
|
-
'Content-Type':
|
|
210
|
-
'User-Agent': this._getUserAgentString(),
|
|
219
|
+
'Content-Type': contentType,
|
|
220
|
+
'User-Agent': this._getUserAgentString(apiMode),
|
|
211
221
|
'X-Stripe-Client-User-Agent': clientUserAgent,
|
|
212
222
|
'X-Stripe-Client-Telemetry': this._getTelemetryHeader(),
|
|
213
223
|
'Stripe-Version': apiVersion,
|
|
214
|
-
'Stripe-Account':
|
|
215
|
-
'
|
|
224
|
+
'Stripe-Account': stripeAccount,
|
|
225
|
+
'Stripe-Context': stripeContext,
|
|
226
|
+
'Idempotency-Key': this._defaultIdempotencyKey(method, userSuppliedSettings, apiMode),
|
|
216
227
|
};
|
|
217
228
|
// As per https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2:
|
|
218
229
|
// A user agent SHOULD send a Content-Length in a request message when
|
|
@@ -241,12 +252,12 @@ class RequestSender {
|
|
|
241
252
|
// If the user supplied, say 'idempotency-key', override instead of appending by ensuring caps are the same.
|
|
242
253
|
(0, utils_js_1.normalizeHeaders)(userSuppliedHeaders));
|
|
243
254
|
}
|
|
244
|
-
_getUserAgentString() {
|
|
255
|
+
_getUserAgentString(apiMode) {
|
|
245
256
|
const packageVersion = this._stripe.getConstant('PACKAGE_VERSION');
|
|
246
257
|
const appInfo = this._stripe._appInfo
|
|
247
258
|
? this._stripe.getAppInfoAsString()
|
|
248
259
|
: '';
|
|
249
|
-
return `Stripe
|
|
260
|
+
return `Stripe/${apiMode} NodeBindings/${packageVersion} ${appInfo}`.trim();
|
|
250
261
|
}
|
|
251
262
|
_getTelemetryHeader() {
|
|
252
263
|
if (this._stripe.getTelemetryEnabled() &&
|
|
@@ -274,8 +285,61 @@ class RequestSender {
|
|
|
274
285
|
}
|
|
275
286
|
}
|
|
276
287
|
}
|
|
277
|
-
|
|
288
|
+
_rawRequest(method, path, params, options) {
|
|
289
|
+
const requestPromise = new Promise((resolve, reject) => {
|
|
290
|
+
let opts;
|
|
291
|
+
try {
|
|
292
|
+
const requestMethod = method.toUpperCase();
|
|
293
|
+
if (requestMethod !== 'POST' &&
|
|
294
|
+
params &&
|
|
295
|
+
Object.keys(params).length !== 0) {
|
|
296
|
+
throw new Error('rawRequest only supports params on POST requests. Please pass null and add your parameters to path.');
|
|
297
|
+
}
|
|
298
|
+
const args = [].slice.call([params, options]);
|
|
299
|
+
// Pull request data and options (headers, auth) from args.
|
|
300
|
+
const dataFromArgs = (0, utils_js_1.getDataFromArgs)(args);
|
|
301
|
+
const data = Object.assign({}, dataFromArgs);
|
|
302
|
+
const calculatedOptions = (0, utils_js_1.getOptionsFromArgs)(args);
|
|
303
|
+
const headers = calculatedOptions.headers;
|
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
305
|
+
const authenticator = calculatedOptions.authenticator;
|
|
306
|
+
opts = {
|
|
307
|
+
requestMethod,
|
|
308
|
+
requestPath: path,
|
|
309
|
+
bodyData: data,
|
|
310
|
+
queryData: {},
|
|
311
|
+
authenticator,
|
|
312
|
+
headers,
|
|
313
|
+
host: null,
|
|
314
|
+
streaming: false,
|
|
315
|
+
settings: {},
|
|
316
|
+
usage: ['raw_request'],
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
catch (err) {
|
|
320
|
+
reject(err);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
function requestCallback(err, response) {
|
|
324
|
+
if (err) {
|
|
325
|
+
reject(err);
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
resolve(response);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const { headers, settings } = opts;
|
|
332
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
333
|
+
const authenticator = opts.authenticator;
|
|
334
|
+
this._request(opts.requestMethod, opts.host, path, opts.bodyData, authenticator, { headers, settings, streaming: opts.streaming }, opts.usage, requestCallback);
|
|
335
|
+
});
|
|
336
|
+
return requestPromise;
|
|
337
|
+
}
|
|
338
|
+
_request(method, host, path, data, authenticator, options, usage = [], callback, requestDataProcessor = null) {
|
|
339
|
+
var _a;
|
|
278
340
|
let requestData;
|
|
341
|
+
authenticator = (_a = authenticator !== null && authenticator !== void 0 ? authenticator : this._stripe._authenticator) !== null && _a !== void 0 ? _a : null;
|
|
342
|
+
const apiMode = (0, utils_js_1.getAPIMode)(path);
|
|
279
343
|
const retryRequest = (requestFn, apiVersion, headers, requestRetries, retryAfter) => {
|
|
280
344
|
return setTimeout(requestFn, this._getSleepTimeInMS(requestRetries, retryAfter), apiVersion, headers, requestRetries + 1);
|
|
281
345
|
};
|
|
@@ -287,50 +351,68 @@ class RequestSender {
|
|
|
287
351
|
options.settings.timeout >= 0
|
|
288
352
|
? options.settings.timeout
|
|
289
353
|
: this._stripe.getApiField('timeout');
|
|
290
|
-
const
|
|
291
|
-
.getApiField('
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
if (RequestSender._shouldRetry(null, requestRetries, maxRetries, error)) {
|
|
322
|
-
return retryRequest(makeRequest, apiVersion, headers, requestRetries, null);
|
|
323
|
-
}
|
|
324
|
-
else {
|
|
325
|
-
const isTimeoutError = error.code && error.code === HttpClient_js_1.HttpClient.TIMEOUT_ERROR_CODE;
|
|
326
|
-
return callback(new Error_js_1.StripeConnectionError({
|
|
327
|
-
message: isTimeoutError
|
|
328
|
-
? `Request aborted due to timeout being reached (${timeout}ms)`
|
|
329
|
-
: RequestSender._generateConnectionErrorMessage(requestRetries),
|
|
354
|
+
const request = {
|
|
355
|
+
host: host || this._stripe.getApiField('host'),
|
|
356
|
+
port: this._stripe.getApiField('port'),
|
|
357
|
+
path: path,
|
|
358
|
+
method: method,
|
|
359
|
+
headers: Object.assign({}, headers),
|
|
360
|
+
body: requestData,
|
|
361
|
+
protocol: this._stripe.getApiField('protocol'),
|
|
362
|
+
};
|
|
363
|
+
authenticator(request)
|
|
364
|
+
.then(() => {
|
|
365
|
+
const req = this._stripe
|
|
366
|
+
.getApiField('httpClient')
|
|
367
|
+
.makeRequest(request.host, request.port, request.path, request.method, request.headers, request.body, request.protocol, timeout);
|
|
368
|
+
const requestStartTime = Date.now();
|
|
369
|
+
// @ts-ignore
|
|
370
|
+
const requestEvent = (0, utils_js_1.removeNullish)({
|
|
371
|
+
api_version: apiVersion,
|
|
372
|
+
account: headers['Stripe-Account'],
|
|
373
|
+
idempotency_key: headers['Idempotency-Key'],
|
|
374
|
+
method,
|
|
375
|
+
path,
|
|
376
|
+
request_start_time: requestStartTime,
|
|
377
|
+
});
|
|
378
|
+
const requestRetries = numRetries || 0;
|
|
379
|
+
const maxRetries = this._getMaxNetworkRetries(options.settings || {});
|
|
380
|
+
this._stripe._emitter.emit('request', requestEvent);
|
|
381
|
+
req
|
|
382
|
+
.then((res) => {
|
|
383
|
+
if (RequestSender._shouldRetry(res, requestRetries, maxRetries)) {
|
|
384
|
+
return retryRequest(makeRequest, apiVersion, headers, requestRetries,
|
|
330
385
|
// @ts-ignore
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
|
|
386
|
+
res.getHeaders()['retry-after']);
|
|
387
|
+
}
|
|
388
|
+
else if (options.streaming && res.getStatusCode() < 400) {
|
|
389
|
+
return this._streamingResponseHandler(requestEvent, usage, callback)(res);
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
return this._jsonResponseHandler(requestEvent, apiMode, usage, callback)(res);
|
|
393
|
+
}
|
|
394
|
+
})
|
|
395
|
+
.catch((error) => {
|
|
396
|
+
if (RequestSender._shouldRetry(null, requestRetries, maxRetries, error)) {
|
|
397
|
+
return retryRequest(makeRequest, apiVersion, headers, requestRetries, null);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
const isTimeoutError = error.code && error.code === HttpClient_js_1.HttpClient.TIMEOUT_ERROR_CODE;
|
|
401
|
+
return callback(new Error_js_1.StripeConnectionError({
|
|
402
|
+
message: isTimeoutError
|
|
403
|
+
? `Request aborted due to timeout being reached (${timeout}ms)`
|
|
404
|
+
: RequestSender._generateConnectionErrorMessage(requestRetries),
|
|
405
|
+
// @ts-ignore
|
|
406
|
+
detail: error,
|
|
407
|
+
}));
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
})
|
|
411
|
+
.catch((e) => {
|
|
412
|
+
throw new Error_js_1.StripeError({
|
|
413
|
+
message: 'Unable to authenticate the request',
|
|
414
|
+
exception: e,
|
|
415
|
+
});
|
|
334
416
|
});
|
|
335
417
|
};
|
|
336
418
|
const prepareAndMakeRequest = (error, data) => {
|
|
@@ -339,9 +421,21 @@ class RequestSender {
|
|
|
339
421
|
}
|
|
340
422
|
requestData = data;
|
|
341
423
|
this._stripe.getClientUserAgent((clientUserAgent) => {
|
|
342
|
-
var _a, _b;
|
|
343
424
|
const apiVersion = this._stripe.getApiField('version');
|
|
344
|
-
const headers = this._makeHeaders(
|
|
425
|
+
const headers = this._makeHeaders({
|
|
426
|
+
contentType: apiMode == 'v2'
|
|
427
|
+
? 'application/json'
|
|
428
|
+
: 'application/x-www-form-urlencoded',
|
|
429
|
+
contentLength: requestData.length,
|
|
430
|
+
apiVersion: apiVersion,
|
|
431
|
+
clientUserAgent,
|
|
432
|
+
method,
|
|
433
|
+
userSuppliedHeaders: options.headers,
|
|
434
|
+
userSuppliedSettings: options.settings,
|
|
435
|
+
stripeAccount: apiMode == 'v2' ? null : this._stripe.getApiField('stripeAccount'),
|
|
436
|
+
stripeContext: apiMode == 'v2' ? this._stripe.getApiField('stripeContext') : null,
|
|
437
|
+
apiMode: apiMode,
|
|
438
|
+
});
|
|
345
439
|
makeRequest(apiVersion, headers, 0);
|
|
346
440
|
});
|
|
347
441
|
};
|
|
@@ -349,7 +443,14 @@ class RequestSender {
|
|
|
349
443
|
requestDataProcessor(method, data, options.headers, prepareAndMakeRequest);
|
|
350
444
|
}
|
|
351
445
|
else {
|
|
352
|
-
|
|
446
|
+
let stringifiedData;
|
|
447
|
+
if (apiMode == 'v2') {
|
|
448
|
+
stringifiedData = data ? (0, utils_js_1.jsonStringifyRequestData)(data) : '';
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
stringifiedData = (0, utils_js_1.queryStringifyRequestData)(data || {}, apiMode);
|
|
452
|
+
}
|
|
453
|
+
prepareAndMakeRequest(null, stringifiedData);
|
|
353
454
|
}
|
|
354
455
|
}
|
|
355
456
|
}
|
package/cjs/StripeResource.js
CHANGED
|
@@ -79,6 +79,7 @@ StripeResource.prototype = {
|
|
|
79
79
|
return parts.join('/').replace(/\/{2,}/g, '/');
|
|
80
80
|
},
|
|
81
81
|
_getRequestOpts(requestArgs, spec, overrideData) {
|
|
82
|
+
var _a;
|
|
82
83
|
// Extract spec values with defaults.
|
|
83
84
|
const requestMethod = (spec.method || 'GET').toUpperCase();
|
|
84
85
|
const usage = spec.usage || [];
|
|
@@ -129,7 +130,7 @@ StripeResource.prototype = {
|
|
|
129
130
|
requestPath,
|
|
130
131
|
bodyData,
|
|
131
132
|
queryData,
|
|
132
|
-
|
|
133
|
+
authenticator: (_a = options.authenticator) !== null && _a !== void 0 ? _a : null,
|
|
133
134
|
headers,
|
|
134
135
|
host: host !== null && host !== void 0 ? host : null,
|
|
135
136
|
streaming,
|
|
@@ -162,10 +163,14 @@ StripeResource.prototype = {
|
|
|
162
163
|
const path = [
|
|
163
164
|
opts.requestPath,
|
|
164
165
|
emptyQuery ? '' : '?',
|
|
165
|
-
(0, utils_js_1.
|
|
166
|
+
(0, utils_js_1.queryStringifyRequestData)(opts.queryData, (0, utils_js_1.getAPIMode)(opts.requestPath)),
|
|
166
167
|
].join('');
|
|
167
168
|
const { headers, settings } = opts;
|
|
168
|
-
this._stripe._requestSender._request(opts.requestMethod, opts.host, path, opts.bodyData, opts.
|
|
169
|
+
this._stripe._requestSender._request(opts.requestMethod, opts.host, path, opts.bodyData, opts.authenticator, {
|
|
170
|
+
headers,
|
|
171
|
+
settings,
|
|
172
|
+
streaming: opts.streaming,
|
|
173
|
+
}, opts.usage, requestCallback, (_a = this.requestDataProcessor) === null || _a === void 0 ? void 0 : _a.bind(this));
|
|
169
174
|
});
|
|
170
175
|
},
|
|
171
176
|
};
|
package/cjs/apiVersion.js
CHANGED
package/cjs/autoPagination.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.makeAutoPaginationMethods = void 0;
|
|
4
4
|
const utils_js_1 = require("./utils.js");
|
|
5
|
-
class
|
|
5
|
+
class V1Iterator {
|
|
6
6
|
constructor(firstPagePromise, requestArgs, spec, stripeResource) {
|
|
7
7
|
this.index = 0;
|
|
8
8
|
this.pagePromise = firstPagePromise;
|
|
@@ -60,7 +60,7 @@ class StripeIterator {
|
|
|
60
60
|
return nextPromise;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
class
|
|
63
|
+
class V1ListIterator extends V1Iterator {
|
|
64
64
|
getNextPage(pageResult) {
|
|
65
65
|
const reverseIteration = isReverseIteration(this.requestArgs);
|
|
66
66
|
const lastId = getLastId(pageResult, reverseIteration);
|
|
@@ -69,7 +69,7 @@ class ListIterator extends StripeIterator {
|
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
-
class
|
|
72
|
+
class V1SearchIterator extends V1Iterator {
|
|
73
73
|
getNextPage(pageResult) {
|
|
74
74
|
if (!pageResult.next_page) {
|
|
75
75
|
throw Error('Unexpected: Stripe API response does not have a well-formed `next_page` field, but `has_more` was true.');
|
|
@@ -79,12 +79,56 @@ class SearchIterator extends StripeIterator {
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
+
class V2ListIterator {
|
|
83
|
+
constructor(firstPagePromise, requestArgs, spec, stripeResource) {
|
|
84
|
+
this.currentPageIterator = (async () => {
|
|
85
|
+
const page = await firstPagePromise;
|
|
86
|
+
return page.data[Symbol.iterator]();
|
|
87
|
+
})();
|
|
88
|
+
this.nextPageUrl = (async () => {
|
|
89
|
+
const page = await firstPagePromise;
|
|
90
|
+
return page.next_page_url || null;
|
|
91
|
+
})();
|
|
92
|
+
this.requestArgs = requestArgs;
|
|
93
|
+
this.spec = spec;
|
|
94
|
+
this.stripeResource = stripeResource;
|
|
95
|
+
}
|
|
96
|
+
async turnPage() {
|
|
97
|
+
const nextPageUrl = await this.nextPageUrl;
|
|
98
|
+
if (!nextPageUrl)
|
|
99
|
+
return null;
|
|
100
|
+
this.spec.fullPath = nextPageUrl;
|
|
101
|
+
const page = await this.stripeResource._makeRequest([], this.spec, {});
|
|
102
|
+
this.nextPageUrl = Promise.resolve(page.next_page_url);
|
|
103
|
+
this.currentPageIterator = Promise.resolve(page.data[Symbol.iterator]());
|
|
104
|
+
return this.currentPageIterator;
|
|
105
|
+
}
|
|
106
|
+
async next() {
|
|
107
|
+
{
|
|
108
|
+
const result = (await this.currentPageIterator).next();
|
|
109
|
+
if (!result.done)
|
|
110
|
+
return { done: false, value: result.value };
|
|
111
|
+
}
|
|
112
|
+
const nextPageIterator = await this.turnPage();
|
|
113
|
+
if (!nextPageIterator) {
|
|
114
|
+
return { done: true, value: undefined };
|
|
115
|
+
}
|
|
116
|
+
const result = nextPageIterator.next();
|
|
117
|
+
if (!result.done)
|
|
118
|
+
return { done: false, value: result.value };
|
|
119
|
+
return { done: true, value: undefined };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
82
122
|
const makeAutoPaginationMethods = (stripeResource, requestArgs, spec, firstPagePromise) => {
|
|
83
|
-
|
|
84
|
-
|
|
123
|
+
const apiMode = (0, utils_js_1.getAPIMode)(spec.fullPath || spec.path);
|
|
124
|
+
if (apiMode !== 'v2' && spec.methodType === 'search') {
|
|
125
|
+
return makeAutoPaginationMethodsFromIterator(new V1SearchIterator(firstPagePromise, requestArgs, spec, stripeResource));
|
|
126
|
+
}
|
|
127
|
+
if (apiMode !== 'v2' && spec.methodType === 'list') {
|
|
128
|
+
return makeAutoPaginationMethodsFromIterator(new V1ListIterator(firstPagePromise, requestArgs, spec, stripeResource));
|
|
85
129
|
}
|
|
86
|
-
if (spec.methodType === 'list') {
|
|
87
|
-
return makeAutoPaginationMethodsFromIterator(new
|
|
130
|
+
if (apiMode === 'v2' && spec.methodType === 'list') {
|
|
131
|
+
return makeAutoPaginationMethodsFromIterator(new V2ListIterator(firstPagePromise, requestArgs, spec, stripeResource));
|
|
88
132
|
}
|
|
89
133
|
return null;
|
|
90
134
|
};
|
|
@@ -31,6 +31,12 @@ class CryptoProvider {
|
|
|
31
31
|
computeHMACSignatureAsync(payload, secret) {
|
|
32
32
|
throw new Error('computeHMACSignatureAsync not implemented.');
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Computes a SHA-256 hash of the data.
|
|
36
|
+
*/
|
|
37
|
+
computeSHA256Async(data) {
|
|
38
|
+
throw new Error('computeSHA256 not implemented.');
|
|
39
|
+
}
|
|
34
40
|
}
|
|
35
41
|
exports.CryptoProvider = CryptoProvider;
|
|
36
42
|
/**
|
|
@@ -19,5 +19,12 @@ class NodeCryptoProvider extends CryptoProvider_js_1.CryptoProvider {
|
|
|
19
19
|
const signature = await this.computeHMACSignature(payload, secret);
|
|
20
20
|
return signature;
|
|
21
21
|
}
|
|
22
|
+
/** @override */
|
|
23
|
+
async computeSHA256Async(data) {
|
|
24
|
+
return new Uint8Array(await crypto
|
|
25
|
+
.createHash('sha256')
|
|
26
|
+
.update(data)
|
|
27
|
+
.digest());
|
|
28
|
+
}
|
|
22
29
|
}
|
|
23
30
|
exports.NodeCryptoProvider = NodeCryptoProvider;
|
|
@@ -37,6 +37,10 @@ class SubtleCryptoProvider extends CryptoProvider_js_1.CryptoProvider {
|
|
|
37
37
|
}
|
|
38
38
|
return signatureHexCodes.join('');
|
|
39
39
|
}
|
|
40
|
+
/** @override */
|
|
41
|
+
async computeSHA256Async(data) {
|
|
42
|
+
return new Uint8Array(await this.subtleCrypto.digest('SHA-256', data));
|
|
43
|
+
}
|
|
40
44
|
}
|
|
41
45
|
exports.SubtleCryptoProvider = SubtleCryptoProvider;
|
|
42
46
|
// Cached mapping of byte to hex representation. We do this once to avoid re-
|
package/cjs/multipart.js
CHANGED
|
@@ -48,7 +48,7 @@ const multipartDataGenerator = (method, data, headers) => {
|
|
|
48
48
|
function multipartRequestDataProcessor(method, data, headers, callback) {
|
|
49
49
|
data = data || {};
|
|
50
50
|
if (method !== 'POST') {
|
|
51
|
-
return callback(null, (0, utils_js_1.
|
|
51
|
+
return callback(null, (0, utils_js_1.queryStringifyRequestData)(data));
|
|
52
52
|
}
|
|
53
53
|
this._stripe._platformFunctions
|
|
54
54
|
.tryBufferData(data)
|