@rebilly/instruments 1.0.2-beta.8 → 2.1.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 (211) 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 +183 -0
  11. package/dist/functions/mount/fetch-data.spec.js +189 -0
  12. package/dist/functions/mount/index.js +158 -251
  13. package/dist/functions/mount/mount.spec.js +24 -121
  14. package/dist/functions/mount/setup-element.js +40 -0
  15. package/dist/functions/mount/setup-framepay.js +46 -0
  16. package/dist/functions/mount/setup-i18n.js +33 -0
  17. package/dist/functions/mount/setup-options.js +96 -0
  18. package/dist/functions/mount/setup-options.spec.js +66 -0
  19. package/dist/functions/mount/setup-storefront.js +34 -0
  20. package/dist/functions/mount/setup-styles.js +43 -0
  21. package/dist/functions/on.js +13 -4
  22. package/dist/functions/on.spec.js +19 -5
  23. package/dist/functions/purchase.js +139 -22
  24. package/dist/functions/purchase.spec.js +23 -19
  25. package/dist/functions/setup.js +85 -0
  26. package/dist/functions/setup.spec.js +87 -0
  27. package/dist/functions/show.js +31 -14
  28. package/dist/functions/show.spec.js +47 -18
  29. package/dist/functions/update.js +53 -27
  30. package/dist/functions/update.spec.js +40 -21
  31. package/dist/i18n/en.json +4 -1
  32. package/dist/i18n/es.json +4 -1
  33. package/dist/index.js +67 -56
  34. package/dist/index.spec.js +7 -27
  35. package/dist/loader/index.js +4 -3
  36. package/dist/storefront/index.js +33 -0
  37. package/dist/storefront/invoices.js +27 -0
  38. package/dist/storefront/models/base-model.js +18 -0
  39. package/dist/storefront/models/invoice-model.js +14 -0
  40. package/dist/storefront/models/plan-model.js +4 -35
  41. package/dist/storefront/models/product-model.js +4 -23
  42. package/dist/storefront/models/summary-model.js +12 -25
  43. package/dist/storefront/models/transaction-model.js +31 -0
  44. package/dist/storefront/payment-instruments.js +47 -0
  45. package/dist/storefront/payment-instruments.spec.js +55 -0
  46. package/dist/storefront/plans.js +15 -24
  47. package/dist/storefront/plans.spec.js +17 -44
  48. package/dist/storefront/products.js +16 -20
  49. package/dist/storefront/products.spec.js +25 -49
  50. package/dist/storefront/purchase.js +28 -16
  51. package/dist/storefront/purchase.spec.js +4 -22
  52. package/dist/storefront/ready-to-pay.js +26 -22
  53. package/dist/storefront/ready-to-pay.spec.js +25 -54
  54. package/dist/storefront/storefront.spec.js +1 -1
  55. package/dist/storefront/summary.js +27 -24
  56. package/dist/storefront/summary.spec.js +44 -86
  57. package/dist/storefront/transactions.js +27 -0
  58. package/dist/style/base/theme.js +3 -3
  59. package/dist/style/components/methods.js +43 -42
  60. package/dist/style/utils/color-values.js +1 -3
  61. package/dist/style/views/confirmation.js +0 -4
  62. package/dist/style/views/method-selector.js +1 -1
  63. package/dist/style/views/modal.js +3 -1
  64. package/dist/style/views/summary.js +5 -1
  65. package/dist/utils/format-currency.js +4 -2
  66. package/dist/utils/has-valid-css-selector.js +1 -1
  67. package/dist/utils/process-property-as-dom-element.js +0 -2
  68. package/dist/views/__snapshots__/summary.spec.js.snap +103 -113
  69. package/dist/views/common/iframe/base-iframe.js +10 -2
  70. package/dist/views/common/iframe/modal-iframe.js +44 -3
  71. package/dist/views/confirmation.js +44 -20
  72. package/dist/views/method-selector/express-methods/apple-pay.js +92 -0
  73. package/dist/views/method-selector/express-methods/google-pay.js +31 -0
  74. package/dist/views/method-selector/express-methods/paypal.js +19 -0
  75. package/dist/views/method-selector/generate-digital-wallet.js +68 -0
  76. package/dist/views/method-selector/generate-digital-wallet.spec.js +135 -0
  77. package/dist/views/method-selector/get-payment-methods.js +28 -8
  78. package/dist/views/method-selector/get-payment-methods.spec.js +25 -26
  79. package/dist/views/method-selector/index.js +55 -86
  80. package/dist/views/method-selector/method-selector.spec.js +80 -69
  81. package/dist/views/method-selector/mount-express-methods.js +38 -62
  82. package/dist/views/method-selector/mount-methods.js +18 -18
  83. package/dist/views/modal.js +21 -15
  84. package/dist/views/result.js +13 -16
  85. package/dist/views/summary.js +170 -114
  86. package/dist/views/summary.spec.js +72 -76
  87. package/package.json +5 -4
  88. package/src/events/base-event.js +15 -17
  89. package/src/events/events.spec.js +6 -4
  90. package/src/events/index.js +6 -3
  91. package/src/functions/destroy.js +12 -13
  92. package/src/functions/destroy.spec.js +30 -31
  93. package/src/functions/mount/fetch-data.js +148 -0
  94. package/src/functions/mount/fetch-data.spec.js +238 -0
  95. package/src/functions/mount/index.js +129 -244
  96. package/src/functions/mount/mount.spec.js +35 -139
  97. package/src/functions/mount/setup-element.js +26 -0
  98. package/src/functions/mount/setup-framepay.js +41 -0
  99. package/src/functions/mount/setup-i18n.js +19 -0
  100. package/src/functions/mount/setup-options.js +100 -0
  101. package/src/functions/mount/setup-options.spec.js +60 -0
  102. package/src/functions/mount/setup-storefront.js +24 -0
  103. package/src/functions/mount/setup-styles.js +30 -0
  104. package/src/functions/on.js +13 -8
  105. package/src/functions/on.spec.js +30 -17
  106. package/src/functions/purchase.js +101 -19
  107. package/src/functions/purchase.spec.js +18 -18
  108. package/src/functions/setup.js +48 -0
  109. package/src/functions/setup.spec.js +98 -0
  110. package/src/functions/show.js +20 -10
  111. package/src/functions/show.spec.js +43 -22
  112. package/src/functions/update.js +50 -27
  113. package/src/functions/update.spec.js +57 -22
  114. package/src/i18n/en.json +4 -1
  115. package/src/i18n/es.json +4 -1
  116. package/src/i18n/i18n.spec.js +6 -4
  117. package/src/i18n/index.js +14 -11
  118. package/src/index.js +41 -52
  119. package/src/index.spec.js +8 -37
  120. package/src/loader/index.js +51 -47
  121. package/src/loader/loader.spec.js +26 -19
  122. package/src/storefront/index.js +37 -7
  123. package/src/storefront/invoices.js +11 -0
  124. package/src/storefront/models/base-model.js +10 -0
  125. package/src/storefront/models/invoice-model.js +3 -0
  126. package/src/storefront/models/plan-model.js +3 -35
  127. package/src/storefront/models/product-model.js +3 -23
  128. package/src/storefront/models/ready-to-pay-model.js +3 -3
  129. package/src/storefront/models/summary-model.js +15 -29
  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 +16 -23
  134. package/src/storefront/plans.spec.js +25 -54
  135. package/src/storefront/products.js +18 -22
  136. package/src/storefront/products.spec.js +23 -54
  137. package/src/storefront/purchase.js +14 -14
  138. package/src/storefront/purchase.spec.js +17 -29
  139. package/src/storefront/ready-to-pay.js +26 -23
  140. package/src/storefront/ready-to-pay.spec.js +41 -71
  141. package/src/storefront/storefront.spec.js +1 -1
  142. package/src/storefront/summary.js +26 -22
  143. package/src/storefront/summary.spec.js +60 -109
  144. package/src/storefront/transactions.js +11 -0
  145. package/src/style/base/theme.js +10 -8
  146. package/src/style/base/theme.spec.js +4 -2
  147. package/src/style/browserslist.js +1 -3
  148. package/src/style/components/button.js +3 -1
  149. package/src/style/components/forms/checkbox.js +3 -1
  150. package/src/style/components/index.js +1 -1
  151. package/src/style/components/loader.js +3 -1
  152. package/src/style/components/methods.js +43 -42
  153. package/src/style/helpers/index.js +1 -1
  154. package/src/style/index.js +2 -1
  155. package/src/style/utils/color-values.js +4 -4
  156. package/src/style/vendor/framepay.js +1 -1
  157. package/src/style/vendor/postmate.js +1 -1
  158. package/src/style/views/confirmation.js +0 -4
  159. package/src/style/views/index.js +1 -1
  160. package/src/style/views/method-selector.js +1 -1
  161. package/src/style/views/modal.js +4 -2
  162. package/src/style/views/summary.js +5 -1
  163. package/src/utils/add-dom-element.js +12 -13
  164. package/src/utils/format-currency.js +6 -2
  165. package/src/utils/has-valid-css-selector.js +2 -2
  166. package/src/utils/is-dom-element.js +1 -1
  167. package/src/utils/process-property-as-dom-element.js +27 -24
  168. package/src/utils/sleep.js +1 -1
  169. package/src/views/__snapshots__/summary.spec.js.snap +103 -113
  170. package/src/views/common/iframe/base-iframe.js +12 -4
  171. package/src/views/common/iframe/event-listeners.js +6 -6
  172. package/src/views/common/iframe/index.js +1 -1
  173. package/src/views/common/iframe/method-iframe.js +3 -6
  174. package/src/views/common/iframe/modal-iframe.js +42 -6
  175. package/src/views/common/iframe/view-iframe.js +3 -5
  176. package/src/views/common/render-utilities.js +3 -3
  177. package/src/views/confirmation.js +34 -25
  178. package/src/views/method-selector/express-methods/apple-pay.js +78 -0
  179. package/src/views/method-selector/express-methods/google-pay.js +24 -0
  180. package/src/views/method-selector/express-methods/paypal.js +7 -0
  181. package/src/views/method-selector/generate-digital-wallet.js +51 -0
  182. package/src/views/method-selector/generate-digital-wallet.spec.js +135 -0
  183. package/src/views/method-selector/get-method-data.js +7 -4
  184. package/src/views/method-selector/get-payment-methods.js +38 -29
  185. package/src/views/method-selector/get-payment-methods.spec.js +26 -33
  186. package/src/views/method-selector/index.js +70 -99
  187. package/src/views/method-selector/method-selector.spec.js +88 -78
  188. package/src/views/method-selector/mount-express-methods.js +36 -60
  189. package/src/views/method-selector/mount-methods.js +32 -21
  190. package/src/views/modal.js +37 -23
  191. package/src/views/result.js +12 -15
  192. package/src/views/summary.js +169 -101
  193. package/src/views/summary.spec.js +99 -74
  194. package/tests/async-utilities.js +22 -0
  195. package/tests/mocks/rebilly-instruments-mock.js +89 -77
  196. package/tests/mocks/storefront-api-mock.js +8 -0
  197. package/tests/mocks/storefront-mock.js +17 -0
  198. package/dist/events/purchase-completed.js +0 -24
  199. package/dist/functions/initialize.js +0 -82
  200. package/dist/functions/initialize.spec.js +0 -34
  201. package/dist/functions/mount/fetch-summary-data.js +0 -31
  202. package/dist/functions/mount/fetch-summary-data.spec.js +0 -45
  203. package/dist/views/method-selector/process-digital-wallet-options.js +0 -35
  204. package/dist/views/method-selector/process-digital-wallet-options.spec.js +0 -80
  205. package/src/events/purchase-completed.js +0 -11
  206. package/src/functions/initialize.js +0 -74
  207. package/src/functions/initialize.spec.js +0 -38
  208. package/src/functions/mount/fetch-summary-data.js +0 -26
  209. package/src/functions/mount/fetch-summary-data.spec.js +0 -46
  210. package/src/views/method-selector/process-digital-wallet-options.js +0 -16
  211. package/src/views/method-selector/process-digital-wallet-options.spec.js +0 -94
