@storecraft/payments-stripe 1.0.16 → 1.2.5

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.
Files changed (3) hide show
  1. package/README.md +4 -7
  2. package/adapter.js +30 -29
  3. package/package.json +2 -3
package/README.md CHANGED
@@ -71,13 +71,10 @@ const app = new App(config)
71
71
  .withPlatform(new NodePlatform())
72
72
  .withDatabase(new MongoDB())
73
73
  .withStorage(new GoogleStorage())
74
- .withPaymentGateways(
75
- {
76
- 'stripe': new Stripe() // config can be inferred from env variables
77
- }
78
- );
79
-
80
- await app.init();
74
+ .withPaymentGateways({
75
+ stripe: new Stripe() // config can be inferred from env variables
76
+ })
77
+ .init();
81
78
 
82
79
  ```
83
80
 
package/adapter.js CHANGED
@@ -33,7 +33,7 @@ export const metadata_storecraft_order_id = 'storecraft_order_id'
33
33
  export class Stripe {
34
34
 
35
35
  /** @satisfies {ENV<Config>} */
36
- static EnvConfig = /** @type{const} */ ({
36
+ static EnvConfig = /** @type {const} */ ({
37
37
  publishable_key: 'STRIPE_PUBLISHABLE_KEY',
38
38
  secret_key: 'STRIPE_SECRET_KEY',
39
39
  webhook_endpoint_secret: 'STRIPE_WEBHOOK_SECRET',
@@ -43,7 +43,6 @@ export class Stripe {
43
43
  /** @type {StripeCls} */ #stripe;
44
44
 
45
45
  /**
46
- *
47
46
  * @param {Config} config
48
47
  */
49
48
  constructor(config={}) {
@@ -69,15 +68,18 @@ export class Stripe {
69
68
  /** @type {Impl["onInit"]} */
70
69
  onInit = (app) => {
71
70
  this.config.publishable_key ??=
72
- app.platform.env[Stripe.EnvConfig.publishable_key];
71
+ app.env[Stripe.EnvConfig.publishable_key];
72
+
73
73
  this.config.secret_key ??=
74
- app.platform.env[Stripe.EnvConfig.secret_key];
74
+ app.env[Stripe.EnvConfig.secret_key];
75
+
75
76
  this.config.webhook_endpoint_secret ??=
76
- app.platform.env[Stripe.EnvConfig.webhook_endpoint_secret];
77
+ app.env[Stripe.EnvConfig.webhook_endpoint_secret];
77
78
  }
78
79
 
79
80
  get stripe() {
80
- const is_valid = this.config.publishable_key && this.config.secret_key;
81
+ const is_valid = this.config.publishable_key &&
82
+ this.config.secret_key;
81
83
 
82
84
  if(!is_valid) {
83
85
  throw new StorecraftError(
@@ -242,7 +244,7 @@ export class Stripe {
242
244
  }
243
245
 
244
246
  if(o) { // just an intent
245
- const date = new Date(o.created).toUTCString();
247
+ const date = new Date(o.created*1000).toUTCString();
246
248
  stat.messages = [
247
249
  `A payment intent of **${fmt(o.amount)}** was initiated at ${date}`,
248
250
  `The status is \`${o.status}\` and the ID is \`${o.id}\``
@@ -276,7 +278,6 @@ export class Stripe {
276
278
 
277
279
  /**
278
280
  * @description [https://docs.stripe.com/webhooks](https://docs.stripe.com/webhooks)
279
- *
280
281
  * @type {Impl["webhook"]}
281
282
  */
282
283
  async webhook(request, response) {
@@ -284,22 +285,13 @@ export class Stripe {
284
285
 
285
286
  let event;
286
287
 
287
- try {
288
- event = await this.stripe.webhooks.constructEventAsync(
289
- request.rawBody, sig, this.config.webhook_endpoint_secret, undefined,
290
- StripeCls.createSubtleCryptoProvider()
291
- );
292
- }
293
- catch (err) {
294
- response.status = 400;
295
- response.end();
296
- console.log(err.message);
297
- return;
298
- }
299
-
300
- let order_id;
301
- /** @type {StripeCls.PaymentIntent} */
302
- let payment_intent;
288
+ event = await this.stripe.webhooks.constructEventAsync(
289
+ request.rawBody, sig, this.config.webhook_endpoint_secret, undefined,
290
+ StripeCls.createSubtleCryptoProvider()
291
+ );
292
+
293
+ /** @type {string} */
294
+ let order_id = undefined;
303
295
 
304
296
  /** @type {PaymentOptionsEnum[keyof PaymentOptionsEnum]} */
305
297
  let payment_status = PaymentOptionsEnum.unpaid;
@@ -310,8 +302,8 @@ export class Stripe {
310
302
  case 'payment_intent.payment_failed':
311
303
  case 'payment_intent.requires_action':
312
304
  case 'payment_intent.amount_capturable_updated':
313
- case 'payment_intent.canceled':
314
- payment_intent = event.data.object;
305
+ case 'payment_intent.canceled': {
306
+ const payment_intent = event.data.object;
315
307
  order_id = payment_intent.metadata[metadata_storecraft_order_id];
316
308
 
317
309
  if(payment_intent.status==='requires_capture')
@@ -324,17 +316,26 @@ export class Stripe {
324
316
  payment_status = PaymentOptionsEnum.unpaid;
325
317
  else if(payment_intent.status==='succeeded')
326
318
  payment_status = PaymentOptionsEnum.captured;
327
-
328
319
  break;
320
+ }
329
321
  case 'charge.refunded':
330
322
  case 'charge.refund.updated':
323
+ const payment_intent = event.data.object;
324
+ order_id = payment_intent.metadata[metadata_storecraft_order_id];
331
325
  payment_status = PaymentOptionsEnum.refunded;
332
-
326
+ break;
333
327
  default: {
334
- console.log(`Unhandled event type ${event.type}`);
328
+ console.log(`Stripe:: We don't handle event of type ${event.type}`);
329
+
335
330
  return undefined;
336
331
  }
337
332
  }
333
+
334
+ if(!order_id) {
335
+ throw new Error(
336
+ `No 'storecraft' 'order_id' found in metadata for event type ${event.type}`
337
+ )
338
+ }
338
339
 
339
340
  // Return a response to acknowledge receipt of the event
340
341
  response.sendJson({received: true});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/payments-stripe",
3
- "version": "1.0.16",
3
+ "version": "1.2.5",
4
4
  "description": "Official Storecraft <-> Stripe integration",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
@@ -21,8 +21,7 @@
21
21
  "types": "types.public.d.ts",
22
22
  "scripts": {
23
23
  "payments-stripe:test": "uvu -c",
24
- "test": "npm run payments-stripe:test",
25
- "prepublishOnly": "npm version patch --force"
24
+ "test": "npm run payments-stripe:test"
26
25
  },
27
26
  "dependencies": {
28
27
  "stripe": "^16.6.0"