@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
@@ -3,56 +3,76 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.validateOptions = validateOptions;
7
+ exports.default = exports.defaults = void 0;
7
8
 
8
9
  var _lodash = _interopRequireDefault(require("lodash.merge"));
9
10
 
10
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12
 
12
- var _default = (options = {}) => {
13
- const {
14
- _dev
15
- } = options; // TODO: validate options
16
-
17
- const _computed = {
18
- paymentMethodsUrl: _dev ? _dev.paymentMethodsUrl || 'https://forms.local.rebilly.dev:3000' : 'https://forms.secure-payments.app'
19
- };
20
- const defaults = {
21
- countryCode: 'US',
22
- locale: 'auto',
23
- paymentInstruments: {
24
- address: {
25
- name: 'default',
26
- region: 'default',
27
- hide: [],
28
- show: [],
29
- require: []
30
- },
31
- compactExpressInstruments: true,
32
- googlePay: {
33
- displayOptions: {
34
- buttonColor: 'black',
35
- buttonType: 'short',
36
- buttonHeight: '44px'
37
- }
38
- },
39
- applePay: {
40
- displayOptions: {
41
- buttonColor: 'black',
42
- buttonType: 'plain',
43
- buttonHeight: '44px'
44
- }
45
- },
46
- paymentCard: {
47
- popup: false
13
+ const defaults = {
14
+ countryCode: 'US',
15
+ locale: 'auto',
16
+ theme: {
17
+ labels: 'floating'
18
+ },
19
+ paymentInstruments: {
20
+ address: {
21
+ name: 'default',
22
+ region: 'default',
23
+ hide: [],
24
+ show: [],
25
+ require: []
26
+ },
27
+ compactExpressInstruments: true,
28
+ googlePay: {
29
+ displayOptions: {
30
+ buttonColor: 'black',
31
+ buttonType: 'short',
32
+ buttonHeight: '44px'
33
+ }
34
+ },
35
+ applePay: {
36
+ displayOptions: {
37
+ buttonColor: 'black',
38
+ buttonType: 'plain',
39
+ buttonHeight: '44px'
48
40
  }
49
41
  },
50
- features: {
51
- autoConfirmation: true,
52
- autoResult: true
42
+ paymentCard: {
43
+ popup: false
53
44
  }
45
+ },
46
+ transactionType: 'purchase',
47
+ features: {
48
+ autoConfirmation: true,
49
+ autoResult: true
50
+ }
51
+ };
52
+ exports.defaults = defaults;
53
+
54
+ function validateOptions(options) {
55
+ // TODO: validate more options
56
+ const purchaseData = [options.items, options.invoiceId, options.money, options.transactionId].filter(v => v);
57
+
58
+ if (purchaseData.length === 0) {
59
+ throw new Error('Must provide purchase data');
60
+ }
61
+
62
+ if (purchaseData.length > 1) {
63
+ throw new Error('Must provide only one purchase data type');
64
+ }
65
+ }
66
+
67
+ var _default = ({
68
+ options = {}
69
+ } = {}) => {
70
+ validateOptions(options);
71
+ const _computed = {
72
+ paymentMethodsUrl: options._dev ? options._dev.paymentMethodsUrl || 'https://forms.local.rebilly.dev:3000' : 'https://forms.secure-payments.app'
54
73
  };
55
- const combinedOptions = (0, _lodash.default)(defaults, {
74
+ const combinedOptions = (0, _lodash.default)({ ...defaults
75
+ }, {
56
76
  organizationId: options.organizationId,
57
77
  publishableKey: options.publishableKey,
58
78
  websiteId: options.websiteId,
@@ -60,18 +80,19 @@ var _default = (options = {}) => {
60
80
  i18n: options.i18n,
61
81
  theme: options.theme,
62
82
  css: options.css,
63
- items: options.items,
64
83
  locale: options.locale,
65
84
  countryCode: options.countryCode,
66
85
  features: options.features,
67
86
  paymentInstruments: options.paymentInstruments,
87
+ transactionType: options.transactionType,
68
88
  _computed
69
- });
70
-
71
- if (_dev) {
72
- combinedOptions._dev = _dev;
73
- }
89
+ }); // Add optional key's
74
90
 
91
+ ['items', 'money', 'invoiceId', 'transactionId', 'customerJwt', '_dev'].forEach(key => {
92
+ if (options[key]) {
93
+ combinedOptions[key] = options[key];
94
+ }
95
+ });
75
96
  return combinedOptions;
76
97
  };
77
98
 
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+
3
+ var _rebillyInstrumentsMock = require("../../../tests/mocks/rebilly-instruments-mock");
4
+
5
+ var _setupOptions = _interopRequireWildcard(require("./setup-options"));
6
+
7
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
8
+
9
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
+
11
+ describe('Setup mount options', () => {
12
+ it("should fill with default options", () => {
13
+ const options = (0, _setupOptions.default)({
14
+ options: {
15
+ items: [{
16
+ planId: "test-plan-id",
17
+ quantity: 1
18
+ }]
19
+ }
20
+ });
21
+ expect(options).toMatchObject(_setupOptions.defaults);
22
+ });
23
+ it("should setup options from mount", async () => {
24
+ const options = {
25
+ publishableKey: 'test-publishable-key',
26
+ organizationId: 'test-organization-id',
27
+ websiteId: 'test-website-id',
28
+ invoiceId: 'test-invoice-id',
29
+ customerJwt: 'test-customer-jwt'
30
+ };
31
+ const rebillyInstruments = await (0, _rebillyInstrumentsMock.RenderMockRebillyInstruments)(options);
32
+ expect(rebillyInstruments.state.options).toMatchObject(options);
33
+ });
34
+ });
35
+ describe('Validate mount options', () => {
36
+ it("should throw an error with no purchase data", async () => {
37
+ let error = null;
38
+
39
+ try {
40
+ const options = {};
41
+ (0, _setupOptions.validateOptions)(options);
42
+ } catch (e) {
43
+ error = e;
44
+ }
45
+
46
+ expect(error.message).toBe('Must provide purchase data');
47
+ });
48
+ it("should throw error if there are more than one purchase property", () => {
49
+ let error = null;
50
+
51
+ try {
52
+ const options = {
53
+ invoiceId: "test-invoice-id",
54
+ items: [{
55
+ planId: "test-plan-id",
56
+ quantity: 1
57
+ }]
58
+ };
59
+ (0, _setupOptions.validateOptions)(options);
60
+ } catch (e) {
61
+ error = e;
62
+ }
63
+
64
+ expect(error.message).toBe('Must provide only one purchase data type');
65
+ });
66
+ });
@@ -10,10 +10,12 @@ var _storefront = _interopRequireDefault(require("../../storefront"));
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
12
12
  var _default = ({
13
- publishableKey,
14
- orgnizationId,
15
- apiMode,
16
- _dev
13
+ options: {
14
+ publishableKey,
15
+ orgnizationId,
16
+ apiMode,
17
+ _dev
18
+ }
17
19
  }) => {
18
20
  const storefront = {
19
21
  publishableKey,
@@ -10,8 +10,10 @@ var _style = require("../../style");
10
10
  var _utils = require("../../utils");
11
11
 
12
12
  var _default = async ({
13
- theme = {},
14
- css
13
+ options: {
14
+ theme = {},
15
+ css
16
+ }
15
17
  } = {}) => {
16
18
  // Adds base stylesheet
17
19
  const style = await (0, _style.mainStyle)(theme || {});
@@ -3,6 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.makePayment = makePayment;
7
+ exports.makePurchase = makePurchase;
8
+ exports.handleApprovalUrl = handleApprovalUrl;
6
9
  exports.purchase = purchase;
7
10
 
8
11
  var _purchase = require("../storefront/purchase");
@@ -11,43 +14,145 @@ var _events = _interopRequireDefault(require("../events"));
11
14
 
12
15
  var _modal = require("../views/modal");
13
16
 
17
+ var _fetchData = require("./mount/fetch-data");
18
+
14
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
20
 
21
+ async function makePayment({
22
+ state,
23
+ payload
24
+ }) {
25
+ const {
26
+ _raw: {
27
+ id: token
28
+ }
29
+ } = payload;
30
+ const data = {
31
+ token,
32
+ ...payload
33
+ };
34
+
35
+ if (state.options.invoiceId) {
36
+ data.invoiceId = state.options.invoiceId;
37
+ }
38
+
39
+ if (state.options.transactionId) {
40
+ data.transactionId = state.options.transactionId;
41
+ }
42
+
43
+ if (state.options.money) {
44
+ data.websiteId = state.options.websiteId;
45
+ data.amount = state.options.money.amount;
46
+ data.currency = state.options.money.currency;
47
+ }
48
+
49
+ let {
50
+ fields
51
+ } = await (0, _purchase.postPayment)({
52
+ state,
53
+ data
54
+ });
55
+ fields = {
56
+ transaction: fields,
57
+ token: fields.token || state.options.customerJwt
58
+ };
59
+
60
+ if (state.data.invoice) {
61
+ fields.invoice = state.data.invoice;
62
+ }
63
+
64
+ return fields;
65
+ }
66
+
67
+ async function makePurchase({
68
+ state,
69
+ payload
70
+ }) {
71
+ const {
72
+ fields
73
+ } = await (0, _purchase.postPurchase)({
74
+ state,
75
+ data: {
76
+ websiteId: state.options.websiteId,
77
+ items: state.options.items,
78
+ paymentInstruction: {
79
+ token: payload._raw.id
80
+ },
81
+ ...payload
82
+ }
83
+ });
84
+ return fields;
85
+ }
86
+
87
+ function handleApprovalUrl({
88
+ state,
89
+ fields
90
+ }) {
91
+ const {
92
+ paymentMethodsUrl
93
+ } = state.options._computed;
94
+ const model = {};
95
+
96
+ if (state.data.isPayment) {
97
+ model.payment = fields;
98
+ } else {
99
+ model.purchase = fields;
100
+ }
101
+
102
+ state.data = new _fetchData.DataInstance({
103
+ state,
104
+ ...fields
105
+ });
106
+ (0, _modal.mountModal)({
107
+ state,
108
+ name: 'rebilly-instruments-approval-url',
109
+ url: `${paymentMethodsUrl}/approval-url`,
110
+ model,
111
+ close: updatedPurchase => {
112
+ _events.default.purchaseCompleted.dispatch(updatedPurchase);
113
+ }
114
+ });
115
+ }
116
+
16
117
  async function purchase({
17
118
  state,
18
- payload: purchasePayload
119
+ payload
19
120
  }) {
20
121
  try {
21
- const {
22
- fields: purchaseFields
23
- } = await (0, _purchase.postPurchase)({
24
- state,
25
- data: purchasePayload
26
- });
27
-
28
- if (purchaseFields.transaction.approvalUrl) {
29
- const {
30
- paymentMethodsUrl
31
- } = state.options._computed;
32
- (0, _modal.mountModal)({
122
+ var _fields$transaction;
123
+
124
+ let fields;
125
+
126
+ if (state.data.isPayment) {
127
+ fields = await makePayment({
33
128
  state,
34
- name: 'rebilly-instruments-approval-url',
35
- url: `${paymentMethodsUrl}/approval-url`,
36
- model: {
37
- purchase: purchaseFields
38
- },
39
- close: updatedPurchase => {
40
- // TODO: Check if this is purchase...
41
- _events.default.purchaseCompleted.dispatch(updatedPurchase);
42
- }
129
+ payload
43
130
  });
44
131
  } else {
45
- _events.default.purchaseCompleted.dispatch(purchaseFields);
132
+ fields = await makePurchase({
133
+ state,
134
+ payload
135
+ });
46
136
  }
47
137
 
48
- return purchaseFields;
138
+ if ((_fields$transaction = fields.transaction) !== null && _fields$transaction !== void 0 && _fields$transaction.approvalUrl) {
139
+ handleApprovalUrl({
140
+ state,
141
+ fields
142
+ });
143
+ } else {
144
+ _events.default.purchaseCompleted.dispatch(fields);
145
+ }
146
+
147
+ return fields;
49
148
  } catch (error) {
50
149
  // TODO: Display error to customer
150
+ console.error(error);
151
+
152
+ if (error.status === 422) {
153
+ error.details.forEach(e => console.error(e));
154
+ }
155
+
51
156
  return error;
52
157
  }
53
158
  }
@@ -18,12 +18,10 @@ describe('RebillyInstruments purchase', () => {
18
18
  it('should be able to make a purchase', async () => {
19
19
  const options = {
20
20
  websiteId: 'test-website-id',
21
- intent: {
22
- items: [{
23
- planId: 'test-plan-id',
24
- quantity: 1
25
- }]
26
- }
21
+ items: [{
22
+ planId: 'test-plan-id',
23
+ quantity: 1
24
+ }]
27
25
  };
28
26
  const token = {
29
27
  id: 'test-token-id'
@@ -45,11 +43,16 @@ describe('RebillyInstruments purchase', () => {
45
43
  };
46
44
  (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/purchase`)).thenReturn((0, _mswWhenThen.ok)(fields));
47
45
  const rebillyInstruments = await (0, _rebillyInstrumentsMock.RenderMockRebillyInstruments)(options);
48
- jest.spyOn(rebillyInstruments.state.storefront.purchase, 'purchase');
46
+ const spyStorefrontPurchase = jest.spyOn(rebillyInstruments.state.storefront.purchase, 'purchase');
49
47
  jest.spyOn(_events.default.purchaseCompleted, 'dispatch');
50
48
  const purchaseCompletedListener = jest.fn();
51
49
  rebillyInstruments.on('purchase-completed', purchaseCompletedListener);
52
50
  const purchasePayload = {
51
+ billingAddress,
52
+ deliveryAddress,
53
+ _raw: token
54
+ };
55
+ const purchasePayloadParsed = {
53
56
  websiteId: rebillyInstruments.state.options.websiteId,
54
57
  items: rebillyInstruments.state.options.items,
55
58
  billingAddress,
@@ -59,9 +62,9 @@ describe('RebillyInstruments purchase', () => {
59
62
  }
60
63
  };
61
64
  await rebillyInstruments.purchase(purchasePayload);
62
- expect(rebillyInstruments.state.storefront.purchase.purchase).toBeCalledTimes(1);
63
- expect(rebillyInstruments.state.storefront.purchase.purchase).toBeCalledWith(expect.objectContaining({
64
- data: purchasePayload
65
+ expect(spyStorefrontPurchase).toBeCalledTimes(1);
66
+ expect(spyStorefrontPurchase).toBeCalledWith(expect.objectContaining({
67
+ data: purchasePayloadParsed
65
68
  }));
66
69
  expect(_events.default.purchaseCompleted.dispatch).toBeCalledTimes(1);
67
70
  expect(purchaseCompletedListener).toBeCalledTimes(1);
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setup = setup;
7
+
8
+ var _paymentInstruments = require("../storefront/payment-instruments");
9
+
10
+ var _events = _interopRequireDefault(require("../events"));
11
+
12
+ var _modal = require("../views/modal");
13
+
14
+ var _fetchData = require("./mount/fetch-data");
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ async function setup({
19
+ state,
20
+ payload
21
+ }) {
22
+ try {
23
+ var _state$options;
24
+
25
+ const {
26
+ instrument,
27
+ transaction
28
+ } = await (0, _paymentInstruments.setupPaymentInstrument)({
29
+ state,
30
+ data: {
31
+ token: payload._raw.id,
32
+ websiteId: (_state$options = state.options) === null || _state$options === void 0 ? void 0 : _state$options.websiteId,
33
+ ...payload
34
+ }
35
+ });
36
+ state.data = new _fetchData.DataInstance({
37
+ state,
38
+ instrument,
39
+ transaction
40
+ });
41
+
42
+ if (transaction.approvalUrl) {
43
+ const {
44
+ paymentMethodsUrl
45
+ } = state.options._computed;
46
+ (0, _modal.mountModal)({
47
+ state,
48
+ name: 'rebilly-instruments-approval-url',
49
+ url: `${paymentMethodsUrl}/approval-url`,
50
+ model: {
51
+ setup: {
52
+ transaction
53
+ }
54
+ },
55
+ close: ({
56
+ transaction: updatedTransaction = transaction
57
+ }) => {
58
+ _events.default.setupCompleted.dispatch({
59
+ instrument,
60
+ transaction: updatedTransaction
61
+ });
62
+ }
63
+ });
64
+ } else {
65
+ _events.default.setupCompleted.dispatch({
66
+ instrument,
67
+ transaction
68
+ });
69
+ }
70
+
71
+ return {
72
+ instrument,
73
+ transaction
74
+ };
75
+ } catch (error) {
76
+ // TODO: Display error to customer
77
+ console.error(error);
78
+
79
+ if (error.status === 422) {
80
+ error.details.forEach(e => console.error(e));
81
+ }
82
+
83
+ return error;
84
+ }
85
+ }
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+
3
+ var _rebillyInstrumentsMock = require("../../tests/mocks/rebilly-instruments-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
+ var _events = _interopRequireDefault(require("../events"));
12
+
13
+ var _asyncUtilities = require("../../tests/async-utilities");
14
+
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+
17
+ describe('RebillyInstruments setup', () => {
18
+ it('should be able to setup a payment instrument', async () => {
19
+ const options = {
20
+ websiteId: 'test-website-id',
21
+ money: {
22
+ amount: 30,
23
+ currency: 'USD'
24
+ },
25
+ transactionType: 'setup'
26
+ };
27
+ const token = {
28
+ id: 'test-token-id'
29
+ };
30
+ const billingAddress = {
31
+ billing: 'address'
32
+ };
33
+ const deliveryAddress = {
34
+ delivery: 'address'
35
+ };
36
+ const paymentInstrumentFields = {
37
+ id: 'payment-instrument-id'
38
+ };
39
+ const paymentInstrumentSetupFields = {
40
+ id: 'payment-instrument-id',
41
+ approvalUrl: null
42
+ };
43
+ (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/payment-instruments`)).thenReturn((0, _mswWhenThen.ok)(paymentInstrumentFields));
44
+ (0, _server.when)((0, _mswWhenThen.post)(`${_storefrontApiMock.storefrontURL}/payment-instruments/*/setup`)).thenReturn((0, _mswWhenThen.ok)(paymentInstrumentSetupFields));
45
+ const rebillyInstruments = await (0, _rebillyInstrumentsMock.RenderMockRebillyInstruments)(options);
46
+ const spyCreatePaymentInstrument = jest.spyOn(rebillyInstruments.state.storefront.paymentInstruments, 'create');
47
+ const spySetupPaymentInstrument = jest.spyOn(rebillyInstruments.state.storefront.paymentInstruments, 'setup');
48
+ const spyDispatchSetupCompleted = jest.spyOn(_events.default.setupCompleted, 'dispatch');
49
+ const setupCompletedListener = jest.fn();
50
+ rebillyInstruments.on('setup-completed', setupCompletedListener);
51
+ const setupPayload = {
52
+ billingAddress,
53
+ deliveryAddress,
54
+ ...options.money,
55
+ _raw: token
56
+ };
57
+ rebillyInstruments.mock.data({
58
+ previewPurchase: {
59
+ currency: 'USD',
60
+ total: 30
61
+ }
62
+ });
63
+ await rebillyInstruments.setup(setupPayload);
64
+ expect(spyCreatePaymentInstrument).toBeCalledTimes(1);
65
+ expect(spyCreatePaymentInstrument).toBeCalledWith(expect.objectContaining({
66
+ data: expect.objectContaining({
67
+ token: token.id
68
+ })
69
+ }));
70
+ expect(spySetupPaymentInstrument).toBeCalledTimes(1);
71
+ expect(spySetupPaymentInstrument).toBeCalledWith(expect.objectContaining({
72
+ id: expect.stringContaining(paymentInstrumentFields.id),
73
+ data: expect.objectContaining({
74
+ websiteId: rebillyInstruments.state.options.websiteId,
75
+ currency: 'USD',
76
+ amount: 30
77
+ })
78
+ }));
79
+ expect(spyDispatchSetupCompleted).toBeCalledTimes(1);
80
+ expect(setupCompletedListener).toBeCalledTimes(1);
81
+ expect(setupCompletedListener).toBeCalledWith(expect.objectContaining({
82
+ instrument: paymentInstrumentFields,
83
+ transaction: paymentInstrumentSetupFields
84
+ }));
85
+ await (0, _asyncUtilities.avoidUnhandledPromises)();
86
+ });
87
+ });
@@ -32,8 +32,10 @@ async function show({
32
32
  iframe.destroy();
33
33
  return false;
34
34
  });
35
- payload.state = state;
36
- (0, _result.mountResult)(payload);
35
+ (0, _result.mountResult)({
36
+ payload,
37
+ state
38
+ });
37
39
  break;
38
40
 
39
41
  case 'confirmation':
@@ -41,8 +43,10 @@ async function show({
41
43
  iframe.destroy();
42
44
  return false;
43
45
  });
44
- payload.state = state;
45
- (0, _confirmation.mountConfirmation)(payload);
46
+ (0, _confirmation.mountConfirmation)({
47
+ payload,
48
+ state
49
+ });
46
50
  break;
47
51
 
48
52
  default: