@rebilly/instruments 4.4.0 → 4.6.0

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 (134) hide show
  1. package/.babelrc +24 -26
  2. package/CHANGELOG.md +14 -0
  3. package/dist/index.js +14 -40
  4. package/dist/index.min.js +14 -40
  5. package/package.json +16 -5
  6. package/project.json +9 -0
  7. package/rollup.config.mjs +21 -26
  8. package/src/data/options-schema/index.js +94 -78
  9. package/src/data/options-schema/schemas/options-schema.js +420 -408
  10. package/src/events/base-event.js +34 -34
  11. package/src/events/events.spec.js +6 -6
  12. package/src/events/index.js +5 -5
  13. package/src/functions/destroy.js +19 -19
  14. package/src/functions/destroy.spec.js +41 -41
  15. package/src/functions/mount/fetch-data.js +200 -193
  16. package/src/functions/mount/fetch-data.spec.js +287 -285
  17. package/src/functions/mount/get-lead-source-data.js +31 -31
  18. package/src/functions/mount/get-lead-source-data.spec.js +19 -19
  19. package/src/functions/mount/index.js +73 -65
  20. package/src/functions/mount/mount.spec.js +77 -66
  21. package/src/functions/mount/setup-element.js +23 -23
  22. package/src/functions/mount/setup-framepay-theme.js +86 -68
  23. package/src/functions/mount/setup-framepay.js +9 -5
  24. package/src/functions/mount/setup-i18n.js +15 -15
  25. package/src/functions/mount/setup-options.js +74 -74
  26. package/src/functions/mount/setup-options.spec.js +325 -289
  27. package/src/functions/mount/setup-storefront.js +15 -20
  28. package/src/functions/mount/setup-styles-vars.js +19 -22
  29. package/src/functions/mount/setup-user-flow.js +51 -47
  30. package/src/functions/on.js +5 -5
  31. package/src/functions/on.spec.js +60 -51
  32. package/src/functions/purchase.js +149 -134
  33. package/src/functions/purchase.spec.js +59 -56
  34. package/src/functions/setup.js +53 -49
  35. package/src/functions/setup.spec.js +88 -75
  36. package/src/functions/show.js +13 -14
  37. package/src/functions/show.spec.js +53 -53
  38. package/src/functions/update.js +30 -28
  39. package/src/functions/update.spec.js +94 -93
  40. package/src/i18n/en.json +32 -32
  41. package/src/i18n/es.json +29 -29
  42. package/src/i18n/i18n.spec.js +18 -18
  43. package/src/i18n/index.js +48 -48
  44. package/src/instance.js +36 -36
  45. package/src/instance.spec.js +29 -27
  46. package/src/loader/index.js +95 -70
  47. package/src/loader/loader.spec.js +63 -63
  48. package/src/state/iframes.js +21 -21
  49. package/src/state/index.js +56 -54
  50. package/src/storefront/account-and-website.js +10 -8
  51. package/src/storefront/account-and-website.spec.js +55 -55
  52. package/src/storefront/deposit-requests.js +6 -6
  53. package/src/storefront/fetch-plans-from-addons-bumpOffer.js +21 -19
  54. package/src/storefront/fetch-products-from-plans.js +52 -51
  55. package/src/storefront/fetch-products-from-plans.spec.js +90 -87
  56. package/src/storefront/index.js +56 -49
  57. package/src/storefront/invoices.js +15 -15
  58. package/src/storefront/invoices.spec.js +69 -65
  59. package/src/storefront/models/account-model.js +29 -32
  60. package/src/storefront/models/base-model.js +6 -9
  61. package/src/storefront/models/deposit-request-model.js +22 -13
  62. package/src/storefront/models/invoice-model.js +16 -16
  63. package/src/storefront/models/payment-metadata.js +4 -4
  64. package/src/storefront/models/plan-model.js +73 -64
  65. package/src/storefront/models/ready-to-pay-model.js +59 -59
  66. package/src/storefront/models/summary-model.js +43 -46
  67. package/src/storefront/models/transaction-model.js +11 -14
  68. package/src/storefront/payment-instruments.js +38 -35
  69. package/src/storefront/payment-instruments.spec.js +81 -62
  70. package/src/storefront/purchase.js +50 -44
  71. package/src/storefront/purchase.spec.js +40 -40
  72. package/src/storefront/ready-to-pay.js +75 -77
  73. package/src/storefront/ready-to-pay.spec.js +59 -54
  74. package/src/storefront/storefront.spec.js +9 -9
  75. package/src/storefront/summary.js +93 -67
  76. package/src/storefront/summary.spec.js +108 -106
  77. package/src/storefront/transactions.js +6 -6
  78. package/src/style/base/default-theme.js +928 -923
  79. package/src/style/base/theme.js +21 -21
  80. package/src/style/base/theme.spec.js +13 -13
  81. package/src/style/index.js +3 -3
  82. package/src/style/utils/border.js +40 -27
  83. package/src/style/utils/color-values.js +18 -18
  84. package/src/style/utils/minifyCss.js +6 -6
  85. package/src/utils/add-dom-element.js +14 -14
  86. package/src/utils/format-currency.js +6 -5
  87. package/src/utils/has-valid-css-selector.js +2 -2
  88. package/src/utils/index.js +6 -6
  89. package/src/utils/is-dom-element.js +1 -1
  90. package/src/utils/process-property-as-dom-element.js +22 -22
  91. package/src/utils/quantity.js +26 -28
  92. package/src/utils/sleep.js +3 -1
  93. package/src/views/amount-selector.js +37 -36
  94. package/src/views/common/iframe/base-iframe.js +53 -52
  95. package/src/views/common/iframe/events/change-iframe-src-handler.js +5 -5
  96. package/src/views/common/iframe/events/dispatch-event-handler.js +4 -4
  97. package/src/views/common/iframe/events/resize-component-handler.js +8 -8
  98. package/src/views/common/iframe/events/show-error-handler.js +2 -2
  99. package/src/views/common/iframe/events/stop-loader-handler.js +8 -8
  100. package/src/views/common/iframe/events/update-addons-handler.js +20 -13
  101. package/src/views/common/iframe/events/update-coupons-handler.js +9 -9
  102. package/src/views/common/iframe/events/update-items-handler.js +26 -22
  103. package/src/views/common/iframe/modal-iframe.js +67 -56
  104. package/src/views/common/iframe/view-iframe.js +11 -11
  105. package/src/views/common/render-utilities.js +2 -2
  106. package/src/views/confirmation.js +33 -30
  107. package/src/views/errors.js +89 -79
  108. package/src/views/form.js +41 -37
  109. package/src/views/method-selector/express-methods.js +46 -46
  110. package/src/views/method-selector/generate-digital-wallet.js +46 -45
  111. package/src/views/method-selector/generate-digital-wallet.spec.js +104 -102
  112. package/src/views/method-selector/generate-framepay-config.js +53 -51
  113. package/src/views/method-selector/generate-framepay-config.spec.js +197 -173
  114. package/src/views/method-selector/get-method-data.js +5 -6
  115. package/src/views/method-selector/get-payment-methods.js +18 -16
  116. package/src/views/method-selector/get-payment-methods.spec.js +29 -27
  117. package/src/views/method-selector/index.js +154 -139
  118. package/src/views/method-selector/method-selector.spec.js +13 -13
  119. package/src/views/method-selector/mount-bump-offer.js +65 -49
  120. package/src/views/method-selector/mount-express-methods.js +89 -85
  121. package/src/views/modal.js +74 -67
  122. package/src/views/result.js +14 -14
  123. package/src/views/summary.js +25 -26
  124. package/tests/async-utilities.js +13 -13
  125. package/tests/mocks/framepay-mock.js +9 -8
  126. package/tests/mocks/rebilly-api-mock.js +5 -3
  127. package/tests/mocks/rebilly-instruments-mock.js +121 -117
  128. package/tests/mocks/storefront-api-mock.js +55 -48
  129. package/tests/mocks/storefront-mock.js +10 -14
  130. package/tests/msw/server.js +6 -6
  131. package/tests/setup-test.js +14 -16
  132. package/vitest.config.js +14 -14
  133. package/.eslintrc.js +0 -34
  134. package/.prettierrc.js +0 -11
