@storecraft/payments-stripe 1.0.17 → 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 +23 -17
  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(
@@ -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) {
@@ -288,10 +289,9 @@ export class Stripe {
288
289
  request.rawBody, sig, this.config.webhook_endpoint_secret, undefined,
289
290
  StripeCls.createSubtleCryptoProvider()
290
291
  );
291
-
292
- let order_id;
293
- /** @type {StripeCls.PaymentIntent} */
294
- let payment_intent;
292
+
293
+ /** @type {string} */
294
+ let order_id = undefined;
295
295
 
296
296
  /** @type {PaymentOptionsEnum[keyof PaymentOptionsEnum]} */
297
297
  let payment_status = PaymentOptionsEnum.unpaid;
@@ -302,8 +302,8 @@ export class Stripe {
302
302
  case 'payment_intent.payment_failed':
303
303
  case 'payment_intent.requires_action':
304
304
  case 'payment_intent.amount_capturable_updated':
305
- case 'payment_intent.canceled':
306
- payment_intent = event.data.object;
305
+ case 'payment_intent.canceled': {
306
+ const payment_intent = event.data.object;
307
307
  order_id = payment_intent.metadata[metadata_storecraft_order_id];
308
308
 
309
309
  if(payment_intent.status==='requires_capture')
@@ -316,20 +316,26 @@ export class Stripe {
316
316
  payment_status = PaymentOptionsEnum.unpaid;
317
317
  else if(payment_intent.status==='succeeded')
318
318
  payment_status = PaymentOptionsEnum.captured;
319
-
320
319
  break;
320
+ }
321
321
  case 'charge.refunded':
322
322
  case 'charge.refund.updated':
323
+ const payment_intent = event.data.object;
324
+ order_id = payment_intent.metadata[metadata_storecraft_order_id];
323
325
  payment_status = PaymentOptionsEnum.refunded;
324
326
  break;
325
327
  default: {
326
- console.log(`Unhandled event type ${event.type}`);
327
-
328
-
328
+ console.log(`Stripe:: We don't handle event of type ${event.type}`);
329
329
 
330
330
  return undefined;
331
331
  }
332
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
+ }
333
339
 
334
340
  // Return a response to acknowledge receipt of the event
335
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.17",
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"