@rebilly/instruments 4.8.1 → 4.8.3
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/CHANGELOG.md +14 -0
- package/dist/index.js +1 -1
- package/dist/index.min.js +1 -1
- package/package.json +21 -19
- package/.babelrc +0 -26
- package/project.json +0 -31
- package/rollup.config.mjs +0 -79
- package/src/data/options-schema/index.js +0 -112
- package/src/data/options-schema/schemas/options-schema.js +0 -448
- package/src/events/base-event.js +0 -47
- package/src/events/events.spec.js +0 -11
- package/src/events/index.js +0 -15
- package/src/functions/destroy.js +0 -29
- package/src/functions/destroy.spec.js +0 -63
- package/src/functions/mount/fetch-data.js +0 -236
- package/src/functions/mount/fetch-data.spec.js +0 -317
- package/src/functions/mount/get-lead-source-data.js +0 -46
- package/src/functions/mount/get-lead-source-data.spec.js +0 -38
- package/src/functions/mount/index.js +0 -105
- package/src/functions/mount/mount.spec.js +0 -96
- package/src/functions/mount/setup-element.js +0 -29
- package/src/functions/mount/setup-framepay-theme.js +0 -104
- package/src/functions/mount/setup-framepay.js +0 -26
- package/src/functions/mount/setup-i18n.js +0 -21
- package/src/functions/mount/setup-options.js +0 -100
- package/src/functions/mount/setup-options.spec.js +0 -353
- package/src/functions/mount/setup-storefront.js +0 -23
- package/src/functions/mount/setup-styles-vars.js +0 -30
- package/src/functions/mount/setup-user-flow.js +0 -60
- package/src/functions/on.js +0 -22
- package/src/functions/on.spec.js +0 -75
- package/src/functions/purchase.js +0 -173
- package/src/functions/purchase.spec.js +0 -80
- package/src/functions/setup.js +0 -66
- package/src/functions/setup.spec.js +0 -111
- package/src/functions/show.js +0 -30
- package/src/functions/show.spec.js +0 -62
- package/src/functions/update.js +0 -40
- package/src/functions/update.spec.js +0 -104
- package/src/i18n/en.json +0 -35
- package/src/i18n/es.json +0 -32
- package/src/i18n/i18n.spec.js +0 -27
- package/src/i18n/index.js +0 -59
- package/src/index.js +0 -3
- package/src/instance.js +0 -48
- package/src/instance.spec.js +0 -44
- package/src/loader/index.js +0 -145
- package/src/loader/loader.spec.js +0 -74
- package/src/state/iframes.js +0 -23
- package/src/state/index.js +0 -64
- package/src/storefront/account-and-website.js +0 -18
- package/src/storefront/account-and-website.spec.js +0 -73
- package/src/storefront/deposit-requests.js +0 -12
- package/src/storefront/fetch-plans-from-addons-bumpOffer.js +0 -30
- package/src/storefront/fetch-products-from-plans.js +0 -59
- package/src/storefront/fetch-products-from-plans.spec.js +0 -113
- package/src/storefront/index.js +0 -78
- package/src/storefront/invoices.js +0 -23
- package/src/storefront/invoices.spec.js +0 -92
- package/src/storefront/models/account-model.js +0 -36
- package/src/storefront/models/base-model.js +0 -7
- package/src/storefront/models/deposit-request-model.js +0 -24
- package/src/storefront/models/invoice-model.js +0 -20
- package/src/storefront/models/payment-metadata.js +0 -7
- package/src/storefront/models/plan-model.js +0 -94
- package/src/storefront/models/product-model.js +0 -3
- package/src/storefront/models/ready-to-pay-model.js +0 -76
- package/src/storefront/models/summary-model.js +0 -56
- package/src/storefront/models/transaction-model.js +0 -16
- package/src/storefront/models/website-model.js +0 -3
- package/src/storefront/payment-instruments.js +0 -47
- package/src/storefront/payment-instruments.spec.js +0 -88
- package/src/storefront/purchase.js +0 -60
- package/src/storefront/purchase.spec.js +0 -53
- package/src/storefront/ready-to-pay.js +0 -96
- package/src/storefront/ready-to-pay.spec.js +0 -76
- package/src/storefront/storefront.spec.js +0 -14
- package/src/storefront/summary.js +0 -114
- package/src/storefront/summary.spec.js +0 -136
- package/src/storefront/transactions.js +0 -12
- package/src/style/base/__snapshots__/theme.spec.js.snap +0 -143
- package/src/style/base/default-theme.js +0 -955
- package/src/style/base/index.js +0 -688
- package/src/style/base/theme.js +0 -30
- package/src/style/base/theme.spec.js +0 -19
- package/src/style/index.js +0 -11
- package/src/style/utils/border.js +0 -47
- package/src/style/utils/color-values.js +0 -35
- package/src/style/utils/minifyCss.js +0 -14
- package/src/utils/add-dom-element.js +0 -17
- package/src/utils/format-currency.js +0 -8
- package/src/utils/has-valid-css-selector.js +0 -4
- package/src/utils/index.js +0 -15
- package/src/utils/is-dom-element.js +0 -3
- package/src/utils/process-property-as-dom-element.js +0 -31
- package/src/utils/quantity.js +0 -47
- package/src/utils/sleep.js +0 -5
- package/src/views/amount-selector.js +0 -46
- package/src/views/common/iframe/base-iframe.js +0 -66
- package/src/views/common/iframe/events/change-iframe-src-handler.js +0 -6
- package/src/views/common/iframe/events/dispatch-event-handler.js +0 -8
- package/src/views/common/iframe/events/resize-component-handler.js +0 -9
- package/src/views/common/iframe/events/show-error-handler.js +0 -5
- package/src/views/common/iframe/events/stop-loader-handler.js +0 -9
- package/src/views/common/iframe/events/update-addons-handler.js +0 -23
- package/src/views/common/iframe/events/update-coupons-handler.js +0 -12
- package/src/views/common/iframe/events/update-items-handler.js +0 -34
- package/src/views/common/iframe/index.js +0 -5
- package/src/views/common/iframe/modal-iframe.js +0 -82
- package/src/views/common/iframe/view-iframe.js +0 -23
- package/src/views/common/render-utilities.js +0 -4
- package/src/views/confirmation.js +0 -45
- package/src/views/errors.js +0 -115
- package/src/views/form.js +0 -61
- package/src/views/method-selector/__snapshots__/method-selector.spec.js.snap +0 -135
- package/src/views/method-selector/express-methods.js +0 -52
- package/src/views/method-selector/generate-digital-wallet.js +0 -53
- package/src/views/method-selector/generate-digital-wallet.spec.js +0 -121
- package/src/views/method-selector/generate-framepay-config.js +0 -56
- package/src/views/method-selector/generate-framepay-config.spec.js +0 -216
- package/src/views/method-selector/get-method-data.js +0 -11
- package/src/views/method-selector/get-payment-methods.js +0 -25
- package/src/views/method-selector/get-payment-methods.spec.js +0 -41
- package/src/views/method-selector/index.js +0 -223
- package/src/views/method-selector/method-selector.spec.js +0 -20
- package/src/views/method-selector/mount-bump-offer.js +0 -120
- package/src/views/method-selector/mount-express-methods.js +0 -107
- package/src/views/modal.js +0 -103
- package/src/views/result.js +0 -24
- package/src/views/summary.js +0 -38
- package/tests/async-utilities.js +0 -22
- package/tests/mocks/framepay-mock.js +0 -15
- package/tests/mocks/rebilly-api-mock.js +0 -11
- package/tests/mocks/rebilly-instruments-mock.js +0 -141
- package/tests/mocks/storefront-api-mock.js +0 -62
- package/tests/mocks/storefront-mock.js +0 -20
- package/tests/msw/server.js +0 -14
- package/tests/setup-test.js +0 -30
- package/vitest.config.js +0 -18
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import BaseModel from './base-model';
|
|
2
|
-
|
|
3
|
-
export class SummaryLineItemModel extends BaseModel {
|
|
4
|
-
constructor({
|
|
5
|
-
unitPrice = 0.0,
|
|
6
|
-
quantity = 0,
|
|
7
|
-
price = 0.0,
|
|
8
|
-
...fields
|
|
9
|
-
} = {}) {
|
|
10
|
-
super(fields);
|
|
11
|
-
this.unitPrice = unitPrice;
|
|
12
|
-
this.quantity = quantity;
|
|
13
|
-
this.price = price;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class SummaryTaxModel extends BaseModel {}
|
|
18
|
-
|
|
19
|
-
export class SummaryDiscountModel extends BaseModel {
|
|
20
|
-
constructor({ amount = 0, ...fields }) {
|
|
21
|
-
super(fields);
|
|
22
|
-
this.amount = amount;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export default class SummaryModel {
|
|
27
|
-
constructor({
|
|
28
|
-
currency = '',
|
|
29
|
-
lineItems = [],
|
|
30
|
-
taxes = [],
|
|
31
|
-
discounts = [],
|
|
32
|
-
subtotalAmount = 0.0,
|
|
33
|
-
taxAmount = 0.0,
|
|
34
|
-
shippingAmount = 0.0,
|
|
35
|
-
discountsAmount = 0.0,
|
|
36
|
-
total = 0.0,
|
|
37
|
-
} = {}) {
|
|
38
|
-
function map(array) {
|
|
39
|
-
const list = Array.isArray(array) ? array : [];
|
|
40
|
-
return {
|
|
41
|
-
// eslint-disable-next-line new-cap
|
|
42
|
-
to: (instance) => list.map((item) => new instance(item)),
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.currency = currency;
|
|
47
|
-
this.lineItems = map(lineItems).to(SummaryLineItemModel);
|
|
48
|
-
this.taxes = map(taxes).to(SummaryTaxModel);
|
|
49
|
-
this.discounts = map(discounts).to(SummaryDiscountModel);
|
|
50
|
-
this.subtotalAmount = subtotalAmount;
|
|
51
|
-
this.taxAmount = taxAmount;
|
|
52
|
-
this.shippingAmount = shippingAmount;
|
|
53
|
-
this.discountsAmount = discountsAmount;
|
|
54
|
-
this.total = total;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import BaseModel from './base-model';
|
|
2
|
-
|
|
3
|
-
export default class TransactionModel extends BaseModel {
|
|
4
|
-
constructor({ invoiceIds = [], ...fields }) {
|
|
5
|
-
super(fields);
|
|
6
|
-
this.invoiceIds = invoiceIds || [];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
get invoiceId() {
|
|
10
|
-
return this.invoiceIds[0] || null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
get hasInvoice() {
|
|
14
|
-
return !!this.invoiceId;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Endpoint } from './index';
|
|
2
|
-
import state from '../state';
|
|
3
|
-
|
|
4
|
-
export async function setupPaymentInstrument({ data }) {
|
|
5
|
-
return Endpoint(async () => {
|
|
6
|
-
// Create payment instrument
|
|
7
|
-
const { fields: instrument } =
|
|
8
|
-
await state.storefront.paymentInstruments.create({ data });
|
|
9
|
-
|
|
10
|
-
// Setup the payment instrument
|
|
11
|
-
state.storefront.setSessionToken(instrument.token || state.options.jwt);
|
|
12
|
-
|
|
13
|
-
const setupPayload = {
|
|
14
|
-
id: instrument.id,
|
|
15
|
-
data: {
|
|
16
|
-
websiteId: state.options.websiteId,
|
|
17
|
-
...data,
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
if (state.data.amountAndCurrency) {
|
|
22
|
-
setupPayload.data = {
|
|
23
|
-
...setupPayload.data,
|
|
24
|
-
...state.data.amountAndCurrency,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (state.data?.transaction?.redirectUrl) {
|
|
29
|
-
setupPayload.data.redirectUrl = `${setupPayload.data.redirectUrl}&originalRedirectUrl=${state.data.transaction.redirectUrl}`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const { fields: transaction } =
|
|
33
|
-
await state.storefront.paymentInstruments.setup(setupPayload);
|
|
34
|
-
|
|
35
|
-
return { instrument, transaction };
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export async function fetchPaymentInstrument() {
|
|
40
|
-
return Endpoint(async () => {
|
|
41
|
-
const { items: instruments } =
|
|
42
|
-
await state.storefront.paymentInstruments.getAll({
|
|
43
|
-
filter: `status:active,inactive,verification-needed`,
|
|
44
|
-
});
|
|
45
|
-
return instruments.map(({ fields }) => fields);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
|
|
2
|
-
import { ok, post } from 'msw-when-then';
|
|
3
|
-
import { when } from 'tests/msw/server';
|
|
4
|
-
import { storefrontURL } from 'tests/mocks/storefront-api-mock';
|
|
5
|
-
|
|
6
|
-
describe('Storefront API Setup', () => {
|
|
7
|
-
it('can make purchase', async () => {
|
|
8
|
-
const instance = new StorefontTestingInstance();
|
|
9
|
-
|
|
10
|
-
const paymentInstrumentFields = {
|
|
11
|
-
id: 'payment-instrument-id',
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const paymentInstrumentSetupFields = {
|
|
15
|
-
id: 'payment-instrument-id',
|
|
16
|
-
approvalUrl: null,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const paymentInstrumentPayload = {
|
|
20
|
-
data: {
|
|
21
|
-
token: 'test-token',
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const paymentInstrumentSetupPayload = {
|
|
26
|
-
id: paymentInstrumentFields.id,
|
|
27
|
-
data: {
|
|
28
|
-
websiteId: 'website-id',
|
|
29
|
-
currency: 'USD',
|
|
30
|
-
amount: 30,
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
when(post(`${storefrontURL}/payment-instruments`)).thenReturn(
|
|
35
|
-
ok(paymentInstrumentFields),
|
|
36
|
-
);
|
|
37
|
-
when(post(`${storefrontURL}/payment-instruments/*/setup`)).thenReturn(
|
|
38
|
-
ok(paymentInstrumentSetupFields),
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
vi.spyOn(instance.storefront.paymentInstruments, 'create');
|
|
42
|
-
vi.spyOn(instance.storefront.paymentInstruments, 'setup');
|
|
43
|
-
|
|
44
|
-
const paymentInstrumentResponse =
|
|
45
|
-
await instance.storefront.paymentInstruments.create(
|
|
46
|
-
paymentInstrumentPayload,
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
const paymentInstrumentSetupResponse =
|
|
50
|
-
await instance.storefront.paymentInstruments.setup(
|
|
51
|
-
paymentInstrumentSetupPayload,
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
expect(instance.storefront.paymentInstruments.create).toBeCalledTimes(
|
|
55
|
-
1,
|
|
56
|
-
);
|
|
57
|
-
expect(instance.storefront.paymentInstruments.create).toBeCalledWith(
|
|
58
|
-
paymentInstrumentPayload,
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
expect(instance.storefront.paymentInstruments.setup).toBeCalledTimes(1);
|
|
62
|
-
expect(instance.storefront.paymentInstruments.setup).toBeCalledWith(
|
|
63
|
-
paymentInstrumentSetupPayload,
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
expect(paymentInstrumentResponse.config).toBeInstanceOf(Object);
|
|
67
|
-
expect(paymentInstrumentResponse.fields).toBeInstanceOf(Object);
|
|
68
|
-
expect(paymentInstrumentResponse.response).toBeInstanceOf(Object);
|
|
69
|
-
|
|
70
|
-
expect(paymentInstrumentResponse.fields).toEqual(
|
|
71
|
-
paymentInstrumentFields,
|
|
72
|
-
);
|
|
73
|
-
expect(paymentInstrumentResponse.fields).toMatchObject(
|
|
74
|
-
paymentInstrumentFields,
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
expect(paymentInstrumentSetupResponse.config).toBeInstanceOf(Object);
|
|
78
|
-
expect(paymentInstrumentSetupResponse.fields).toBeInstanceOf(Object);
|
|
79
|
-
expect(paymentInstrumentSetupResponse.response).toBeInstanceOf(Object);
|
|
80
|
-
|
|
81
|
-
expect(paymentInstrumentSetupResponse.fields).toEqual(
|
|
82
|
-
paymentInstrumentSetupFields,
|
|
83
|
-
);
|
|
84
|
-
expect(paymentInstrumentSetupResponse.fields).toMatchObject(
|
|
85
|
-
paymentInstrumentSetupFields,
|
|
86
|
-
);
|
|
87
|
-
});
|
|
88
|
-
});
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import state from '../state';
|
|
2
|
-
import { Endpoint } from './index';
|
|
3
|
-
|
|
4
|
-
export async function postPurchase({ data }) {
|
|
5
|
-
return Endpoint(async () => {
|
|
6
|
-
// https://storefront-api-docs.rebilly.com/tag/Purchases/operation/StorefrontPostPurchase
|
|
7
|
-
const purchaseSchemaKeys = [
|
|
8
|
-
'websiteId',
|
|
9
|
-
'paymentInstruction',
|
|
10
|
-
'items',
|
|
11
|
-
'billingAddress',
|
|
12
|
-
'deliveryAddress',
|
|
13
|
-
'shippingRateId',
|
|
14
|
-
'couponIds',
|
|
15
|
-
'password',
|
|
16
|
-
'redirectUrl',
|
|
17
|
-
];
|
|
18
|
-
Object.keys(data).forEach((key) => {
|
|
19
|
-
if (!purchaseSchemaKeys.includes(key)) {
|
|
20
|
-
delete data[key];
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
return state.storefront.purchase.purchase({ data });
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function postPayment({ data }) {
|
|
28
|
-
return Endpoint(async () => {
|
|
29
|
-
const {
|
|
30
|
-
token,
|
|
31
|
-
transactionId,
|
|
32
|
-
invoiceId,
|
|
33
|
-
websiteId,
|
|
34
|
-
paymentInstrumentId,
|
|
35
|
-
} = data;
|
|
36
|
-
// https://storefront-api-docs.rebilly.com/tag/Purchases/operation/StorefrontPostPayment
|
|
37
|
-
const purchaseSchemaKeys = ['riskMetadata', 'redirectUrl'];
|
|
38
|
-
if (token) {
|
|
39
|
-
purchaseSchemaKeys.push('token');
|
|
40
|
-
}
|
|
41
|
-
if (paymentInstrumentId) {
|
|
42
|
-
purchaseSchemaKeys.push('paymentInstrumentId');
|
|
43
|
-
}
|
|
44
|
-
if (transactionId) {
|
|
45
|
-
purchaseSchemaKeys.push('transactionId');
|
|
46
|
-
}
|
|
47
|
-
if (invoiceId) {
|
|
48
|
-
purchaseSchemaKeys.push('invoiceId');
|
|
49
|
-
}
|
|
50
|
-
if (websiteId) {
|
|
51
|
-
purchaseSchemaKeys.push('amount', 'currency', 'websiteId');
|
|
52
|
-
}
|
|
53
|
-
Object.keys(data).forEach((key) => {
|
|
54
|
-
if (!purchaseSchemaKeys.includes(key)) {
|
|
55
|
-
delete data[key];
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
return state.storefront.purchase.payment({ data });
|
|
59
|
-
});
|
|
60
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
|
|
2
|
-
import { ok, post } from 'msw-when-then';
|
|
3
|
-
import { when } from 'tests/msw/server';
|
|
4
|
-
import { storefrontURL } from 'tests/mocks/storefront-api-mock';
|
|
5
|
-
|
|
6
|
-
describe('Storefront API Purchase', () => {
|
|
7
|
-
it('can make purchase', async () => {
|
|
8
|
-
const instance = StorefontTestingInstance();
|
|
9
|
-
const payload = {
|
|
10
|
-
websiteId: 'test-website-id',
|
|
11
|
-
items: [
|
|
12
|
-
{
|
|
13
|
-
planId: 'test-plan-id-1',
|
|
14
|
-
quantity: 1,
|
|
15
|
-
},
|
|
16
|
-
],
|
|
17
|
-
billingAddress: {
|
|
18
|
-
firstName: 'Test',
|
|
19
|
-
lastName: 'Customer',
|
|
20
|
-
},
|
|
21
|
-
paymentInstruction: {
|
|
22
|
-
token: 'test-token',
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
const fields = {
|
|
26
|
-
orderId: 'test-order-id',
|
|
27
|
-
invoice: {
|
|
28
|
-
id: 'test-invoice-id',
|
|
29
|
-
},
|
|
30
|
-
transaction: {
|
|
31
|
-
id: 'test-transaction-id',
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
when(post(`${storefrontURL}/purchase`)).thenReturn(ok(fields));
|
|
36
|
-
|
|
37
|
-
vi.spyOn(instance.storefront.purchase, 'purchase');
|
|
38
|
-
|
|
39
|
-
const requestResponse = await instance.storefront.purchase.purchase(
|
|
40
|
-
payload,
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
expect(instance.storefront.purchase.purchase).toBeCalledTimes(1);
|
|
44
|
-
expect(instance.storefront.purchase.purchase).toBeCalledWith(payload);
|
|
45
|
-
|
|
46
|
-
expect(requestResponse.config).toBeInstanceOf(Object);
|
|
47
|
-
expect(requestResponse.fields).toBeInstanceOf(Object);
|
|
48
|
-
expect(requestResponse.response).toBeInstanceOf(Object);
|
|
49
|
-
|
|
50
|
-
expect(requestResponse.fields).toEqual(fields);
|
|
51
|
-
expect(requestResponse.fields).toMatchObject(fields);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { collectData } from '@rebilly/risk-data-collector';
|
|
2
|
-
import { paymentMethods } from '@rebilly/api-metadata';
|
|
3
|
-
import ReadyToPayModel from './models/ready-to-pay-model';
|
|
4
|
-
import { Endpoint } from './index';
|
|
5
|
-
import state from '../state';
|
|
6
|
-
import { mapItemsQuantities } from '../utils/quantity';
|
|
7
|
-
|
|
8
|
-
export function filterReadyToPay(readyToPay) {
|
|
9
|
-
const paymentMethodsMetadata = [...paymentMethods];
|
|
10
|
-
const EXPRESS_METHODS = ['Google Pay', 'Apple Pay', 'paypal'];
|
|
11
|
-
const BLOCKED_METHODS = [
|
|
12
|
-
// account number, routing number, and account type required
|
|
13
|
-
'echeck',
|
|
14
|
-
|
|
15
|
-
// payment instrument is required
|
|
16
|
-
// card number is required
|
|
17
|
-
// card cvv is required
|
|
18
|
-
// expiration month is required
|
|
19
|
-
// expiration year is required
|
|
20
|
-
'Khelocard',
|
|
21
|
-
|
|
22
|
-
// Klarna authorization token is required
|
|
23
|
-
// Klarna session ID is required
|
|
24
|
-
'Klarna',
|
|
25
|
-
];
|
|
26
|
-
const isExpressMethod = ({ method, feature }) =>
|
|
27
|
-
EXPRESS_METHODS.includes(method) ||
|
|
28
|
-
EXPRESS_METHODS.includes(feature?.name);
|
|
29
|
-
return readyToPay
|
|
30
|
-
.filter((readyData) => {
|
|
31
|
-
// Remove result for "old" paypal method
|
|
32
|
-
const isOldPayPal =
|
|
33
|
-
readyData.method === 'paypal' && !readyData.feature;
|
|
34
|
-
// Remove result for plaid method
|
|
35
|
-
const isPlaid = readyData.method === 'ach' && readyData.feature;
|
|
36
|
-
// Remove result for blocked method
|
|
37
|
-
const isBlocked = BLOCKED_METHODS.includes(readyData.method);
|
|
38
|
-
|
|
39
|
-
return !isOldPayPal && !isPlaid && !isBlocked;
|
|
40
|
-
})
|
|
41
|
-
.map((fields, index) => {
|
|
42
|
-
const metadata =
|
|
43
|
-
paymentMethodsMetadata.find(
|
|
44
|
-
(methodMetadata) =>
|
|
45
|
-
methodMetadata.apiName === fields.method,
|
|
46
|
-
) || {};
|
|
47
|
-
metadata.isExpressMethod = isExpressMethod(fields);
|
|
48
|
-
|
|
49
|
-
return new ReadyToPayModel({
|
|
50
|
-
index,
|
|
51
|
-
metadata,
|
|
52
|
-
...fields,
|
|
53
|
-
});
|
|
54
|
-
})
|
|
55
|
-
.filter(({ metadata }) => metadata.storefrontEnabled);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export async function fetchReadyToPay() {
|
|
59
|
-
return Endpoint(async () => {
|
|
60
|
-
if (!state.data.riskMetadata) {
|
|
61
|
-
const { riskMetadata: data } = await collectData();
|
|
62
|
-
state.data.riskMetadata = data;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const data = {
|
|
66
|
-
riskMetadata: state.data.riskMetadata,
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
data.websiteId = state.options?.websiteId || null;
|
|
70
|
-
|
|
71
|
-
if (state.options?.items) {
|
|
72
|
-
data.items = mapItemsQuantities(state.options.items);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (state.options?.money && state.data?.amountAndCurrency) {
|
|
76
|
-
const money = state.data.amountAndCurrency;
|
|
77
|
-
data.amount = money.amount;
|
|
78
|
-
data.currency = money.currency;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (state.data?.account?.address) {
|
|
82
|
-
data.billingAddress = state.data.account.address;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (state.data?.deposit) {
|
|
86
|
-
data.amount = state.data.deposit.amount;
|
|
87
|
-
data.currency = state.data.deposit.currency;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const { fields: readyToPayFields } =
|
|
91
|
-
await state.storefront.purchase.readyToPay({ data });
|
|
92
|
-
const readyToPay = Object.values(readyToPayFields);
|
|
93
|
-
|
|
94
|
-
return filterReadyToPay(readyToPay);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { StorefontTestingInstance } from 'tests/mocks/storefront-mock';
|
|
2
|
-
import { ok, post } from 'msw-when-then';
|
|
3
|
-
import { when } from 'tests/msw/server';
|
|
4
|
-
import { storefrontURL } from 'tests/mocks/storefront-api-mock';
|
|
5
|
-
import { fetchReadyToPay } from './ready-to-pay';
|
|
6
|
-
import ReadyToPayModel from './models/ready-to-pay-model';
|
|
7
|
-
import { expectConfigurationError } from 'tests/async-utilities';
|
|
8
|
-
import PaymentMetadataModel from './models/payment-metadata';
|
|
9
|
-
import { paymentMethods } from '@rebilly/api-metadata';
|
|
10
|
-
|
|
11
|
-
describe('Storefront API Ready to Pay', () => {
|
|
12
|
-
it('can fetch ready to pay', async () => {
|
|
13
|
-
const readyToPayPayload = [
|
|
14
|
-
{
|
|
15
|
-
method: 'payment-card',
|
|
16
|
-
feature: null,
|
|
17
|
-
brands: ['Visa'],
|
|
18
|
-
filters: [],
|
|
19
|
-
},
|
|
20
|
-
];
|
|
21
|
-
const options = {
|
|
22
|
-
websiteId: 'test-website-id',
|
|
23
|
-
items: [
|
|
24
|
-
{
|
|
25
|
-
planId: 'test-plan-id',
|
|
26
|
-
quantity: 1,
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const paymentCardMetadata = [...paymentMethods].find(
|
|
32
|
-
(method) => method.apiName === 'payment-card',
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
when(post(`${storefrontURL}/ready-to-pay`)).thenReturn(
|
|
36
|
-
ok(readyToPayPayload),
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
const instance = StorefontTestingInstance({
|
|
40
|
-
options,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
vi.spyOn(instance.storefront.purchase, 'readyToPay');
|
|
44
|
-
|
|
45
|
-
const response = await fetchReadyToPay({
|
|
46
|
-
state: instance,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
expect(instance.storefront.purchase.readyToPay).toBeCalledTimes(1);
|
|
50
|
-
expect(instance.storefront.purchase.readyToPay).toBeCalledWith({
|
|
51
|
-
data: expect.objectContaining({
|
|
52
|
-
items: options.items,
|
|
53
|
-
websiteId: options.websiteId,
|
|
54
|
-
riskMetadata: expect.anything(),
|
|
55
|
-
}),
|
|
56
|
-
});
|
|
57
|
-
expect(response).toBeInstanceOf(Array);
|
|
58
|
-
expect(response[0]).toBeInstanceOf(ReadyToPayModel);
|
|
59
|
-
expect(response).toEqual([
|
|
60
|
-
new ReadyToPayModel({
|
|
61
|
-
index: 0,
|
|
62
|
-
...readyToPayPayload[0],
|
|
63
|
-
metadata: new PaymentMetadataModel(paymentCardMetadata),
|
|
64
|
-
}),
|
|
65
|
-
]);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should throw errors with no options', async () => {
|
|
69
|
-
const noConfigOrOptionsInstance = new StorefontTestingInstance({
|
|
70
|
-
options: null,
|
|
71
|
-
});
|
|
72
|
-
await expectConfigurationError(
|
|
73
|
-
fetchReadyToPay({ state: noConfigOrOptionsInstance }),
|
|
74
|
-
);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { MockStorefront } from 'tests/mocks/storefront-mock';
|
|
2
|
-
|
|
3
|
-
describe('Storefront application programming interface', () => {
|
|
4
|
-
it('can create rebilly-js-sdk instance', () => {
|
|
5
|
-
const storefront = MockStorefront();
|
|
6
|
-
expect(storefront).toHaveProperty('setPublishableKey');
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('has required endpoints', () => {
|
|
10
|
-
const storefront = MockStorefront();
|
|
11
|
-
expect(storefront).toHaveProperty('purchase');
|
|
12
|
-
expect(storefront.purchase).toHaveProperty('readyToPay');
|
|
13
|
-
});
|
|
14
|
-
});
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import cloneDeep from 'lodash.clonedeep';
|
|
2
|
-
import state from '../state';
|
|
3
|
-
import SummaryModel from './models/summary-model';
|
|
4
|
-
import { Endpoint } from './index';
|
|
5
|
-
import {
|
|
6
|
-
mapItemsQuantities,
|
|
7
|
-
createQuantitiesMap,
|
|
8
|
-
updateItemQuantityFromMap,
|
|
9
|
-
} from '../utils/quantity';
|
|
10
|
-
|
|
11
|
-
export async function fetchSummary({ data = null } = {}) {
|
|
12
|
-
return Endpoint(async () => {
|
|
13
|
-
const items = state.options?.items;
|
|
14
|
-
const websiteId = state.options?.websiteId || null;
|
|
15
|
-
const bumpOfferProductsIds =
|
|
16
|
-
state.options?.bumpOffer?.map(({ planId }) => planId) || [];
|
|
17
|
-
|
|
18
|
-
const payload = {
|
|
19
|
-
data: {
|
|
20
|
-
websiteId,
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const isBumpOfferSummary = () =>
|
|
25
|
-
state.data.summaryLineItems.every((item) =>
|
|
26
|
-
bumpOfferProductsIds.includes(item.planId),
|
|
27
|
-
);
|
|
28
|
-
if (!isBumpOfferSummary() && !state.data.acceptBumpOffer) {
|
|
29
|
-
payload.data.items = mapItemsQuantities(
|
|
30
|
-
state.data.summaryLineItems,
|
|
31
|
-
);
|
|
32
|
-
} else if (items) {
|
|
33
|
-
if (state.data.acceptBumpOffer) {
|
|
34
|
-
payload.data.items = state.options.bumpOffer;
|
|
35
|
-
} else {
|
|
36
|
-
payload.data.items = mapItemsQuantities(items);
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
payload.data.items = mapItemsQuantities(
|
|
40
|
-
state.data.summaryLineItems,
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
if (state.data?.addons.length > 0) {
|
|
44
|
-
payload.data.items = payload.data.items.concat(
|
|
45
|
-
state.data.addons.map(mapItemsQuantities),
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
!state.data.acceptBumpOffer &&
|
|
52
|
-
state.data?.previewPurchase?.addonLineItems?.length > 0
|
|
53
|
-
) {
|
|
54
|
-
payload.data.items = payload.data.items.concat(
|
|
55
|
-
state.data.previewPurchase.addonLineItems,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (state.data?.amountAndCurrency) {
|
|
60
|
-
payload.data = {
|
|
61
|
-
...payload.data,
|
|
62
|
-
...state.data.amountAndCurrency,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (data?.billingAddress) {
|
|
67
|
-
payload.data.billingAddress = data.billingAddress;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (data?.deliveryAddress) {
|
|
71
|
-
payload.data.deliveryAddress = data.deliveryAddress;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (state.data?.couponIds) {
|
|
75
|
-
payload.data.couponIds = state.data.couponIds;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const { fields: summaryFields } =
|
|
79
|
-
await state.storefront.purchase.preview(payload);
|
|
80
|
-
|
|
81
|
-
let transformedFields = summaryFields;
|
|
82
|
-
if (!state.data.acceptBumpOffer) {
|
|
83
|
-
const quantitiesByPlanId = createQuantitiesMap(items);
|
|
84
|
-
|
|
85
|
-
transformedFields = cloneDeep(summaryFields);
|
|
86
|
-
if (transformedFields.lineItems) {
|
|
87
|
-
if (state.data?.previewPurchase?.addonLineItems) {
|
|
88
|
-
const addonsMap =
|
|
89
|
-
state.data.previewPurchase.addonLineItems.reduce(
|
|
90
|
-
(map, curr) => {
|
|
91
|
-
map.set(curr.planId, curr);
|
|
92
|
-
return map;
|
|
93
|
-
},
|
|
94
|
-
new Map(),
|
|
95
|
-
);
|
|
96
|
-
transformedFields.lineItems =
|
|
97
|
-
transformedFields.lineItems.filter(
|
|
98
|
-
(item) => !addonsMap.has(item.planId),
|
|
99
|
-
);
|
|
100
|
-
transformedFields.addonLineItems =
|
|
101
|
-
state.data.previewPurchase.addonLineItems;
|
|
102
|
-
}
|
|
103
|
-
transformedFields.lineItems.forEach(
|
|
104
|
-
updateItemQuantityFromMap(quantitiesByPlanId),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// In case of preview purchase gets call again for updating the values
|
|
110
|
-
state.data.previewPurchase = transformedFields;
|
|
111
|
-
|
|
112
|
-
return new SummaryModel(transformedFields);
|
|
113
|
-
});
|
|
114
|
-
}
|