@infosoftas/s4-orders-js-sdk 0.2.0 → 0.2.1
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/.editorconfig +8 -8
- package/.release-it.json +10 -10
- package/README.md +13 -6
- package/build/postcss-scope-tailwind.cjs +0 -0
- package/components.json +0 -0
- package/package.json +1 -1
- package/src/App.completion.test.tsx +46 -107
- package/src/App.css +0 -14
- package/src/App.paymentReturn.test.tsx +341 -0
- package/src/App.tsx +20 -51
- package/src/ErrorBoundary.tsx +41 -41
- package/src/api/InvoiceApi.ts +23 -23
- package/src/api/SubscribeApi.ts +18 -18
- package/src/bundle.ts +4 -4
- package/src/components/Alert/Alert.tsx +23 -23
- package/src/components/Button/Button.tsx +39 -39
- package/src/components/EHFForm/EHFForm.tsx +0 -3
- package/src/components/FormFields/AddressField.tsx +55 -55
- package/src/components/FormFields/EmailField.tsx +53 -53
- package/src/components/FormFields/FormFields.helper.ts +34 -34
- package/src/components/FormFields/FormFieldsMapper.ts +34 -34
- package/src/components/FormFields/InputField.tsx +52 -52
- package/src/components/FormFields/NameField.tsx +52 -52
- package/src/components/FormFields/PhoneField.tsx +53 -53
- package/src/components/FormFields/TermsCheckbox.tsx +70 -70
- package/src/components/FormFields/ToggleFiled.tsx +52 -52
- package/src/components/FormFields/fieldValidators.ts +71 -71
- package/src/components/OIOForm/OIOForm.tsx +0 -3
- package/src/components/OrderDenialModal/OrderDenialModal.tsx +102 -102
- package/src/components/OrderForm/ContactDetailsSection.tsx +71 -71
- package/src/components/OrderForm/OrderForm.tsx +0 -3
- package/src/components/OrderForm/PaymentMethodContent.tsx +62 -62
- package/src/components/OrderForm/PaymentMethodSection.tsx +79 -79
- package/src/components/OrderForm/SubmitConfirmationDialog.tsx +55 -55
- package/src/components/OrderForm/orderFormState.ts +171 -171
- package/src/components/OrderForm/useSlotElement.tsx +89 -89
- package/src/components/ui/alert-dialog.tsx +0 -0
- package/src/components/ui/alert.tsx +0 -0
- package/src/components/ui/button.tsx +0 -0
- package/src/components/ui/card.tsx +0 -0
- package/src/components/ui/checkbox.tsx +0 -0
- package/src/components/ui/field.tsx +0 -0
- package/src/components/ui/input.tsx +0 -0
- package/src/components/ui/label.tsx +0 -0
- package/src/components/ui/radio-group.tsx +0 -0
- package/src/components/ui/separator.tsx +0 -0
- package/src/components/ui/skeleton.tsx +0 -0
- package/src/components/ui/spinner.tsx +0 -0
- package/src/components/ui/switch.tsx +0 -0
- package/src/components/ui/textarea.tsx +0 -0
- package/src/constants/index.ts +13 -13
- package/src/custom.d.ts +0 -0
- package/src/enums/api.ts +5 -5
- package/src/enums/general.ts +4 -2
- package/src/hooks/useOrderForm.tsx +0 -3
- package/src/hooks/usePaymentReturn.tsx +139 -0
- package/src/hooks/useQueryParams.ts +5 -5
- package/src/index.tsx +30 -30
- package/src/lib/utils.ts +1 -1
- package/src/types/api.ts +28 -28
- package/src/types/general.ts +0 -1
- package/src/utils/helper.ts +78 -78
- package/src/utils/order.helper.test.ts +81 -0
- package/src/utils/order.helper.ts +38 -25
- package/tsconfig.json +1 -0
- package/src/components/MainIframe/MainIframe.tsx +0 -42
- package/src/hooks/useIframe.tsx +0 -59
- package/src/hooks/useMessageEvent.tsx +0 -194
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
act,
|
|
5
|
+
cleanup,
|
|
6
|
+
fireEvent,
|
|
7
|
+
render,
|
|
8
|
+
screen,
|
|
9
|
+
} from '@testing-library/react';
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
11
|
+
|
|
12
|
+
import App from './App';
|
|
13
|
+
import { MessageEventTypeEnum } from './enums/general';
|
|
14
|
+
import type { ConfigType } from './types/general';
|
|
15
|
+
import type { OrderInfoType } from './types/order';
|
|
16
|
+
|
|
17
|
+
const submission = vi.hoisted(() => ({
|
|
18
|
+
url: null as string | null,
|
|
19
|
+
data: null as unknown as OrderInfoType,
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
vi.mock('./components/OrderForm/OrderForm', () => ({
|
|
23
|
+
default: ({
|
|
24
|
+
callback,
|
|
25
|
+
}: {
|
|
26
|
+
callback: (url: string | null, data: OrderInfoType) => void;
|
|
27
|
+
}) => (
|
|
28
|
+
<button onClick={() => callback(submission.url, submission.data)}>
|
|
29
|
+
Submit order
|
|
30
|
+
</button>
|
|
31
|
+
),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
const response = (body: unknown, status = 200) =>
|
|
35
|
+
new Response(JSON.stringify(body), { status });
|
|
36
|
+
|
|
37
|
+
const fetchMock = vi.fn<typeof fetch>();
|
|
38
|
+
|
|
39
|
+
const renderApp = (props: Partial<ConfigType> = {}) =>
|
|
40
|
+
render(
|
|
41
|
+
<App
|
|
42
|
+
apiKey="tenant-key"
|
|
43
|
+
apiUrl="https://orders.example.test"
|
|
44
|
+
domElementId="sdk-order"
|
|
45
|
+
templatePackageId="template-1"
|
|
46
|
+
subscriberId=""
|
|
47
|
+
userId=""
|
|
48
|
+
identityProviderId=""
|
|
49
|
+
organizationId="organization-1"
|
|
50
|
+
settings={{
|
|
51
|
+
successText: 'Order completed',
|
|
52
|
+
orderProcessingText: 'Processing order',
|
|
53
|
+
}}
|
|
54
|
+
{...props}
|
|
55
|
+
/>
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const returnTo = (search: string) =>
|
|
59
|
+
window.history.replaceState({}, '', `/${search}`);
|
|
60
|
+
|
|
61
|
+
const advance = async (ms = 0) => {
|
|
62
|
+
await act(async () => {
|
|
63
|
+
await vi.advanceTimersByTimeAsync(ms);
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const requests = () =>
|
|
68
|
+
fetchMock.mock.calls.map(([url, init]) => `${init?.method} ${url}`);
|
|
69
|
+
|
|
70
|
+
const postedMessages = (type: MessageEventTypeEnum) =>
|
|
71
|
+
vi
|
|
72
|
+
.mocked(window.postMessage)
|
|
73
|
+
.mock.calls.map(([message]) => message as { type?: string })
|
|
74
|
+
.filter((message) => message.type === type);
|
|
75
|
+
|
|
76
|
+
const foreignMessages = [
|
|
77
|
+
{ type: 'cancel' },
|
|
78
|
+
{ type: 'complete', orderId: 'foreign-order', agreementId: 'x' },
|
|
79
|
+
{ type: 'completeOrder', orderId: 'foreign-order', agreementId: 'x' },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const postForeignMessages = () =>
|
|
83
|
+
act(() => {
|
|
84
|
+
for (const data of foreignMessages) {
|
|
85
|
+
window.dispatchEvent(new MessageEvent('message', { data }));
|
|
86
|
+
window.dispatchEvent(
|
|
87
|
+
new MessageEvent('message', { data, source: window })
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('full-page payment returns', () => {
|
|
93
|
+
beforeEach(() => {
|
|
94
|
+
vi.useFakeTimers();
|
|
95
|
+
fetchMock.mockReset();
|
|
96
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
97
|
+
returnTo('');
|
|
98
|
+
sessionStorage.clear();
|
|
99
|
+
localStorage.clear();
|
|
100
|
+
submission.url = null;
|
|
101
|
+
submission.data = null as unknown as OrderInfoType;
|
|
102
|
+
vi.spyOn(window, 'postMessage').mockImplementation(() => undefined);
|
|
103
|
+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
afterEach(() => {
|
|
107
|
+
cleanup();
|
|
108
|
+
vi.useRealTimers();
|
|
109
|
+
vi.restoreAllMocks();
|
|
110
|
+
vi.unstubAllGlobals();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('ignores payment messages when no checkout is active', async () => {
|
|
114
|
+
renderApp();
|
|
115
|
+
postForeignMessages();
|
|
116
|
+
await advance(1000);
|
|
117
|
+
|
|
118
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
119
|
+
expect(
|
|
120
|
+
screen.getByRole('button', { name: 'Submit order' })
|
|
121
|
+
).toBeTruthy();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('does not let payment messages abort, cancel or redirect an active completion', async () => {
|
|
125
|
+
let finish!: (value: Response) => void;
|
|
126
|
+
fetchMock.mockResolvedValueOnce(response(null)).mockImplementationOnce(
|
|
127
|
+
() =>
|
|
128
|
+
new Promise((resolve) => {
|
|
129
|
+
finish = resolve;
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
returnTo(
|
|
133
|
+
'?action=complete&S4OrderId=returned-order&TransactionId=tx-1'
|
|
134
|
+
);
|
|
135
|
+
renderApp();
|
|
136
|
+
await advance();
|
|
137
|
+
|
|
138
|
+
postForeignMessages();
|
|
139
|
+
await advance();
|
|
140
|
+
|
|
141
|
+
expect(requests()).toEqual([
|
|
142
|
+
'POST https://orders.example.test/order/returned-order/complete',
|
|
143
|
+
'GET https://orders.example.test/order/returned-order',
|
|
144
|
+
]);
|
|
145
|
+
expect(fetchMock.mock.calls[1][1]?.signal?.aborted).toBe(false);
|
|
146
|
+
|
|
147
|
+
await act(async () => {
|
|
148
|
+
finish(response({ id: 'returned-order', status: 'Completed' }));
|
|
149
|
+
});
|
|
150
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
151
|
+
expect(
|
|
152
|
+
postedMessages(MessageEventTypeEnum.ORDER_FLOW_COMPLETE)
|
|
153
|
+
).toEqual([expect.objectContaining({ orderId: 'returned-order' })]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('sends SwedbankPay to the payment terminal with a full-page redirect', async () => {
|
|
157
|
+
submission.url = `${window.location.origin}/#swedbank-terminal`;
|
|
158
|
+
submission.data = {
|
|
159
|
+
orderId: 'swedbank-order',
|
|
160
|
+
paymentMethod: 'SwedbankPay',
|
|
161
|
+
subscriberId: 'subscriber-7',
|
|
162
|
+
};
|
|
163
|
+
renderApp({ showIframe: true } as Partial<ConfigType>);
|
|
164
|
+
|
|
165
|
+
fireEvent.click(screen.getByRole('button', { name: 'Submit order' }));
|
|
166
|
+
await advance(300);
|
|
167
|
+
|
|
168
|
+
expect(window.location.hash).toBe('#swedbank-terminal');
|
|
169
|
+
expect(document.querySelector('iframe')).toBeNull();
|
|
170
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
171
|
+
// Coming back with the browser's back button shows a usable form.
|
|
172
|
+
expect(
|
|
173
|
+
screen.getByRole('button', { name: 'Submit order' })
|
|
174
|
+
).toBeTruthy();
|
|
175
|
+
expect(screen.queryByTestId('sdk-order-loading-skeleton')).toBeNull();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('completes a SwedbankPay return', async () => {
|
|
179
|
+
fetchMock
|
|
180
|
+
.mockResolvedValueOnce(response(null))
|
|
181
|
+
.mockResolvedValueOnce(
|
|
182
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
183
|
+
);
|
|
184
|
+
returnTo(
|
|
185
|
+
'?action=complete&S4OrderId=returned-order&TransactionId=tx-1'
|
|
186
|
+
);
|
|
187
|
+
renderApp();
|
|
188
|
+
await advance();
|
|
189
|
+
|
|
190
|
+
expect(requests()).toEqual([
|
|
191
|
+
'POST https://orders.example.test/order/returned-order/complete',
|
|
192
|
+
'GET https://orders.example.test/order/returned-order',
|
|
193
|
+
]);
|
|
194
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('cancels the order on a SwedbankPay cancel return and reopens the form', async () => {
|
|
198
|
+
fetchMock.mockResolvedValueOnce(response(null));
|
|
199
|
+
returnTo('?action=cancel&S4OrderId=returned-order&TransactionId=tx-1');
|
|
200
|
+
renderApp();
|
|
201
|
+
await advance();
|
|
202
|
+
|
|
203
|
+
expect(requests()).toEqual([
|
|
204
|
+
'DELETE https://orders.example.test/order/returned-order',
|
|
205
|
+
]);
|
|
206
|
+
expect(
|
|
207
|
+
screen.getByRole('button', { name: 'Submit order' })
|
|
208
|
+
).toBeTruthy();
|
|
209
|
+
expect(
|
|
210
|
+
postedMessages(MessageEventTypeEnum.ORDER_FLOW_CANCEL)
|
|
211
|
+
).toHaveLength(1);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('cancels the stored Mollie order on a cancel return without an order ID', async () => {
|
|
215
|
+
fetchMock.mockResolvedValueOnce(response(null));
|
|
216
|
+
sessionStorage.setItem(
|
|
217
|
+
'mollieOrderId',
|
|
218
|
+
JSON.stringify({ id: 'mollie-order', ts: Date.now() })
|
|
219
|
+
);
|
|
220
|
+
returnTo('?action=cancel');
|
|
221
|
+
renderApp();
|
|
222
|
+
await advance();
|
|
223
|
+
|
|
224
|
+
expect(requests()).toEqual([
|
|
225
|
+
'DELETE https://orders.example.test/order/mollie-order',
|
|
226
|
+
]);
|
|
227
|
+
expect(sessionStorage.getItem('mollieOrderId')).toBeNull();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('completes a Mollie return', async () => {
|
|
231
|
+
fetchMock
|
|
232
|
+
.mockResolvedValueOnce(response(null))
|
|
233
|
+
.mockResolvedValueOnce(
|
|
234
|
+
response({ id: 'mollie-order', status: 'Completed' })
|
|
235
|
+
);
|
|
236
|
+
returnTo('?action=complete&S4OrderId=mollie-order');
|
|
237
|
+
renderApp();
|
|
238
|
+
await advance();
|
|
239
|
+
|
|
240
|
+
expect(requests()).toEqual([
|
|
241
|
+
'POST https://orders.example.test/order/mollie-order/complete',
|
|
242
|
+
'GET https://orders.example.test/order/mollie-order',
|
|
243
|
+
]);
|
|
244
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('completes a Vipps return', async () => {
|
|
248
|
+
fetchMock
|
|
249
|
+
.mockResolvedValueOnce(response(null))
|
|
250
|
+
.mockResolvedValueOnce(
|
|
251
|
+
response({ id: 'vipps-order', status: 'Completed' })
|
|
252
|
+
);
|
|
253
|
+
returnTo('?orderId=vipps-order&agreementId=agreement-1');
|
|
254
|
+
renderApp();
|
|
255
|
+
await advance();
|
|
256
|
+
|
|
257
|
+
expect(requests()).toEqual([
|
|
258
|
+
'POST https://orders.example.test/order/vipps-order/complete',
|
|
259
|
+
'GET https://orders.example.test/order/vipps-order',
|
|
260
|
+
]);
|
|
261
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe('payment returns inside a legacy checkout iframe', () => {
|
|
266
|
+
const swedbankComplete =
|
|
267
|
+
'?action=complete&S4OrderId=returned-order&TransactionId=tx-1';
|
|
268
|
+
|
|
269
|
+
const insideFrame = (location: object) => {
|
|
270
|
+
const parent = { location } as unknown as Window;
|
|
271
|
+
vi.spyOn(window, 'top', 'get').mockReturnValue(parent);
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
beforeEach(() => {
|
|
275
|
+
vi.useFakeTimers();
|
|
276
|
+
fetchMock.mockReset();
|
|
277
|
+
fetchMock.mockResolvedValue(response(null));
|
|
278
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
279
|
+
returnTo('');
|
|
280
|
+
sessionStorage.clear();
|
|
281
|
+
vi.spyOn(window, 'postMessage').mockImplementation(() => undefined);
|
|
282
|
+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
afterEach(() => {
|
|
286
|
+
cleanup();
|
|
287
|
+
vi.useRealTimers();
|
|
288
|
+
vi.restoreAllMocks();
|
|
289
|
+
vi.unstubAllGlobals();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it.each([
|
|
293
|
+
['a SwedbankPay completion', swedbankComplete],
|
|
294
|
+
['a cancellation', '?action=cancel&S4OrderId=returned-order'],
|
|
295
|
+
['a Vipps return', '?orderId=vipps-order&agreementId=agreement-1'],
|
|
296
|
+
])(
|
|
297
|
+
'moves %s to the top-level page without handling it in the frame',
|
|
298
|
+
async (_label, search) => {
|
|
299
|
+
const replace = vi.fn();
|
|
300
|
+
insideFrame({ origin: window.location.origin, replace });
|
|
301
|
+
sessionStorage.setItem(
|
|
302
|
+
'mollieOrderId',
|
|
303
|
+
JSON.stringify({ id: 'mollie-order', ts: Date.now() })
|
|
304
|
+
);
|
|
305
|
+
returnTo(search);
|
|
306
|
+
renderApp();
|
|
307
|
+
await advance(1000);
|
|
308
|
+
|
|
309
|
+
expect(replace).toHaveBeenCalledExactlyOnceWith(
|
|
310
|
+
window.location.href
|
|
311
|
+
);
|
|
312
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
313
|
+
expect(sessionStorage.getItem('mollieOrderId')).not.toBeNull();
|
|
314
|
+
}
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
it('handles the return in the frame when the parent page is on another origin', async () => {
|
|
318
|
+
insideFrame({
|
|
319
|
+
get origin(): string {
|
|
320
|
+
throw new DOMException('Blocked', 'SecurityError');
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
returnTo(swedbankComplete);
|
|
324
|
+
renderApp({ completionMode: 'external' });
|
|
325
|
+
await advance();
|
|
326
|
+
|
|
327
|
+
expect(requests()).toEqual([
|
|
328
|
+
'POST https://orders.example.test/order/returned-order/complete',
|
|
329
|
+
]);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('does nothing in a frame without a payment return', async () => {
|
|
333
|
+
const replace = vi.fn();
|
|
334
|
+
insideFrame({ origin: window.location.origin, replace });
|
|
335
|
+
renderApp();
|
|
336
|
+
await advance(1000);
|
|
337
|
+
|
|
338
|
+
expect(replace).not.toHaveBeenCalled();
|
|
339
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
340
|
+
});
|
|
341
|
+
});
|
package/src/App.tsx
CHANGED
|
@@ -6,11 +6,10 @@ import {
|
|
|
6
6
|
PaymentMethodEnum,
|
|
7
7
|
UserActionEnum,
|
|
8
8
|
} from './enums/general';
|
|
9
|
-
import
|
|
9
|
+
import usePaymentReturn from './hooks/usePaymentReturn';
|
|
10
10
|
import OrderForm from './components/OrderForm/OrderForm';
|
|
11
11
|
import OIOForm from './components/OIOForm/OIOForm';
|
|
12
12
|
import EHFForm from './components/EHFForm/EHFForm';
|
|
13
|
-
import MainIframe from './components/MainIframe/MainIframe';
|
|
14
13
|
import Alert from './components/Alert/Alert';
|
|
15
14
|
import { Skeleton } from './components/ui/skeleton';
|
|
16
15
|
import { Card, CardContent } from './components/ui/card';
|
|
@@ -55,7 +54,6 @@ const App: FC<ConfigType> = ({
|
|
|
55
54
|
identityProviderId,
|
|
56
55
|
organizationId,
|
|
57
56
|
redirectUrl,
|
|
58
|
-
showIframe,
|
|
59
57
|
paymentMethodsOptions,
|
|
60
58
|
invoiceAddressSelection,
|
|
61
59
|
availablePaymentMethods = [],
|
|
@@ -101,7 +99,6 @@ const App: FC<ConfigType> = ({
|
|
|
101
99
|
}) => {
|
|
102
100
|
const [loading, setLoading] = useState<boolean>(false);
|
|
103
101
|
const [showOrderForm, setShowOrderForm] = useState<boolean>(true);
|
|
104
|
-
const [iframeSrc, setIframeSrc] = useState<string | null>(null);
|
|
105
102
|
const [orderId, setOrderId] = useState<string | null>(null);
|
|
106
103
|
const [subscriberSdkId, setSubscriberSdkId] = useState<string | undefined>(
|
|
107
104
|
subscriberId || undefined
|
|
@@ -161,7 +158,6 @@ const App: FC<ConfigType> = ({
|
|
|
161
158
|
setLoading(true);
|
|
162
159
|
setCompletionState(null);
|
|
163
160
|
setIsConfirmed(false);
|
|
164
|
-
setIframeSrc(null);
|
|
165
161
|
setShowOrderForm(false);
|
|
166
162
|
setIsFailed(false);
|
|
167
163
|
setFailedMsg('');
|
|
@@ -310,38 +306,27 @@ const App: FC<ConfigType> = ({
|
|
|
310
306
|
setShowOrderForm(true);
|
|
311
307
|
};
|
|
312
308
|
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
setCompletionState(null);
|
|
322
|
-
try {
|
|
323
|
-
if (orderIdToCancel) {
|
|
324
|
-
await handleOrderDelete(orderIdToCancel);
|
|
325
|
-
}
|
|
326
|
-
} catch (error) {
|
|
327
|
-
console.log(error);
|
|
309
|
+
const handlePaymentCancel = async (canceledOrderId?: string) => {
|
|
310
|
+
const orderIdToCancel =
|
|
311
|
+
canceledOrderId || activeCompletion.current?.orderId || orderId;
|
|
312
|
+
activeCompletion.current?.controller.abort();
|
|
313
|
+
setCompletionState(null);
|
|
314
|
+
try {
|
|
315
|
+
if (orderIdToCancel) {
|
|
316
|
+
await handleOrderDelete(orderIdToCancel);
|
|
328
317
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
setOrderId(null);
|
|
332
|
-
setOrderInfo(null);
|
|
333
|
-
setIframeSrc(null);
|
|
334
|
-
setIsConfirmed(false);
|
|
335
|
-
setShowOrderForm(true);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.log(error);
|
|
336
320
|
}
|
|
321
|
+
|
|
322
|
+
setLoading(false);
|
|
323
|
+
setOrderId(null);
|
|
324
|
+
setOrderInfo(null);
|
|
325
|
+
setIsConfirmed(false);
|
|
326
|
+
setShowOrderForm(true);
|
|
337
327
|
};
|
|
338
328
|
|
|
339
|
-
|
|
340
|
-
messageCallback,
|
|
341
|
-
handleMessageEvent,
|
|
342
|
-
!!showIframe,
|
|
343
|
-
orderInfo
|
|
344
|
-
);
|
|
329
|
+
usePaymentReturn(messageCallback, handlePaymentCancel, orderInfo);
|
|
345
330
|
|
|
346
331
|
const handleForm = (url: string | null, data?: OrderInfoType | null) => {
|
|
347
332
|
activeCompletion.current?.controller.abort();
|
|
@@ -383,14 +368,6 @@ const App: FC<ConfigType> = ({
|
|
|
383
368
|
form: FormTypeEnum.OIO,
|
|
384
369
|
});
|
|
385
370
|
} else if (url) {
|
|
386
|
-
if (
|
|
387
|
-
showIframe &&
|
|
388
|
-
data?.paymentMethod === PaymentMethodEnum.SwedbankPay
|
|
389
|
-
) {
|
|
390
|
-
setIframeSrc(url);
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
371
|
if (
|
|
395
372
|
data?.paymentMethod === PaymentMethodEnum.Mollie &&
|
|
396
373
|
data?.orderId
|
|
@@ -418,8 +395,6 @@ const App: FC<ConfigType> = ({
|
|
|
418
395
|
setShowOrderForm(true);
|
|
419
396
|
setFormType(FormTypeEnum.ORDER);
|
|
420
397
|
}
|
|
421
|
-
|
|
422
|
-
setIframeSrc(null);
|
|
423
398
|
};
|
|
424
399
|
|
|
425
400
|
const handleInvoiceBack = () => {
|
|
@@ -492,13 +467,11 @@ const App: FC<ConfigType> = ({
|
|
|
492
467
|
return (
|
|
493
468
|
<ErrorBoundary>
|
|
494
469
|
<div
|
|
495
|
-
className=
|
|
496
|
-
iframeSrc ? 'show-iframe' : ''
|
|
497
|
-
}`}
|
|
470
|
+
className="sdk-order-container sdk-scope"
|
|
498
471
|
data-testid="sdk-order-module-id"
|
|
499
472
|
>
|
|
500
473
|
{moduleTitle && <h1 className="text-center">{moduleTitle}</h1>}
|
|
501
|
-
{
|
|
474
|
+
{showOrderForm && (
|
|
502
475
|
<>
|
|
503
476
|
{formType === FormTypeEnum.OIO && (
|
|
504
477
|
<OIOForm
|
|
@@ -524,7 +497,6 @@ const App: FC<ConfigType> = ({
|
|
|
524
497
|
identityProviderId={identityProviderId}
|
|
525
498
|
organizationId={organizationId}
|
|
526
499
|
redirectUrl={redirectUrl}
|
|
527
|
-
showIframe={showIframe}
|
|
528
500
|
paymentMethodsOptions={paymentMethodsOptions}
|
|
529
501
|
language={language}
|
|
530
502
|
merchantAgreementUrl={merchantAgreementUrl}
|
|
@@ -585,7 +557,6 @@ const App: FC<ConfigType> = ({
|
|
|
585
557
|
identityProviderId={identityProviderId}
|
|
586
558
|
organizationId={organizationId}
|
|
587
559
|
redirectUrl={redirectUrl}
|
|
588
|
-
showIframe={showIframe}
|
|
589
560
|
paymentMethodsOptions={paymentMethodsOptions}
|
|
590
561
|
language={language}
|
|
591
562
|
merchantAgreementUrl={merchantAgreementUrl}
|
|
@@ -671,7 +642,6 @@ const App: FC<ConfigType> = ({
|
|
|
671
642
|
}
|
|
672
643
|
defaultValues={orderFormValues}
|
|
673
644
|
redirectUrl={redirectUrl}
|
|
674
|
-
showIframe={showIframe}
|
|
675
645
|
paymentMethodsOptions={
|
|
676
646
|
paymentMethodsOptions
|
|
677
647
|
}
|
|
@@ -716,7 +686,6 @@ const App: FC<ConfigType> = ({
|
|
|
716
686
|
)}
|
|
717
687
|
</>
|
|
718
688
|
)}
|
|
719
|
-
{showIframe && <MainIframe iframeSrc={iframeSrc} />}
|
|
720
689
|
{completionState && (
|
|
721
690
|
<Card role="status" aria-live="polite">
|
|
722
691
|
<CardContent>
|
package/src/ErrorBoundary.tsx
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import React, { Component, type ErrorInfo, type ReactNode } from 'react';
|
|
2
|
-
|
|
3
|
-
interface Props {
|
|
4
|
-
children?: ReactNode;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface State {
|
|
8
|
-
hasError: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
class ErrorBoundary extends Component<Props, State> {
|
|
12
|
-
public state: State = {
|
|
13
|
-
hasError: false,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
public static getDerivedStateFromError(): State {
|
|
17
|
-
// Update state so the next render will show the fallback UI.
|
|
18
|
-
return { hasError: true };
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
22
|
-
console.error('Uncaught error:', error, errorInfo);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public render() {
|
|
26
|
-
if (this.state.hasError) {
|
|
27
|
-
return (
|
|
28
|
-
<div className="layout-wrapper">
|
|
29
|
-
<h1>
|
|
30
|
-
Something went wrong. Please reload the page and try
|
|
31
|
-
again.
|
|
32
|
-
</h1>
|
|
33
|
-
</div>
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return this.props.children;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export default ErrorBoundary;
|
|
1
|
+
import React, { Component, type ErrorInfo, type ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
interface Props {
|
|
4
|
+
children?: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface State {
|
|
8
|
+
hasError: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class ErrorBoundary extends Component<Props, State> {
|
|
12
|
+
public state: State = {
|
|
13
|
+
hasError: false,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
public static getDerivedStateFromError(): State {
|
|
17
|
+
// Update state so the next render will show the fallback UI.
|
|
18
|
+
return { hasError: true };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
22
|
+
console.error('Uncaught error:', error, errorInfo);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public render() {
|
|
26
|
+
if (this.state.hasError) {
|
|
27
|
+
return (
|
|
28
|
+
<div className="layout-wrapper">
|
|
29
|
+
<h1>
|
|
30
|
+
Something went wrong. Please reload the page and try
|
|
31
|
+
again.
|
|
32
|
+
</h1>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return this.props.children;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default ErrorBoundary;
|
package/src/api/InvoiceApi.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import fetcher, { getSdkApiKey } from '../utils/fetcher';
|
|
2
|
-
|
|
3
|
-
type RequestInvoiceLookupType = {
|
|
4
|
-
network: string;
|
|
5
|
-
value: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
type InvoiceLookupResponseType = {
|
|
9
|
-
receivesInvoice: boolean;
|
|
10
|
-
recievesInvoice: boolean; // TODO: Should be deleted in the future (misspelled), use "receivesInvoice" instead of this.
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const invoiceLookup = (
|
|
14
|
-
body: RequestInvoiceLookupType
|
|
15
|
-
): Promise<InvoiceLookupResponseType> =>
|
|
16
|
-
fetcher<InvoiceLookupResponseType, RequestInvoiceLookupType>({
|
|
17
|
-
method: 'POST',
|
|
18
|
-
url: '/invoicelookup',
|
|
19
|
-
headers: {
|
|
20
|
-
'S4-ORDERS-API-KEY': getSdkApiKey(),
|
|
21
|
-
},
|
|
22
|
-
body,
|
|
23
|
-
}) as Promise<InvoiceLookupResponseType>;
|
|
1
|
+
import fetcher, { getSdkApiKey } from '../utils/fetcher';
|
|
2
|
+
|
|
3
|
+
type RequestInvoiceLookupType = {
|
|
4
|
+
network: string;
|
|
5
|
+
value: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type InvoiceLookupResponseType = {
|
|
9
|
+
receivesInvoice: boolean;
|
|
10
|
+
recievesInvoice: boolean; // TODO: Should be deleted in the future (misspelled), use "receivesInvoice" instead of this.
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const invoiceLookup = (
|
|
14
|
+
body: RequestInvoiceLookupType
|
|
15
|
+
): Promise<InvoiceLookupResponseType> =>
|
|
16
|
+
fetcher<InvoiceLookupResponseType, RequestInvoiceLookupType>({
|
|
17
|
+
method: 'POST',
|
|
18
|
+
url: '/invoicelookup',
|
|
19
|
+
headers: {
|
|
20
|
+
'S4-ORDERS-API-KEY': getSdkApiKey(),
|
|
21
|
+
},
|
|
22
|
+
body,
|
|
23
|
+
}) as Promise<InvoiceLookupResponseType>;
|
package/src/api/SubscribeApi.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import fetcher, { getSdkApiKey } from '../utils/fetcher';
|
|
2
|
-
import type { CreateSubscriberRequestType } from '../types/api';
|
|
3
|
-
|
|
4
|
-
type CreateSubscriberResponseType = {
|
|
5
|
-
id: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const createSubscriber = (
|
|
9
|
-
body: CreateSubscriberRequestType
|
|
10
|
-
): Promise<CreateSubscriberResponseType> =>
|
|
11
|
-
fetcher<CreateSubscriberResponseType, CreateSubscriberRequestType>({
|
|
12
|
-
method: 'POST',
|
|
13
|
-
url: '/subscriber',
|
|
14
|
-
headers: {
|
|
15
|
-
'S4-ORDERS-API-KEY': getSdkApiKey(),
|
|
16
|
-
},
|
|
17
|
-
body,
|
|
18
|
-
}) as Promise<CreateSubscriberResponseType>;
|
|
1
|
+
import fetcher, { getSdkApiKey } from '../utils/fetcher';
|
|
2
|
+
import type { CreateSubscriberRequestType } from '../types/api';
|
|
3
|
+
|
|
4
|
+
type CreateSubscriberResponseType = {
|
|
5
|
+
id: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const createSubscriber = (
|
|
9
|
+
body: CreateSubscriberRequestType
|
|
10
|
+
): Promise<CreateSubscriberResponseType> =>
|
|
11
|
+
fetcher<CreateSubscriberResponseType, CreateSubscriberRequestType>({
|
|
12
|
+
method: 'POST',
|
|
13
|
+
url: '/subscriber',
|
|
14
|
+
headers: {
|
|
15
|
+
'S4-ORDERS-API-KEY': getSdkApiKey(),
|
|
16
|
+
},
|
|
17
|
+
body,
|
|
18
|
+
}) as Promise<CreateSubscriberResponseType>;
|
package/src/bundle.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Bundle-only entry: apps compiling src/ directly can't process this raw Tailwind CSS.
|
|
2
|
-
import './App.css';
|
|
3
|
-
|
|
4
|
-
export * from './index';
|
|
1
|
+
// Bundle-only entry: apps compiling src/ directly can't process this raw Tailwind CSS.
|
|
2
|
+
import './App.css';
|
|
3
|
+
|
|
4
|
+
export * from './index';
|