@storecraft/payments-stripe 1.0.10 → 1.0.12

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 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
- webhook_endpoint_secret: 'whsec_.....',
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.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * @import { Config } from './types.public.js'
3
+ * @import { ENV } from '@storecraft/core';
3
4
  * @import { OrderData, PaymentGatewayStatus } from '@storecraft/core/api'
4
5
  * @import { payment_gateway } from '@storecraft/core/payments'
5
6
  * @import { ApiRequest, ApiResponse } from '@storecraft/core/rest'
@@ -31,25 +32,22 @@ export const metadata_storecraft_order_id = 'storecraft_order_id'
31
32
  */
32
33
  export class Stripe {
33
34
 
34
- /** @type {Config} */ #_config;
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
+ });
35
41
 
36
- /**
37
- *
38
- * @param {Config} config
39
- */
40
- constructor(config) {
41
- this.#_config = this.#validate_and_resolve_config(config);
42
- this.stripe = new StripeCls(
43
- this.#_config.secret_key, this.#_config.stripe_config ?? {}
44
- );
45
- }
42
+ /** @type {Config} */ #_config;
43
+ /** @type {StripeCls} */ #stripe;
46
44
 
47
45
  /**
48
46
  *
49
47
  * @param {Config} config
50
48
  */
51
- #validate_and_resolve_config(config) {
52
- config = {
49
+ constructor(config={}) {
50
+ this.#_config = {
53
51
  stripe_config: {
54
52
  httpClient: StripeCls.createFetchHttpClient()
55
53
  },
@@ -65,9 +63,21 @@ export class Stripe {
65
63
  },
66
64
  },
67
65
  ...config,
68
- }
66
+ };
67
+ }
69
68
 
70
- const is_valid = config.publishable_key && config.secret_key;
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
+ }
78
+
79
+ get stripe() {
80
+ const is_valid = this.config.publishable_key && this.config.secret_key;
71
81
 
72
82
  if(!is_valid) {
73
83
  throw new StorecraftError(
@@ -76,7 +86,11 @@ export class Stripe {
76
86
  )
77
87
  }
78
88
 
79
- return config;
89
+ this.#stripe = this.#stripe ?? new StripeCls(
90
+ this.config.secret_key, this.config.stripe_config ?? {}
91
+ );
92
+
93
+ return this.#stripe;
80
94
  }
81
95
 
82
96
  get info() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/payments-stripe",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Official Storecraft <-> Stripe integration",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
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: string;
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: string;
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