@rebilly/instruments 2.0.0-beta → 3.0.1-beta.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 (197) hide show
  1. package/dist/events/index.js +2 -1
  2. package/dist/functions/mount/fetch-data.js +187 -0
  3. package/dist/functions/mount/fetch-data.spec.js +189 -0
  4. package/dist/functions/mount/index.js +132 -164
  5. package/dist/functions/mount/mount.spec.js +2 -4
  6. package/dist/functions/mount/setup-element.js +40 -0
  7. package/dist/functions/mount/setup-framepay-theme.js +95 -0
  8. package/dist/functions/mount/setup-framepay.js +5 -1
  9. package/dist/functions/mount/setup-i18n.js +33 -0
  10. package/dist/functions/mount/setup-options.js +68 -47
  11. package/dist/functions/mount/setup-options.spec.js +66 -0
  12. package/dist/functions/mount/setup-storefront.js +6 -4
  13. package/dist/functions/mount/setup-styles.js +4 -2
  14. package/dist/functions/purchase.js +129 -24
  15. package/dist/functions/purchase.spec.js +13 -10
  16. package/dist/functions/setup.js +85 -0
  17. package/dist/functions/setup.spec.js +87 -0
  18. package/dist/functions/show.js +8 -4
  19. package/dist/functions/show.spec.js +9 -5
  20. package/dist/functions/update.js +39 -24
  21. package/dist/functions/update.spec.js +0 -4
  22. package/dist/i18n/en.json +5 -2
  23. package/dist/i18n/es.json +4 -1
  24. package/dist/index.js +17 -3
  25. package/dist/index.spec.js +3 -16
  26. package/dist/loader/index.js +4 -3
  27. package/dist/storefront/index.js +33 -0
  28. package/dist/storefront/invoices.js +27 -0
  29. package/dist/storefront/models/base-model.js +18 -0
  30. package/dist/storefront/models/invoice-model.js +14 -0
  31. package/dist/storefront/models/plan-model.js +4 -35
  32. package/dist/storefront/models/product-model.js +4 -23
  33. package/dist/storefront/models/summary-model.js +12 -25
  34. package/dist/storefront/models/transaction-model.js +31 -0
  35. package/dist/storefront/payment-instruments.js +47 -0
  36. package/dist/storefront/payment-instruments.spec.js +55 -0
  37. package/dist/storefront/plans.js +10 -18
  38. package/dist/storefront/plans.spec.js +3 -13
  39. package/dist/storefront/products.js +10 -13
  40. package/dist/storefront/products.spec.js +12 -19
  41. package/dist/storefront/purchase.js +23 -12
  42. package/dist/storefront/purchase.spec.js +1 -20
  43. package/dist/storefront/ready-to-pay.js +18 -15
  44. package/dist/storefront/ready-to-pay.spec.js +2 -12
  45. package/dist/storefront/summary.js +21 -17
  46. package/dist/storefront/summary.spec.js +4 -15
  47. package/dist/storefront/transactions.js +27 -0
  48. package/dist/style/base/__snapshots__/theme.spec.js.snap +188 -45
  49. package/dist/style/base/default-theme.js +699 -0
  50. package/dist/style/base/index.js +48 -16
  51. package/dist/style/base/theme.js +16 -48
  52. package/dist/style/base/theme.spec.js +4 -15
  53. package/dist/style/components/address.js +3 -3
  54. package/dist/style/components/button.js +32 -22
  55. package/dist/style/components/divider.js +9 -9
  56. package/dist/style/components/forms/checkbox.js +12 -9
  57. package/dist/style/components/forms/field.js +18 -6
  58. package/dist/style/components/forms/form.js +2 -2
  59. package/dist/style/components/forms/input.js +54 -13
  60. package/dist/style/components/forms/label.js +39 -18
  61. package/dist/style/components/forms/select.js +54 -22
  62. package/dist/style/components/forms/validation.js +53 -6
  63. package/dist/style/components/icons.js +4 -4
  64. package/dist/style/components/loader.js +5 -3
  65. package/dist/style/components/methods.js +18 -15
  66. package/dist/style/components/overlay.js +5 -5
  67. package/dist/style/helpers/index.js +46 -46
  68. package/dist/style/index.js +3 -1
  69. package/dist/style/payment-instruments/payment-card.js +4 -4
  70. package/dist/style/utils/border.js +47 -0
  71. package/dist/style/utils/color-values.js +39 -3
  72. package/dist/style/utils/remove-empty-null.js +20 -0
  73. package/dist/style/vendor/framepay.js +11 -8
  74. package/dist/style/vendor/postmate.js +2 -2
  75. package/dist/style/views/confirmation.js +13 -13
  76. package/dist/style/views/method-selector.js +2 -2
  77. package/dist/style/views/modal.js +6 -6
  78. package/dist/style/views/result.js +4 -4
  79. package/dist/style/views/summary.js +26 -22
  80. package/dist/views/__snapshots__/summary.spec.js.snap +77 -119
  81. package/dist/views/common/iframe/base-iframe.js +2 -0
  82. package/dist/views/common/iframe/modal-iframe.js +50 -4
  83. package/dist/views/confirmation.js +19 -8
  84. package/dist/views/method-selector/generate-digital-wallet.js +12 -3
  85. package/dist/views/method-selector/generate-digital-wallet.spec.js +11 -0
  86. package/dist/views/method-selector/get-payment-methods.js +13 -2
  87. package/dist/views/method-selector/get-payment-methods.spec.js +21 -19
  88. package/dist/views/method-selector/index.js +23 -34
  89. package/dist/views/method-selector/method-selector.spec.js +50 -55
  90. package/dist/views/method-selector/mount-methods.js +5 -8
  91. package/dist/views/modal.js +8 -2
  92. package/dist/views/result.js +3 -4
  93. package/dist/views/summary.js +156 -97
  94. package/dist/views/summary.spec.js +53 -58
  95. package/package.json +4 -2
  96. package/src/events/index.js +2 -1
  97. package/src/functions/mount/fetch-data.js +152 -0
  98. package/src/functions/mount/fetch-data.spec.js +238 -0
  99. package/src/functions/mount/index.js +101 -158
  100. package/src/functions/mount/mount.spec.js +3 -5
  101. package/src/functions/mount/setup-element.js +26 -0
  102. package/src/functions/mount/setup-framepay-theme.js +82 -0
  103. package/src/functions/mount/setup-framepay.js +5 -1
  104. package/src/functions/mount/setup-i18n.js +19 -0
  105. package/src/functions/mount/setup-options.js +78 -48
  106. package/src/functions/mount/setup-options.spec.js +60 -0
  107. package/src/functions/mount/setup-storefront.js +6 -4
  108. package/src/functions/mount/setup-styles.js +4 -2
  109. package/src/functions/on.spec.js +1 -1
  110. package/src/functions/purchase.js +99 -23
  111. package/src/functions/purchase.spec.js +10 -10
  112. package/src/functions/setup.js +48 -0
  113. package/src/functions/setup.spec.js +98 -0
  114. package/src/functions/show.js +2 -4
  115. package/src/functions/show.spec.js +3 -4
  116. package/src/functions/update.js +40 -25
  117. package/src/functions/update.spec.js +0 -4
  118. package/src/i18n/en.json +5 -2
  119. package/src/i18n/es.json +4 -1
  120. package/src/index.js +9 -3
  121. package/src/index.spec.js +3 -21
  122. package/src/loader/index.js +3 -3
  123. package/src/storefront/index.js +28 -0
  124. package/src/storefront/invoices.js +11 -0
  125. package/src/storefront/models/base-model.js +10 -0
  126. package/src/storefront/models/invoice-model.js +3 -0
  127. package/src/storefront/models/plan-model.js +3 -35
  128. package/src/storefront/models/product-model.js +3 -23
  129. package/src/storefront/models/summary-model.js +12 -19
  130. package/src/storefront/models/transaction-model.js +19 -0
  131. package/src/storefront/payment-instruments.js +30 -0
  132. package/src/storefront/payment-instruments.spec.js +69 -0
  133. package/src/storefront/plans.js +6 -17
  134. package/src/storefront/plans.spec.js +4 -11
  135. package/src/storefront/products.js +8 -16
  136. package/src/storefront/products.spec.js +16 -22
  137. package/src/storefront/purchase.js +14 -16
  138. package/src/storefront/purchase.spec.js +2 -14
  139. package/src/storefront/ready-to-pay.js +13 -16
  140. package/src/storefront/ready-to-pay.spec.js +3 -10
  141. package/src/storefront/summary.js +19 -17
  142. package/src/storefront/summary.spec.js +5 -12
  143. package/src/storefront/transactions.js +11 -0
  144. package/src/style/base/__snapshots__/theme.spec.js.snap +188 -45
  145. package/src/style/base/default-theme.js +674 -0
  146. package/src/style/base/index.js +48 -16
  147. package/src/style/base/theme.js +17 -47
  148. package/src/style/base/theme.spec.js +4 -16
  149. package/src/style/components/address.js +3 -3
  150. package/src/style/components/button.js +32 -24
  151. package/src/style/components/divider.js +9 -9
  152. package/src/style/components/forms/checkbox.js +11 -11
  153. package/src/style/components/forms/field.js +18 -6
  154. package/src/style/components/forms/form.js +2 -2
  155. package/src/style/components/forms/input.js +54 -13
  156. package/src/style/components/forms/label.js +39 -18
  157. package/src/style/components/forms/select.js +54 -22
  158. package/src/style/components/forms/validation.js +53 -6
  159. package/src/style/components/icons.js +4 -4
  160. package/src/style/components/loader.js +4 -5
  161. package/src/style/components/methods.js +18 -15
  162. package/src/style/components/overlay.js +5 -5
  163. package/src/style/helpers/index.js +46 -46
  164. package/src/style/index.js +2 -1
  165. package/src/style/payment-instruments/payment-card.js +4 -4
  166. package/src/style/utils/border.js +34 -0
  167. package/src/style/utils/color-values.js +27 -1
  168. package/src/style/utils/remove-empty-null.js +10 -0
  169. package/src/style/vendor/framepay.js +11 -8
  170. package/src/style/vendor/postmate.js +2 -2
  171. package/src/style/views/confirmation.js +13 -13
  172. package/src/style/views/method-selector.js +2 -2
  173. package/src/style/views/modal.js +6 -6
  174. package/src/style/views/result.js +4 -4
  175. package/src/style/views/summary.js +26 -22
  176. package/src/views/__snapshots__/summary.spec.js.snap +77 -119
  177. package/src/views/common/iframe/base-iframe.js +2 -0
  178. package/src/views/common/iframe/modal-iframe.js +45 -3
  179. package/src/views/confirmation.js +15 -5
  180. package/src/views/method-selector/generate-digital-wallet.js +10 -3
  181. package/src/views/method-selector/generate-digital-wallet.spec.js +10 -0
  182. package/src/views/method-selector/get-payment-methods.js +7 -2
  183. package/src/views/method-selector/get-payment-methods.spec.js +26 -23
  184. package/src/views/method-selector/index.js +21 -28
  185. package/src/views/method-selector/method-selector.spec.js +49 -64
  186. package/src/views/method-selector/mount-methods.js +5 -8
  187. package/src/views/modal.js +6 -2
  188. package/src/views/result.js +4 -3
  189. package/src/views/summary.js +161 -117
  190. package/src/views/summary.spec.js +60 -75
  191. package/tests/mocks/rebilly-instruments-mock.js +37 -7
  192. package/tests/mocks/storefront-api-mock.js +8 -0
  193. package/tests/mocks/storefront-mock.js +17 -0
  194. package/dist/functions/mount/fetch-summary-data.js +0 -46
  195. package/dist/functions/mount/fetch-summary-data.spec.js +0 -43
  196. package/src/functions/mount/fetch-summary-data.js +0 -29
  197. package/src/functions/mount/fetch-summary-data.spec.js +0 -40
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+
3
+ var _storefrontMock = require("../../tests/mocks/storefront-mock");
4
+
5
+ var _mswWhenThen = require("msw-when-then");
6
+
7
+ var _server = require("../../tests/msw/server");
8
+
9
+ var _storefrontApiMock = require("../../tests/mocks/storefront-api-mock");
10
+
11
+ describe('Storefront API Setup', () => {
12
+ it('can make purchase', async () => {
13
+ const instance = new _storefrontMock.StorefontTestingInstance();
14
+ const paymentInstrumentFields = {
15
+ id: 'payment-instrument-id'
16
+ };
17
+ const paymentInstrumentSetupFields = {
18
+ id: 'payment-instrument-id',
19
+ approvalUrl: null
20
+ };
21
+ const paymentInstrumentPayload = {
22
+ data: {
23
+ token: 'test-token'
24
+ }
25
+ };
26
+ const paymentInstrumentSetupPayload = {
27
+ id: paymentInstrumentFields.id,
28
+ data: {
29
+ websiteId: 'website-id',
30
+ currency: 'USD',
31
+ amount: 30
32
+ }
33
+ };
34
+ (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/payment-instruments`)).thenReturn((0, _mswWhenThen.ok)(paymentInstrumentFields));
35
+ (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/payment-instruments/*/setup`)).thenReturn((0, _mswWhenThen.ok)(paymentInstrumentSetupFields));
36
+ jest.spyOn(instance.storefront.paymentInstruments, 'create');
37
+ jest.spyOn(instance.storefront.paymentInstruments, 'setup');
38
+ const paymentInstrumentResponse = await instance.storefront.paymentInstruments.create(paymentInstrumentPayload);
39
+ const paymentInstrumentSetupResponse = await instance.storefront.paymentInstruments.setup(paymentInstrumentSetupPayload);
40
+ expect(instance.storefront.paymentInstruments.create).toBeCalledTimes(1);
41
+ expect(instance.storefront.paymentInstruments.create).toBeCalledWith(paymentInstrumentPayload);
42
+ expect(instance.storefront.paymentInstruments.setup).toBeCalledTimes(1);
43
+ expect(instance.storefront.paymentInstruments.setup).toBeCalledWith(paymentInstrumentSetupPayload);
44
+ expect(paymentInstrumentResponse.config).toBeInstanceOf(Object);
45
+ expect(paymentInstrumentResponse.fields).toBeInstanceOf(Object);
46
+ expect(paymentInstrumentResponse.response).toBeInstanceOf(Object);
47
+ expect(paymentInstrumentResponse.fields).toEqual(paymentInstrumentFields);
48
+ expect(paymentInstrumentResponse.fields).toMatchObject(paymentInstrumentFields);
49
+ expect(paymentInstrumentSetupResponse.config).toBeInstanceOf(Object);
50
+ expect(paymentInstrumentSetupResponse.fields).toBeInstanceOf(Object);
51
+ expect(paymentInstrumentSetupResponse.response).toBeInstanceOf(Object);
52
+ expect(paymentInstrumentSetupResponse.fields).toEqual(paymentInstrumentSetupFields);
53
+ expect(paymentInstrumentSetupResponse.fields).toMatchObject(paymentInstrumentSetupFields);
54
+ });
55
+ });
@@ -7,29 +7,23 @@ exports.fetchPlans = fetchPlans;
7
7
 
