@storecraft/payments-stripe 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -13
- package/adapter.html.js +7 -2
- package/adapter.js +51 -42
- package/package.json +1 -1
- package/types.public.d.ts +7 -5
package/README.md
CHANGED
@@ -19,14 +19,15 @@ import { Stripe } from '@storecraft/payments-stripe';
|
|
19
19
|
import { Stripe as StripeCls } from 'stripe';
|
20
20
|
|
21
21
|
const config = {
|
22
|
-
//`stripe` publishable key
|
23
|
-
publishable_key: 'pk_....',
|
22
|
+
//`stripe` publishable key. if missing, it will be inferred from env variable
|
23
|
+
publishable_key: env.process.STRIPE_PUBLISHABLE_KEY // 'pk_....',
|
24
24
|
|
25
|
-
// `stripe` private secret
|
26
|
-
secret: 'sk_.....',
|
25
|
+
// `stripe` private secret. if missing, it will be inferred from env variable
|
26
|
+
secret: process.env.STRIPE_SECRET_KEY //'sk_.....',
|
27
27
|
|
28
|
-
// (Optional) `stripe` private `webhook` secret
|
29
|
-
|
28
|
+
// (Optional) `stripe` private `webhook` secret.
|
29
|
+
// if missing, it will be inferred from env variable
|
30
|
+
webhook_endpoint_secret: process.env.STRIPE_WEBHOOK_SECRET // 'whsec_.....',
|
30
31
|
|
31
32
|
// config options for `stripe`
|
32
33
|
stripe_config: {
|
@@ -49,6 +50,12 @@ const config = {
|
|
49
50
|
}
|
50
51
|
|
51
52
|
new Stripe(config);
|
53
|
+
|
54
|
+
// or, env variables will be inferred by
|
55
|
+
// - `STRIPE_PUBLISHABLE_KEY`
|
56
|
+
// - `STRIPE_SECRET_KEY`
|
57
|
+
// - `STRIPE_WEBHOOK_SECRET`
|
58
|
+
new Stripe();
|
52
59
|
```
|
53
60
|
|
54
61
|
## In Storecraft App
|
@@ -66,13 +73,7 @@ const app = new App(config)
|
|
66
73
|
.withStorage(new GoogleStorage())
|
67
74
|
.withPaymentGateways(
|
68
75
|
{
|
69
|
-
'stripe': new Stripe(
|
70
|
-
{
|
71
|
-
publishable_key: process.env.STRIPE_PUBLISHABLE_KEY,
|
72
|
-
secret_key: process.env.STRIPE_SECRET_KEY,
|
73
|
-
webhook_endpoint_secret: process.env.STRIPE_WEBHOOK_SECRET
|
74
|
-
}
|
75
|
-
),
|
76
|
+
'stripe': new Stripe() // config can be inferred from env variables
|
76
77
|
}
|
77
78
|
);
|
78
79
|
|
package/adapter.html.js
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* @import { Config } from './types.public.js'
|
3
|
+
* @import { OrderData } from '@storecraft/core/api'
|
4
|
+
*/
|
5
|
+
|
1
6
|
/**
|
2
7
|
*
|
3
8
|
* @description Official `Stripe` UI integration with `storecraft`.
|
@@ -11,8 +16,8 @@
|
|
11
16
|
* - Expiry date: 12/2027
|
12
17
|
* - CVC code: 897
|
13
18
|
*
|
14
|
-
* @param {
|
15
|
-
* @param {Partial<
|
19
|
+
* @param {Config} config
|
20
|
+
* @param {Partial<OrderData>} order_data
|
16
21
|
*/
|
17
22
|
export default function html_buy_ui(config, order_data) {
|
18
23
|
|
package/adapter.js
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* @import { Config } from './types.public.js'
|
3
|
+
* @import { ENV } from '@storecraft/core';
|
4
|
+
* @import { OrderData, PaymentGatewayStatus } from '@storecraft/core/api'
|
5
|
+
* @import { payment_gateway } from '@storecraft/core/payments'
|
6
|
+
* @import { ApiRequest, ApiResponse } from '@storecraft/core/rest'
|
7
|
+
* @import { } from './types.private.js'
|
8
|
+
*/
|
9
|
+
|
1
10
|
import {
|
2
11
|
CheckoutStatusEnum, PaymentOptionsEnum
|
3
12
|
} from '@storecraft/core/api/types.api.enums.js';
|
@@ -7,11 +16,7 @@ import { Stripe as StripeCls } from 'stripe'
|
|
7
16
|
|
8
17
|
/**
|
9
18
|
* @typedef {StripeCls.Response<StripeCls.PaymentIntent>} CheckoutCreateResult
|
10
|
-
* @typedef {
|
11
|
-
* @typedef {import('@storecraft/core/api').CheckoutStatusEnum} CheckoutStatusOptions
|
12
|
-
* @typedef {import('@storecraft/core/api').OrderData} OrderData
|
13
|
-
* @typedef {import('./types.public.d.ts').Config} Config
|
14
|
-
* @typedef {import('@storecraft/core/payments').payment_gateway<Config, CheckoutCreateResult>} payment_gateway
|
19
|
+
* @typedef {payment_gateway<Config, CheckoutCreateResult>} Impl
|
15
20
|
*/
|
16
21
|
|
17
22
|
/**
|
@@ -21,31 +26,28 @@ import { Stripe as StripeCls } from 'stripe'
|
|
21
26
|
export const metadata_storecraft_order_id = 'storecraft_order_id'
|
22
27
|
|
23
28
|
/**
|
24
|
-
* @implements {
|
29
|
+
* @implements {Impl}
|
25
30
|
*
|
26
31
|
* @description **Stripe** gateway (https://docs.stripe.com/payments/place-a-hold-on-a-payment-method)
|
27
32
|
*/
|
28
33
|
export class Stripe {
|
29
34
|
|
30
|
-
/** @
|
35
|
+
/** @satisfies {ENV<Config>} */
|
36
|
+
static EnvConfig = /** @type{const} */ ({
|
37
|
+
publishable_key: 'STRIPE_PUBLISHABLE_KEY',
|
38
|
+
secret_key: 'STRIPE_SECRET_KEY',
|
39
|
+
webhook_endpoint_secret: 'STRIPE_WEBHOOK_SECRET',
|
40
|
+
});
|
31
41
|
|
32
|
-
/**
|
33
|
-
|
34
|
-
* @param {Config} config
|
35
|
-
*/
|
36
|
-
constructor(config) {
|
37
|
-
this.#_config = this.#validate_and_resolve_config(config);
|
38
|
-
this.stripe = new StripeCls(
|
39
|
-
this.#_config.secret_key, this.#_config.stripe_config ?? {}
|
40
|
-
);
|
41
|
-
}
|
42
|
+
/** @type {Config} */ #_config;
|
43
|
+
/** @type {StripeCls} */ #stripe;
|
42
44
|
|
43
45
|
/**
|
44
46
|
*
|
45
47
|
* @param {Config} config
|
46
48
|
*/
|
47
|
-
|
48
|
-
|
49
|
+
constructor(config={}) {
|
50
|
+
this.#_config = {
|
49
51
|
stripe_config: {
|
50
52
|
httpClient: StripeCls.createFetchHttpClient()
|
51
53
|
},
|
@@ -61,9 +63,21 @@ export class Stripe {
|
|
61
63
|
},
|
62
64
|
},
|
63
65
|
...config,
|
64
|
-
}
|
66
|
+
};
|
67
|
+
}
|
68
|
+
|
69
|
+
/** @type {Impl["onInit"]} */
|
70
|
+
onInit = (app) => {
|
71
|
+
this.config.publishable_key ??=
|
72
|
+
app.platform.env[Stripe.EnvConfig.publishable_key];
|
73
|
+
this.config.secret_key ??=
|
74
|
+
app.platform.env[Stripe.EnvConfig.secret_key];
|
75
|
+
this.config.webhook_endpoint_secret ??=
|
76
|
+
app.platform.env[Stripe.EnvConfig.webhook_endpoint_secret];
|
77
|
+
}
|
65
78
|
|
66
|
-
|
79
|
+
get stripe() {
|
80
|
+
const is_valid = this.config.publishable_key && this.config.secret_key;
|
67
81
|
|
68
82
|
if(!is_valid) {
|
69
83
|
throw new StorecraftError(
|
@@ -72,7 +86,11 @@ export class Stripe {
|
|
72
86
|
)
|
73
87
|
}
|
74
88
|
|
75
|
-
|
89
|
+
this.#stripe = this.#stripe ?? new StripeCls(
|
90
|
+
this.config.secret_key, this.config.stripe_config ?? {}
|
91
|
+
);
|
92
|
+
|
93
|
+
return this.#stripe;
|
76
94
|
}
|
77
95
|
|
78
96
|
get info() {
|
@@ -110,7 +128,7 @@ export class Stripe {
|
|
110
128
|
|
111
129
|
/**
|
112
130
|
*
|
113
|
-
* @type {
|
131
|
+
* @type {Impl["invokeAction"]}
|
114
132
|
*/
|
115
133
|
invokeAction(action_handle) {
|
116
134
|
switch (action_handle) {
|
@@ -129,9 +147,7 @@ export class Stripe {
|
|
129
147
|
/**
|
130
148
|
* @description (Optional) buy link ui
|
131
149
|
*
|
132
|
-
* @
|
133
|
-
*
|
134
|
-
* @return {Promise<string>} html
|
150
|
+
* @type {Impl["onBuyLinkHtml"]}
|
135
151
|
*/
|
136
152
|
async onBuyLinkHtml(order) {
|
137
153
|
|
@@ -143,9 +159,7 @@ export class Stripe {
|
|
143
159
|
/**
|
144
160
|
* @description on checkout create `hook`
|
145
161
|
*
|
146
|
-
* @
|
147
|
-
*
|
148
|
-
* @return {Promise<CheckoutCreateResult>}
|
162
|
+
* @type {Impl["onCheckoutCreate"]}
|
149
163
|
*/
|
150
164
|
async onCheckoutCreate(order) {
|
151
165
|
|
@@ -169,12 +183,10 @@ export class Stripe {
|
|
169
183
|
* client side into their servers, and then you are notified via a
|
170
184
|
* webhook.
|
171
185
|
*
|
172
|
-
* @
|
173
|
-
*
|
174
|
-
* @return {ReturnType<payment_gateway["onCheckoutComplete"]>}
|
186
|
+
* @type {Impl["onCheckoutComplete"]}
|
175
187
|
*/
|
176
188
|
async onCheckoutComplete(create_result) {
|
177
|
-
|
189
|
+
|
178
190
|
const intent = await this.stripe.paymentIntents.confirm(
|
179
191
|
create_result.id
|
180
192
|
);
|
@@ -215,11 +227,7 @@ export class Stripe {
|
|
215
227
|
/**
|
216
228
|
* @description Fetch the order and analyze it's status
|
217
229
|
*
|
218
|
-
*
|
219
|
-
* @param {CheckoutCreateResult} create_result
|
220
|
-
*
|
221
|
-
*
|
222
|
-
* @returns {Promise<PaymentGatewayStatus>}
|
230
|
+
* @type {Impl["status"]}
|
223
231
|
*/
|
224
232
|
async status(create_result) {
|
225
233
|
const o = await this.retrieve_order(create_result);
|
@@ -268,10 +276,8 @@ export class Stripe {
|
|
268
276
|
|
269
277
|
/**
|
270
278
|
* @description [https://docs.stripe.com/webhooks](https://docs.stripe.com/webhooks)
|
271
|
-
* @param {import('@storecraft/core/rest').ApiRequest} request
|
272
|
-
* @param {import('@storecraft/core/rest').ApiResponse} response
|
273
279
|
*
|
274
|
-
* @type {
|
280
|
+
* @type {Impl["webhook"]}
|
275
281
|
*/
|
276
282
|
async webhook(request, response) {
|
277
283
|
const sig = request.headers.get('Stripe-Signature');
|
@@ -295,6 +301,7 @@ export class Stripe {
|
|
295
301
|
/** @type {StripeCls.PaymentIntent} */
|
296
302
|
let payment_intent;
|
297
303
|
|
304
|
+
/** @type {PaymentOptionsEnum[keyof PaymentOptionsEnum]} */
|
298
305
|
let payment_status = PaymentOptionsEnum.unpaid;
|
299
306
|
|
300
307
|
// Handle the event
|
@@ -323,8 +330,10 @@ export class Stripe {
|
|
323
330
|
case 'charge.refund.updated':
|
324
331
|
payment_status = PaymentOptionsEnum.refunded;
|
325
332
|
|
326
|
-
default:
|
333
|
+
default: {
|
327
334
|
console.log(`Unhandled event type ${event.type}`);
|
335
|
+
return undefined;
|
336
|
+
}
|
328
337
|
}
|
329
338
|
|
330
339
|
// Return a response to acknowledge receipt of the event
|
package/package.json
CHANGED
package/types.public.d.ts
CHANGED
@@ -7,19 +7,21 @@ import type { Stripe as StripeCls } from 'stripe'
|
|
7
7
|
export type Config = {
|
8
8
|
|
9
9
|
/**
|
10
|
-
* @description `stripe` publishable key
|
10
|
+
* @description `stripe` publishable key. If missing, will be inferred from env variable `STRIPE_PUBLISHABLE_KEY`
|
11
11
|
*/
|
12
|
-
publishable_key
|
12
|
+
publishable_key?: string;
|
13
13
|
|
14
14
|
/**
|
15
|
-
* @description `stripe` private secret
|
15
|
+
* @description `stripe` private secret. If missing, will be inferred from env variable `STRIPE_SECRET_KEY`
|
16
16
|
*/
|
17
|
-
secret_key
|
17
|
+
secret_key?: string;
|
18
18
|
|
19
19
|
/**
|
20
20
|
* @description (Optional) `webhook` Endpoint private secret in case
|
21
21
|
* you are configuring webhook for async payments
|
22
|
-
* [https://docs.stripe.com/webhooks?verify=check-signatures-library](https://docs.stripe.com/webhooks?verify=check-signatures-library)
|
22
|
+
* [https://docs.stripe.com/webhooks?verify=check-signatures-library](https://docs.stripe.com/webhooks?verify=check-signatures-library).
|
23
|
+
*
|
24
|
+
* If missing, will be inferred from env variable `STRIPE_WEBHOOK_SECRET`
|
23
25
|
*/
|
24
26
|
webhook_endpoint_secret?: string;
|
25
27
|
|