@rebilly/instruments 1.0.2-beta.9 → 3.0.0-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 (255) hide show
  1. package/.babelrc +13 -4
  2. package/.eslintrc.js +3 -0
  3. package/.prettierrc.js +11 -0
  4. package/README.md +15 -314
  5. package/dist/events/base-event.js +6 -9
  6. package/dist/events/events.spec.js +4 -4
  7. package/dist/events/index.js +2 -1
  8. package/dist/functions/destroy.js +12 -14
  9. package/dist/functions/destroy.spec.js +3 -3
  10. package/dist/functions/mount/fetch-data.js +187 -0
  11. package/dist/functions/mount/fetch-data.spec.js +189 -0
  12. package/dist/functions/mount/index.js +164 -251
  13. package/dist/functions/mount/mount.spec.js +25 -124
  14. package/dist/functions/mount/setup-element.js +40 -0
  15. package/dist/functions/mount/setup-framepay-theme.js +95 -0
  16. package/dist/functions/mount/setup-framepay.js +46 -0
  17. package/dist/functions/mount/setup-i18n.js +33 -0
  18. package/dist/functions/mount/setup-options.js +99 -0
  19. package/dist/functions/mount/setup-options.spec.js +66 -0
  20. package/dist/functions/mount/setup-storefront.js +34 -0
  21. package/dist/functions/mount/setup-styles.js +43 -0
  22. package/dist/functions/on.js +13 -4
  23. package/dist/functions/on.spec.js +19 -5
  24. package/dist/functions/purchase.js +139 -22
  25. package/dist/functions/purchase.spec.js +23 -19
  26. package/dist/functions/setup.js +85 -0
  27. package/dist/functions/setup.spec.js +87 -0
  28. package/dist/functions/show.js +31 -14
  29. package/dist/functions/show.spec.js +47 -18
  30. package/dist/functions/update.js +53 -27
  31. package/dist/functions/update.spec.js +40 -21
  32. package/dist/i18n/en.json +5 -2
  33. package/dist/i18n/es.json +4 -1
  34. package/dist/index.js +67 -56
  35. package/dist/index.spec.js +7 -27
  36. package/dist/loader/index.js +4 -3
  37. package/dist/storefront/index.js +33 -0
  38. package/dist/storefront/invoices.js +27 -0
  39. package/dist/storefront/models/base-model.js +18 -0
  40. package/dist/storefront/models/invoice-model.js +14 -0
  41. package/dist/storefront/models/plan-model.js +4 -35
  42. package/dist/storefront/models/product-model.js +4 -23
  43. package/dist/storefront/models/summary-model.js +12 -25
  44. package/dist/storefront/models/transaction-model.js +31 -0
  45. package/dist/storefront/payment-instruments.js +47 -0
  46. package/dist/storefront/payment-instruments.spec.js +55 -0
  47. package/dist/storefront/plans.js +15 -24
  48. package/dist/storefront/plans.spec.js +17 -44
  49. package/dist/storefront/products.js +16 -20
  50. package/dist/storefront/products.spec.js +25 -49
  51. package/dist/storefront/purchase.js +28 -16
  52. package/dist/storefront/purchase.spec.js +4 -22
  53. package/dist/storefront/ready-to-pay.js +26 -22
  54. package/dist/storefront/ready-to-pay.spec.js +25 -54
  55. package/dist/storefront/storefront.spec.js +1 -1
  56. package/dist/storefront/summary.js +27 -24
  57. package/dist/storefront/summary.spec.js +44 -86
  58. package/dist/storefront/transactions.js +27 -0
  59. package/dist/style/base/__snapshots__/theme.spec.js.snap +188 -45
  60. package/dist/style/base/default-theme.js +699 -0
  61. package/dist/style/base/index.js +48 -16
  62. package/dist/style/base/theme.js +15 -48
  63. package/dist/style/base/theme.spec.js +4 -15
  64. package/dist/style/components/address.js +3 -3
  65. package/dist/style/components/button.js +32 -22
  66. package/dist/style/components/divider.js +9 -9
  67. package/dist/style/components/forms/checkbox.js +12 -9
  68. package/dist/style/components/forms/field.js +18 -6
  69. package/dist/style/components/forms/form.js +2 -2
  70. package/dist/style/components/forms/input.js +54 -13
  71. package/dist/style/components/forms/label.js +44 -18
  72. package/dist/style/components/forms/select.js +54 -22
  73. package/dist/style/components/forms/validation.js +53 -6
  74. package/dist/style/components/icons.js +4 -4
  75. package/dist/style/components/loader.js +5 -3
  76. package/dist/style/components/methods.js +52 -48
  77. package/dist/style/components/overlay.js +5 -5
  78. package/dist/style/helpers/index.js +46 -46
  79. package/dist/style/index.js +3 -1
  80. package/dist/style/payment-instruments/payment-card.js +4 -4
  81. package/dist/style/utils/border.js +47 -0
  82. package/dist/style/utils/color-values.js +37 -3
  83. package/dist/style/utils/remove-empty-null.js +20 -0
  84. package/dist/style/vendor/framepay.js +11 -8
  85. package/dist/style/vendor/postmate.js +2 -2
  86. package/dist/style/views/confirmation.js +13 -13
  87. package/dist/style/views/method-selector.js +3 -3
  88. package/dist/style/views/modal.js +8 -6
  89. package/dist/style/views/result.js +4 -4
  90. package/dist/style/views/summary.js +26 -22
  91. package/dist/utils/format-currency.js +4 -2
  92. package/dist/utils/has-valid-css-selector.js +1 -1
  93. package/dist/utils/process-property-as-dom-element.js +0 -2
  94. package/dist/views/__snapshots__/summary.spec.js.snap +72 -118
  95. package/dist/views/common/iframe/base-iframe.js +10 -2
  96. package/dist/views/common/iframe/modal-iframe.js +50 -4
  97. package/dist/views/confirmation.js +44 -20
  98. package/dist/views/method-selector/express-methods/apple-pay.js +92 -0
  99. package/dist/views/method-selector/express-methods/google-pay.js +31 -0
  100. package/dist/views/method-selector/express-methods/paypal.js +19 -0
  101. package/dist/views/method-selector/generate-digital-wallet.js +68 -0
  102. package/dist/views/method-selector/generate-digital-wallet.spec.js +135 -0
  103. package/dist/views/method-selector/get-payment-methods.js +27 -7
  104. package/dist/views/method-selector/get-payment-methods.spec.js +25 -26
  105. package/dist/views/method-selector/index.js +55 -86
  106. package/dist/views/method-selector/method-selector.spec.js +80 -69
  107. package/dist/views/method-selector/mount-express-methods.js +38 -62
  108. package/dist/views/method-selector/mount-methods.js +18 -18
  109. package/dist/views/modal.js +18 -12
  110. package/dist/views/result.js +13 -16
  111. package/dist/views/summary.js +176 -114
  112. package/dist/views/summary.spec.js +72 -76
  113. package/package.json +7 -4
  114. package/src/events/base-event.js +15 -17
  115. package/src/events/events.spec.js +6 -4
  116. package/src/events/index.js +6 -3
  117. package/src/functions/destroy.js +12 -13
  118. package/src/functions/destroy.spec.js +30 -31
  119. package/src/functions/mount/fetch-data.js +152 -0
  120. package/src/functions/mount/fetch-data.spec.js +238 -0
  121. package/src/functions/mount/index.js +131 -244
  122. package/src/functions/mount/mount.spec.js +35 -141
  123. package/src/functions/mount/setup-element.js +26 -0
  124. package/src/functions/mount/setup-framepay-theme.js +82 -0
  125. package/src/functions/mount/setup-framepay.js +41 -0
  126. package/src/functions/mount/setup-i18n.js +19 -0
  127. package/src/functions/mount/setup-options.js +103 -0
  128. package/src/functions/mount/setup-options.spec.js +60 -0
  129. package/src/functions/mount/setup-storefront.js +24 -0
  130. package/src/functions/mount/setup-styles.js +30 -0
  131. package/src/functions/on.js +13 -8
  132. package/src/functions/on.spec.js +30 -17
  133. package/src/functions/purchase.js +101 -19
  134. package/src/functions/purchase.spec.js +18 -18
  135. package/src/functions/setup.js +48 -0
  136. package/src/functions/setup.spec.js +98 -0
  137. package/src/functions/show.js +20 -10
  138. package/src/functions/show.spec.js +43 -22
  139. package/src/functions/update.js +50 -27
  140. package/src/functions/update.spec.js +57 -22
  141. package/src/i18n/en.json +5 -2
  142. package/src/i18n/es.json +4 -1
  143. package/src/i18n/i18n.spec.js +6 -4
  144. package/src/i18n/index.js +14 -11
  145. package/src/index.js +41 -52
  146. package/src/index.spec.js +8 -37
  147. package/src/loader/index.js +51 -47
  148. package/src/loader/loader.spec.js +26 -19
  149. package/src/storefront/index.js +37 -7
  150. package/src/storefront/invoices.js +11 -0
  151. package/src/storefront/models/base-model.js +10 -0
  152. package/src/storefront/models/invoice-model.js +3 -0
  153. package/src/storefront/models/plan-model.js +3 -35
  154. package/src/storefront/models/product-model.js +3 -23
  155. package/src/storefront/models/ready-to-pay-model.js +3 -3
  156. package/src/storefront/models/summary-model.js +15 -29
  157. package/src/storefront/models/transaction-model.js +19 -0
  158. package/src/storefront/payment-instruments.js +30 -0
  159. package/src/storefront/payment-instruments.spec.js +69 -0
  160. package/src/storefront/plans.js +16 -23
  161. package/src/storefront/plans.spec.js +25 -54
  162. package/src/storefront/products.js +18 -22
  163. package/src/storefront/products.spec.js +23 -54
  164. package/src/storefront/purchase.js +14 -14
  165. package/src/storefront/purchase.spec.js +17 -29
  166. package/src/storefront/ready-to-pay.js +26 -23
  167. package/src/storefront/ready-to-pay.spec.js +41 -71
  168. package/src/storefront/storefront.spec.js +1 -1
  169. package/src/storefront/summary.js +26 -22
  170. package/src/storefront/summary.spec.js +60 -109
  171. package/src/storefront/transactions.js +11 -0
  172. package/src/style/base/__snapshots__/theme.spec.js.snap +188 -45
  173. package/src/style/base/default-theme.js +674 -0
  174. package/src/style/base/index.js +48 -16
  175. package/src/style/base/theme.js +20 -48
  176. package/src/style/base/theme.spec.js +5 -15
  177. package/src/style/browserslist.js +1 -3
  178. package/src/style/components/address.js +3 -3
  179. package/src/style/components/button.js +32 -22
  180. package/src/style/components/divider.js +9 -9
  181. package/src/style/components/forms/checkbox.js +11 -9
  182. package/src/style/components/forms/field.js +18 -6
  183. package/src/style/components/forms/form.js +2 -2
  184. package/src/style/components/forms/input.js +54 -13
  185. package/src/style/components/forms/label.js +44 -18
  186. package/src/style/components/forms/select.js +54 -22
  187. package/src/style/components/forms/validation.js +53 -6
  188. package/src/style/components/icons.js +4 -4
  189. package/src/style/components/index.js +1 -1
  190. package/src/style/components/loader.js +4 -3
  191. package/src/style/components/methods.js +52 -48
  192. package/src/style/components/overlay.js +5 -5
  193. package/src/style/helpers/index.js +46 -46
  194. package/src/style/index.js +4 -2
  195. package/src/style/payment-instruments/payment-card.js +4 -4
  196. package/src/style/utils/border.js +34 -0
  197. package/src/style/utils/color-values.js +30 -4
  198. package/src/style/utils/remove-empty-null.js +10 -0
  199. package/src/style/vendor/framepay.js +12 -9
  200. package/src/style/vendor/postmate.js +3 -3
  201. package/src/style/views/confirmation.js +13 -13
  202. package/src/style/views/index.js +1 -1
  203. package/src/style/views/method-selector.js +3 -3
  204. package/src/style/views/modal.js +9 -7
  205. package/src/style/views/result.js +4 -4
  206. package/src/style/views/summary.js +26 -22
  207. package/src/utils/add-dom-element.js +12 -13
  208. package/src/utils/format-currency.js +6 -2
  209. package/src/utils/has-valid-css-selector.js +2 -2
  210. package/src/utils/is-dom-element.js +1 -1
  211. package/src/utils/process-property-as-dom-element.js +27 -24
  212. package/src/utils/sleep.js +1 -1
  213. package/src/views/__snapshots__/summary.spec.js.snap +72 -118
  214. package/src/views/common/iframe/base-iframe.js +12 -4
  215. package/src/views/common/iframe/event-listeners.js +6 -6
  216. package/src/views/common/iframe/index.js +1 -1
  217. package/src/views/common/iframe/method-iframe.js +3 -6
  218. package/src/views/common/iframe/modal-iframe.js +48 -6
  219. package/src/views/common/iframe/view-iframe.js +3 -5
  220. package/src/views/common/render-utilities.js +3 -3
  221. package/src/views/confirmation.js +34 -25
  222. package/src/views/method-selector/express-methods/apple-pay.js +78 -0
  223. package/src/views/method-selector/express-methods/google-pay.js +24 -0
  224. package/src/views/method-selector/express-methods/paypal.js +7 -0
  225. package/src/views/method-selector/generate-digital-wallet.js +51 -0
  226. package/src/views/method-selector/generate-digital-wallet.spec.js +135 -0
  227. package/src/views/method-selector/get-method-data.js +7 -4
  228. package/src/views/method-selector/get-payment-methods.js +38 -31
  229. package/src/views/method-selector/get-payment-methods.spec.js +26 -33
  230. package/src/views/method-selector/index.js +70 -99
  231. package/src/views/method-selector/method-selector.spec.js +88 -78
  232. package/src/views/method-selector/mount-express-methods.js +36 -60
  233. package/src/views/method-selector/mount-methods.js +32 -21
  234. package/src/views/modal.js +36 -22
  235. package/src/views/result.js +12 -15
  236. package/src/views/summary.js +175 -101
  237. package/src/views/summary.spec.js +99 -74
  238. package/tests/async-utilities.js +22 -0
  239. package/tests/mocks/rebilly-instruments-mock.js +89 -77
  240. package/tests/mocks/storefront-api-mock.js +8 -0
  241. package/tests/mocks/storefront-mock.js +17 -0
  242. package/dist/events/purchase-completed.js +0 -24
  243. package/dist/functions/initialize.js +0 -82
  244. package/dist/functions/initialize.spec.js +0 -34
  245. package/dist/functions/mount/fetch-summary-data.js +0 -31
  246. package/dist/functions/mount/fetch-summary-data.spec.js +0 -45
  247. package/dist/views/method-selector/process-digital-wallet-options.js +0 -35
  248. package/dist/views/method-selector/process-digital-wallet-options.spec.js +0 -80
  249. package/src/events/purchase-completed.js +0 -11
  250. package/src/functions/initialize.js +0 -74
  251. package/src/functions/initialize.spec.js +0 -38
  252. package/src/functions/mount/fetch-summary-data.js +0 -26
  253. package/src/functions/mount/fetch-summary-data.spec.js +0 -46
  254. package/src/views/method-selector/process-digital-wallet-options.js +0 -16
  255. package/src/views/method-selector/process-digital-wallet-options.spec.js +0 -94
