@rebilly/instruments 3.8.2-beta.0 → 3.9.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +5 -5
- package/dist/index.min.js +5 -5
- package/package.json +1 -1
- package/src/functions/mount/fetch-data.js +11 -3
- package/src/functions/mount/fetch-data.spec.js +27 -0
- package/src/storefront/account.js +11 -0
- package/src/storefront/models/account-model.js +39 -0
- package/src/storefront/payment-instruments.js +1 -1
- package/src/views/common/iframe/modal-iframe.js +2 -2
- package/src/views/method-selector/express-methods/index.js +1 -1
- package/src/views/method-selector/mount-methods.js +1 -0
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import { fetchReadyToPay } from '../../storefront/ready-to-pay';
|
|
|
5
5
|
import { fetchSummary } from '../../storefront/summary';
|
|
6
6
|
import { fetchInvoice as FetchInvoice } from '../../storefront/invoices';
|
|
7
7
|
import { fetchTransaction as FetchTransaction } from '../../storefront/transactions';
|
|
8
|
+
import { fetchAccount as FetchAccount } from '../../storefront/account';
|
|
8
9
|
|
|
9
10
|
export class DataInstance {
|
|
10
11
|
constructor({
|
|
@@ -104,12 +105,14 @@ export async function fetchData({
|
|
|
104
105
|
riskMetadata = null,
|
|
105
106
|
summaryPayload = null,
|
|
106
107
|
|
|
107
|
-
//
|
|
108
|
+
// Dependency injectable functions
|
|
108
109
|
fetchInvoice = FetchInvoice,
|
|
109
|
-
fetchTransaction = FetchTransaction
|
|
110
|
+
fetchTransaction = FetchTransaction,
|
|
111
|
+
fetchAccount = FetchAccount
|
|
110
112
|
}) {
|
|
111
113
|
try {
|
|
112
114
|
let transaction = null;
|
|
115
|
+
let account = null;
|
|
113
116
|
if (state.options?.transactionId) {
|
|
114
117
|
transaction = await fetchTransaction({data: {
|
|
115
118
|
id: state.options.transactionId
|
|
@@ -130,11 +133,16 @@ export async function fetchData({
|
|
|
130
133
|
riskMetadata = data;
|
|
131
134
|
}
|
|
132
135
|
|
|
136
|
+
if (state.options?.jwt) {
|
|
137
|
+
account = await fetchAccount({state});
|
|
138
|
+
}
|
|
139
|
+
|
|
133
140
|
state.data = new DataInstance({
|
|
134
141
|
state,
|
|
135
142
|
invoice,
|
|
136
143
|
transaction,
|
|
137
|
-
riskMetadata
|
|
144
|
+
riskMetadata,
|
|
145
|
+
customerAddress: account?.address || null,
|
|
138
146
|
});
|
|
139
147
|
|
|
140
148
|
const [readyToPay, previewPurchase] = await Promise.all([
|
|
@@ -72,6 +72,33 @@ describe('fetchData function', () => {
|
|
|
72
72
|
expect(mockFetchInvoice).toBeCalledTimes(0);
|
|
73
73
|
|
|
74
74
|
});
|
|
75
|
+
|
|
76
|
+
it('Should fetch account when JWT is supplied', async () => {
|
|
77
|
+
const mockFetchAccount = jest.fn();
|
|
78
|
+
const accountState = {
|
|
79
|
+
options: {
|
|
80
|
+
jwt: 'TEST_JWT'
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await fetchData({
|
|
85
|
+
state: accountState,
|
|
86
|
+
fetchAccount: mockFetchAccount,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect(mockFetchAccount).toBeCalledTimes(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('Should not fetch account when there JWT is not supplied', async () => {
|
|
93
|
+
const mockFetchAccount = jest.fn();
|
|
94
|
+
|
|
95
|
+
await fetchData({
|
|
96
|
+
state: {},
|
|
97
|
+
fetchAccount: mockFetchAccount,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(mockFetchAccount).toBeCalledTimes(0);
|
|
101
|
+
});
|
|
75
102
|
});
|
|
76
103
|
|
|
77
104
|
describe('DataInstance', () => {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import AccountModel from './models/account-model';
|
|
2
|
+
import { Endpoint } from './index';
|
|
3
|
+
|
|
4
|
+
export async function fetchAccount({ state = null }) {
|
|
5
|
+
return Endpoint({state}, async () => {
|
|
6
|
+
state.storefront.setSessionToken(state.options.jwt);
|
|
7
|
+
const {fields} = await state.storefront.account.get();
|
|
8
|
+
|
|
9
|
+
return new AccountModel(fields);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import BaseModel from './base-model';
|
|
2
|
+
|
|
3
|
+
export class AddressModel {
|
|
4
|
+
constructor({
|
|
5
|
+
firstName = null,
|
|
6
|
+
lastName = null,
|
|
7
|
+
organization = null,
|
|
8
|
+
address = null,
|
|
9
|
+
address2 = null,
|
|
10
|
+
city = null,
|
|
11
|
+
region = null,
|
|
12
|
+
country = null,
|
|
13
|
+
postalCode = null,
|
|
14
|
+
emails = [],
|
|
15
|
+
phoneNumbers = [],
|
|
16
|
+
} = {}) {
|
|
17
|
+
this.firstName = firstName;
|
|
18
|
+
this.lastName = lastName;
|
|
19
|
+
this.organization = organization;
|
|
20
|
+
this.address = address;
|
|
21
|
+
this.address2 = address2;
|
|
22
|
+
this.city = city;
|
|
23
|
+
this.region = region;
|
|
24
|
+
this.country = country;
|
|
25
|
+
this.postalCode = postalCode;
|
|
26
|
+
this.email = emails.find(v => v.primary)?.value || null;
|
|
27
|
+
this.phoneNumber = phoneNumbers.find(v => v.primary)?.value || null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default class AccountModel extends BaseModel {
|
|
32
|
+
constructor({
|
|
33
|
+
primaryAddress = null,
|
|
34
|
+
...fields
|
|
35
|
+
} = {}) {
|
|
36
|
+
super(fields);
|
|
37
|
+
this.address = new AddressModel(primaryAddress);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -24,7 +24,7 @@ export async function setupPaymentInstrument({ data, state }) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
if (state.data?.transaction?.redirectUrl) {
|
|
27
|
-
setupPayload.data.redirectUrl = `${setupPayload.data.redirectUrl}&
|
|
27
|
+
setupPayload.data.redirectUrl = `${setupPayload.data.redirectUrl}&originalRedirectUrl=${state.data.transaction.redirectUrl}`
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
const { fields: transaction } = await state.storefront.paymentInstruments.setup(setupPayload);
|
|
@@ -30,7 +30,7 @@ export class ModalIframe extends BaseIframe {
|
|
|
30
30
|
window.addEventListener('message', async (event) => {
|
|
31
31
|
if(event.data === 'rebilly-instruments-approval-url-close') {
|
|
32
32
|
if (this.state.options.transactionType === 'purchase') {
|
|
33
|
-
this.state.storefront.setSessionToken(this.state.data.token);
|
|
33
|
+
this.state.storefront.setSessionToken(this.state.data.token || this.state.options.jwt);
|
|
34
34
|
|
|
35
35
|
const [
|
|
36
36
|
{fields: transaction},
|
|
@@ -52,7 +52,7 @@ export class ModalIframe extends BaseIframe {
|
|
|
52
52
|
}
|
|
53
53
|
close(updatedPurchase);
|
|
54
54
|
} else if (this.state.options.transactionType === 'setup') {
|
|
55
|
-
this.state.storefront.setSessionToken(this.state.data.instrument.token);
|
|
55
|
+
this.state.storefront.setSessionToken(this.state.data.instrument.token || this.state.options.jwt);
|
|
56
56
|
const {
|
|
57
57
|
fields: transaction
|
|
58
58
|
} = await this.state.storefront.transactions.get({id: this.state.data.transaction.id});
|
|
@@ -65,6 +65,7 @@ export function MountMethods({
|
|
|
65
65
|
'payment-card'
|
|
66
66
|
].includes(methodId) && state.options.paymentInstruments[methodType]?.popup;
|
|
67
67
|
const model = {
|
|
68
|
+
customerAddress: state.data.customerAddress,
|
|
68
69
|
options: state.options,
|
|
69
70
|
mainStyle: state.mainStyle,
|
|
70
71
|
plans: state.data.plans,
|