@@ -2,43 +2,46 @@ import { Endpoint } from './index';
2
2
  import state from '../state';
3
3
 
4
4
  export async function setupPaymentInstrument({ data }) {
5
- return Endpoint(async () => {
6
- // Create payment instrument
7
- const { fields: instrument } = await state.storefront.paymentInstruments.create({data});
8
-
9
- // Setup the payment instrument
10
- state.storefront.setSessionToken(instrument.token || state.options.jwt);
11
-
12
- const setupPayload = {
13
- id: instrument.id,
14
- data: {
15
- websiteId: state.options.websiteId,
16
- ...data
17
- }
18
- }
19
-
20
- if(state.data.amountAndCurrency) {
21
- setupPayload.data = {
22
- ...setupPayload.data,
23
- ...state.data.amountAndCurrency
24
- }
25
- }
26
-
27
- if (state.data?.transaction?.redirectUrl) {
28
- setupPayload.data.redirectUrl = `${setupPayload.data.redirectUrl}&originalRedirectUrl=${state.data.transaction.redirectUrl}`
29
- }
30
-
31
- const { fields: transaction } = await state.storefront.paymentInstruments.setup(setupPayload);
32
-
33
- return {instrument, transaction};
34
- });
5
+ return Endpoint(async () => {
6
+ // Create payment instrument
7
+ const { fields: instrument } =
8
+ await state.storefront.paymentInstruments.create({ data });
9
+
10
+ // Setup the payment instrument
11
+ state.storefront.setSessionToken(instrument.token || state.options.jwt);
12
+
13
+ const setupPayload = {
14
+ id: instrument.id,
15
+ data: {
16
+ websiteId: state.options.websiteId,
17
+ ...data,
18
+ },
19
+ };
20
+
21
+ if (state.data.amountAndCurrency) {
22
+ setupPayload.data = {
23
+ ...setupPayload.data,
24
+ ...state.data.amountAndCurrency,
25
+ };
26
+ }
27
+
28
+ if (state.data?.transaction?.redirectUrl) {
29
+ setupPayload.data.redirectUrl = `${setupPayload.data.redirectUrl}&originalRedirectUrl=${state.data.transaction.redirectUrl}`;
30
+ }
31
+
32
+ const { fields: transaction } =
33
+ await state.storefront.paymentInstruments.setup(setupPayload);
34
+
35
+ return { instrument, transaction };
36
+ });
35
37
  }