@@ -3,121 +3,180 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.MountSummary = MountSummary;
7
- exports.UpdateSummary = UpdateSummary;
8
- exports.baseSummaryHTML = void 0;
6
+ exports.lineItemHTML = lineItemHTML;
7
+ exports.summaryBreakdownHTML = summaryBreakdownHTML;
8
+ exports.mountSummary = mountSummary;
9
+ exports.updateSummary = updateSummary;
9
10
 
10
11
  var _riskDataCollector = require("@rebilly/risk-data-collector");
11
12
 
12
13
  var _utils = require("../utils");
13
14
 
14
- var _fetchSummaryData = require("../functions/mount/fetch-summary-data");
15
-
16
- const baseSummaryHTML = `
17
- <div class="rebilly-instruments-content">
18
- <div class="rebilly-instruments-summary-line-items"></div>
19
- <div class="rebilly-instruments-summary-breakdown"></div>
20
- </div>
21
- `;
22
- exports.baseSummaryHTML = baseSummaryHTML;
23
-
24
- function MountSummary({
25
- summary,
26
- plans,
27
- products
28
- } = {}) {
29
- this.summary.innerHTML += baseSummaryHTML;
30
- const itemsContainer = document.querySelector('.rebilly-instruments-summary-line-items');
31
- const summaryBreakdown = document.querySelector('.rebilly-instruments-summary-breakdown');
15
+ var _fetchData = require("../functions/mount/fetch-data");
16
+
17
+ function lineItemHTML({
18
+ state,
19
+ lineItem
20
+ }) {
21
+ var _state$data;
22
+
32
23
  const {
33
- intent: {
34
- items: optionItems
35
- }
36
- } = this.options;
37
- const hasThumbnails = optionItems.some(optionItem => optionItem.thumbnail);
24
+ products
25
+ } = state.data;
38
26
  const {
39
- lineItems,
40
- currency,
41
- discountsAmount,
42
- shippingAmount,
43
- subtotalAmount,
44
- taxAmount,
45
- total
46
- } = summary;
47
- lineItems.forEach(lineItem => {
48
- const {
49
- thumbnail
50
- } = optionItems.find(optionItem => optionItem.planId === lineItem.planId);
51
- const {
52
- name
53
- } = plans.find(plan => plan.id === lineItem.planId);
54
- const {
55
- description
56
- } = products.find(product => product.id === lineItem.productId);
57
- itemsContainer.innerHTML += `
58
- <div class="rebilly-instruments-summary-line-item">
59
- ${hasThumbnails ? `
60
- <figure class="rebilly-instruments-summary-line-item-figure">
61
- ${thumbnail ? `<img src="${thumbnail}" alt="${name}">` : ''}
62
- </figure>
63
- ` : ''}
64
- <div class="rebilly-instruments-summary-line-item-synopsis">
65
- <p class="rebilly-instruments-summary-line-item-synopsis-title">${name}</p>
66
- ${description ? `
67
- <p class="rebilly-instruments-summary-line-item-synopsis-description">${description}</p>
68
- ` : ''}
69
- </div>
70
- <div class="rebilly-instruments-summary-line-item-price-breakdown">
71
- <p class="rebilly-instruments-summary-line-item-price-breakdown-quantity">${lineItem.quantity}</p>
72
- <svg class="rebilly-instruments-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
73
- <path d="M12 10.5858l2.8284-2.8284c.3906-.3906 1.0237-.3906 1.4142 0 .3906.3905.3906 1.0236 0 1.4142L13.4142 12l2.8284 2.8284c.3906.3906.3906 1.0237 0 1.4142-.3905.3906-1.0236.3906-1.4142 0L12 13.4142l-2.8284 2.8284c-.3906.3906-1.0237.3906-1.4142 0-.3906-.3905-.3906-1.0236 0-1.4142L10.5858 12 7.7574 9.1716c-.3906-.3906-.3906-1.0237 0-1.4142.3905-.3906 1.0236-.3906 1.4142 0L12 10.5858z" fill-rule="nonzero"/>
74
- </svg>
75
- <p class="rebilly-instruments-summary-line-item-price-breakdown-unit-price">${(0, _utils.formatCurrency)(lineItem.unitPrice, currency)}</p>
76
- </div>
27
+ currency = null
28
+ } = (_state$data = state.data) === null || _state$data === void 0 ? void 0 : _state$data.amountAndCurrency;
29
+ const {
30
+ items: optionItems = []
31
+ } = state.options;
32
+ const hasThumbnails = optionItems === null || optionItems === void 0 ? void 0 : optionItems.some(optionItem => optionItem.thumbnail);
33
+ const {
34
+ thumbnail = null
35
+ } = optionItems.find(optionItem => optionItem.planId === lineItem.planId) || {};
36
+ const {
37
+ name
38
+ } = products.find(product => product.id === lineItem.productId);
39
+ return `
40
+ <div class="rebilly-instruments-summary-line-item">
41
+ ${hasThumbnails ? `
42
+ <figure class="rebilly-instruments-summary-line-item-figure">
43
+ ${thumbnail ? `<img src="${thumbnail}" alt="${name}">` : ''}
44
+ </figure>
45
+ ` : ''}
46
+ <div class="rebilly-instruments-summary-line-item-synopsis">
47
+ <p class="rebilly-instruments-summary-line-item-synopsis-title">${name}</p>
48
+ ${lineItem.description ? `
49
+ <p class="rebilly-instruments-summary-line-item-synopsis-description">${lineItem.description}</p>
50
+ ` : ''}
51
+ </div>
52
+ <div class="rebilly-instruments-summary-line-item-price-breakdown">
53
+ <p class="rebilly-instruments-summary-line-item-price-breakdown-quantity">${lineItem.quantity}</p>
54
+ <svg class="rebilly-instruments-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
55
+ <path d="M12 10.5858l2.8284-2.8284c.3906-.3906 1.0237-.3906 1.4142 0 .3906.3905.3906 1.0236 0 1.4142L13.4142 12l2.8284 2.8284c.3906.3906.3906 1.0237 0 1.4142-.3905.3906-1.0236.3906-1.4142 0L12 13.4142l-2.8284 2.8284c-.3906.3906-1.0237.3906-1.4142 0-.3906-.3905-.3906-1.0236 0-1.4142L10.5858 12 7.7574 9.1716c-.3906-.3906-.3906-1.0237 0-1.4142.3905-.3906 1.0236-.3906 1.4142 0L12 10.5858z" fill-rule="nonzero"/>
56
+ </svg>
57
+ <p class="rebilly-instruments-summary-line-item-price-breakdown-unit-price">${(0, _utils.formatCurrency)(lineItem.unitPrice, currency)}</p>
77
58
  </div>
78
- `;
79
- });
80
- summaryBreakdown.innerHTML = `
81
- <table>
82
- <colgroup>
83
- <col>
84
- <col>
85
- </colgroup>
86
- <tr class="rebilly-instruments-summary-breakdown-sub-total">
87
- <td data-rebilly-i18n="summary.subTotal">Sub Total</td>
88
- <td>${(0, _utils.formatCurrency)(subtotalAmount, currency)}</td>
89
- </tr>
90
- <tr class="rebilly-instruments-summary-breakdown-discounts">
91
- <td data-rebilly-i18n="summary.discounts">Discounts</td>
92
- <td>${(0, _utils.formatCurrency)(discountsAmount, currency)}</td>
93
- </tr>
94
- <tr class="rebilly-instruments-summary-breakdown-taxes">
95
- <td data-rebilly-i18n="summary.taxes">Taxes</td>
96
- <td>${(0, _utils.formatCurrency)(taxAmount, currency)}</td>
97
- </tr>
98
- <tr class="rebilly-instruments-summary-breakdown-taxes">
99
- <td data-rebilly-i18n="summary.shipping">Shipping</td>
100
- <td>${(0, _utils.formatCurrency)(shippingAmount, currency)}</td>
101
- </tr>
102
- </table>
103
- <div class="rebilly-instruments-summary-breakdown-total">
104
- <p data-rebilly-i18n="summary.total">Total</p>
105
- <p class="rebilly-instruments-summary-breakdown-total-amount">
106
- <span class="rebilly-instruments-summary-breakdown-total-amount-currency">${currency}</span>${(0, _utils.formatCurrency)(total, currency)}
107
- </p>
108
59
  </div>
109
60
  `;
110
- this.translate.translateItems();
111
- this.loader.stopLoading({
61
+ }
62
+
63
+ function summaryBreakdownHTML({
64
+ state,
65
+ element
66
+ }) {
67
+ const {
68
+ amount = null,
69
+ currency = null,
70
+ discountsAmount = null,
71
+ shippingAmount = null,
72
+ subtotalAmount = null,
73
+ taxAmount = null
74
+ } = { ...state.data.amountAndCurrency,
75
+ ...state.data.summaryItems
76
+ };
77
+ let table = null;
78
+ [{
79
+ label: 'Sub Total',
80
+ class: 'sub-total',
81
+ i18n: 'summary.subTotal',
82
+ value: subtotalAmount
83
+ }, {
84
+ label: 'Discounts',
85
+ class: 'discounts',
86
+ i18n: 'summary.discounts',
87
+ value: discountsAmount
88
+ }, {
89
+ label: 'Taxes',
90
+ class: 'taxes',
91
+ i18n: 'summary.taxes',
92
+ value: taxAmount
93
+ }, {
94
+ label: 'Shipping',
95
+ class: 'shipping',
96
+ i18n: 'summary.shipping',
97
+ value: shippingAmount
98
+ }].filter(({
99
+ value
100
+ }) => value !== null).forEach(item => {
101
+ if (!table) {
102
+ table = document.createElement('table');
103
+ table.insertAdjacentHTML('beforeend', `
104
+ <colgroup>
105
+ <col>
106
+ <col>
107
+ </colgroup>
108
+ `);
109
+ }
110
+
111
+ const row = document.createElement('tr');
112
+ row.setAttribute('class', `rebilly-instruments-summary-breakdown-${item.class}`);
113
+ row.insertAdjacentHTML('beforeend', `
114
+ <td data-rebilly-i18n="${item.i18n}">${item.label}</td>
115
+ <td>${(0, _utils.formatCurrency)(item.value, currency)}</td>
116
+ `);
117
+ table.appendChild(row);
118
+ });
119
+
120
+ if (table) {
121
+ element.appendChild(table);
122
+ }
123
+
124
+ const totalElement = document.createElement('div');
125
+ totalElement.setAttribute('class', 'rebilly-instruments-summary-breakdown-total');
126
+ totalElement.insertAdjacentHTML('beforeend', `
127
+ <p data-rebilly-i18n="summary.total">Total</p>
128
+ <p class="rebilly-instruments-summary-breakdown-total-amount">
129
+ <span class="rebilly-instruments-summary-breakdown-total-amount-currency">
130
+ ${currency}
131
+ </span>
132
+ ${(0, _utils.formatCurrency)(amount, currency)}
133
+ <p>
134
+ `);
135
+ element.appendChild(totalElement);
136
+ return element;
137
+ }
138
+
139
+ async function mountSummary({
140
+ state
141
+ }) {
142
+ const contentContainer = document.createElement('div');
143
+ contentContainer.setAttribute('class', 'rebilly-instruments-content');
144
+ const lineItems = state.data.summaryLineItems;
145
+
146
+ if (lineItems) {
147
+ const lineItemsElement = document.createElement('div');
148
+ lineItemsElement.setAttribute('class', 'rebilly-instruments-summary-line-items');
149
+ lineItems.forEach(lineItem => {
150
+ lineItemsElement.insertAdjacentHTML('beforeend', lineItemHTML({
151
+ state,
152
+ lineItem
153
+ }));
154
+ });
155
+ contentContainer.appendChild(lineItemsElement);
156
+ }
157
+
158
+ const summaryBreakdownElement = document.createElement('div');
159
+ summaryBreakdownElement.setAttribute('class', 'rebilly-instruments-summary-breakdown');
160
+ contentContainer.appendChild(summaryBreakdownHTML({
161
+ state,
162
+ element: summaryBreakdownElement
163
+ }));
164
+ state.summary.appendChild(contentContainer);
165
+ state.summary.style.minHeight = '';
166
+ state.translate.translateItems();
167
+ state.loader.stopLoading({
112
168
  section: 'summary',
113
169
  id: 'initSummary'
114
170
  });
115
171
  }
116
172
 
117
- ;
173
+ async function updateSummary({
174
+ state,
175
+ instrument = null
176
+ }) {
177
+ var _state$data$transacti;
118
178
 
119
- async function UpdateSummary(instrument = null) {
120
- this.loader.startLoading({
179
+ state.loader.startLoading({
121
180
  section: 'summary',
122
181
  id: 'initSummary'
123
182
  });
@@ -132,22 +191,19 @@ async function UpdateSummary(instrument = null) {
132
191
  };
133
192
  }
134
193
 
135
- const {
136
- readyToPay,
137
- summary: summaryData,
138
- plans,
139
- products
140
- } = await _fetchSummaryData.FetchSummaryData.call(this, {
194
+ state.data = await (0, _fetchData.fetchData)({
141
195
  riskMetadata,
142
- summaryPayload
196
+ summaryPayload,
197
+ state
143
198
  });
144
- this.summaryOptions = {
145
- summary: summaryData,
146
- readyToPay,
147
- plans,
148
- products
149
- };
150
- const itemsContainer = this.summary.querySelector('.rebilly-instruments-content');
199
+
200
+ if (state.data.transaction && ((_state$data$transacti = state.data.transaction) === null || _state$data$transacti === void 0 ? void 0 : _state$data$transacti.type) === 'setup') {
201
+ state.options.transactionType = 'setup';
202
+ }
203
+
204
+ const itemsContainer = state.summary.querySelector('.rebilly-instruments-content');
151
205
  itemsContainer === null || itemsContainer === void 0 ? void 0 : itemsContainer.remove();
152
- MountSummary.call(this, this.summaryOptions);
206
+ mountSummary({
207
+ state
208
+ });
153
209
  }
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
 
3
- var _summaryModel = _interopRequireDefault(require("@/storefront/models/summary-model"));
3
+ var _summaryModel = _interopRequireDefault(require("../storefront/models/summary-model"));
4
4
 
5
- var _planModel = _interopRequireDefault(require("@/storefront/models/plan-model"));
5
+ var _planModel = _interopRequireDefault(require("../storefront/models/plan-model"));
6
6
 
7
- var _productModel = _interopRequireDefault(require("@/storefront/models/product-model"));
7
+ var _productModel = _interopRequireDefault(require("../storefront/models/product-model"));
8
8
 
9
9
  var _loader = require("../loader");
10
10
 
@@ -12,6 +12,8 @@ var _i18n = require("../i18n");
12
12
 
13
13
  var _summary = require("./summary");
14
14
 
15
+ var _fetchData = require("../functions/mount/fetch-data");
16
+
15
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
18
 
17
19
  describe('Summary component', () => {
@@ -24,93 +26,88 @@ describe('Summary component', () => {
24
26
 
25
27
  class TestMountSummaryInstance {
26
28
  constructor({
27
- configs = {},
28
29
  options = {},
29
30
  summary = summaryElement,
30
31
  loader = new _loader.Loader(),
31
- translate = new _i18n.Translate()
32
+ translate = new _i18n.Translate(),
33
+ data = {}
32
34
  } = {}) {
33
- this.configs = configs;
34
35
  this.options = options;
35
36
  this.summary = summary;
36
37
  this.loader = loader;
37
38
  this.translate = translate;
38
- }
39
-
40
- mountSummary(summaryProductsPlans, ...args) {
41
- this.loader.DOM.summary = this.summary;
42
- return _summary.MountSummary.apply(this, args.concat(summaryProductsPlans));
39
+ this.data = new _fetchData.DataInstance({
40
+ state: {
41
+ options
42
+ },
43
+ previewPurchase: new _summaryModel.default({
44
+ currency: 'USD',
45
+ lineItems: [{
46
+ type: 'debit',
47
+ description: 'My Awesome Product',
48
+ unitPrice: 30,
49
+ quantity: 1,
50
+ price: 30,
51
+ productId: 'test-product-1',
52
+ planId: 'my-awesome-product'
53
+ }, {
54
+ type: 'debit',
55
+ description: 'Awesome T-Shirt',
56
+ unitPrice: 20,
57
+ quantity: 2,
58
+ price: 40,
59
+ productId: 'test-product-2',
60
+ planId: 'awesome-t-shirt'
61
+ }],
62
+ subtotalAmount: 70,
63
+ taxAmount: 0,
64
+ shippingAmount: 0,
65
+ discountsAmount: 0,
66
+ total: 70
67
+ }),
68
+ plans: [new _planModel.default({
69
+ name: 'My Awesome Product',
70
+ id: 'my-awesome-product',
71
+ productId: 'test-product-1'
72
+ }), new _planModel.default({
73
+ name: 'My Awesome T-Shirt',
74
+ id: 'awesome-t-shirt',
75
+ productId: 'test-product-2'
76
+ })],
77
+ products: [new _productModel.default({
78
+ name: 'My Awesome Product',
79
+ id: 'test-product-1'
80
+ }), new _productModel.default({
81
+ name: 'My Awesome T-Shirt',
82
+ id: 'test-product-2'
83
+ })],
84
+ ...data
85
+ });
43
86
  }
44
87
 
45
88
  }
46
89
 
47
90
  const options = {
48
- intent: {
49
- countryCode: 'US',
50
- items: [{
51
- planId: 'my-awesome-product',
52
- quantity: 1,
53
- thumbnail: ''
54
- }, {
55
- planId: 'awesome-t-shirt',
56
- quantity: 2,
57
- thumbnail: ''
58
- }]
59
- }
60
- };
61
- const summaryData = new _summaryModel.default({
62
- currency: "USD",
63
- lineItems: [{
64
- type: "debit",
65
- description: "My Awesome Product",
66
- unitPrice: 30,
91
+ websiteId: 'test-website-id',
92
+ countryCode: 'US',
93
+ items: [{
94
+ planId: 'my-awesome-product',
67
95
  quantity: 1,
68
- price: 30,
69
- productId: "test-product-1",
70
- planId: "my-awesome-product"
96
+ thumbnail: ''
71
97
  }, {
72
- type: "debit",
73
- description: "Awesome T-Shirt",
74
- unitPrice: 20,
98
+ planId: 'awesome-t-shirt',
75
99
  quantity: 2,
76
- price: 40,
77
- productId: "test-product-2",
78
- planId: "awesome-t-shirt"
79
- }],
80
- subtotalAmount: 70,
81
- taxAmount: 0,
82
- shippingAmount: 0,
83
- discountsAmount: 0,
84
- total: 70
85
- });
86
- const planData = [new _planModel.default({
87
- name: 'My Awesome Product',
88
- id: 'my-awesome-product',
89
- productId: 'test-product-1'
90
- }), new _planModel.default({
91
- name: 'My Awesome T-Shirt',
92
- id: 'awesome-t-shirt',
93
- productId: 'test-product-2'
94
- })];
95
- const productData = [new _productModel.default({
96
- description: 'My Awesome Product',
97
- id: 'test-product-1'
98
- }), new _productModel.default({
99
- description: 'My Awesome T-Shirt',
100
- id: 'test-product-2'
101
- })];
102
- const configs = {
103
- websiteId: 'test-website-id'
100
+ thumbnail: ''
101
+ }]
104
102
  };
105
103
  it('should render the summary correctly', () => {
106
104
  const mountSummaryInstance = new TestMountSummaryInstance({
107
- configs,
108
105
  options
109
- });
110
- mountSummaryInstance.mountSummary({
111
- summary: summaryData,
112
- plans: planData,
113
- products: productData
106
+ }); //TODO: improve design to avoid this manual change
107
+
108
+ mountSummaryInstance.loader.DOM.summary = mountSummaryInstance.summary;
109
+ (0, _summary.mountSummary)({
110
+ state: mountSummaryInstance
114
111
  }); // Number of line items
115
112
 
116
113
  const itemsContainer = document.querySelector('.rebilly-instruments-summary-line-items');
@@ -121,15 +118,14 @@ describe('Summary component', () => {
121
118
  });
122
119
  it('should not render the plan description if its falsy', () => {
123
120
  const mountSummaryInstance = new TestMountSummaryInstance({
124
- configs,
125
121
  options
126
122
  }); // Making one product description falsy
127
123
 
128
- productData[0].description = null;
129
- mountSummaryInstance.mountSummary({
130
- summary: summaryData,
131
- plans: planData,
132
- products: productData
124
+ mountSummaryInstance.data.previewPurchase.lineItems[0].description = null; //TODO: improve design to avoid this manual change
125
+
126
+ mountSummaryInstance.loader.DOM.summary = mountSummaryInstance.summary;
127
+ (0, _summary.mountSummary)({
128
+ state: mountSummaryInstance
133
129
  }); // Check that only one description is render
134
130
 
135
131
  const itemsSynopsysDescription = document.querySelectorAll('.rebilly-instruments-summary-line-item-synopsis-description');
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@rebilly/instruments",
3
- "version": "1.0.2-beta.8",
3
+ "version": "2.1.1-beta.0",
4
4
  "author": "Rebilly",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "build": "yarn babel src --out-dir dist --copy-files",
9
9
  "dev": "yarn babel src --watch --out-dir dist",
10
- "test": "yarn jest"
10
+ "test": "yarn jest",
11
+ "test:watch": "yarn jest --watchAll"
11
12
  },
12
13
  "dependencies": {
13
14
  "@babel/cli": "^7.14.5",
@@ -19,9 +20,9 @@
19
20
  "lodash.isequal": "^4.5.0",
20
21
  "lodash.kebabcase": "^4.1.1",
21
22
  "lodash.merge": "^4.6.2",
23
+ "popostmate": "^1.6.4",
22
24
  "postcss": "^8.3.6",
23
- "postmate": "^1.5.2",
24
- "rebilly-js-sdk": "^42.0.2",
25
+ "rebilly-js-sdk": "^44.4.0",
25
26
  "values.js": "^2.0.0"
26
27
  },
27
28
  "devDependencies": {
@@ -5,42 +5,40 @@ export class RegisteredListeners {
5
5
  document.addEventListener(eventName, callback, false);
6
6
 
7
7
  if (!(eventName in this._listeners)) {
8
- this._listeners[eventName] = []
8
+ this._listeners[eventName] = [];
9
9
  }
10
10
  this._listeners[eventName].push(callback);
11
11
  }
12
12
 
13
13
  removeAll() {
14
- Object.keys(this._listeners).forEach(eventName => {
15
- this._listeners[eventName].forEach(callback =>
16
- document.removeEventListener(eventName, callback, false));
17
- })
14
+ Object.keys(this._listeners).forEach((eventName) => {
15
+ this._listeners[eventName].forEach((callback) =>
16
+ document.removeEventListener(eventName, callback, false)
17
+ );
18
+ });
18
19
  this._listeners = {};
19
20
  }
20
21
  }
21
22
 
22
23
  export const registeredListeners = new RegisteredListeners();
23
24
 
24
- export default class BaseEvent {
25
+ export default class BaseEvent {
25
26
  /**
26
- * @param {string} name
27
+ * @param {string} name
27
28
  */
28
29
  constructor(name) {
29
- this.name = name;
30
+ // Using namespaced internal name as a protection mechanism
31
+ const PREFIX = 'rebilly-instruments-';
32
+ this.internalName = PREFIX+ name;
30
33
  }
31
34
 
32
- addEventListener (callback) {
33
- const innerCallback = ({detail}) => callback(detail);
34
- registeredListeners.add(this.name, innerCallback);
35
+ addEventListener(callback) {
36
+ const innerCallback = ({ detail }) => callback(detail);
37
+ registeredListeners.add(this.internalName, innerCallback);
35
38
  }
36
39
 
37
- removeEventListener() {
38
- document.removeEventListener(this.name, this.callback, false);
39
- this.callback = null;
40
- }
41
-
42
40
  dispatch(detail) {
43
- const event = new CustomEvent(this.name, {
41
+ const event = new CustomEvent(this.internalName, {
44
42
  bubbles: true,
45
43
  detail
46
44
  });
@@ -2,8 +2,10 @@ import kebabCase from 'lodash.kebabcase';
2
2
  import events, { publicEventNames } from '@/events';
3
3
 
4
4
  it('Should export all public event names', async () => {
5
- Object.keys(events).forEach(internalEventName => {
6
- expect(events[internalEventName].name).toEqual(kebabCase(internalEventName))
5
+ Object.keys(events).forEach((internalEventName) => {
6
+ expect(events[internalEventName].internalName).toEqual(
7
+ `rebilly-instruments-${kebabCase(internalEventName)}`
8
+ );
7
9
  expect(kebabCase(internalEventName) in publicEventNames);
8
- })
9
- });
10
+ });
11
+ });
@@ -3,9 +3,12 @@ import BaseEvent from './base-event';
3
3
 
4
4
  const events = {
5
5
  instrumentReady: new BaseEvent('instrument-ready'),
6
- purchaseCompleted: new BaseEvent('purchase-completed')
7
- }
6
+ purchaseCompleted: new BaseEvent('purchase-completed'),
7
+ setupCompleted: new BaseEvent('setup-completed')
8
+ };
8
9
 
9
10
  export default events;
10
11
 
11
- export const publicEventNames = Object.keys(events).map(internalName => kebabCase(internalName));
12
+ export const publicEventNames = Object.keys(events).map((internalName) =>
13
+ kebabCase(internalName)
14
+ );
@@ -1,23 +1,22 @@
1
- import {cancellation} from 'rebilly-js-sdk';
1
+ import { cancellation } from 'rebilly-js-sdk';
2
2
  import { registeredListeners } from '../events/base-event';
3
3
  import { sleep } from '../utils';
4
4
 
5
- export async function Destroy() {
5
+ export async function destroy({ state }) {
6
6
  // wait to allow for cancellation to catch any pending api requests
7
7
  const sleepMilliseconds = 1000;
8
8
  await sleep(sleepMilliseconds);
9
-
10
- [...this.iframeComponents || {}].forEach(iframe => {
11
- iframe.component?.destroy();
9
+ [...(state.iframeComponents || {})].forEach((iframe) => {
10
+ iframe.destroy();
12
11
  });
13
12
 
14
13
  registeredListeners.removeAll(document);
15
-
16
- this.iframeComponents = [];
17
- this.hasMounted = false;
18
-
19
- this.summary.textContent = '';
20
- this.form.textContent = '';
14
+
15
+ state.iframeComponents = [];
16
+ state.hasMounted = false;
17
+
18
+ state.summary.textContent = '';
19
+ state.form.textContent = '';
21
20
  cancellation.cancelAll();
22
- this.loader.clearAll();
23
- };
21
+ state.loader.clearAll();
22
+ }