stripe 11.9.1 → 11.10.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 +19 -16
- package/VERSION +1 -1
- package/lib/Webhooks.js +144 -143
- package/lib/platform/NodePlatformFunctions.js +23 -0
- package/lib/platform/PlatformFunctions.js +48 -0
- package/lib/platform/WebPlatformFunctions.js +18 -0
- package/lib/stripe.common.js +299 -306
- package/lib/stripe.node.js +2 -15
- package/lib/stripe.worker.js +2 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 11.10.0 - 2023-02-09
|
|
4
|
+
* [#1679](https://github.com/stripe/stripe-node/pull/1679) Enable library to work in worker environments without extra configuration.
|
|
5
|
+
|
|
3
6
|
## 11.9.1 - 2023-02-03
|
|
4
7
|
* [#1672](https://github.com/stripe/stripe-node/pull/1672) Update main entrypoint on package.json
|
|
5
8
|
|
|
6
9
|
## 11.9.0 - 2023-02-02
|
|
7
10
|
* [#1669](https://github.com/stripe/stripe-node/pull/1669) API Updates
|
|
8
|
-
* Add support for `resume` method on resource `Subscription`
|
|
9
|
-
* Add support for `payment_link` on `CheckoutSessionListParams`
|
|
10
|
-
* Add support for `trial_settings` on `CheckoutSessionCreateParams.subscription_data`, `SubscriptionCreateParams`, `SubscriptionUpdateParams`, and `Subscription`
|
|
11
|
-
* Add support for `shipping_cost` on `CreditNoteCreateParams`, `CreditNotePreviewLinesParams`, `CreditNotePreviewParams`, `CreditNote`, `InvoiceCreateParams`, `InvoiceUpdateParams`, and `Invoice`
|
|
12
|
-
* Add support for `amount_shipping` on `CreditNote` and `Invoice`
|
|
13
|
-
* Add support for `shipping_details` on `InvoiceCreateParams`, `InvoiceUpdateParams`, and `Invoice`
|
|
14
|
-
* Add support for `subscription_resume_at` on `InvoiceUpcomingLinesParams` and `InvoiceUpcomingParams`
|
|
15
|
-
* Change `IssuingCardholderCreateParams.individual.first_name`, `IssuingCardholderCreateParams.individual.last_name`, `IssuingCardholderUpdateParams.individual.first_name`, and `IssuingCardholderUpdateParams.individual.last_name` to be optional
|
|
16
|
-
* Change type of `Issuing.Cardholder.individual.first_name` and `Issuing.Cardholder.individual.last_name` from `string` to `string | null`
|
|
17
|
-
* Add support for `invoice_creation` on `PaymentLinkCreateParams`, `PaymentLinkUpdateParams`, and `PaymentLink`
|
|
18
|
-
* Add support for new value `America/Ciudad_Juarez` on enum `ReportingReportRunCreateParams.parameters.timezone`
|
|
19
|
-
* Add support for new value `paused` on enum `SubscriptionListParams.status`
|
|
20
|
-
* Add support for new value `paused` on enum `Subscription.status`
|
|
21
|
-
* Add support for new values `customer.subscription.paused` and `customer.subscription.resumed` on enums `WebhookEndpointCreateParams.enabled_events[]` and `WebhookEndpointUpdateParams.enabled_events[]`
|
|
22
|
-
* Add support for new value `funding_reversed` on enum `CustomerCashBalanceTransaction.type`
|
|
23
|
-
|
|
11
|
+
* Add support for `resume` method on resource `Subscription`
|
|
12
|
+
* Add support for `payment_link` on `CheckoutSessionListParams`
|
|
13
|
+
* Add support for `trial_settings` on `CheckoutSessionCreateParams.subscription_data`, `SubscriptionCreateParams`, `SubscriptionUpdateParams`, and `Subscription`
|
|
14
|
+
* Add support for `shipping_cost` on `CreditNoteCreateParams`, `CreditNotePreviewLinesParams`, `CreditNotePreviewParams`, `CreditNote`, `InvoiceCreateParams`, `InvoiceUpdateParams`, and `Invoice`
|
|
15
|
+
* Add support for `amount_shipping` on `CreditNote` and `Invoice`
|
|
16
|
+
* Add support for `shipping_details` on `InvoiceCreateParams`, `InvoiceUpdateParams`, and `Invoice`
|
|
17
|
+
* Add support for `subscription_resume_at` on `InvoiceUpcomingLinesParams` and `InvoiceUpcomingParams`
|
|
18
|
+
* Change `IssuingCardholderCreateParams.individual.first_name`, `IssuingCardholderCreateParams.individual.last_name`, `IssuingCardholderUpdateParams.individual.first_name`, and `IssuingCardholderUpdateParams.individual.last_name` to be optional
|
|
19
|
+
* Change type of `Issuing.Cardholder.individual.first_name` and `Issuing.Cardholder.individual.last_name` from `string` to `string | null`
|
|
20
|
+
* Add support for `invoice_creation` on `PaymentLinkCreateParams`, `PaymentLinkUpdateParams`, and `PaymentLink`
|
|
21
|
+
* Add support for new value `America/Ciudad_Juarez` on enum `ReportingReportRunCreateParams.parameters.timezone`
|
|
22
|
+
* Add support for new value `paused` on enum `SubscriptionListParams.status`
|
|
23
|
+
* Add support for new value `paused` on enum `Subscription.status`
|
|
24
|
+
* Add support for new values `customer.subscription.paused` and `customer.subscription.resumed` on enums `WebhookEndpointCreateParams.enabled_events[]` and `WebhookEndpointUpdateParams.enabled_events[]`
|
|
25
|
+
* Add support for new value `funding_reversed` on enum `CustomerCashBalanceTransaction.type`
|
|
26
|
+
|
|
24
27
|
* [#1670](https://github.com/stripe/stripe-node/pull/1670) Change default entrypoint to stripe.node
|
|
25
28
|
* [#1668](https://github.com/stripe/stripe-node/pull/1668) Use EventTarget in worker / browser runtimes
|
|
26
29
|
* [#1667](https://github.com/stripe/stripe-node/pull/1667) fix: added support for TypeScript "NodeNext" module resolution
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
11.
|
|
1
|
+
11.10.0
|
package/lib/Webhooks.js
CHANGED
|
@@ -1,155 +1,156 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const _Error = require("./Error");
|
|
3
3
|
const { StripeError, StripeSignatureVerificationError } = _Error;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
4
|
+
function createWebhooks(platformFunctions) {
|
|
5
|
+
const Webhook = {
|
|
6
|
+
DEFAULT_TOLERANCE: 300,
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
signature: null,
|
|
9
|
+
constructEvent(payload, header, secret, tolerance, cryptoProvider) {
|
|
10
|
+
this.signature.verifyHeader(payload, header, secret, tolerance || Webhook.DEFAULT_TOLERANCE, cryptoProvider);
|
|
11
|
+
const jsonPayload = payload instanceof Uint8Array
|
|
12
|
+
? JSON.parse(new TextDecoder('utf8').decode(payload))
|
|
13
|
+
: JSON.parse(payload);
|
|
14
|
+
return jsonPayload;
|
|
15
|
+
},
|
|
16
|
+
async constructEventAsync(payload, header, secret, tolerance, cryptoProvider) {
|
|
17
|
+
await this.signature.verifyHeaderAsync(payload, header, secret, tolerance || Webhook.DEFAULT_TOLERANCE, cryptoProvider);
|
|
18
|
+
const jsonPayload = payload instanceof Uint8Array
|
|
19
|
+
? JSON.parse(new TextDecoder('utf8').decode(payload))
|
|
20
|
+
: JSON.parse(payload);
|
|
21
|
+
return jsonPayload;
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* Generates a header to be used for webhook mocking
|
|
25
|
+
*
|
|
26
|
+
* @typedef {object} opts
|
|
27
|
+
* @property {number} timestamp - Timestamp of the header. Defaults to Date.now()
|
|
28
|
+
* @property {string} payload - JSON stringified payload object, containing the 'id' and 'object' parameters
|
|
29
|
+
* @property {string} secret - Stripe webhook secret 'whsec_...'
|
|
30
|
+
* @property {string} scheme - Version of API to hit. Defaults to 'v1'.
|
|
31
|
+
* @property {string} signature - Computed webhook signature
|
|
32
|
+
* @property {CryptoProvider} cryptoProvider - Crypto provider to use for computing the signature if none was provided. Defaults to NodeCryptoProvider.
|
|
33
|
+
*/
|
|
34
|
+
generateTestHeaderString: function (opts) {
|
|
35
|
+
if (!opts) {
|
|
36
|
+
throw new StripeError({
|
|
37
|
+
message: 'Options are required',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
opts.timestamp =
|
|
41
|
+
Math.floor(opts.timestamp) || Math.floor(Date.now() / 1000);
|
|
42
|
+
opts.scheme = opts.scheme || signature.EXPECTED_SCHEME;
|
|
43
|
+
opts.cryptoProvider = opts.cryptoProvider || getCryptoProvider();
|
|
44
|
+
opts.signature =
|
|
45
|
+
opts.signature ||
|
|
46
|
+
opts.cryptoProvider.computeHMACSignature(opts.timestamp + '.' + opts.payload, opts.secret);
|
|
47
|
+
const generatedHeader = [
|
|
48
|
+
't=' + opts.timestamp,
|
|
49
|
+
opts.scheme + '=' + opts.signature,
|
|
50
|
+
].join(',');
|
|
51
|
+
return generatedHeader;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
const signature = {
|
|
55
|
+
EXPECTED_SCHEME: 'v1',
|
|
56
|
+
verifyHeader(encodedPayload, encodedHeader, secret, tolerance, cryptoProvider) {
|
|
57
|
+
const { decodedHeader: header, decodedPayload: payload, details, } = parseEventDetails(encodedPayload, encodedHeader, this.EXPECTED_SCHEME);
|
|
58
|
+
cryptoProvider = cryptoProvider || getCryptoProvider();
|
|
59
|
+
const expectedSignature = cryptoProvider.computeHMACSignature(makeHMACContent(payload, details), secret);
|
|
60
|
+
validateComputedSignature(payload, header, details, expectedSignature, tolerance);
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
async verifyHeaderAsync(encodedPayload, encodedHeader, secret, tolerance, cryptoProvider) {
|
|
64
|
+
const { decodedHeader: header, decodedPayload: payload, details, } = parseEventDetails(encodedPayload, encodedHeader, this.EXPECTED_SCHEME);
|
|
65
|
+
cryptoProvider = cryptoProvider || getCryptoProvider();
|
|
66
|
+
const expectedSignature = await cryptoProvider.computeHMACSignatureAsync(makeHMACContent(payload, details), secret);
|
|
67
|
+
return validateComputedSignature(payload, header, details, expectedSignature, tolerance);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
function makeHMACContent(payload, details) {
|
|
71
|
+
return `${details.timestamp}.${payload}`;
|
|
72
|
+
}
|
|
73
|
+
function parseEventDetails(encodedPayload, encodedHeader, expectedScheme) {
|
|
74
|
+
const textDecoder = new TextDecoder('utf8');
|
|
75
|
+
const decodedPayload = encodedPayload instanceof Uint8Array
|
|
76
|
+
? textDecoder.decode(encodedPayload)
|
|
77
|
+
: encodedPayload;
|
|
78
|
+
// Express's type for `Request#headers` is `string | []string`
|
|
79
|
+
// which is because the `set-cookie` header is an array,
|
|
80
|
+
// but no other headers are an array (docs: https://nodejs.org/api/http.html#http_message_headers)
|
|
81
|
+
// (Express's Request class is an extension of http.IncomingMessage, and doesn't appear to be relevantly modified: https://github.com/expressjs/express/blob/master/lib/request.js#L31)
|
|
82
|
+
if (Array.isArray(encodedHeader)) {
|
|
83
|
+
throw new Error('Unexpected: An array was passed as a header, which should not be possible for the stripe-signature header.');
|
|
84
|
+
}
|
|
85
|
+
const decodedHeader = encodedHeader instanceof Uint8Array
|
|
86
|
+
? textDecoder.decode(encodedHeader)
|
|
87
|
+
: encodedHeader;
|
|
88
|
+
const details = parseHeader(decodedHeader, expectedScheme);
|
|
89
|
+
if (!details || details.timestamp === -1) {
|
|
90
|
+
throw new StripeSignatureVerificationError(decodedHeader, decodedPayload, {
|
|
91
|
+
message: 'Unable to extract timestamp and signatures from header',
|
|
37
92
|
});
|
|
38
93
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
].join(',');
|
|
50
|
-
return generatedHeader;
|
|
51
|
-
},
|
|
52
|
-
_createCryptoProvider: () => null,
|
|
53
|
-
_platformFunctions: null,
|
|
54
|
-
};
|
|
55
|
-
const signature = {
|
|
56
|
-
EXPECTED_SCHEME: 'v1',
|
|
57
|
-
verifyHeader(encodedPayload, encodedHeader, secret, tolerance, cryptoProvider) {
|
|
58
|
-
const { decodedHeader: header, decodedPayload: payload, details, } = parseEventDetails(encodedPayload, encodedHeader, this.EXPECTED_SCHEME);
|
|
59
|
-
cryptoProvider = cryptoProvider || getCryptoProvider();
|
|
60
|
-
const expectedSignature = cryptoProvider.computeHMACSignature(makeHMACContent(payload, details), secret);
|
|
61
|
-
validateComputedSignature(payload, header, details, expectedSignature, tolerance);
|
|
62
|
-
return true;
|
|
63
|
-
},
|
|
64
|
-
async verifyHeaderAsync(encodedPayload, encodedHeader, secret, tolerance, cryptoProvider) {
|
|
65
|
-
const { decodedHeader: header, decodedPayload: payload, details, } = parseEventDetails(encodedPayload, encodedHeader, this.EXPECTED_SCHEME);
|
|
66
|
-
cryptoProvider = cryptoProvider || getCryptoProvider();
|
|
67
|
-
const expectedSignature = await cryptoProvider.computeHMACSignatureAsync(makeHMACContent(payload, details), secret);
|
|
68
|
-
return validateComputedSignature(payload, header, details, expectedSignature, tolerance);
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
function makeHMACContent(payload, details) {
|
|
72
|
-
return `${details.timestamp}.${payload}`;
|
|
73
|
-
}
|
|
74
|
-
function parseEventDetails(encodedPayload, encodedHeader, expectedScheme) {
|
|
75
|
-
const textDecoder = new TextDecoder('utf8');
|
|
76
|
-
const decodedPayload = encodedPayload instanceof Uint8Array
|
|
77
|
-
? textDecoder.decode(encodedPayload)
|
|
78
|
-
: encodedPayload;
|
|
79
|
-
// Express's type for `Request#headers` is `string | []string`
|
|
80
|
-
// which is because the `set-cookie` header is an array,
|
|
81
|
-
// but no other headers are an array (docs: https://nodejs.org/api/http.html#http_message_headers)
|
|
82
|
-
// (Express's Request class is an extension of http.IncomingMessage, and doesn't appear to be relevantly modified: https://github.com/expressjs/express/blob/master/lib/request.js#L31)
|
|
83
|
-
if (Array.isArray(encodedHeader)) {
|
|
84
|
-
throw new Error('Unexpected: An array was passed as a header, which should not be possible for the stripe-signature header.');
|
|
85
|
-
}
|
|
86
|
-
const decodedHeader = encodedHeader instanceof Uint8Array
|
|
87
|
-
? textDecoder.decode(encodedHeader)
|
|
88
|
-
: encodedHeader;
|
|
89
|
-
const details = parseHeader(decodedHeader, expectedScheme);
|
|
90
|
-
if (!details || details.timestamp === -1) {
|
|
91
|
-
throw new StripeSignatureVerificationError(decodedHeader, decodedPayload, {
|
|
92
|
-
message: 'Unable to extract timestamp and signatures from header',
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
if (!details.signatures.length) {
|
|
96
|
-
throw new StripeSignatureVerificationError(decodedHeader, decodedPayload, {
|
|
97
|
-
message: 'No signatures found with expected scheme',
|
|
98
|
-
});
|
|
94
|
+
if (!details.signatures.length) {
|
|
95
|
+
throw new StripeSignatureVerificationError(decodedHeader, decodedPayload, {
|
|
96
|
+
message: 'No signatures found with expected scheme',
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
decodedPayload,
|
|
101
|
+
decodedHeader,
|
|
102
|
+
details,
|
|
103
|
+
};
|
|
99
104
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
105
|
+
function validateComputedSignature(payload, header, details, expectedSignature, tolerance) {
|
|
106
|
+
const signatureFound = !!details.signatures.filter(platformFunctions.secureCompare.bind(platformFunctions, expectedSignature)).length;
|
|
107
|
+
if (!signatureFound) {
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
throw new StripeSignatureVerificationError(header, payload, {
|
|
110
|
+
message: 'No signatures found matching the expected signature for payload.' +
|
|
111
|
+
' Are you passing the raw request body you received from Stripe?' +
|
|
112
|
+
' https://github.com/stripe/stripe-node#webhook-signing',
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const timestampAge = Math.floor(Date.now() / 1000) - details.timestamp;
|
|
116
|
+
if (tolerance > 0 && timestampAge > tolerance) {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
throw new StripeSignatureVerificationError(header, payload, {
|
|
119
|
+
message: 'Timestamp outside the tolerance zone',
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return true;
|
|
115
123
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
124
|
+
function parseHeader(header, scheme) {
|
|
125
|
+
if (typeof header !== 'string') {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return header.split(',').reduce((accum, item) => {
|
|
129
|
+
const kv = item.split('=');
|
|
130
|
+
if (kv[0] === 't') {
|
|
131
|
+
accum.timestamp = parseInt(kv[1], 10);
|
|
132
|
+
}
|
|
133
|
+
if (kv[0] === scheme) {
|
|
134
|
+
accum.signatures.push(kv[1]);
|
|
135
|
+
}
|
|
136
|
+
return accum;
|
|
137
|
+
}, {
|
|
138
|
+
timestamp: -1,
|
|
139
|
+
signatures: [],
|
|
121
140
|
});
|
|
122
141
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (kv[0] === 't') {
|
|
132
|
-
accum.timestamp = parseInt(kv[1], 10);
|
|
133
|
-
}
|
|
134
|
-
if (kv[0] === scheme) {
|
|
135
|
-
accum.signatures.push(kv[1]);
|
|
142
|
+
let webhooksCryptoProviderInstance = null;
|
|
143
|
+
/**
|
|
144
|
+
* Lazily instantiate a CryptoProvider instance. This is a stateless object
|
|
145
|
+
* so a singleton can be used here.
|
|
146
|
+
*/
|
|
147
|
+
function getCryptoProvider() {
|
|
148
|
+
if (!webhooksCryptoProviderInstance) {
|
|
149
|
+
webhooksCryptoProviderInstance = platformFunctions.createDefaultCryptoProvider();
|
|
136
150
|
}
|
|
137
|
-
return
|
|
138
|
-
}, {
|
|
139
|
-
timestamp: -1,
|
|
140
|
-
signatures: [],
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
let webhooksCryptoProviderInstance = null;
|
|
144
|
-
/**
|
|
145
|
-
* Lazily instantiate a CryptoProvider instance. This is a stateless object
|
|
146
|
-
* so a singleton can be used here.
|
|
147
|
-
*/
|
|
148
|
-
function getCryptoProvider() {
|
|
149
|
-
if (!webhooksCryptoProviderInstance) {
|
|
150
|
-
webhooksCryptoProviderInstance = Webhook._createCryptoProvider();
|
|
151
|
+
return webhooksCryptoProviderInstance;
|
|
151
152
|
}
|
|
152
|
-
|
|
153
|
+
Webhook.signature = signature;
|
|
154
|
+
return Webhook;
|
|
153
155
|
}
|
|
154
|
-
|
|
155
|
-
module.exports = Webhook;
|
|
156
|
+
module.exports = createWebhooks;
|
|
@@ -5,6 +5,11 @@ const _Error = require("../Error");
|
|
|
5
5
|
const StripeError = _Error.StripeError;
|
|
6
6
|
const utils = require("../utils");
|
|
7
7
|
const PlatformFunctions = require("./PlatformFunctions");
|
|
8
|
+
const _HttpClient = require("../net/HttpClient");
|
|
9
|
+
const HttpClient = _HttpClient.HttpClient;
|
|
10
|
+
const _NodeHttpClient = require("../net/NodeHttpClient");
|
|
11
|
+
const NodeHttpClient = _NodeHttpClient.NodeHttpClient;
|
|
12
|
+
const NodeCryptoProvider = require("../crypto/NodeCryptoProvider");
|
|
8
13
|
class StreamProcessingError extends StripeError {
|
|
9
14
|
}
|
|
10
15
|
/**
|
|
@@ -102,5 +107,23 @@ class NodePlatformFunctions extends PlatformFunctions {
|
|
|
102
107
|
});
|
|
103
108
|
});
|
|
104
109
|
}
|
|
110
|
+
/** @override */
|
|
111
|
+
createNodeHttpClient(agent) {
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
return new NodeHttpClient(agent);
|
|
114
|
+
}
|
|
115
|
+
/** @override */
|
|
116
|
+
createDefaultHttpClient() {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
return new NodeHttpClient(null);
|
|
119
|
+
}
|
|
120
|
+
/** @override */
|
|
121
|
+
createNodeCryptoProvider() {
|
|
122
|
+
return new NodeCryptoProvider();
|
|
123
|
+
}
|
|
124
|
+
/** @override */
|
|
125
|
+
createDefaultCryptoProvider() {
|
|
126
|
+
return this.createNodeCryptoProvider();
|
|
127
|
+
}
|
|
105
128
|
}
|
|
106
129
|
module.exports = NodePlatformFunctions;
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
const _HttpClient = require("../net/HttpClient");
|
|
3
|
+
const HttpClient = _HttpClient.HttpClient;
|
|
4
|
+
const _FetchHttpClient = require("../net/FetchHttpClient");
|
|
5
|
+
const FetchHttpClient = _FetchHttpClient.FetchHttpClient;
|
|
6
|
+
const SubtleCryptoProvider = require("../crypto/SubtleCryptoProvider");
|
|
2
7
|
/**
|
|
3
8
|
* Interface encapsulating various utility functions whose
|
|
4
9
|
* implementations depend on the platform / JS runtime.
|
|
5
10
|
*/
|
|
6
11
|
class PlatformFunctions {
|
|
12
|
+
constructor() {
|
|
13
|
+
this._fetchFn = null;
|
|
14
|
+
this._agent = null;
|
|
15
|
+
}
|
|
7
16
|
/**
|
|
8
17
|
* Gets uname with Node's built-in `exec` function, if available.
|
|
9
18
|
*/
|
|
@@ -48,5 +57,44 @@ class PlatformFunctions {
|
|
|
48
57
|
tryBufferData(data) {
|
|
49
58
|
throw new Error('tryBufferData not implemented.');
|
|
50
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates an HTTP client which uses the Node `http` and `https` packages
|
|
62
|
+
* to issue requests.
|
|
63
|
+
*/
|
|
64
|
+
createNodeHttpClient(agent) {
|
|
65
|
+
throw new Error('createNodeHttpClient not implemented.');
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates an HTTP client for issuing Stripe API requests which uses the Web
|
|
69
|
+
* Fetch API.
|
|
70
|
+
*
|
|
71
|
+
* A fetch function can optionally be passed in as a parameter. If none is
|
|
72
|
+
* passed, will default to the default `fetch` function in the global scope.
|
|
73
|
+
*/
|
|
74
|
+
createFetchHttpClient(fetchFn) {
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
return new FetchHttpClient(fetchFn);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates an HTTP client using runtime-specific APIs.
|
|
80
|
+
*/
|
|
81
|
+
createDefaultHttpClient() {
|
|
82
|
+
throw new Error('createDefaultHttpClient not implemented.');
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Creates a CryptoProvider which uses the Node `crypto` package for its computations.
|
|
86
|
+
*/
|
|
87
|
+
createNodeCryptoProvider() {
|
|
88
|
+
throw new Error('createNodeCryptoProvider not implemented.');
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Creates a CryptoProvider which uses the SubtleCrypto interface of the Web Crypto API.
|
|
92
|
+
*/
|
|
93
|
+
createSubtleCryptoProvider(subtleCrypto) {
|
|
94
|
+
return new SubtleCryptoProvider(subtleCrypto);
|
|
95
|
+
}
|
|
96
|
+
createDefaultCryptoProvider() {
|
|
97
|
+
throw new Error('createDefaultCryptoProvider not implemented.');
|
|
98
|
+
}
|
|
51
99
|
}
|
|
52
100
|
module.exports = PlatformFunctions;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const StripeEmitter = require("../StripeEmitter");
|
|
3
3
|
const PlatformFunctions = require("./PlatformFunctions");
|
|
4
|
+
const _HttpClient = require("../net/HttpClient");
|
|
5
|
+
const HttpClient = _HttpClient.HttpClient;
|
|
4
6
|
/**
|
|
5
7
|
* Specializes WebPlatformFunctions using APIs available in Web workers.
|
|
6
8
|
*/
|
|
@@ -20,5 +22,21 @@ class WebPlatformFunctions extends PlatformFunctions {
|
|
|
20
22
|
}
|
|
21
23
|
return Promise.resolve(data);
|
|
22
24
|
}
|
|
25
|
+
/** @override */
|
|
26
|
+
createNodeHttpClient() {
|
|
27
|
+
throw new Error('Stripe: `createNodeHttpClient()` is not available in non-Node environments. Please use `createFetchHttpClient()` instead.');
|
|
28
|
+
}
|
|
29
|
+
/** @override */
|
|
30
|
+
createDefaultHttpClient() {
|
|
31
|
+
return super.createFetchHttpClient();
|
|
32
|
+
}
|
|
33
|
+
/** @override */
|
|
34
|
+
createNodeCryptoProvider() {
|
|
35
|
+
throw new Error('Stripe: `createNodeCryptoProvider()` is not available in non-Node environments. Please use `createSubtleCryptoProvider()` instead.');
|
|
36
|
+
}
|
|
37
|
+
/** @override */
|
|
38
|
+
createDefaultCryptoProvider() {
|
|
39
|
+
return this.createSubtleCryptoProvider();
|
|
40
|
+
}
|
|
23
41
|
}
|
|
24
42
|
module.exports = WebPlatformFunctions;
|
package/lib/stripe.common.js
CHANGED
|
@@ -6,10 +6,8 @@ const DEFAULT_PORT = '443';
|
|
|
6
6
|
const DEFAULT_BASE_PATH = '/v1/';
|
|
7
7
|
const DEFAULT_API_VERSION = null;
|
|
8
8
|
const DEFAULT_TIMEOUT = 80000;
|
|
9
|
-
Stripe.PACKAGE_VERSION = require('../package.json').version;
|
|
10
9
|
const utils = require("./utils");
|
|
11
10
|
const { determineProcessUserAgentProperties } = utils;
|
|
12
|
-
Stripe.USER_AGENT = Object.assign({ bindings_version: Stripe.PACKAGE_VERSION, lang: 'node', publisher: 'stripe', uname: null, typescript: false }, determineProcessUserAgentProperties());
|
|
13
11
|
const MAX_NETWORK_RETRY_DELAY_SEC = 2;
|
|
14
12
|
const INITIAL_NETWORK_RETRY_DELAY_SEC = 0.5;
|
|
15
13
|
const APP_INFO_PROPERTIES = ['name', 'version', 'url', 'partner_id'];
|
|
@@ -29,325 +27,320 @@ const ALLOWED_CONFIG_PROPERTIES = [
|
|
|
29
27
|
];
|
|
30
28
|
const StripeResource = require("./StripeResource");
|
|
31
29
|
const RequestSender = require("./RequestSender");
|
|
32
|
-
Stripe.StripeResource = StripeResource;
|
|
33
|
-
Stripe.resources = resources;
|
|
34
30
|
const HttpClient = require("./net/HttpClient");
|
|
35
|
-
Stripe.HttpClient = HttpClient.HttpClient;
|
|
36
|
-
Stripe.HttpClientResponse = HttpClient.HttpClientResponse;
|
|
37
31
|
const CryptoProvider = require("./crypto/CryptoProvider");
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Stripe.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
writable: false,
|
|
51
|
-
});
|
|
52
|
-
this.VERSION = Stripe.PACKAGE_VERSION;
|
|
53
|
-
this.on = this._emitter.on.bind(this._emitter);
|
|
54
|
-
this.once = this._emitter.once.bind(this._emitter);
|
|
55
|
-
this.off = this._emitter.removeListener.bind(this._emitter);
|
|
56
|
-
if (props.protocol &&
|
|
57
|
-
props.protocol !== 'https' &&
|
|
58
|
-
(!props.host || /\.stripe\.com$/.test(props.host))) {
|
|
59
|
-
throw new Error('The `https` protocol must be used when sending requests to `*.stripe.com`');
|
|
60
|
-
}
|
|
61
|
-
const agent = props.httpAgent || null;
|
|
62
|
-
this._api = {
|
|
63
|
-
auth: null,
|
|
64
|
-
host: props.host || DEFAULT_HOST,
|
|
65
|
-
port: props.port || DEFAULT_PORT,
|
|
66
|
-
protocol: props.protocol || 'https',
|
|
67
|
-
basePath: DEFAULT_BASE_PATH,
|
|
68
|
-
version: props.apiVersion || DEFAULT_API_VERSION,
|
|
69
|
-
timeout: utils.validateInteger('timeout', props.timeout, DEFAULT_TIMEOUT),
|
|
70
|
-
maxNetworkRetries: utils.validateInteger('maxNetworkRetries', props.maxNetworkRetries, 0),
|
|
71
|
-
agent: agent,
|
|
72
|
-
httpClient: props.httpClient || Stripe.createHttpClient(agent),
|
|
73
|
-
dev: false,
|
|
74
|
-
stripeAccount: props.stripeAccount || null,
|
|
75
|
-
};
|
|
76
|
-
const typescript = props.typescript || false;
|
|
77
|
-
if (typescript !== Stripe.USER_AGENT.typescript) {
|
|
78
|
-
// The mutation here is uncomfortable, but likely fastest;
|
|
79
|
-
// serializing the user agent involves shelling out to the system,
|
|
80
|
-
// and given some users may instantiate the library many times without switching between TS and non-TS,
|
|
81
|
-
// we only want to incur the performance hit when that actually happens.
|
|
82
|
-
Stripe.USER_AGENT.typescript = typescript;
|
|
83
|
-
}
|
|
84
|
-
if (props.appInfo) {
|
|
85
|
-
this._setAppInfo(props.appInfo);
|
|
86
|
-
}
|
|
87
|
-
this._prepResources();
|
|
88
|
-
this._setApiKey(key);
|
|
89
|
-
this.errors = _Error;
|
|
90
|
-
this.webhooks = require('./Webhooks');
|
|
91
|
-
this.webhooks._platformFunctions = Stripe._platformFunctions;
|
|
92
|
-
this._prevRequestMetrics = [];
|
|
93
|
-
this._enableTelemetry = props.telemetry !== false;
|
|
94
|
-
this._requestSender = new RequestSender(this, Stripe.StripeResource.MAX_BUFFERED_REQUEST_METRICS);
|
|
95
|
-
// Expose StripeResource on the instance too
|
|
96
|
-
// @ts-ignore
|
|
97
|
-
this.StripeResource = Stripe.StripeResource;
|
|
98
|
-
this._platformFunctions = Stripe._platformFunctions;
|
|
99
|
-
}
|
|
100
|
-
Stripe.errors = _Error;
|
|
101
|
-
Stripe.webhooks = require('./Webhooks');
|
|
102
|
-
// @ts-ignore
|
|
103
|
-
Stripe.createHttpClient = null;
|
|
104
|
-
Stripe.createNodeHttpClient = (agent) => {
|
|
105
|
-
throw new Error('Stripe: createNodeHttpClient() is not available in non-Node environments. When instantiating the Stripe client, please set the `httpClient` configuration option to `Stripe.createFetchHttpClient()`, or to your own implementation of `HttpClient`.');
|
|
106
|
-
};
|
|
107
|
-
/**
|
|
108
|
-
* Creates an HTTP client for issuing Stripe API requests which uses the Web
|
|
109
|
-
* Fetch API.
|
|
110
|
-
*
|
|
111
|
-
* A fetch function can optionally be passed in as a parameter. If none is
|
|
112
|
-
* passed, will default to the default `fetch` function in the global scope.
|
|
113
|
-
*/
|
|
114
|
-
Stripe.createFetchHttpClient = (fetchFn) => {
|
|
115
|
-
const { FetchHttpClient } = require('./net/FetchHttpClient');
|
|
116
|
-
return new FetchHttpClient(fetchFn);
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* Create a CryptoProvider which uses the built-in Node crypto libraries for
|
|
120
|
-
* its crypto operations.
|
|
121
|
-
*/
|
|
122
|
-
Stripe.createNodeCryptoProvider = () => {
|
|
123
|
-
throw new Error('Stripe: `createNodeCryptoProvider()` is not available in non-Node environments. Please use `createSubtleCryptoProvider()` instead.');
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Creates a CryptoProvider which uses the Subtle Crypto API from the Web
|
|
127
|
-
* Crypto API spec for its crypto operations.
|
|
128
|
-
*
|
|
129
|
-
* A SubtleCrypto interface can optionally be passed in as a parameter. If none
|
|
130
|
-
* is passed, will default to the default `crypto.subtle` object in the global
|
|
131
|
-
* scope.
|
|
132
|
-
*/
|
|
133
|
-
Stripe.createSubtleCryptoProvider = (subtleCrypto) => {
|
|
134
|
-
const SubtleCryptoProvider = require('./crypto/SubtleCryptoProvider');
|
|
135
|
-
return new SubtleCryptoProvider(subtleCrypto);
|
|
136
|
-
};
|
|
137
|
-
Stripe.prototype = {
|
|
138
|
-
// Properties are set in the constructor above
|
|
139
|
-
_appInfo: undefined,
|
|
140
|
-
on: null,
|
|
141
|
-
off: null,
|
|
142
|
-
once: null,
|
|
143
|
-
VERSION: null,
|
|
144
|
-
StripeResource: null,
|
|
145
|
-
webhooks: null,
|
|
146
|
-
errors: null,
|
|
147
|
-
_api: null,
|
|
148
|
-
_prevRequestMetrics: null,
|
|
149
|
-
_emitter: null,
|
|
150
|
-
_enableTelemetry: null,
|
|
151
|
-
_requestSender: null,
|
|
152
|
-
_platformFunctions: null,
|
|
153
|
-
/**
|
|
154
|
-
* @private
|
|
155
|
-
*/
|
|
156
|
-
_setApiKey(key) {
|
|
157
|
-
if (key) {
|
|
158
|
-
this._setApiField('auth', `Bearer ${key}`);
|
|
32
|
+
const createWebhooks = require("./Webhooks");
|
|
33
|
+
function createStripe(platformFunctions) {
|
|
34
|
+
Stripe.PACKAGE_VERSION = require('../package.json').version;
|
|
35
|
+
Stripe.USER_AGENT = Object.assign({ bindings_version: Stripe.PACKAGE_VERSION, lang: 'node', publisher: 'stripe', uname: null, typescript: false }, determineProcessUserAgentProperties());
|
|
36
|
+
Stripe.StripeResource = StripeResource;
|
|
37
|
+
Stripe.resources = resources;
|
|
38
|
+
Stripe.HttpClient = HttpClient.HttpClient;
|
|
39
|
+
Stripe.HttpClientResponse = HttpClient.HttpClientResponse;
|
|
40
|
+
Stripe.CryptoProvider = CryptoProvider;
|
|
41
|
+
function Stripe(key, config = {}) {
|
|
42
|
+
if (!(this instanceof Stripe)) {
|
|
43
|
+
return new Stripe(key, config);
|
|
159
44
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
45
|
+
const props = this._getPropsFromConfig(config);
|
|
46
|
+
this._platformFunctions = platformFunctions;
|
|
47
|
+
Object.defineProperty(this, '_emitter', {
|
|
48
|
+
value: this._platformFunctions.createEmitter(),
|
|
49
|
+
enumerable: false,
|
|
50
|
+
configurable: false,
|
|
51
|
+
writable: false,
|
|
52
|
+
});
|
|
53
|
+
this.VERSION = Stripe.PACKAGE_VERSION;
|
|
54
|
+
this.on = this._emitter.on.bind(this._emitter);
|
|
55
|
+
this.once = this._emitter.once.bind(this._emitter);
|
|
56
|
+
this.off = this._emitter.removeListener.bind(this._emitter);
|
|
57
|
+
if (props.protocol &&
|
|
58
|
+
props.protocol !== 'https' &&
|
|
59
|
+
(!props.host || /\.stripe\.com$/.test(props.host))) {
|
|
60
|
+
throw new Error('The `https` protocol must be used when sending requests to `*.stripe.com`');
|
|
168
61
|
}
|
|
169
|
-
|
|
170
|
-
|
|
62
|
+
const agent = props.httpAgent || null;
|
|
63
|
+
this._api = {
|
|
64
|
+
auth: null,
|
|
65
|
+
host: props.host || DEFAULT_HOST,
|
|
66
|
+
port: props.port || DEFAULT_PORT,
|
|
67
|
+
protocol: props.protocol || 'https',
|
|
68
|
+
basePath: DEFAULT_BASE_PATH,
|
|
69
|
+
version: props.apiVersion || DEFAULT_API_VERSION,
|
|
70
|
+
timeout: utils.validateInteger('timeout', props.timeout, DEFAULT_TIMEOUT),
|
|
71
|
+
maxNetworkRetries: utils.validateInteger('maxNetworkRetries', props.maxNetworkRetries, 0),
|
|
72
|
+
agent: agent,
|
|
73
|
+
httpClient: props.httpClient ||
|
|
74
|
+
(agent
|
|
75
|
+
? this._platformFunctions.createNodeHttpClient(agent)
|
|
76
|
+
: this._platformFunctions.createDefaultHttpClient()),
|
|
77
|
+
dev: false,
|
|
78
|
+
stripeAccount: props.stripeAccount || null,
|
|
79
|
+
};
|
|
80
|
+
const typescript = props.typescript || false;
|
|
81
|
+
if (typescript !== Stripe.USER_AGENT.typescript) {
|
|
82
|
+
// The mutation here is uncomfortable, but likely fastest;
|
|
83
|
+
// serializing the user agent involves shelling out to the system,
|
|
84
|
+
// and given some users may instantiate the library many times without switching between TS and non-TS,
|
|
85
|
+
// we only want to incur the performance hit when that actually happens.
|
|
86
|
+
Stripe.USER_AGENT.typescript = typescript;
|
|
171
87
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
88
|
+
if (props.appInfo) {
|
|
89
|
+
this._setAppInfo(props.appInfo);
|
|
90
|
+
}
|
|
91
|
+
this._prepResources();
|
|
92
|
+
this._setApiKey(key);
|
|
93
|
+
this.errors = _Error;
|
|
94
|
+
this.webhooks = createWebhooks(platformFunctions);
|
|
95
|
+
this._prevRequestMetrics = [];
|
|
96
|
+
this._enableTelemetry = props.telemetry !== false;
|
|
97
|
+
this._requestSender = new RequestSender(this, Stripe.StripeResource.MAX_BUFFERED_REQUEST_METRICS);
|
|
98
|
+
// Expose StripeResource on the instance too
|
|
180
99
|
// @ts-ignore
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
*/
|
|
187
|
-
_setApiField(key, value) {
|
|
188
|
-
this._api[key] = value;
|
|
189
|
-
},
|
|
190
|
-
/**
|
|
191
|
-
* @private
|
|
192
|
-
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
193
|
-
* if you use this, detailing your use-case.
|
|
194
|
-
*
|
|
195
|
-
* It may be deprecated and removed in the future.
|
|
196
|
-
*/
|
|
197
|
-
getApiField(key) {
|
|
198
|
-
return this._api[key];
|
|
199
|
-
},
|
|
200
|
-
setClientId(clientId) {
|
|
201
|
-
this._clientId = clientId;
|
|
202
|
-
},
|
|
203
|
-
getClientId() {
|
|
204
|
-
return this._clientId;
|
|
205
|
-
},
|
|
100
|
+
this.StripeResource = Stripe.StripeResource;
|
|
101
|
+
}
|
|
102
|
+
Stripe.errors = _Error;
|
|
103
|
+
Stripe.webhooks = require('./Webhooks');
|
|
104
|
+
Stripe.createNodeHttpClient = platformFunctions.createNodeHttpClient;
|
|
206
105
|
/**
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
* if you use this, detailing your use-case.
|
|
106
|
+
* Creates an HTTP client for issuing Stripe API requests which uses the Web
|
|
107
|
+
* Fetch API.
|
|
210
108
|
*
|
|
211
|
-
*
|
|
109
|
+
* A fetch function can optionally be passed in as a parameter. If none is
|
|
110
|
+
* passed, will default to the default `fetch` function in the global scope.
|
|
212
111
|
*/
|
|
213
|
-
|
|
214
|
-
switch (c) {
|
|
215
|
-
case 'DEFAULT_HOST':
|
|
216
|
-
return DEFAULT_HOST;
|
|
217
|
-
case 'DEFAULT_PORT':
|
|
218
|
-
return DEFAULT_PORT;
|
|
219
|
-
case 'DEFAULT_BASE_PATH':
|
|
220
|
-
return DEFAULT_BASE_PATH;
|
|
221
|
-
case 'DEFAULT_API_VERSION':
|
|
222
|
-
return DEFAULT_API_VERSION;
|
|
223
|
-
case 'DEFAULT_TIMEOUT':
|
|
224
|
-
return DEFAULT_TIMEOUT;
|
|
225
|
-
case 'MAX_NETWORK_RETRY_DELAY_SEC':
|
|
226
|
-
return MAX_NETWORK_RETRY_DELAY_SEC;
|
|
227
|
-
case 'INITIAL_NETWORK_RETRY_DELAY_SEC':
|
|
228
|
-
return INITIAL_NETWORK_RETRY_DELAY_SEC;
|
|
229
|
-
}
|
|
230
|
-
return Stripe[c];
|
|
231
|
-
},
|
|
232
|
-
getMaxNetworkRetries() {
|
|
233
|
-
return this.getApiField('maxNetworkRetries');
|
|
234
|
-
},
|
|
112
|
+
Stripe.createFetchHttpClient = platformFunctions.createFetchHttpClient;
|
|
235
113
|
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
114
|
+
* Create a CryptoProvider which uses the built-in Node crypto libraries for
|
|
115
|
+
* its crypto operations.
|
|
238
116
|
*/
|
|
239
|
-
|
|
240
|
-
const val = utils.validateInteger(prop, n, defaultVal);
|
|
241
|
-
this._setApiField(prop, val);
|
|
242
|
-
},
|
|
243
|
-
getMaxNetworkRetryDelay() {
|
|
244
|
-
return MAX_NETWORK_RETRY_DELAY_SEC;
|
|
245
|
-
},
|
|
246
|
-
getInitialNetworkRetryDelay() {
|
|
247
|
-
return INITIAL_NETWORK_RETRY_DELAY_SEC;
|
|
248
|
-
},
|
|
117
|
+
Stripe.createNodeCryptoProvider = platformFunctions.createNodeCryptoProvider;
|
|
249
118
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
* if you use this, detailing your use-case.
|
|
119
|
+
* Creates a CryptoProvider which uses the Subtle Crypto API from the Web
|
|
120
|
+
* Crypto API spec for its crypto operations.
|
|
253
121
|
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* speed advantage.
|
|
258
|
-
*/
|
|
259
|
-
getClientUserAgent(cb) {
|
|
260
|
-
return this.getClientUserAgentSeeded(Stripe.USER_AGENT, cb);
|
|
261
|
-
},
|
|
262
|
-
/**
|
|
263
|
-
* @private
|
|
264
|
-
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
265
|
-
* if you use this, detailing your use-case.
|
|
266
|
-
*
|
|
267
|
-
* It may be deprecated and removed in the future.
|
|
268
|
-
*
|
|
269
|
-
* Gets a JSON version of a User-Agent by encoding a seeded object and
|
|
270
|
-
* fetching a uname from the system.
|
|
122
|
+
* A SubtleCrypto interface can optionally be passed in as a parameter. If none
|
|
123
|
+
* is passed, will default to the default `crypto.subtle` object in the global
|
|
124
|
+
* scope.
|
|
271
125
|
*/
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
126
|
+
Stripe.createSubtleCryptoProvider =
|
|
127
|
+
platformFunctions.createSubtleCryptoProvider;
|
|
128
|
+
Stripe.prototype = {
|
|
129
|
+
// Properties are set in the constructor above
|
|
130
|
+
_appInfo: undefined,
|
|
131
|
+
on: null,
|
|
132
|
+
off: null,
|
|
133
|
+
once: null,
|
|
134
|
+
VERSION: null,
|
|
135
|
+
StripeResource: null,
|
|
136
|
+
webhooks: null,
|
|
137
|
+
errors: null,
|
|
138
|
+
_api: null,
|
|
139
|
+
_prevRequestMetrics: null,
|
|
140
|
+
_emitter: null,
|
|
141
|
+
_enableTelemetry: null,
|
|
142
|
+
_requestSender: null,
|
|
143
|
+
_platformFunctions: null,
|
|
144
|
+
/**
|
|
145
|
+
* @private
|
|
146
|
+
*/
|
|
147
|
+
_setApiKey(key) {
|
|
148
|
+
if (key) {
|
|
149
|
+
this._setApiField('auth', `Bearer ${key}`);
|
|
278
150
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
151
|
+
},
|
|
152
|
+
/**
|
|
153
|
+
* @private
|
|
154
|
+
* This may be removed in the future.
|
|
155
|
+
*/
|
|
156
|
+
_setAppInfo(info) {
|
|
157
|
+
if (info && typeof info !== 'object') {
|
|
158
|
+
throw new Error('AppInfo must be an object.');
|
|
284
159
|
}
|
|
285
|
-
if (
|
|
286
|
-
|
|
160
|
+
if (info && !info.name) {
|
|
161
|
+
throw new Error('AppInfo.name is required');
|
|
287
162
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
* It may be deprecated and removed in the future.
|
|
297
|
-
*/
|
|
298
|
-
getAppInfoAsString() {
|
|
299
|
-
if (!this._appInfo) {
|
|
300
|
-
return '';
|
|
301
|
-
}
|
|
302
|
-
let formatted = this._appInfo.name;
|
|
303
|
-
if (this._appInfo.version) {
|
|
304
|
-
formatted += `/${this._appInfo.version}`;
|
|
305
|
-
}
|
|
306
|
-
if (this._appInfo.url) {
|
|
307
|
-
formatted += ` (${this._appInfo.url})`;
|
|
308
|
-
}
|
|
309
|
-
return formatted;
|
|
310
|
-
},
|
|
311
|
-
getTelemetryEnabled() {
|
|
312
|
-
return this._enableTelemetry;
|
|
313
|
-
},
|
|
314
|
-
/**
|
|
315
|
-
* @private
|
|
316
|
-
* This may be removed in the future.
|
|
317
|
-
*/
|
|
318
|
-
_prepResources() {
|
|
319
|
-
for (const name in resources) {
|
|
163
|
+
info = info || {};
|
|
164
|
+
this._appInfo = APP_INFO_PROPERTIES.reduce((accum, prop) => {
|
|
165
|
+
if (typeof info[prop] == 'string') {
|
|
166
|
+
accum = accum || {};
|
|
167
|
+
accum[prop] = info[prop];
|
|
168
|
+
}
|
|
169
|
+
return accum;
|
|
170
|
+
},
|
|
320
171
|
// @ts-ignore
|
|
321
|
-
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
172
|
+
undefined);
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* @private
|
|
176
|
+
* This may be removed in the future.
|
|
177
|
+
*/
|
|
178
|
+
_setApiField(key, value) {
|
|
179
|
+
this._api[key] = value;
|
|
180
|
+
},
|
|
181
|
+
/**
|
|
182
|
+
* @private
|
|
183
|
+
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
184
|
+
* if you use this, detailing your use-case.
|
|
185
|
+
*
|
|
186
|
+
* It may be deprecated and removed in the future.
|
|
187
|
+
*/
|
|
188
|
+
getApiField(key) {
|
|
189
|
+
return this._api[key];
|
|
190
|
+
},
|
|
191
|
+
setClientId(clientId) {
|
|
192
|
+
this._clientId = clientId;
|
|
193
|
+
},
|
|
194
|
+
getClientId() {
|
|
195
|
+
return this._clientId;
|
|
196
|
+
},
|
|
197
|
+
/**
|
|
198
|
+
* @private
|
|
199
|
+
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
200
|
+
* if you use this, detailing your use-case.
|
|
201
|
+
*
|
|
202
|
+
* It may be deprecated and removed in the future.
|
|
203
|
+
*/
|
|
204
|
+
getConstant: (c) => {
|
|
205
|
+
switch (c) {
|
|
206
|
+
case 'DEFAULT_HOST':
|
|
207
|
+
return DEFAULT_HOST;
|
|
208
|
+
case 'DEFAULT_PORT':
|
|
209
|
+
return DEFAULT_PORT;
|
|
210
|
+
case 'DEFAULT_BASE_PATH':
|
|
211
|
+
return DEFAULT_BASE_PATH;
|
|
212
|
+
case 'DEFAULT_API_VERSION':
|
|
213
|
+
return DEFAULT_API_VERSION;
|
|
214
|
+
case 'DEFAULT_TIMEOUT':
|
|
215
|
+
return DEFAULT_TIMEOUT;
|
|
216
|
+
case 'MAX_NETWORK_RETRY_DELAY_SEC':
|
|
217
|
+
return MAX_NETWORK_RETRY_DELAY_SEC;
|
|
218
|
+
case 'INITIAL_NETWORK_RETRY_DELAY_SEC':
|
|
219
|
+
return INITIAL_NETWORK_RETRY_DELAY_SEC;
|
|
220
|
+
}
|
|
221
|
+
return Stripe[c];
|
|
222
|
+
},
|
|
223
|
+
getMaxNetworkRetries() {
|
|
224
|
+
return this.getApiField('maxNetworkRetries');
|
|
225
|
+
},
|
|
226
|
+
/**
|
|
227
|
+
* @private
|
|
228
|
+
* This may be removed in the future.
|
|
229
|
+
*/
|
|
230
|
+
_setApiNumberField(prop, n, defaultVal) {
|
|
231
|
+
const val = utils.validateInteger(prop, n, defaultVal);
|
|
232
|
+
this._setApiField(prop, val);
|
|
233
|
+
},
|
|
234
|
+
getMaxNetworkRetryDelay() {
|
|
235
|
+
return MAX_NETWORK_RETRY_DELAY_SEC;
|
|
236
|
+
},
|
|
237
|
+
getInitialNetworkRetryDelay() {
|
|
238
|
+
return INITIAL_NETWORK_RETRY_DELAY_SEC;
|
|
239
|
+
},
|
|
240
|
+
/**
|
|
241
|
+
* @private
|
|
242
|
+
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
243
|
+
* if you use this, detailing your use-case.
|
|
244
|
+
*
|
|
245
|
+
* It may be deprecated and removed in the future.
|
|
246
|
+
*
|
|
247
|
+
* Gets a JSON version of a User-Agent and uses a cached version for a slight
|
|
248
|
+
* speed advantage.
|
|
249
|
+
*/
|
|
250
|
+
getClientUserAgent(cb) {
|
|
251
|
+
return this.getClientUserAgentSeeded(Stripe.USER_AGENT, cb);
|
|
252
|
+
},
|
|
253
|
+
/**
|
|
254
|
+
* @private
|
|
255
|
+
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
256
|
+
* if you use this, detailing your use-case.
|
|
257
|
+
*
|
|
258
|
+
* It may be deprecated and removed in the future.
|
|
259
|
+
*
|
|
260
|
+
* Gets a JSON version of a User-Agent by encoding a seeded object and
|
|
261
|
+
* fetching a uname from the system.
|
|
262
|
+
*/
|
|
263
|
+
getClientUserAgentSeeded(seed, cb) {
|
|
264
|
+
this._platformFunctions.getUname().then((uname) => {
|
|
265
|
+
var _a;
|
|
266
|
+
const userAgent = {};
|
|
267
|
+
for (const field in seed) {
|
|
268
|
+
userAgent[field] = encodeURIComponent((_a = seed[field]) !== null && _a !== void 0 ? _a : 'null');
|
|
269
|
+
}
|
|
270
|
+
// URI-encode in case there are unusual characters in the system's uname.
|
|
271
|
+
userAgent.uname = encodeURIComponent(uname || 'UNKNOWN');
|
|
272
|
+
const client = this.getApiField('httpClient');
|
|
273
|
+
if (client) {
|
|
274
|
+
userAgent.httplib = encodeURIComponent(client.getClientName());
|
|
275
|
+
}
|
|
276
|
+
if (this._appInfo) {
|
|
277
|
+
userAgent.application = this._appInfo;
|
|
278
|
+
}
|
|
279
|
+
cb(JSON.stringify(userAgent));
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
/**
|
|
283
|
+
* @private
|
|
284
|
+
* Please open or upvote an issue at github.com/stripe/stripe-node
|
|
285
|
+
* if you use this, detailing your use-case.
|
|
286
|
+
*
|
|
287
|
+
* It may be deprecated and removed in the future.
|
|
288
|
+
*/
|
|
289
|
+
getAppInfoAsString() {
|
|
290
|
+
if (!this._appInfo) {
|
|
291
|
+
return '';
|
|
292
|
+
}
|
|
293
|
+
let formatted = this._appInfo.name;
|
|
294
|
+
if (this._appInfo.version) {
|
|
295
|
+
formatted += `/${this._appInfo.version}`;
|
|
296
|
+
}
|
|
297
|
+
if (this._appInfo.url) {
|
|
298
|
+
formatted += ` (${this._appInfo.url})`;
|
|
299
|
+
}
|
|
300
|
+
return formatted;
|
|
301
|
+
},
|
|
302
|
+
getTelemetryEnabled() {
|
|
303
|
+
return this._enableTelemetry;
|
|
304
|
+
},
|
|
305
|
+
/**
|
|
306
|
+
* @private
|
|
307
|
+
* This may be removed in the future.
|
|
308
|
+
*/
|
|
309
|
+
_prepResources() {
|
|
310
|
+
for (const name in resources) {
|
|
311
|
+
// @ts-ignore
|
|
312
|
+
this[utils.pascalToCamelCase(name)] = new resources[name](this);
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
/**
|
|
316
|
+
* @private
|
|
317
|
+
* This may be removed in the future.
|
|
318
|
+
*/
|
|
319
|
+
_getPropsFromConfig(config) {
|
|
320
|
+
// If config is null or undefined, just bail early with no props
|
|
321
|
+
if (!config) {
|
|
322
|
+
return {};
|
|
323
|
+
}
|
|
324
|
+
// config can be an object or a string
|
|
325
|
+
const isString = typeof config === 'string';
|
|
326
|
+
const isObject = config === Object(config) && !Array.isArray(config);
|
|
327
|
+
if (!isObject && !isString) {
|
|
328
|
+
throw new Error('Config must either be an object or a string');
|
|
329
|
+
}
|
|
330
|
+
// If config is a string, we assume the old behavior of passing in a string representation of the api version
|
|
331
|
+
if (isString) {
|
|
332
|
+
return {
|
|
333
|
+
apiVersion: config,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
// If config is an object, we assume the new behavior and make sure it doesn't contain any unexpected values
|
|
337
|
+
const values = Object.keys(config).filter((value) => !ALLOWED_CONFIG_PROPERTIES.includes(value));
|
|
338
|
+
if (values.length > 0) {
|
|
339
|
+
throw new Error(`Config object may only contain the following: ${ALLOWED_CONFIG_PROPERTIES.join(', ')}`);
|
|
340
|
+
}
|
|
341
|
+
return config;
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
return Stripe;
|
|
345
|
+
}
|
|
346
|
+
module.exports = { createStripe };
|
package/lib/stripe.node.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const _Stripe = require("./stripe.common");
|
|
4
4
|
const NodePlatformFunctions = require("./platform/NodePlatformFunctions");
|
|
5
|
-
Stripe
|
|
6
|
-
Stripe.webhooks._platformFunctions = Stripe._platformFunctions;
|
|
7
|
-
const createNodeHttpClient = (agent) => {
|
|
8
|
-
const { NodeHttpClient } = require('./net/NodeHttpClient');
|
|
9
|
-
return new NodeHttpClient(agent);
|
|
10
|
-
};
|
|
11
|
-
Stripe.createNodeHttpClient = createNodeHttpClient;
|
|
12
|
-
Stripe.createHttpClient = createNodeHttpClient;
|
|
13
|
-
const createNodeCryptoProvider = () => {
|
|
14
|
-
const NodeCryptoProvider = require('./crypto/NodeCryptoProvider');
|
|
15
|
-
return new NodeCryptoProvider();
|
|
16
|
-
};
|
|
17
|
-
Stripe.createNodeCryptoProvider = createNodeCryptoProvider;
|
|
18
|
-
Stripe.webhooks._createCryptoProvider = createNodeCryptoProvider;
|
|
5
|
+
const Stripe = _Stripe.createStripe(new NodePlatformFunctions());
|
|
19
6
|
module.exports = Stripe;
|
|
20
7
|
// expose constructor as a named property to enable mocking with Sinon.JS
|
|
21
8
|
module.exports.Stripe = Stripe;
|
package/lib/stripe.worker.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const _Stripe = require("./stripe.common");
|
|
4
4
|
const WebPlatformFunctions = require("./platform/WebPlatformFunctions");
|
|
5
|
-
Stripe
|
|
6
|
-
Stripe.webhooks._platformFunctions = Stripe._platformFunctions;
|
|
7
|
-
Stripe.createHttpClient = Stripe.createFetchHttpClient;
|
|
8
|
-
Stripe.webhooks._createCryptoProvider = Stripe.createSubtleCryptoProvider;
|
|
5
|
+
const Stripe = _Stripe.createStripe(new WebPlatformFunctions());
|
|
9
6
|
module.exports = Stripe;
|
|
10
7
|
// expose constructor as a named property to enable mocking with Sinon.JS
|
|
11
8
|
module.exports.Stripe = Stripe;
|