@@ -1,100 +1,70 @@
1
- import { MockStorefront } from 'tests/mocks/storefront-mock';
1
+ import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
2
2
  import { ok, post } from 'msw-when-then';
3
3
  import { when } from 'tests/msw/server';
4
4
  import { storefrontURL } from 'tests/mocks/storefront-api-mock';
5
- import { FetchReadyToPay } from './ready-to-pay';
5
+ import { fetchReadyToPay } from './ready-to-pay';
6
6
  import ReadyToPayModel from './models/ready-to-pay-model';
7
+ import { expectConfigurationError } from 'tests/async-utilities';
7
8
 
8
9
  describe('Storefront API Ready to Pay', () => {
9
- class TestReadyToPayInstance {
10
- constructor({
11
- configs = {},
12
- options = {}
13
- } = {}) {
14
- this.configs = configs;
15
- this.options = options;
16
- this.storefront = MockStorefront();
17
- }
18
-
19
- fetchReadyToPay(...args) {
20
- return FetchReadyToPay.apply(this, args);
21
- }
22
- }
23
-
24
- it ('can fetch ready to pay', async () => {
25
- const readyToPayPayload = [{
26
- method: 'payment-card',
27
- feature:null,
28
- brands: ['Visa'],
29
- filters:[]
30
- }];
31
- const configs = {
32
- websiteId: 'test-website-id'
33
- };
34
- const options = {
35
- intent: {
36
- items: [
37
- {
38
- planId: 'test-plan-id',
39
- quantity: 1
40
- }
41
- ]
10
+ it('can fetch ready to pay', async () => {
11
+ const readyToPayPayload = [
12
+ {
13
+ method: 'payment-card',
14
+ feature: null,
15
+ brands: ['Visa'],
16
+ filters: []
42
17
  }
18
+ ];
19
+ const options = {
20
+ websiteId: 'test-website-id',
21
+ items: [
22
+ {
23
+ planId: 'test-plan-id',
24
+ quantity: 1
25
+ }
26
+ ]
43
27
  };
44
28
 
45
- when(post(`${storefrontURL}/ready-to-pay`)).thenReturn(ok(readyToPayPayload));
29
+ when(post(`${storefrontURL}/ready-to-pay`)).thenReturn(
30
+ ok(readyToPayPayload)
31
+ );
46
32
 
47
- const instance = new TestReadyToPayInstance({
48
- configs,
49
- options,
33
+ const instance = StorefontTestingInstance({
34
+ options
50
35
  });
51
36
 
52
37
  jest.spyOn(instance.storefront.purchase, 'readyToPay');
53
38
 
54
- const response = await instance.fetchReadyToPay();
55
-
39
+ const riskMetadata = null;
40
+ const response = await fetchReadyToPay({ state: instance, riskMetadata });
41
+
56
42
  expect(instance.storefront.purchase.readyToPay).toBeCalledTimes(1);
57
43
  expect(instance.storefront.purchase.readyToPay).toBeCalledWith({
58
44
  data: {
59
- items: options.intent.items,
60
- websiteId: configs.websiteId,
45
+ items: options.items,
46
+ websiteId: options.websiteId,
61
47
  riskMetadata: {}
62
48
  }
63
49
  });
64
50
  expect(response).toBeInstanceOf(Array);
65
51
  expect(response[0]).toBeInstanceOf(ReadyToPayModel);
66
- expect(response)
67
- .toEqual([new ReadyToPayModel({
52
+ expect(response).toEqual([
53
+ new ReadyToPayModel({
68
54
  index: 0,
69
55
  ...readyToPayPayload[0]
70
- })]);
56
+ })
57
+ ]);
71
58
  });
72
59
 
73
- it ('should throw errors with no configs or options', () => {
74
- const NoConfigOrOptionsError = new Error('Could not use Rebilly Instruments configurations or mount options to fetch Rebilly data');
75
- const noConfigOrOptionsInstance = new TestReadyToPayInstance({
76
- configs: null,
77
- options: null,
78
- });
79
- const noConfigInstance = new TestReadyToPayInstance({
80
- configs: null,
81
- options: {},
82
- });
83
- const noOptionsInstance = new TestReadyToPayInstance({
84
- configs: {},
85
- options: null,
86
- });
87
-
88
- expect(async () => {
89
- await noConfigOrOptionsInstance.fetchReadyToPay();
90
- }).rejects.toEqual(NoConfigOrOptionsError);
60
+ it('should throw errors with no options', async () => {
61
+ const riskMetadata = null;
91
62
 
92
- expect(async () => {
93
- await noConfigInstance.fetchReadyToPay();
94
- }).rejects.toEqual(NoConfigOrOptionsError);
95
-
96
- expect(async () => {
97
- await noOptionsInstance.fetchReadyToPay();
98
- }).rejects.toEqual(NoConfigOrOptionsError);
63
+ const noConfigOrOptionsInstance = new StorefontTestingInstance({
64
+ options: null
65
+ });
66
+ await expectConfigurationError(
67
+ fetchReadyToPay({ riskMetadata, state: noConfigOrOptionsInstance })
68
+ );
99
69
  });
100
70
  });
@@ -11,4 +11,4 @@ describe('Storefront application programming interface', () => {
11
11
  expect(storefront).toHaveProperty('purchase');
12
12
  expect(storefront.purchase).toHaveProperty('readyToPay');
13
13
  });
14
- });
14
+ });
@@ -1,37 +1,41 @@
1
1
  import SummaryModel from './models/summary-model';
2
+ import { Endpoint } from './index';
2
3
 
3
- export async function FetchSummary ({
4
- data = null
5
- } = {}) {
6
- if (!this.storefront) {
7
- throw new Error('Could not access rebilly-js-sdk instance');
8
- }
9
-
10
- if (!this.configs || !this.options) {
11
- throw new Error('Could not use Rebilly Instruments configurations or mount options to fetch Rebilly data');
12
- }
13
-
14
- try {
15
- const websiteId = this.configs?.websiteId || null;
16
- const items = this.options?.intent?.items || [];
4
+ export async function fetchSummary({ data = null, state = null } = {}) {
5
+ return Endpoint({state}, async () => {
6
+ const websiteId = state.options?.websiteId || null;
17
7
 
18
8
  const payload = {
19
9
  data: {
20
- websiteId,
21
- items
10
+ websiteId
11
+ }
12
+ };
13
+
14
+ if (state.options?.items) {
15
+ payload.data.items = state.options.items;
16
+ }
17
+
18
+ if (state.data?.amountAndCurrency) {
19
+ payload.data = {
20
+ ...payload.data,
21
+ ...state.data.amountAndCurrency
22
22
  }
23
23
  }
24
+
24
25
  if (data?.billingAddress) {
25
26
  payload.data.billingAddress = data.billingAddress;
26
27
  }
27
- if(data?.deliveryAddress) {
28
+ if (data?.deliveryAddress) {
28
29
  payload.data.deliveryAddress = data.deliveryAddress;
29
30
  }
30
31
 
31
- const {fields: summaryFields} = await this.storefront.purchase.preview(payload);
32
+ const { fields: summaryFields } = await state.storefront.purchase.preview(
33
+ payload
34
+ );
35
+
36
+ // In case of preview purchase gets call again for updating the values
37
+ state.data.previewPurchase = summaryFields;
32
38
 
33
39
  return new SummaryModel(summaryFields);
34
- } catch (error) {
35
- throw error;
36
- }
37
- };
40
+ });
41
+ }
@@ -1,40 +1,21 @@
1
- import { MockStorefront } from 'tests/mocks/storefront-mock';
1
+ import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
2
2
  import { ok, post } from 'msw-when-then';
3
3
  import { when } from 'tests/msw/server';
4
4
  import { storefrontURL } from 'tests/mocks/storefront-api-mock';
5
- import { FetchSummary } from './summary';
5
+ import { fetchSummary } from './summary';
6
6
  import SummaryModel from '@/storefront/models/summary-model';
7
+ import { expectConfigurationError } from 'tests/async-utilities';
7
8
 
8
9
  describe('Storefront API Summary', () => {
9
- class TestSummaryInstance {
10
- constructor({
11
- configs = {},
12
- options = {},
13
- storefront = null,
14
- } = {}) {
15
- this.configs = configs;
16
- this.options = options;
17
- this.storefront = storefront === null ? MockStorefront() : storefront;
18
- }
19
-
20
- fetchSummary(...args) {
21
- return FetchSummary.apply(this, args);
22
- }
23
- }
24
-
25
- it ('can fetch preview', async () => {
26
- const configs = {
27
- websiteId: 'test-website-id'
28
- };
10
+ it('can fetch preview', async () => {
29
11
  const options = {
30
- intent: {
31
- items: [
32
- {
33
- planId: 'test-plan-id',
34
- quantity: 1
35
- }
36
- ]
37
- }
12
+ websiteId: 'test-website-id',
13
+ items: [
14
+ {
15
+ planId: 'test-plan-id',
16
+ quantity: 1
17
+ }
18
+ ]
38
19
  };
39
20
  const testSummary = {
40
21
  currency: 'USD',
@@ -42,142 +23,112 @@ describe('Storefront API Summary', () => {
42
23
  {
43
24
  description: 'test-plan-id-1',
44
25
  planId: 'test-plan-id-1',
45
- quantity: 1,
26
+ quantity: 1
46
27
  }
47
28
  ]
48
29
  };
49
30
 
50
31
  when(post(`${storefrontURL}/preview-purchase`)).thenReturn(ok(testSummary));
51
32
 
52
- const instance = new TestSummaryInstance({
53
- configs,
54
- options,
33
+ const instance = StorefontTestingInstance({
34
+ options
55
35
  });
56
36
 
57
37
  jest.spyOn(instance.storefront.purchase, 'preview');
58
38
 
59
- const response = await instance.fetchSummary();
60
-
39
+ const response = await fetchSummary({ state: instance });
40
+
61
41
  expect(instance.storefront.purchase.preview).toBeCalledTimes(1);
62
42
  expect(instance.storefront.purchase.preview).toBeCalledWith({
63
43
  data: {
64
- items: options.intent.items,
65
- websiteId: configs.websiteId,
44
+ items: options.items,
45
+ websiteId: options.websiteId
66
46
  }
67
47
  });
68
48
  expect(response).toBeInstanceOf(SummaryModel);
69
49
  expect(response).toEqual(new SummaryModel(testSummary));
70
-
71
50
  });
72
51
 
73
- it ('Adds billing address to preview payload', async () => {
74
- const configs = {
75
- websiteId: 'test-website-id'
76
- };
52
+ it('Adds billing address to preview payload', async () => {
77
53
  const options = {
78
- intent: {
79
- items: [
80
- {
81
- planId: 'test-plan-id',
82
- quantity: 1
83
- }
84
- ]
85
- }
54
+ websiteId: 'test-website-id',
55
+ items: [
56
+ {
57
+ planId: 'test-plan-id',
58
+ quantity: 1
59
+ }
60
+ ]
86
61
  };
87
62
  const billingAddress = {
88
63
  firstName: 'Test',
89
64
  lastName: 'Customer'
90
65
  };
91
- const instance = new TestSummaryInstance({
92
- configs,
93
- options,
66
+ const instance = StorefontTestingInstance({
67
+ options
94
68
  });
95
69
 
96
70
  jest.spyOn(instance.storefront.purchase, 'preview');
97
71
 
98
- await instance.fetchSummary({
72
+ await fetchSummary({
99
73
  data: {
100
- billingAddress,
101
- }
74
+ billingAddress
75
+ },
76
+ state: instance
102
77
  });
103
-
78
+
104
79
  expect(instance.storefront.purchase.preview).toBeCalledTimes(1);
105
80
  expect(instance.storefront.purchase.preview).toBeCalledWith({
106
81
  data: {
107
- items: options.intent.items,
108
- websiteId: configs.websiteId,
109
- billingAddress,
82
+ items: options.items,
83
+ websiteId: options.websiteId,
84
+ billingAddress
110
85
  }
111
86
  });
112
87
  });
113
88
 
114
- it ('Adds delivery address to preview payload', async () => {
115
- const configs = {
116
- websiteId: 'test-website-id'
117
- };
89
+ it('Adds delivery address to preview payload', async () => {
118
90
  const options = {
119
- intent: {
120
- items: [
121
- {
122
- planId: 'test-plan-id',
123
- quantity: 1
124
- }
125
- ]
126
- }
91
+ websiteId: 'test-website-id',
92
+ items: [
93
+ {
94
+ planId: 'test-plan-id',
95
+ quantity: 1
96
+ }
97
+ ]
127
98
  };
128
99
  const deliveryAddress = {
129
100
  firstName: 'Test',
130
101
  lastName: 'Customer'
131
102
  };
132
- const instance = new TestSummaryInstance({
133
- configs,
134
- options,
103
+ const instance = StorefontTestingInstance({
104
+ options
135
105
  });
136
106
 
137
107
  jest.spyOn(instance.storefront.purchase, 'preview');
138
108
 
139
- await instance.fetchSummary({
109
+ await fetchSummary({
140
110
  data: {
141
- deliveryAddress,
142
- }
111
+ deliveryAddress
112
+ },
113
+ state: instance
143
114
  });
144
-
115
+
145
116
  expect(instance.storefront.purchase.preview).toBeCalledTimes(1);
146
117
  expect(instance.storefront.purchase.preview).toBeCalledWith({
147
118
  data: {
148
- items: options.intent.items,
149
- websiteId: configs.websiteId,
150
- deliveryAddress,
119
+ items: options.items,
120
+ websiteId: options.websiteId,
121
+ deliveryAddress
151
122
  }
152
123
  });
153
-
154
124
  });
155
125
 
156
- it ('should throw errors with no configs or options', () => {
157
- const NoConfigOrOptionsError = new Error('Could not use Rebilly Instruments configurations or mount options to fetch Rebilly data');
158
- const noConfigOrOptionsInstance = new TestSummaryInstance({
159
- configs: null,
160
- options: null,
161
- });
162
- const noConfigInstance = new TestSummaryInstance({
163
- configs: null,
164
- options: {},
165
- });
166
- const noOptionsInstance = new TestSummaryInstance({
167
- configs: {},
168
- options: null,
126
+ it('should throw errors with no options', async () => {
127
+ const noConfigOrOptionsInstance = StorefontTestingInstance({
128
+ options: null
169
129
  });
170
-
171
- expect(async () => {
172
- await noConfigOrOptionsInstance.fetchSummary();
173
- }).rejects.toEqual(NoConfigOrOptionsError);
174
-
175
- expect(async () => {
176
- await noConfigInstance.fetchSummary();
177
- }).rejects.toEqual(NoConfigOrOptionsError);
178
-
179
- expect(async () => {
180
- await noOptionsInstance.fetchSummary();
181
- }).rejects.toEqual(NoConfigOrOptionsError);
130
+ await expectConfigurationError(
131
+ fetchSummary({ state: noConfigOrOptionsInstance })
132
+ );
182
133
  });
183
134
  });
@@ -0,0 +1,11 @@
1
+ import TransactionModel from './models/transaction-model';
2
+ import { Endpoint } from './index';
3
+
4
+ export async function fetchTransaction({ data = null, state = null }) {
5
+ return Endpoint({state}, async () => {
6
+ state.storefront.setSessionToken(state.options.customerJwt);
7
+ const {fields} = await state.storefront.transactions.get(data);
8
+
9
+ return new TransactionModel(fields);
10
+ });
11
+ }