36
38
 
37
39
  export async function fetchPaymentInstrument() {
38
- return Endpoint(async () => {
39
- const { items: instruments } = await state.storefront.paymentInstruments.getAll({
40
- filter: `status:active,inactive,verification-needed`,
40
+ return Endpoint(async () => {
41
+ const { items: instruments } =
42
+ await state.storefront.paymentInstruments.getAll({
43
+ filter: `status:active,inactive,verification-needed`,
44
+ });
45
+ return instruments.map(({ fields }) => fields);
41
46
  });
42
- return instruments.map(({fields}) => fields);
43
- });
44
47
  }
@@ -4,66 +4,85 @@ import { when } from 'tests/msw/server';
4
4
  import { storefrontURL } from 'tests/mocks/storefront-api-mock';
5
5
 
6
6
  describe('Storefront API Setup', () => {
7
- it('can make purchase', async () => {
8
- const instance = new StorefontTestingInstance();
9
-
10
- const paymentInstrumentFields = {
11
- id: 'payment-instrument-id'
12
- };
13
-
14
- const paymentInstrumentSetupFields = {
15
- id: 'payment-instrument-id',
16
- approvalUrl: null
17
- };
18
-
19
- const paymentInstrumentPayload = {
20
- data: {
21
- token: 'test-token'
22
- }
23
- };
24
-
25
- const paymentInstrumentSetupPayload = {
26
- id: paymentInstrumentFields.id,
27
- data: {
28
- websiteId: 'website-id',
29
- currency: 'USD',
30
- amount: 30
31
- }
32
- };
33
-
34
- when(post(`${storefrontURL}/payment-instruments`)).thenReturn(ok(paymentInstrumentFields));
35
- when(post(`${storefrontURL}/payment-instruments/*/setup`)).thenReturn(ok(paymentInstrumentSetupFields));
36
-
37
-
38
- vi.spyOn(instance.storefront.paymentInstruments, 'create');
39
- vi.spyOn(instance.storefront.paymentInstruments, 'setup');
40
-
41
- const paymentInstrumentResponse = await instance.storefront.paymentInstruments.create(
42
- paymentInstrumentPayload
43
- );
44
-
45
- const paymentInstrumentSetupResponse = await instance.storefront.paymentInstruments.setup(
46
- paymentInstrumentSetupPayload
47
- );
48
-
49
- expect(instance.storefront.paymentInstruments.create).toBeCalledTimes(1);
50
- expect(instance.storefront.paymentInstruments.create).toBeCalledWith(paymentInstrumentPayload);
51
-
52
- expect(instance.storefront.paymentInstruments.setup).toBeCalledTimes(1);
53
- expect(instance.storefront.paymentInstruments.setup).toBeCalledWith(paymentInstrumentSetupPayload);
54
-
55
- expect(paymentInstrumentResponse.config).toBeInstanceOf(Object);
56
- expect(paymentInstrumentResponse.fields).toBeInstanceOf(Object);
57
- expect(paymentInstrumentResponse.response).toBeInstanceOf(Object);
58
-
59
- expect(paymentInstrumentResponse.fields).toEqual(paymentInstrumentFields);
60
- expect(paymentInstrumentResponse.fields).toMatchObject(paymentInstrumentFields);
61
-
62
- expect(paymentInstrumentSetupResponse.config).toBeInstanceOf(Object);
63
- expect(paymentInstrumentSetupResponse.fields).toBeInstanceOf(Object);
64
- expect(paymentInstrumentSetupResponse.response).toBeInstanceOf(Object);
65
-
66
- expect(paymentInstrumentSetupResponse.fields).toEqual(paymentInstrumentSetupFields);
67
- expect(paymentInstrumentSetupResponse.fields).toMatchObject(paymentInstrumentSetupFields);
68
- });
7
+ it('can make purchase', async () => {
8
+ const instance = new StorefontTestingInstance();
9
+
10
+ const paymentInstrumentFields = {
11
+ id: 'payment-instrument-id',
12
+ };
13
+
14
+ const paymentInstrumentSetupFields = {
15
+ id: 'payment-instrument-id',
16
+ approvalUrl: null,
17
+ };
18
+
19
+ const paymentInstrumentPayload = {
20
+ data: {
21
+ token: 'test-token',
22
+ },
23
+ };
24
+
25
+ const paymentInstrumentSetupPayload = {
26
+ id: paymentInstrumentFields.id,
27
+ data: {
28
+ websiteId: 'website-id',
29
+ currency: 'USD',
30
+ amount: 30,
31
+ },
32
+ };
33
+
34
+ when(post(`${storefrontURL}/payment-instruments`)).thenReturn(
35
+ ok(paymentInstrumentFields),
36
+ );
37
+ when(post(`${storefrontURL}/payment-instruments/*/setup`)).thenReturn(
38
+ ok(paymentInstrumentSetupFields),
39
+ );
40
+
41
+ vi.spyOn(instance.storefront.paymentInstruments, 'create');
42
+ vi.spyOn(instance.storefront.paymentInstruments, 'setup');
43
+
44
+ const paymentInstrumentResponse =
45
+ await instance.storefront.paymentInstruments.create(
46
+ paymentInstrumentPayload,
47
+ );
48
+
49
+ const paymentInstrumentSetupResponse =
50
+ await instance.storefront.paymentInstruments.setup(
51
+ paymentInstrumentSetupPayload,
52
+ );
53
+
54
+ expect(instance.storefront.paymentInstruments.create).toBeCalledTimes(
55
+ 1,
56
+ );
57
+ expect(instance.storefront.paymentInstruments.create).toBeCalledWith(
58
+ paymentInstrumentPayload,
59
+ );
60
+
61
+ expect(instance.storefront.paymentInstruments.setup).toBeCalledTimes(1);
62
+ expect(instance.storefront.paymentInstruments.setup).toBeCalledWith(
63
+ paymentInstrumentSetupPayload,
64
+ );
65
+
66
+ expect(paymentInstrumentResponse.config).toBeInstanceOf(Object);
67
+ expect(paymentInstrumentResponse.fields).toBeInstanceOf(Object);
68
+ expect(paymentInstrumentResponse.response).toBeInstanceOf(Object);
69
+
70
+ expect(paymentInstrumentResponse.fields).toEqual(
71
+ paymentInstrumentFields,
72
+ );
73
+ expect(paymentInstrumentResponse.fields).toMatchObject(
74
+ paymentInstrumentFields,
75
+ );
76
+
77
+ expect(paymentInstrumentSetupResponse.config).toBeInstanceOf(Object);
78
+ expect(paymentInstrumentSetupResponse.fields).toBeInstanceOf(Object);
79
+ expect(paymentInstrumentSetupResponse.response).toBeInstanceOf(Object);
80
+
81
+ expect(paymentInstrumentSetupResponse.fields).toEqual(
82
+ paymentInstrumentSetupFields,
83
+ );
84
+ expect(paymentInstrumentSetupResponse.fields).toMatchObject(
85
+ paymentInstrumentSetupFields,
86
+ );
87
+ });
69
88
  });
@@ -2,53 +2,59 @@ import state from '../state';
2
2
  import { Endpoint } from './index';
3
3
 
4
4
  export async function postPurchase({ data }) {
5
- return Endpoint(async () => {
6
- // https://storefront-api-docs.rebilly.com/tag/Purchases/operation/StorefrontPostPurchase
7
- const purchaseSchemaKeys = [
8
- 'websiteId',
9
- 'paymentInstruction',
10
- 'items',
11
- 'billingAddress',
12
- 'deliveryAddress',
13
- 'shippingRateId',
14
- 'couponIds',
15
- 'password',
16
- 'redirectUrl',
17
- ];
18
- Object.keys(data).forEach((key) => {
19
- if (!purchaseSchemaKeys.includes(key)) {
20
- delete data[key];
21
- }
5
+ return Endpoint(async () => {
6
+ // https://storefront-api-docs.rebilly.com/tag/Purchases/operation/StorefrontPostPurchase
7
+ const purchaseSchemaKeys = [
8
+ 'websiteId',
9
+ 'paymentInstruction',
10
+ 'items',
11
+ 'billingAddress',
12
+ 'deliveryAddress',
13
+ 'shippingRateId',
14
+ 'couponIds',
15
+ 'password',
16
+ 'redirectUrl',
17
+ ];
18
+ Object.keys(data).forEach((key) => {
19
+ if (!purchaseSchemaKeys.includes(key)) {
20
+ delete data[key];
21
+ }
22
+ });
23
+ return state.storefront.purchase.purchase({ data });
22
24
  });
23
- return state.storefront.purchase.purchase({data});
24
- });
25
25
  }
26
26
 
27
27
  export async function postPayment({ data }) {
28
- return Endpoint(async () => {
29
- const {token, transactionId, invoiceId, websiteId, paymentInstrumentId} = data;
30
- // https://storefront-api-docs.rebilly.com/tag/Purchases/operation/StorefrontPostPayment
31
- const purchaseSchemaKeys = ['riskMetadata', 'redirectUrl'];
32
- if (token) {
33
- purchaseSchemaKeys.push('token');
34
- }
35
- if (paymentInstrumentId) {
36
- purchaseSchemaKeys.push('paymentInstrumentId');
37
- }
38
- if (transactionId) {
39
- purchaseSchemaKeys.push('transactionId');
40
- }
41
- if (invoiceId) {
42
- purchaseSchemaKeys.push('invoiceId');
43
- }
44
- if (websiteId) {
45
- purchaseSchemaKeys.push('amount', 'currency', 'websiteId');
46
- }
47
- Object.keys(data).forEach((key) => {
48
- if (!purchaseSchemaKeys.includes(key)) {
49
- delete data[key];
50
- }
28
+ return Endpoint(async () => {
29
+ const {
30
+ token,
31
+ transactionId,
32
+ invoiceId,
33
+ websiteId,
34
+ paymentInstrumentId,
35
+ } = data;
36
+ // https://storefront-api-docs.rebilly.com/tag/Purchases/operation/StorefrontPostPayment
37
+ const purchaseSchemaKeys = ['riskMetadata', 'redirectUrl'];
38
+ if (token) {
39
+ purchaseSchemaKeys.push('token');
40
+ }
41
+ if (paymentInstrumentId) {
42
+ purchaseSchemaKeys.push('paymentInstrumentId');
43
+ }
44
+ if (transactionId) {
45
+ purchaseSchemaKeys.push('transactionId');
46
+ }
47
+ if (invoiceId) {
48
+ purchaseSchemaKeys.push('invoiceId');
49
+ }
50
+ if (websiteId) {
51
+ purchaseSchemaKeys.push('amount', 'currency', 'websiteId');
52
+ }
53
+ Object.keys(data).forEach((key) => {
54
+ if (!purchaseSchemaKeys.includes(key)) {
55
+ delete data[key];
56
+ }
57
+ });
58
+ return state.storefront.purchase.payment({ data });
51
59
  });
52
- return state.storefront.purchase.payment({data});
53
- });
54
60
  }
@@ -4,50 +4,50 @@ import { when } from 'tests/msw/server';
4
4
  import { storefrontURL } from 'tests/mocks/storefront-api-mock';
5
5
 
6
6
  describe('Storefront API Purchase', () => {
7
- it('can make purchase', async () => {
8
- const instance = StorefontTestingInstance();
9
- const payload = {
10
- websiteId: 'test-website-id',
11
- items: [
12
- {
13
- planId: 'test-plan-id-1',
14
- quantity: 1
15
- }
16
- ],
17
- billingAddress: {
18
- firstName: 'Test',
19
- lastName: 'Customer'
20
- },
21
- paymentInstruction: {
22
- token: 'test-token'
23
- }
24
- };
25
- const fields = {
26
- orderId: 'test-order-id',
27
- invoice: {
28
- id: 'test-invoice-id'
29
- },
30
- transaction: {
31
- id: 'test-transaction-id'
32
- }
33
- };
7
+ it('can make purchase', async () => {
8
+ const instance = StorefontTestingInstance();
9
+ const payload = {
10
+ websiteId: 'test-website-id',
11
+ items: [
12
+ {
13
+ planId: 'test-plan-id-1',
14
+ quantity: 1,
15
+ },
16
+ ],
17
+ billingAddress: {
18
+ firstName: 'Test',
19
+ lastName: 'Customer',
20
+ },
21
+ paymentInstruction: {
22
+ token: 'test-token',
23
+ },
24
+ };
25
+ const fields = {
26
+ orderId: 'test-order-id',
27
+ invoice: {
28
+ id: 'test-invoice-id',
29
+ },
30
+ transaction: {
31
+ id: 'test-transaction-id',
32
+ },
33
+ };
34
34
 
35
- when(post(`${storefrontURL}/purchase`)).thenReturn(ok(fields));
35
+ when(post(`${storefrontURL}/purchase`)).thenReturn(ok(fields));
36
36
 
37
- vi.spyOn(instance.storefront.purchase, 'purchase');
37
+ vi.spyOn(instance.storefront.purchase, 'purchase');
38
38
 
39
- const requestResponse = await instance.storefront.purchase.purchase(
40
- payload
41
- );
39
+ const requestResponse = await instance.storefront.purchase.purchase(
40
+ payload,
41
+ );
42
42
 
43
- expect(instance.storefront.purchase.purchase).toBeCalledTimes(1);
44
- expect(instance.storefront.purchase.purchase).toBeCalledWith(payload);
43
+ expect(instance.storefront.purchase.purchase).toBeCalledTimes(1);
44
+ expect(instance.storefront.purchase.purchase).toBeCalledWith(payload);
45
45
 
46
- expect(requestResponse.config).toBeInstanceOf(Object);
47
- expect(requestResponse.fields).toBeInstanceOf(Object);
48
- expect(requestResponse.response).toBeInstanceOf(Object);
46
+ expect(requestResponse.config).toBeInstanceOf(Object);
47
+ expect(requestResponse.fields).toBeInstanceOf(Object);
48
+ expect(requestResponse.response).toBeInstanceOf(Object);
49
49
 
50
- expect(requestResponse.fields).toEqual(fields);
51
- expect(requestResponse.fields).toMatchObject(fields);
52
- });
50
+ expect(requestResponse.fields).toEqual(fields);
51
+ expect(requestResponse.fields).toMatchObject(fields);
52
+ });
53
53
  });
@@ -6,93 +6,91 @@ import state from '../state';
6
6
  import { mapItemsQuantities } from '../utils/quantity';
7
7
 
8
8
  export function filterReadyToPay(readyToPay) {
9
- const paymentMethodsMetadata = [...paymentMethods];
10
- const EXPRESS_METHODS = [
11
- 'Google Pay',
12
- 'Apple Pay',
13
- 'paypal',
14
- ];
15
- const BLOCKED_METHODS = [
16
- // account number, routing number, and account type required
17
- 'echeck',
9
+ const paymentMethodsMetadata = [...paymentMethods];
10
+ const EXPRESS_METHODS = ['Google Pay', 'Apple Pay', 'paypal'];
11
+ const BLOCKED_METHODS = [
12
+ // account number, routing number, and account type required
13
+ 'echeck',
18
14
 
19
- // payment instrument is required
20
- // card number is required
21
- // card cvv is required
22
- // expiration month is required
23
- // expiration year is required
24
- 'Khelocard',
25
-
26
- // Klarna authorization token is required
27
- // Klarna session ID is required
28
- 'Klarna',
29
- ];
30
- const isExpressMethod = ({ method, feature }) => (
31
- EXPRESS_METHODS.includes(method) ||
32
- EXPRESS_METHODS.includes(feature?.name)
33
- );
34
- return readyToPay
35
- .filter(readyData => {
36
- // Remove result for "old" paypal method
37
- const isOldPayPal = readyData.method === 'paypal' && !readyData.feature;
38
- // Remove result for plaid method
39
- const isPlaid = readyData.method === 'ach' && readyData.feature;
40
- // Remove result for blocked method
41
- const isBlocked = BLOCKED_METHODS.includes(readyData.method)
15
+ // payment instrument is required
16
+ // card number is required
17
+ // card cvv is required
18
+ // expiration month is required
19
+ // expiration year is required
20
+ 'Khelocard',
42
21
 
43
- return !isOldPayPal && !isPlaid && !isBlocked
44
- })
45
- .map((fields, index) => {
46
- const metadata = paymentMethodsMetadata
47
- .find(methodMetadata => methodMetadata.apiName === fields.method) || {};
48
- metadata.isExpressMethod = isExpressMethod(fields);
22
+ // Klarna authorization token is required
23
+ // Klarna session ID is required
24
+ 'Klarna',
25
+ ];
26
+ const isExpressMethod = ({ method, feature }) =>
27
+ EXPRESS_METHODS.includes(method) ||
28
+ EXPRESS_METHODS.includes(feature?.name);
29
+ return readyToPay
30
+ .filter((readyData) => {
31
+ // Remove result for "old" paypal method
32
+ const isOldPayPal =
33
+ readyData.method === 'paypal' && !readyData.feature;
34
+ // Remove result for plaid method
35
+ const isPlaid = readyData.method === 'ach' && readyData.feature;
36
+ // Remove result for blocked method
37
+ const isBlocked = BLOCKED_METHODS.includes(readyData.method);
49
38
 
50
- return new ReadyToPayModel({
51
- index,
52
- metadata,
53
- ...fields
54
- });
55
- })
56
- .filter(({metadata}) => metadata.storefrontEnabled);
39
+ return !isOldPayPal && !isPlaid && !isBlocked;
40
+ })
41
+ .map((fields, index) => {
42
+ const metadata =
43
+ paymentMethodsMetadata.find(
44
+ (methodMetadata) =>
45
+ methodMetadata.apiName === fields.method,
46
+ ) || {};
47
+ metadata.isExpressMethod = isExpressMethod(fields);
48
+
49
+ return new ReadyToPayModel({
50
+ index,
51
+ metadata,
52
+ ...fields,
53
+ });
54
+ })
55
+ .filter(({ metadata }) => metadata.storefrontEnabled);
57
56
  }
58
57
 
59
- export async function fetchReadyToPay({
60
- riskMetadata = null,
61
- }) {
62
- return Endpoint(async () => {
63
- if (!riskMetadata) {
64
- const { riskMetadata: data } = await collectData();
65
- riskMetadata = data;
66
- }
58
+ export async function fetchReadyToPay({ riskMetadata = null }) {
59
+ return Endpoint(async () => {
60
+ if (!riskMetadata) {
61
+ const { riskMetadata: data } = await collectData();
62
+ riskMetadata = data;
63
+ }
64
+
65
+ const data = {
66
+ riskMetadata,
67
+ };
67
68
 
68
- const data = {
69
- riskMetadata
70
- };
69
+ data.websiteId = state.options?.websiteId || null;
71
70
 
72
- data.websiteId = state.options?.websiteId || null;
71
+ if (state.options?.items) {
72
+ data.items = mapItemsQuantities(state.options.items);
73
+ }
73
74
 
74
- if (state.options?.items) {
75
- data.items = mapItemsQuantities(state.options.items);
76
- }
75
+ if (state.options?.money && state.data?.amountAndCurrency) {
76
+ const money = state.data.amountAndCurrency;
77
+ data.amount = money.amount;
78
+ data.currency = money.currency;
79
+ }
77
80
 
78
- if (state.options?.money && state.data?.amountAndCurrency) {
79
- const money = state.data.amountAndCurrency;
80
- data.amount = money.amount;
81
- data.currency = money.currency;
82
- }
81
+ if (state.data?.account?.address) {
82
+ data.billingAddress = state.data.account.address;
83
+ }
83
84
 
84
- if (state.data?.account?.address) {
85
- data.billingAddress = state.data.account.address;
86
- }
85
+ if (state.data?.deposit) {
86
+ data.amount = state.data.deposit.amount;
87
+ data.currency = state.data.deposit.currency;
88
+ }
87
89
 
88
- if (state.data?.deposit) {
89
- data.amount = state.data.deposit.amount;
90
- data.currency = state.data.deposit.currency;
91
- }
90
+ const { fields: readyToPayFields } =
91
+ await state.storefront.purchase.readyToPay({ data });
92
+ const readyToPay = Object.values(readyToPayFields);
92
93
 
93
- const { fields: readyToPayFields } = await state.storefront.purchase.readyToPay({ data });
94
- const readyToPay = Object.values(readyToPayFields);
95
-
96
- return filterReadyToPay(readyToPay);
97
- });
94
+ return filterReadyToPay(readyToPay);
95
+ });
98
96
  }