@storecraft/payments-paypal 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,13 +19,13 @@ npm i @storecraft/payments-paypal
19
19
 
20
20
  ## Howto
21
21
 
22
- ```js
23
- import { PayPal } from '@storecraft/payments-paypal';
22
+ ```ts
23
+ import { PayPal, Config } from '@storecraft/payments-paypal';
24
24
 
25
- const config = {
25
+ const config: Config = {
26
26
  env: 'prod',
27
- client_id: '<get-from-your-paypal-dashboard>',
28
- secret: '<get-from-your-paypal-dashboard>',
27
+ client_id: process.env.PAYPAL_CLIENT_ID,
28
+ secret: env.process.PAYPAL_SECRET,
29
29
  currency_code: 'USD',
30
30
  intent_on_checkout: 'AUTHORIZE'
31
31
  }
@@ -48,13 +48,7 @@ const app = new App(config)
48
48
  .withStorage(new GoogleStorage())
49
49
  .withPaymentGateways(
50
50
  {
51
- 'paypal_standard_prod': new Paypal(
52
- {
53
- client_id: process.env.PAYPAL_CLIENT_ID,
54
- secret: process.env.PAYPAL_SECRET,
55
- env: 'prod'
56
- }
57
- ),
51
+ 'paypal_standard_prod': new Paypal() // config can be inferred from env variables
58
52
  }
59
53
  );
60
54
 
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 { paypal_order, paypal_order_request } from './types.private.js'
@@ -16,6 +17,7 @@ import html_buy_ui from './adapter.html.js';
16
17
  * @typedef {payment_gateway<Config, CreateResult>} Impl
17
18
  */
18
19
 
20
+
19
21
  /**
20
22
  * @description **Paypal Payment** gateway (https://developer.paypal.com/docs/checkout/)
21
23
  *
@@ -23,29 +25,46 @@ import html_buy_ui from './adapter.html.js';
23
25
  */
24
26
  export class Paypal {
25
27
 
26
- /** @type {Config} */ #_config;
28
+ /** @satisfies {ENV<Config>} */
29
+ static EnvConfigProd = /** @type{const} */ ({
30
+ client_id: `PAYPAL_CLIENT_ID_PROD`,
31
+ secret: 'PAYPAL_SECRET_PROD',
32
+ });
33
+
34
+ /** @satisfies {ENV<Config>} */
35
+ static EnvConfigTest = /** @type{const} */ ({
36
+ client_id: `PAYPAL_CLIENT_ID_TEST`,
37
+ secret: 'PAYPAL_SECRET_TEST',
38
+ });
27
39
 
28
- /**
29
- *
30
- * @param {Config} config
31
- */
32
- constructor(config) {
33
- this.#_config = this.#validate_and_resolve_config(config);
34
- }
40
+ /** @type {Config} */ #_config;
35
41
 
36
42
  /**
37
43
  *
38
44
  * @param {Config} config
39
45
  */
40
- #validate_and_resolve_config(config) {
41
- config = {
46
+ constructor(config={}) {
47
+ // this.#_config = this.#validate_and_resolve_config(config);
48
+ this.#_config = {
42
49
  default_currency_code: 'USD',
43
50
  env: 'prod',
44
51
  intent_on_checkout: 'AUTHORIZE',
45
52
  ...config,
46
53
  }
54
+ }
47
55
 
48
- const is_valid = config.client_id && config.secret;
56
+ /** @type {Impl["onInit"]} */
57
+ onInit = (app) => {
58
+ const is_prod = Boolean(this.config.env==='prod');
59
+ this.config.client_id ??= app.platform.env[
60
+ is_prod ? Paypal.EnvConfigProd.client_id : Paypal.EnvConfigTest.client_id
61
+ ] ?? 'PAYPAL_CLIENT_ID';
62
+
63
+ this.config.secret ??= app.platform.env[
64
+ is_prod ? Paypal.EnvConfigProd.secret : Paypal.EnvConfigTest.secret
65
+ ] ?? 'PAYPAL_SECRET';
66
+
67
+ const is_valid = this.config.client_id && this.config.secret;
49
68
 
50
69
  if(!is_valid) {
51
70
  throw new StorecraftError(
@@ -53,8 +72,6 @@ export class Paypal {
53
72
  Missing client_id or secret`
54
73
  )
55
74
  }
56
-
57
- return config;
58
75
  }
59
76
 
60
77
  get info() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/payments-paypal",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Official Paypal integration with Storecraft",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
package/types.public.d.ts CHANGED
@@ -6,26 +6,33 @@ export { Paypal } from './adapter.js';
6
6
  export type Config = {
7
7
  /**
8
8
  * @description default currency code
9
+ * @default 'USD'
9
10
  */
10
11
  default_currency_code?: string;
11
12
 
12
13
  /**
13
14
  * @description the environment
15
+ * @default 'prod'
14
16
  */
15
- env: 'prod' | 'test';
17
+ env?: 'prod' | 'test';
16
18
 
17
19
  /**
18
- * @description private client id
20
+ * @description private client id. If missing,
21
+ * - if `env===prod` -> will be inferred from env variable `PAYPAL_CLIENT_ID_PROD` or `PAYPAL_CLIENT_ID`
22
+ * - if `env===test` -> will be inferred from env variable `PAYPAL_CLIENT_ID_TEST` or `PAYPAL_CLIENT_ID`
19
23
  */
20
- client_id: string;
24
+ client_id?: string;
21
25
 
22
26
  /**
23
- * @description private secret
27
+ * @description private secret. If missing,
28
+ * - if `env===prod` -> will be inferred from env variable `PAYPAL_SECRET_PROD` or `PAYPAL_SECRET`
29
+ * - if `env===test` -> will be inferred from env variable `PAYPAL_SECRET_TEST` or `PAYPAL_SECRET`
24
30
  */
25
- secret: string;
31
+ secret?: string;
26
32
 
27
33
  /**
28
34
  * @description default intent to `authorize` or `capture` on order creation
35
+ * @default 'AUTHORIZE'
29
36
  */
30
37
  intent_on_checkout?: 'AUTHORIZE' | 'CAPTURE';
31
38
  }