8
8
  var _planModel = _interopRequireDefault(require("./models/plan-model"));
9
9
 
10
+ var _index = require("./index");
11
+
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
 
12
14
  async function fetchPlans({
13
- data = null,
14
- state = null
15
+ state = {},
16
+ data = {}
15
17
  }) {
16
- if (!state.storefront) {
17
- throw new Error('Could not access rebilly-js-sdk instance');
18
- }
19
-
20
- if (!state.options) {
21
- throw new Error('Could not use Rebilly Instruments mount options to fetch Rebilly data');
22
- }
23
-
24
- try {
25
- const {
26
- lineItems
27
- } = data || {};
18
+ return (0, _index.Endpoint)({
19
+ state
20
+ }, async () => {
21
+ const lineItems = data.lineItems || state.data.summaryLineItems;
28
22
  const filterByPlanId = {
29
23
  filter: ''
30
24
  };
31
25
 
32
- if (lineItems) {
26
+ if (lineItems.length) {
33
27
  filterByPlanId.filter = `id:${lineItems.map(item => item.planId).join(',')}`;
34
28
  }
35
29
 
@@ -39,7 +33,5 @@ async function fetchPlans({
39
33
  return planItems.map(({
40
34
  fields
41
35
  }) => new _planModel.default(fields));
42
- } catch (error) {
43
- throw error;
44
- }
36
+ });
45
37
  }
@@ -17,23 +17,13 @@ var _asyncUtilities = require("../../tests/async-utilities");
17
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
18
 
19
19
  describe('Storefront API Plan', () => {
20
- class TestPlansInstance {
21
- constructor({
22
- options = {}
23
- } = {}) {
24
- this.options = options;
25
- this.storefront = (0, _storefrontMock.MockStorefront)();
26
- }
27
-
28
- }
29
-
30
20
  it('can fetch plans', async () => {
31
21
  const testPlan = {
32
22
  name: 'Test Plan',
33
23
  id: 'test-plan-id-1'
34
24
  };
35
25
  (0, _server.when)((0, _mswWhenThen.get)(`${_storefrontApiMock.storefrontURL}/plans`)).thenReturn((0, _mswWhenThen.ok)([testPlan]));
36
- const instance = new TestPlansInstance();
26
+ const instance = new _storefrontMock.StorefontTestingInstance();
37
27
  jest.spyOn(instance.storefront.plans, 'getAll');
38
28
  const response = await (0, _plans.fetchPlans)({
39
29
  state: instance
@@ -47,7 +37,7 @@ describe('Storefront API Plan', () => {
47
37
  expect(response).toEqual([new _planModel.default(testPlan)]);
48
38
  });
49
39
  it('can fetch plans with filter', async () => {
50
- const instance = new TestPlansInstance();
40
+ const instance = new _storefrontMock.StorefontTestingInstance();
51
41
  jest.spyOn(instance.storefront.plans, 'getAll');
52
42
  await (0, _plans.fetchPlans)({
53
43
  state: instance,
@@ -64,7 +54,7 @@ describe('Storefront API Plan', () => {
64
54
  });
65
55
  });
66
56
  it('should throw errors with no options', async () => {
67
- const noConfigOrOptionsInstance = new TestPlansInstance({
57
+ const noConfigOrOptionsInstance = new _storefrontMock.StorefontTestingInstance({
68
58
  options: null
69
59
  });
70
60
  await (0, _asyncUtilities.expectConfigurationError)((0, _plans.fetchPlans)({
@@ -7,22 +7,21 @@ exports.fetchProducts = fetchProducts;
7
7
 
8
8
  var _productModel = _interopRequireDefault(require("./models/product-model"));
9
9
 
10
+ var _index = require("./index");
11
+
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
 
12
14
  async function fetchProducts({
13
- data = null,
14
15
  state
15
16
  }) {
16
- if (!state.storefront) {
17
- throw new Error('Could not access rebilly-js-sdk instance');
18
- }
19
-
20
- if (!state.options) {
21
- throw new Error('Could not use Rebilly Instruments mount options to fetch Rebilly data');
22
- }
17
+ return (0, _index.Endpoint)({
18
+ state
19
+ }, async () => {
20
+ if (state.data.plans === null) {
21
+ return null;
22
+ }
23
23
 
24
- try {
25
- const plansData = data || [];
24
+ const plansData = state.data.plans || [];
26
25
  const filterByProductId = {
27
26
  filter: ''
28
27
  };
@@ -37,7 +36,5 @@ async function fetchProducts({
37
36
  return productItems.map(({
38
37
  fields
39
38
  }) => new _productModel.default(fields));
40
- } catch (error) {
41
- throw error;
42
- }
39
+ });
43
40
  }
@@ -17,23 +17,13 @@ var _asyncUtilities = require("../../tests/async-utilities");
17
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
18
 
19
19
  describe('Storefront API Plan', () => {
20
- class TestProductsInstance {
21
- constructor({
22
- options = {}
23
- } = {}) {
24
- this.options = options;
25
- this.storefront = (0, _storefrontMock.MockStorefront)();
26
- }
27
-
28
- }
29
-
30
20
  it('can fetch products', async () => {
31
21
  const testProduct = {
32
22
  name: 'Test Product',
33
23
  id: 'test-product-id-1'
34
24
  };
35
25
  (0, _server.when)((0, _mswWhenThen.get)(`${_storefrontApiMock.storefrontURL}/products`)).thenReturn((0, _mswWhenThen.ok)([testProduct]));
36
- const instance = new TestProductsInstance();
26
+ const instance = new _storefrontMock.StorefontTestingInstance();
37
27
  jest.spyOn(instance.storefront.products, 'getAll');
38
28
  const response = await (0, _products.fetchProducts)({
39
29
  state: instance
@@ -47,22 +37,25 @@ describe('Storefront API Plan', () => {
47
37
  expect(response).toEqual([new _productModel.default(testProduct)]);
48
38
  });
49
39
  it('can fetch products with filter', async () => {
50
- const instance = new TestProductsInstance();
40
+ const instance = new _storefrontMock.StorefontTestingInstance({
41
+ data: {
42
+ plans: [{
43
+ productId: 'test-product-1'
44
+ }, {
45
+ productId: 'test-product-2'
46
+ }]
47
+ }
48
+ });
51
49
  jest.spyOn(instance.storefront.products, 'getAll');
52
50
  await (0, _products.fetchProducts)({
53
- state: instance,
54
- data: [{
55
- productId: 'test-product-1'
56
- }, {
57
- productId: 'test-product-2'
58
- }]
51
+ state: instance
59
52
  });
60
53
  expect(instance.storefront.products.getAll).toBeCalledWith({
61
54
  filter: 'id:test-product-1,test-product-2'
62
55
  });
63
56
  });
64
57
  it('should throw errors with no options', async () => {
65
- const noConfigOrOptionsInstance = new TestProductsInstance({
58
+ const noConfigOrOptionsInstance = new _storefrontMock.StorefontTestingInstance({
66
59
  options: null
67
60
  });
68
61
  await (0, _asyncUtilities.expectConfigurationError)((0, _products.fetchProducts)({
@@ -4,29 +4,40 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.postPurchase = postPurchase;
7
+ exports.postPayment = postPayment;
8
+
9
+ var _index = require("./index");
7
10
 
8
11
  async function postPurchase({
9
12
  data,
10
13
  state
11
14
  }) {
12
- if (!state.storefront) {
13
- throw new Error('Could not access rebilly-js-sdk instance');
14
- }
15
+ return (0, _index.Endpoint)({
16
+ state
17
+ }, async () => {
18
+ if (data._raw) {
19
+ delete data._raw;
20
+ }
15
21
 
16
- if (!state.options) {
17
- throw new Error('Could not use Rebilly Instruments mount options to fetch Rebilly data');
18
- }
22
+ return state.storefront.purchase.purchase({
23
+ data
24
+ });
25
+ });
26
+ }
19
27
 
20
- try {
28
+ async function postPayment({
29
+ data,
30
+ state
31
+ }) {
32
+ return (0, _index.Endpoint)({
33
+ state
34
+ }, async () => {
21
35
  if (data._raw) {
22
36
  delete data._raw;
23
37
  }
24
38
 
25
- const resp = await state.storefront.purchase.purchase({
39
+ return state.storefront.purchase.payment({
26
40
  data
27
41
  });
28
- return resp;
29
- } catch (error) {
30
- throw error;
31
- }
42
+ });
32
43
  }
@@ -8,28 +8,9 @@ var _server = require("../../tests/msw/server");
8
8
 
9
9
  var _storefrontApiMock = require("../../tests/mocks/storefront-api-mock");
10
10
 
11
- var _purchase = require("./purchase");
12
-
13
11
  describe('Storefront API Purchase', () => {
14
- class TestPurchaseInstance {
15
- constructor({
16
- options = {}
17
- } = {}) {
18
- this.options = options;
19
- this.storefront = (0, _storefrontMock.MockStorefront)();
20
- }
21
-
22
- postPurchase(...args) {
23
- return (0, _purchase.postPurchase)({
24
- state: this,
25
- data: args
26
- });
27
- }
28
-
29
- }
30
-
31
12
  it('can make purchase', async () => {
32
- const instance = new TestPurchaseInstance();
13
+ const instance = (0, _storefrontMock.StorefontTestingInstance)();
33
14
  const payload = {
34
15
  websiteId: 'test-website-id',
35
16
  items: [{
@@ -9,22 +9,18 @@ var _riskDataCollector = require("@rebilly/risk-data-collector");
9
9
 
10
10
  var _readyToPayModel = _interopRequireDefault(require("./models/ready-to-pay-model"));
11
11
 
12
+ var _index = require("./index");
13
+
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
15
 
14
16
  async function fetchReadyToPay({
15
17
  state,
16
18
  riskMetadata = null
17
19
  }) {
18
- if (!state.storefront) {
19
- throw new Error('Could not access rebilly-js-sdk instance');
20
- }
21
-
22
- if (!state.options) {
23
- throw new Error('Could not use Rebilly Instruments mount options to fetch Rebilly data');
24
- }
25
-
26
- try {
27
- var _state$options, _state$options2;
20
+ return (0, _index.Endpoint)({
21
+ state
22
+ }, async () => {
23
+ var _state$options, _state$options2, _state$data;
28
24
 
29
25
  if (!riskMetadata) {
30
26
  const {
@@ -34,12 +30,21 @@ async function fetchReadyToPay({
34
30
  }
35
31
 
36
32
  const websiteId = ((_state$options = state.options) === null || _state$options === void 0 ? void 0 : _state$options.websiteId) || null;
37
- const items = ((_state$options2 = state.options) === null || _state$options2 === void 0 ? void 0 : _state$options2.items) || [];
38
33
  const data = {
39
- items,
40
34
  websiteId,
41
35
  riskMetadata
42
36
  };
37
+
38
+ if ((_state$options2 = state.options) !== null && _state$options2 !== void 0 && _state$options2.items) {
39
+ data.items = state.options.items;
40
+ }
41
+
42
+ if ((_state$data = state.data) !== null && _state$data !== void 0 && _state$data.amountAndCurrency) {
43
+ const money = state.data.amountAndCurrency;
44
+ data.amount = money.amount;
45
+ data.currency = money.currency;
46
+ }
47
+
43
48
  const {
44
49
  fields: readyToPayFields
45
50
  } = await state.storefront.purchase.readyToPay({
@@ -49,7 +54,5 @@ async function fetchReadyToPay({
49
54
  index,
50
55
  ...fields
51
56
  }));
52
- } catch (error) {
53
- throw error;
54
- }
57
+ });
55
58
  }
@@ -17,16 +17,6 @@ var _asyncUtilities = require("../../tests/async-utilities");
17
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
18
 
19
19
  describe('Storefront API Ready to Pay', () => {
20
- class TestReadyToPayInstance {
21
- constructor({
22
- options = {}
23
- } = {}) {
24
- this.options = options;
25
- this.storefront = (0, _storefrontMock.MockStorefront)();
26
- }
27
-
28
- }
29
-
30
20
  it('can fetch ready to pay', async () => {
31
21
  const readyToPayPayload = [{
32
22
  method: 'payment-card',
@@ -42,7 +32,7 @@ describe('Storefront API Ready to Pay', () => {
42
32
  }]
43
33
  };
44
34
  (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/ready-to-pay`)).thenReturn((0, _mswWhenThen.ok)(readyToPayPayload));
45
- const instance = new TestReadyToPayInstance({
35
+ const instance = (0, _storefrontMock.StorefontTestingInstance)({
46
36
  options
47
37
  });
48
38
  jest.spyOn(instance.storefront.purchase, 'readyToPay');
@@ -68,7 +58,7 @@ describe('Storefront API Ready to Pay', () => {
68
58
  });
69
59
  it('should throw errors with no options', async () => {
70
60
  const riskMetadata = null;
71
- const noConfigOrOptionsInstance = new TestReadyToPayInstance({
61
+ const noConfigOrOptionsInstance = new _storefrontMock.StorefontTestingInstance({
72
62
  options: null
73
63
  });
74
64
  await (0, _asyncUtilities.expectConfigurationError)((0, _readyToPay.fetchReadyToPay)({
@@ -7,32 +7,36 @@ exports.fetchSummary = fetchSummary;
7
7
 
8
8
  var _summaryModel = _interopRequireDefault(require("./models/summary-model"));
9
9
 
10
+ var _index = require("./index");
11
+
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
 
12
14
  async function fetchSummary({
13
15
  data = null,
14
16
  state = null
15
17
  } = {}) {
16
- if (!state.storefront) {
17
- throw new Error('Could not access rebilly-js-sdk instance');
18
- }
19
-
20
- if (!state.options) {
21
- throw new Error('Could not use Rebilly Instruments mount options to fetch Rebilly data');
22
- }
23
-
24
- try {
25
- var _state$options, _state$options2;
18
+ return (0, _index.Endpoint)({
19
+ state
20
+ }, async () => {
21
+ var _state$options, _state$options2, _state$data;
26
22
 
27
23
  const websiteId = ((_state$options = state.options) === null || _state$options === void 0 ? void 0 : _state$options.websiteId) || null;
28
- const items = ((_state$options2 = state.options) === null || _state$options2 === void 0 ? void 0 : _state$options2.items) || [];
29
24
  const payload = {
30
25
  data: {
31
- websiteId,
32
- items
26
+ websiteId
33
27
  }
34
28
  };
35
29
 
30
+ if ((_state$options2 = state.options) !== null && _state$options2 !== void 0 && _state$options2.items) {
31
+ payload.data.items = state.options.items;
32
+ }
33
+
34
+ if ((_state$data = state.data) !== null && _state$data !== void 0 && _state$data.amountAndCurrency) {
35
+ payload.data = { ...payload.data,
36
+ ...state.data.amountAndCurrency
37
+ };
38
+ }
39
+
36
40
  if (data !== null && data !== void 0 && data.billingAddress) {
37
41
  payload.data.billingAddress = data.billingAddress;
38
42
  }
@@ -43,9 +47,9 @@ async function fetchSummary({
43
47
 
44
48
  const {
45
49
  fields: summaryFields
46
- } = await state.storefront.purchase.preview(payload);
50
+ } = await state.storefront.purchase.preview(payload); // In case of preview purchase gets call again for updating the values
51
+
52
+ state.data.previewPurchase = summaryFields;
47
53
  return new _summaryModel.default(summaryFields);
48
- } catch (error) {
49
- throw error;
50
- }
54
+ });
51
55
  }
@@ -17,17 +17,6 @@ var _asyncUtilities = require("../../tests/async-utilities");
17
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
18
 
19
19
  describe('Storefront API Summary', () => {
20
- class TestSummaryInstance {
21
- constructor({
22
- options = {},
23
- storefront = null
24
- } = {}) {
25
- this.options = options;
26
- this.storefront = storefront === null ? (0, _storefrontMock.MockStorefront)() : storefront;
27
- }
28
-
29
- }
30
-
31
20
  it('can fetch preview', async () => {
32
21
  const options = {
33
22
  websiteId: 'test-website-id',
@@ -45,7 +34,7 @@ describe('Storefront API Summary', () => {
45
34
  }]
46
35
  };
47
36
  (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/preview-purchase`)).thenReturn((0, _mswWhenThen.ok)(testSummary));
48
- const instance = new TestSummaryInstance({
37
+ const instance = (0, _storefrontMock.StorefontTestingInstance)({
49
38
  options
50
39
  });
51
40
  jest.spyOn(instance.storefront.purchase, 'preview');
@@ -74,7 +63,7 @@ describe('Storefront API Summary', () => {
74
63
  firstName: 'Test',
75
64
  lastName: 'Customer'
76
65
  };
77
- const instance = new TestSummaryInstance({
66
+ const instance = (0, _storefrontMock.StorefontTestingInstance)({
78
67
  options
79
68
  });
80
69
  jest.spyOn(instance.storefront.purchase, 'preview');
@@ -105,7 +94,7 @@ describe('Storefront API Summary', () => {
105
94
  firstName: 'Test',
106
95
  lastName: 'Customer'
107
96
  };
108
- const instance = new TestSummaryInstance({
97
+ const instance = (0, _storefrontMock.StorefontTestingInstance)({
109
98
  options
110
99
  });
111
100
  jest.spyOn(instance.storefront.purchase, 'preview');
@@ -125,7 +114,7 @@ describe('Storefront API Summary', () => {
125
114
  });
126
115
  });
127
116
  it('should throw errors with no options', async () => {
128
- const noConfigOrOptionsInstance = new TestSummaryInstance({
117
+ const noConfigOrOptionsInstance = (0, _storefrontMock.StorefontTestingInstance)({
129
118
  options: null
130
119
  });
131
120
  await (0, _asyncUtilities.expectConfigurationError)((0, _summary.fetchSummary)({
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.fetchTransaction = fetchTransaction;
7
+
8
+ var _transactionModel = _interopRequireDefault(require("./models/transaction-model"));
9
+
10
+ var _index = require("./index");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ async function fetchTransaction({
15
+ data = null,
16
+ state = null
17
+ }) {
18
+ return (0, _index.Endpoint)({
19
+ state
20
+ }, async () => {
21
+ state.storefront.setSessionToken(state.options.customerJwt);
22
+ const {
23
+ fields
24
+ } = await state.storefront.transactions.get(data);
25
+ return new _transactionModel.default(fields);
26
+ });
27
+ }