@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.
- package/dist/events/index.js +2 -1
- package/dist/functions/mount/fetch-data.js +187 -0
- package/dist/functions/mount/fetch-data.spec.js +189 -0
- package/dist/functions/mount/index.js +132 -164
- package/dist/functions/mount/mount.spec.js +2 -4
- package/dist/functions/mount/setup-element.js +40 -0
- package/dist/functions/mount/setup-framepay-theme.js +95 -0
- package/dist/functions/mount/setup-framepay.js +5 -1
- package/dist/functions/mount/setup-i18n.js +33 -0
- package/dist/functions/mount/setup-options.js +68 -47
- package/dist/functions/mount/setup-options.spec.js +66 -0
- package/dist/functions/mount/setup-storefront.js +6 -4
- package/dist/functions/mount/setup-styles.js +4 -2
- package/dist/functions/purchase.js +129 -24
- package/dist/functions/purchase.spec.js +13 -10
- package/dist/functions/setup.js +85 -0
- package/dist/functions/setup.spec.js +87 -0
- package/dist/functions/show.js +8 -4
- package/dist/functions/show.spec.js +9 -5
- package/dist/functions/update.js +39 -24
- package/dist/functions/update.spec.js +0 -4
- package/dist/i18n/en.json +5 -2
- package/dist/i18n/es.json +4 -1
- package/dist/index.js +17 -3
- package/dist/index.spec.js +3 -16
- package/dist/loader/index.js +4 -3
- package/dist/storefront/index.js +33 -0
- package/dist/storefront/invoices.js +27 -0
- package/dist/storefront/models/base-model.js +18 -0
- package/dist/storefront/models/invoice-model.js +14 -0
- package/dist/storefront/models/plan-model.js +4 -35
- package/dist/storefront/models/product-model.js +4 -23
- package/dist/storefront/models/summary-model.js +12 -25
- package/dist/storefront/models/transaction-model.js +31 -0
- package/dist/storefront/payment-instruments.js +47 -0
- package/dist/storefront/payment-instruments.spec.js +55 -0
- package/dist/storefront/plans.js +10 -18
- package/dist/storefront/plans.spec.js +3 -13
- package/dist/storefront/products.js +10 -13
- package/dist/storefront/products.spec.js +12 -19
- package/dist/storefront/purchase.js +23 -12
- package/dist/storefront/purchase.spec.js +1 -20
- package/dist/storefront/ready-to-pay.js +18 -15
- package/dist/storefront/ready-to-pay.spec.js +2 -12
- package/dist/storefront/summary.js +21 -17
- package/dist/storefront/summary.spec.js +4 -15
- package/dist/storefront/transactions.js +27 -0
- package/dist/style/base/__snapshots__/theme.spec.js.snap +188 -45
- package/dist/style/base/default-theme.js +699 -0
- package/dist/style/base/index.js +48 -16
- package/dist/style/base/theme.js +16 -48
- package/dist/style/base/theme.spec.js +4 -15
- package/dist/style/components/address.js +3 -3
- package/dist/style/components/button.js +32 -22
- package/dist/style/components/divider.js +9 -9
- package/dist/style/components/forms/checkbox.js +12 -9
- package/dist/style/components/forms/field.js +18 -6
- package/dist/style/components/forms/form.js +2 -2
- package/dist/style/components/forms/input.js +54 -13
- package/dist/style/components/forms/label.js +39 -18
- package/dist/style/components/forms/select.js +54 -22
- package/dist/style/components/forms/validation.js +53 -6
- package/dist/style/components/icons.js +4 -4
- package/dist/style/components/loader.js +5 -3
- package/dist/style/components/methods.js +18 -15
- package/dist/style/components/overlay.js +5 -5
- package/dist/style/helpers/index.js +46 -46
- package/dist/style/index.js +3 -1
- package/dist/style/payment-instruments/payment-card.js +4 -4
- package/dist/style/utils/border.js +47 -0
- package/dist/style/utils/color-values.js +39 -3
- package/dist/style/utils/remove-empty-null.js +20 -0
- package/dist/style/vendor/framepay.js +11 -8
- package/dist/style/vendor/postmate.js +2 -2
- package/dist/style/views/confirmation.js +13 -13
- package/dist/style/views/method-selector.js +2 -2
- package/dist/style/views/modal.js +6 -6
- package/dist/style/views/result.js +4 -4
- package/dist/style/views/summary.js +26 -22
- package/dist/views/__snapshots__/summary.spec.js.snap +77 -119
- package/dist/views/common/iframe/base-iframe.js +2 -0
- package/dist/views/common/iframe/modal-iframe.js +50 -4
- package/dist/views/confirmation.js +19 -8
- package/dist/views/method-selector/generate-digital-wallet.js +12 -3
- package/dist/views/method-selector/generate-digital-wallet.spec.js +11 -0
- package/dist/views/method-selector/get-payment-methods.js +13 -2
- package/dist/views/method-selector/get-payment-methods.spec.js +21 -19
- package/dist/views/method-selector/index.js +23 -34
- package/dist/views/method-selector/method-selector.spec.js +50 -55
- package/dist/views/method-selector/mount-methods.js +5 -8
- package/dist/views/modal.js +8 -2
- package/dist/views/result.js +3 -4
- package/dist/views/summary.js +156 -97
- package/dist/views/summary.spec.js +53 -58
- package/package.json +4 -2
- package/src/events/index.js +2 -1
- package/src/functions/mount/fetch-data.js +152 -0
- package/src/functions/mount/fetch-data.spec.js +238 -0
- package/src/functions/mount/index.js +101 -158
- package/src/functions/mount/mount.spec.js +3 -5
- package/src/functions/mount/setup-element.js +26 -0
- package/src/functions/mount/setup-framepay-theme.js +82 -0
- package/src/functions/mount/setup-framepay.js +5 -1
- package/src/functions/mount/setup-i18n.js +19 -0
- package/src/functions/mount/setup-options.js +78 -48
- package/src/functions/mount/setup-options.spec.js +60 -0
- package/src/functions/mount/setup-storefront.js +6 -4
- package/src/functions/mount/setup-styles.js +4 -2
- package/src/functions/on.spec.js +1 -1
- package/src/functions/purchase.js +99 -23
- package/src/functions/purchase.spec.js +10 -10
- package/src/functions/setup.js +48 -0
- package/src/functions/setup.spec.js +98 -0
- package/src/functions/show.js +2 -4
- package/src/functions/show.spec.js +3 -4
- package/src/functions/update.js +40 -25
- package/src/functions/update.spec.js +0 -4
- package/src/i18n/en.json +5 -2
- package/src/i18n/es.json +4 -1
- package/src/index.js +9 -3
- package/src/index.spec.js +3 -21
- package/src/loader/index.js +3 -3
- package/src/storefront/index.js +28 -0
- package/src/storefront/invoices.js +11 -0
- package/src/storefront/models/base-model.js +10 -0
- package/src/storefront/models/invoice-model.js +3 -0
- package/src/storefront/models/plan-model.js +3 -35
- package/src/storefront/models/product-model.js +3 -23
- package/src/storefront/models/summary-model.js +12 -19
- package/src/storefront/models/transaction-model.js +19 -0
- package/src/storefront/payment-instruments.js +30 -0
- package/src/storefront/payment-instruments.spec.js +69 -0
- package/src/storefront/plans.js +6 -17
- package/src/storefront/plans.spec.js +4 -11
- package/src/storefront/products.js +8 -16
- package/src/storefront/products.spec.js +16 -22
- package/src/storefront/purchase.js +14 -16
- package/src/storefront/purchase.spec.js +2 -14
- package/src/storefront/ready-to-pay.js +13 -16
- package/src/storefront/ready-to-pay.spec.js +3 -10
- package/src/storefront/summary.js +19 -17
- package/src/storefront/summary.spec.js +5 -12
- package/src/storefront/transactions.js +11 -0
- package/src/style/base/__snapshots__/theme.spec.js.snap +188 -45
- package/src/style/base/default-theme.js +674 -0
- package/src/style/base/index.js +48 -16
- package/src/style/base/theme.js +17 -47
- package/src/style/base/theme.spec.js +4 -16
- package/src/style/components/address.js +3 -3
- package/src/style/components/button.js +32 -24
- package/src/style/components/divider.js +9 -9
- package/src/style/components/forms/checkbox.js +11 -11
- package/src/style/components/forms/field.js +18 -6
- package/src/style/components/forms/form.js +2 -2
- package/src/style/components/forms/input.js +54 -13
- package/src/style/components/forms/label.js +39 -18
- package/src/style/components/forms/select.js +54 -22
- package/src/style/components/forms/validation.js +53 -6
- package/src/style/components/icons.js +4 -4
- package/src/style/components/loader.js +4 -5
- package/src/style/components/methods.js +18 -15
- package/src/style/components/overlay.js +5 -5
- package/src/style/helpers/index.js +46 -46
- package/src/style/index.js +2 -1
- package/src/style/payment-instruments/payment-card.js +4 -4
- package/src/style/utils/border.js +34 -0
- package/src/style/utils/color-values.js +27 -1
- package/src/style/utils/remove-empty-null.js +10 -0
- package/src/style/vendor/framepay.js +11 -8
- package/src/style/vendor/postmate.js +2 -2
- package/src/style/views/confirmation.js +13 -13
- package/src/style/views/method-selector.js +2 -2
- package/src/style/views/modal.js +6 -6
- package/src/style/views/result.js +4 -4
- package/src/style/views/summary.js +26 -22
- package/src/views/__snapshots__/summary.spec.js.snap +77 -119
- package/src/views/common/iframe/base-iframe.js +2 -0
- package/src/views/common/iframe/modal-iframe.js +45 -3
- package/src/views/confirmation.js +15 -5
- package/src/views/method-selector/generate-digital-wallet.js +10 -3
- package/src/views/method-selector/generate-digital-wallet.spec.js +10 -0
- package/src/views/method-selector/get-payment-methods.js +7 -2
- package/src/views/method-selector/get-payment-methods.spec.js +26 -23
- package/src/views/method-selector/index.js +21 -28
- package/src/views/method-selector/method-selector.spec.js +49 -64
- package/src/views/method-selector/mount-methods.js +5 -8
- package/src/views/modal.js +6 -2
- package/src/views/result.js +4 -3
- package/src/views/summary.js +161 -117
- package/src/views/summary.spec.js +60 -75
- package/tests/mocks/rebilly-instruments-mock.js +37 -7
- package/tests/mocks/storefront-api-mock.js +8 -0
- package/tests/mocks/storefront-mock.js +17 -0
- package/dist/functions/mount/fetch-summary-data.js +0 -46
- package/dist/functions/mount/fetch-summary-data.spec.js +0 -43
- package/src/functions/mount/fetch-summary-data.js +0 -29
- package/src/functions/mount/fetch-summary-data.spec.js +0 -40
|
@@ -12,7 +12,7 @@ var _i18n = require("../i18n");
|
|
|
12
12
|
|
|
13
13
|
var _summary = require("./summary");
|
|
14
14
|
|
|
15
|
-
var
|
|
15
|
+
var _fetchData = require("../functions/mount/fetch-data");
|
|
16
16
|
|
|
17
17
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
18
|
|
|
@@ -29,12 +29,60 @@ describe('Summary component', () => {
|
|
|
29
29
|
options = {},
|
|
30
30
|
summary = summaryElement,
|
|
31
31
|
loader = new _loader.Loader(),
|
|
32
|
-
translate = new _i18n.Translate()
|
|
32
|
+
translate = new _i18n.Translate(),
|
|
33
|
+
data = {}
|
|
33
34
|
} = {}) {
|
|
34
35
|
this.options = options;
|
|
35
36
|
this.summary = summary;
|
|
36
37
|
this.loader = loader;
|
|
37
38
|
this.translate = translate;
|
|
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
|
+
});
|
|
38
86
|
}
|
|
39
87
|
|
|
40
88
|
}
|
|
@@ -52,47 +100,6 @@ describe('Summary component', () => {
|
|
|
52
100
|
thumbnail: ''
|
|
53
101
|
}]
|
|
54
102
|
};
|
|
55
|
-
const summaryData = new _summaryModel.default({
|
|
56
|
-
currency: 'USD',
|
|
57
|
-
lineItems: [{
|
|
58
|
-
type: 'debit',
|
|
59
|
-
description: 'My Awesome Product',
|
|
60
|
-
unitPrice: 30,
|
|
61
|
-
quantity: 1,
|
|
62
|
-
price: 30,
|
|
63
|
-
productId: 'test-product-1',
|
|
64
|
-
planId: 'my-awesome-product'
|
|
65
|
-
}, {
|
|
66
|
-
type: 'debit',
|
|
67
|
-
description: 'Awesome T-Shirt',
|
|
68
|
-
unitPrice: 20,
|
|
69
|
-
quantity: 2,
|
|
70
|
-
price: 40,
|
|
71
|
-
productId: 'test-product-2',
|
|
72
|
-
planId: 'awesome-t-shirt'
|
|
73
|
-
}],
|
|
74
|
-
subtotalAmount: 70,
|
|
75
|
-
taxAmount: 0,
|
|
76
|
-
shippingAmount: 0,
|
|
77
|
-
discountsAmount: 0,
|
|
78
|
-
total: 70
|
|
79
|
-
});
|
|
80
|
-
const planData = [new _planModel.default({
|
|
81
|
-
name: 'My Awesome Product',
|
|
82
|
-
id: 'my-awesome-product',
|
|
83
|
-
productId: 'test-product-1'
|
|
84
|
-
}), new _planModel.default({
|
|
85
|
-
name: 'My Awesome T-Shirt',
|
|
86
|
-
id: 'awesome-t-shirt',
|
|
87
|
-
productId: 'test-product-2'
|
|
88
|
-
})];
|
|
89
|
-
const productData = [new _productModel.default({
|
|
90
|
-
description: 'My Awesome Product',
|
|
91
|
-
id: 'test-product-1'
|
|
92
|
-
}), new _productModel.default({
|
|
93
|
-
description: 'My Awesome T-Shirt',
|
|
94
|
-
id: 'test-product-2'
|
|
95
|
-
})];
|
|
96
103
|
it('should render the summary correctly', () => {
|
|
97
104
|
const mountSummaryInstance = new TestMountSummaryInstance({
|
|
98
105
|
options
|
|
@@ -100,12 +107,7 @@ describe('Summary component', () => {
|
|
|
100
107
|
|
|
101
108
|
mountSummaryInstance.loader.DOM.summary = mountSummaryInstance.summary;
|
|
102
109
|
(0, _summary.mountSummary)({
|
|
103
|
-
state: mountSummaryInstance
|
|
104
|
-
summaryOptions: {
|
|
105
|
-
summary: summaryData,
|
|
106
|
-
plans: planData,
|
|
107
|
-
products: productData
|
|
108
|
-
}
|
|
110
|
+
state: mountSummaryInstance
|
|
109
111
|
}); // Number of line items
|
|
110
112
|
|
|
111
113
|
const itemsContainer = document.querySelector('.rebilly-instruments-summary-line-items');
|
|
@@ -115,22 +117,15 @@ describe('Summary component', () => {
|
|
|
115
117
|
expect(summaryBreakdown).toMatchSnapshot();
|
|
116
118
|
});
|
|
117
119
|
it('should not render the plan description if its falsy', () => {
|
|
118
|
-
const state = new _index.RebillyInstrumentsInstance();
|
|
119
|
-
state.options = options;
|
|
120
120
|
const mountSummaryInstance = new TestMountSummaryInstance({
|
|
121
121
|
options
|
|
122
122
|
}); // Making one product description falsy
|
|
123
123
|
|
|
124
|
-
|
|
124
|
+
mountSummaryInstance.data.previewPurchase.lineItems[0].description = null; //TODO: improve design to avoid this manual change
|
|
125
125
|
|
|
126
126
|
mountSummaryInstance.loader.DOM.summary = mountSummaryInstance.summary;
|
|
127
127
|
(0, _summary.mountSummary)({
|
|
128
|
-
state: mountSummaryInstance
|
|
129
|
-
summaryOptions: {
|
|
130
|
-
summary: summaryData,
|
|
131
|
-
plans: planData,
|
|
132
|
-
products: productData
|
|
133
|
-
}
|
|
128
|
+
state: mountSummaryInstance
|
|
134
129
|
}); // Check that only one description is render
|
|
135
130
|
|
|
136
131
|
const itemsSynopsysDescription = document.querySelectorAll('.rebilly-instruments-summary-line-item-synopsis-description');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rebilly/instruments",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1-beta.0",
|
|
4
4
|
"author": "Rebilly",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,13 +16,15 @@
|
|
|
16
16
|
"@babel/preset-env": "^7.14.7",
|
|
17
17
|
"@rebilly/risk-data-collector": "^2.0.1",
|
|
18
18
|
"autoprefixer": "^10.3.4",
|
|
19
|
+
"css": "^3.0.0",
|
|
19
20
|
"lodash.camelcase": "^4.3.0",
|
|
20
21
|
"lodash.isequal": "^4.5.0",
|
|
21
22
|
"lodash.kebabcase": "^4.1.1",
|
|
22
23
|
"lodash.merge": "^4.6.2",
|
|
23
24
|
"popostmate": "^1.6.4",
|
|
24
25
|
"postcss": "^8.3.6",
|
|
25
|
-
"
|
|
26
|
+
"postcss-custom-properties": "^12.0.0",
|
|
27
|
+
"rebilly-js-sdk": "^44.4.0",
|
|
26
28
|
"values.js": "^2.0.0"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
package/src/events/index.js
CHANGED
|
@@ -3,7 +3,8 @@ import BaseEvent from './base-event';
|
|
|
3
3
|
|
|
4
4
|
const events = {
|
|
5
5
|
instrumentReady: new BaseEvent('instrument-ready'),
|
|
6
|
-
purchaseCompleted: new BaseEvent('purchase-completed')
|
|
6
|
+
purchaseCompleted: new BaseEvent('purchase-completed'),
|
|
7
|
+
setupCompleted: new BaseEvent('setup-completed')
|
|
7
8
|
};
|
|
8
9
|
|
|
9
10
|
export default events;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { collectData } from '@rebilly/risk-data-collector';
|
|
2
|
+
import { fetchPlans } from '../../storefront/plans';
|
|
3
|
+
import { fetchProducts } from '../../storefront/products';
|
|
4
|
+
import { fetchReadyToPay } from '../../storefront/ready-to-pay';
|
|
5
|
+
import { fetchSummary } from '../../storefront/summary';
|
|
6
|
+
import { fetchInvoice as FetchInvoice } from '../../storefront/invoices';
|
|
7
|
+
import { fetchTransaction as FetchTransaction } from '../../storefront/transactions';
|
|
8
|
+
|
|
9
|
+
export class DataInstance {
|
|
10
|
+
constructor({
|
|
11
|
+
state = {},
|
|
12
|
+
...fields
|
|
13
|
+
} = {}) {
|
|
14
|
+
Object.entries({
|
|
15
|
+
...state.data || {},
|
|
16
|
+
...fields
|
|
17
|
+
}).forEach(([key, value]) => {
|
|
18
|
+
this[key] = value;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
this.money = state.options?.money || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get amountAndCurrency() {
|
|
25
|
+
let currency;
|
|
26
|
+
let amount;
|
|
27
|
+
if (this.previewPurchase) {
|
|
28
|
+
currency = this.previewPurchase.currency;
|
|
29
|
+
amount = this.previewPurchase.total;
|
|
30
|
+
} else if (this.invoice) {
|
|
31
|
+
currency = this.invoice.currency;
|
|
32
|
+
amount = this.invoice.amount;
|
|
33
|
+
} else if (this.transaction) {
|
|
34
|
+
currency = this.transaction.currency;
|
|
35
|
+
amount = this.transaction.amount;
|
|
36
|
+
} else if (this.money) {
|
|
37
|
+
currency = this.money.currency;
|
|
38
|
+
amount = this.money.amount;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
amount,
|
|
42
|
+
currency
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get isPayment() {
|
|
47
|
+
return (this.invoice || this.transaction || this.money);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get isPurchase() {
|
|
51
|
+
return this.previewPurchase;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get summaryItems() {
|
|
55
|
+
const {
|
|
56
|
+
discountsAmount = null,
|
|
57
|
+
shippingAmount = null,
|
|
58
|
+
subtotalAmount = null,
|
|
59
|
+
taxAmount = null,
|
|
60
|
+
} = (this.previewPurchase || this.invoice || {});
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
discountsAmount,
|
|
64
|
+
shippingAmount,
|
|
65
|
+
subtotalAmount,
|
|
66
|
+
taxAmount
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get summaryLineItems() {
|
|
71
|
+
let lineItems = [];
|
|
72
|
+
if (this.invoice) {
|
|
73
|
+
({
|
|
74
|
+
items: lineItems = [],
|
|
75
|
+
} = this.invoice)
|
|
76
|
+
} else {
|
|
77
|
+
({
|
|
78
|
+
lineItems = [],
|
|
79
|
+
} = this.previewPurchase || {});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return lineItems;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get isShippingRequired() {
|
|
86
|
+
return this.products.some(product => product.requiresShipping);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function fetchData({
|
|
91
|
+
state = null,
|
|
92
|
+
riskMetadata = null,
|
|
93
|
+
summaryPayload = null,
|
|
94
|
+
|
|
95
|
+
// Dependancy injectable functions
|
|
96
|
+
fetchInvoice = FetchInvoice,
|
|
97
|
+
fetchTransaction = FetchTransaction
|
|
98
|
+
}) {
|
|
99
|
+
try {
|
|
100
|
+
let transaction = null;
|
|
101
|
+
if (state.options?.transactionId) {
|
|
102
|
+
transaction = await fetchTransaction({data: {
|
|
103
|
+
id: state.options.transactionId
|
|
104
|
+
}, state});
|
|
105
|
+
state.data.transaction = transaction;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let invoice = null;
|
|
109
|
+
if (state.options?.invoiceId || state.data?.transaction?.hasInvoice) {
|
|
110
|
+
invoice = await fetchInvoice({data: {
|
|
111
|
+
id: state.options?.invoiceId || state.data?.transaction?.invoiceId
|
|
112
|
+
}, state});
|
|
113
|
+
state.data.invoice = invoice;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!riskMetadata) {
|
|
117
|
+
const { riskMetadata: data } = await collectData();
|
|
118
|
+
riskMetadata = data;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
state.data = new DataInstance({
|
|
122
|
+
state,
|
|
123
|
+
invoice,
|
|
124
|
+
transaction,
|
|
125
|
+
riskMetadata
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const [readyToPay, previewPurchase] = await Promise.all([
|
|
129
|
+
state.data?.readyToPay || fetchReadyToPay({ riskMetadata, state }),
|
|
130
|
+
state.options.items ? fetchSummary({ data: summaryPayload, state }) : null
|
|
131
|
+
]);
|
|
132
|
+
|
|
133
|
+
const plans = await fetchPlans({ state });
|
|
134
|
+
state.data.plans = plans;
|
|
135
|
+
const products = await fetchProducts({ state });
|
|
136
|
+
|
|
137
|
+
return new DataInstance({
|
|
138
|
+
state,
|
|
139
|
+
readyToPay,
|
|
140
|
+
previewPurchase,
|
|
141
|
+
plans,
|
|
142
|
+
products,
|
|
143
|
+
invoice,
|
|
144
|
+
transaction,
|
|
145
|
+
riskMetadata
|
|
146
|
+
});
|
|
147
|
+
} catch(error) {
|
|
148
|
+
// console.log(error);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return new DataInstance({});
|
|
152
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { fetchData, DataInstance } from './fetch-data';
|
|
2
|
+
import TransactionModel from '../../storefront/models/transaction-model';
|
|
3
|
+
|
|
4
|
+
describe('fetchData function', () => {
|
|
5
|
+
it('Should use correct invoice id for invoiceId', async () => {
|
|
6
|
+
const mockFetchInvoice = jest.fn();
|
|
7
|
+
const invoiceId = 'test-invoice-id';
|
|
8
|
+
const invoiceState = {
|
|
9
|
+
options: {
|
|
10
|
+
invoiceId
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
fetchData({
|
|
15
|
+
state: invoiceState,
|
|
16
|
+
fetchInvoice: mockFetchInvoice
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
expect(mockFetchInvoice).toBeCalledTimes(1);
|
|
20
|
+
expect(mockFetchInvoice).toBeCalledWith(
|
|
21
|
+
expect.objectContaining({
|
|
22
|
+
data:{
|
|
23
|
+
id: invoiceId
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('Should use correct invoice id for transaction with invoiceIds', async () => {
|
|
30
|
+
const mockFetchInvoice = jest.fn();
|
|
31
|
+
const invoiceId = 'test-invoice-id';
|
|
32
|
+
const invoiceState = {
|
|
33
|
+
options: {},
|
|
34
|
+
data: {
|
|
35
|
+
transaction: new TransactionModel({
|
|
36
|
+
invoiceIds: [invoiceId]
|
|
37
|
+
}),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fetchData({
|
|
42
|
+
state: invoiceState,
|
|
43
|
+
fetchInvoice: mockFetchInvoice
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(mockFetchInvoice).toBeCalledTimes(1);
|
|
47
|
+
expect(mockFetchInvoice).toBeCalledWith(
|
|
48
|
+
expect.objectContaining({
|
|
49
|
+
data:{
|
|
50
|
+
id: invoiceId
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('Should not fetch invoice for transaction with no invoice Ids', async () => {
|
|
57
|
+
const mockFetchInvoice = jest.fn();
|
|
58
|
+
const invoiceState = {
|
|
59
|
+
options: {},
|
|
60
|
+
data: {
|
|
61
|
+
transaction: new TransactionModel({
|
|
62
|
+
invoiceIds: []
|
|
63
|
+
}),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fetchData({
|
|
68
|
+
state: invoiceState,
|
|
69
|
+
fetchInvoice: mockFetchInvoice
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
expect(mockFetchInvoice).toBeCalledTimes(0);
|
|
73
|
+
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('DataInstance', () => {
|
|
78
|
+
it ('Should correctly determine amountAndCurrency', () => {
|
|
79
|
+
const expectedAmountAndCurrency = {
|
|
80
|
+
amount: 10,
|
|
81
|
+
currency: 'USD'
|
|
82
|
+
}
|
|
83
|
+
let fetchedData = new DataInstance({
|
|
84
|
+
state: {
|
|
85
|
+
options: {
|
|
86
|
+
money: {
|
|
87
|
+
...expectedAmountAndCurrency
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
expect(fetchedData.amountAndCurrency).toEqual(
|
|
93
|
+
expect.objectContaining(expectedAmountAndCurrency)
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
fetchedData = new DataInstance({
|
|
97
|
+
previewPurchase: {
|
|
98
|
+
total: expectedAmountAndCurrency.amount,
|
|
99
|
+
currency: expectedAmountAndCurrency.currency
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
expect(fetchedData.amountAndCurrency).toEqual(
|
|
103
|
+
expect.objectContaining(expectedAmountAndCurrency)
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
fetchedData = new DataInstance({
|
|
107
|
+
invoice: {
|
|
108
|
+
...expectedAmountAndCurrency
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
expect(fetchedData.amountAndCurrency).toEqual(
|
|
112
|
+
expect.objectContaining(expectedAmountAndCurrency)
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
fetchedData = new DataInstance({
|
|
116
|
+
transaction: {
|
|
117
|
+
...expectedAmountAndCurrency
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
expect(fetchedData.amountAndCurrency).toEqual(
|
|
121
|
+
expect.objectContaining(expectedAmountAndCurrency)
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
fetchedData = new DataInstance();
|
|
125
|
+
expect(fetchedData.amountAndCurrency).toEqual(
|
|
126
|
+
expect.objectContaining({
|
|
127
|
+
amount: undefined,
|
|
128
|
+
currency: undefined
|
|
129
|
+
})
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it ('Should correctly determine isPayment', () => {
|
|
134
|
+
let fetchedData = new DataInstance({
|
|
135
|
+
state: {
|
|
136
|
+
options: {
|
|
137
|
+
money: true
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
expect(fetchedData.isPayment).toBeTruthy();
|
|
142
|
+
|
|
143
|
+
fetchedData = new DataInstance({
|
|
144
|
+
invoice: true
|
|
145
|
+
});
|
|
146
|
+
expect(fetchedData.isPayment).toBeTruthy();
|
|
147
|
+
|
|
148
|
+
fetchedData = new DataInstance({
|
|
149
|
+
transaction: true
|
|
150
|
+
});
|
|
151
|
+
expect(fetchedData.isPayment).toBeTruthy();
|
|
152
|
+
|
|
153
|
+
fetchedData = new DataInstance({
|
|
154
|
+
previewPurchase: true
|
|
155
|
+
});
|
|
156
|
+
expect(fetchedData.isPayment).toBeFalsy();
|
|
157
|
+
|
|
158
|
+
fetchedData = new DataInstance({});
|
|
159
|
+
expect(fetchedData.isPayment).toBeFalsy();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it ('Should correctly determine isPurchase', () => {
|
|
163
|
+
let fetchedData = new DataInstance({
|
|
164
|
+
previewPurchase: true
|
|
165
|
+
});
|
|
166
|
+
expect(fetchedData.isPurchase).toBeTruthy();
|
|
167
|
+
|
|
168
|
+
fetchedData = new DataInstance({
|
|
169
|
+
state: {
|
|
170
|
+
options: {
|
|
171
|
+
money: true
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
expect(fetchedData.isPurchase).toBeFalsy();
|
|
176
|
+
|
|
177
|
+
fetchedData = new DataInstance({
|
|
178
|
+
invoice: true
|
|
179
|
+
});
|
|
180
|
+
expect(fetchedData.isPurchase).toBeFalsy();
|
|
181
|
+
|
|
182
|
+
fetchedData = new DataInstance({
|
|
183
|
+
transaction: true
|
|
184
|
+
});
|
|
185
|
+
expect(fetchedData.isPurchase).toBeFalsy();
|
|
186
|
+
|
|
187
|
+
fetchedData = new DataInstance({});
|
|
188
|
+
expect(fetchedData.isPurchase).toBeFalsy();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it ('Should get summaryItems', () => {
|
|
192
|
+
const expectedSummaryItems = {
|
|
193
|
+
discountsAmount: 1,
|
|
194
|
+
shippingAmount: 2,
|
|
195
|
+
subtotalAmount: 3,
|
|
196
|
+
taxAmount: 4
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
let fetchedData = new DataInstance({
|
|
200
|
+
previewPurchase: {
|
|
201
|
+
...expectedSummaryItems
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
expect(fetchedData.summaryItems).toEqual(
|
|
205
|
+
expect.objectContaining(expectedSummaryItems)
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
fetchedData = new DataInstance({
|
|
209
|
+
invoice: {
|
|
210
|
+
...expectedSummaryItems
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
expect(fetchedData.summaryItems).toEqual(
|
|
214
|
+
expect.objectContaining(expectedSummaryItems)
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it ('Should get summaryLineItems', () => {
|
|
219
|
+
const expectedSummaryLineItems = [{
|
|
220
|
+
line: 'item'
|
|
221
|
+
}];
|
|
222
|
+
|
|
223
|
+
let fetchedData = new DataInstance({
|
|
224
|
+
previewPurchase: {
|
|
225
|
+
lineItems: expectedSummaryLineItems
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
expect(fetchedData.summaryLineItems).toEqual(expectedSummaryLineItems);
|
|
229
|
+
|
|
230
|
+
fetchedData = new DataInstance({
|
|
231
|
+
invoice: {
|
|
232
|
+
items: expectedSummaryLineItems
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
expect(fetchedData.summaryLineItems).toEqual(expectedSummaryLineItems);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|