@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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PaymentMethodEnum, PaymentReturnActionEnum } from '../enums/general';
|
|
2
2
|
import type { AgreementsType, OrderFormInputsType } from '../types/order';
|
|
3
3
|
import type { OrderFormFieldType } from '../types/general';
|
|
4
4
|
import { FIELD_TYPES } from '../components/FormFields/FormFields.helper';
|
|
@@ -6,7 +6,6 @@ import { FIELD_TYPES } from '../components/FormFields/FormFields.helper';
|
|
|
6
6
|
type AgreementModelType = {
|
|
7
7
|
paymentMethod: PaymentMethodEnum;
|
|
8
8
|
redirectUrl?: string;
|
|
9
|
-
showIframe?: boolean;
|
|
10
9
|
phoneNumber?: string;
|
|
11
10
|
generateSubscriberContact: boolean;
|
|
12
11
|
language: string;
|
|
@@ -35,10 +34,30 @@ const mapToPaymentProviderLanguage = (language: string) => {
|
|
|
35
34
|
return 'en-US';
|
|
36
35
|
};
|
|
37
36
|
|
|
37
|
+
// Parameters a payment provider adds on return. A return URL built from a page
|
|
38
|
+
// that is itself a return would otherwise carry the previous attempt's values,
|
|
39
|
+
// and the first `action`/`S4OrderId` wins when the next return is read.
|
|
40
|
+
const PAYMENT_RETURN_PARAMS = [
|
|
41
|
+
'action',
|
|
42
|
+
's4orderid',
|
|
43
|
+
'transactionid',
|
|
44
|
+
'orderid',
|
|
45
|
+
'agreementid',
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const withoutPaymentReturnParams = (href: string) => {
|
|
49
|
+
const url = new URL(href);
|
|
50
|
+
for (const key of [...url.searchParams.keys()]) {
|
|
51
|
+
if (PAYMENT_RETURN_PARAMS.includes(key.toLowerCase())) {
|
|
52
|
+
url.searchParams.delete(key);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return url;
|
|
56
|
+
};
|
|
57
|
+
|
|
38
58
|
export const prepareAgreementModel = ({
|
|
39
59
|
paymentMethod,
|
|
40
60
|
redirectUrl,
|
|
41
|
-
showIframe,
|
|
42
61
|
phoneNumber,
|
|
43
62
|
generateSubscriberContact,
|
|
44
63
|
language,
|
|
@@ -51,28 +70,22 @@ export const prepareAgreementModel = ({
|
|
|
51
70
|
const existUrl = new URL(window.location.href);
|
|
52
71
|
const journey = existUrl.searchParams.get('journey');
|
|
53
72
|
const hashToken = existUrl.searchParams.get('hashToken');
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
: redirectUrl || window.location.href;
|
|
57
|
-
const cancelUrl = new URL(url);
|
|
58
|
-
cancelUrl.searchParams.append('action', `${MessageEventTypeEnum.CANCEL}`);
|
|
59
|
-
if (journey) {
|
|
60
|
-
cancelUrl.searchParams.append('journey', journey);
|
|
61
|
-
}
|
|
62
|
-
if (hashToken) {
|
|
63
|
-
cancelUrl.searchParams.append('hashToken', hashToken);
|
|
64
|
-
}
|
|
65
|
-
const confirmUrl = new URL(url);
|
|
66
|
-
confirmUrl.searchParams.append(
|
|
67
|
-
'action',
|
|
68
|
-
`${MessageEventTypeEnum.COMPLETE}`
|
|
73
|
+
const baseUrl = withoutPaymentReturnParams(
|
|
74
|
+
redirectUrl || window.location.href
|
|
69
75
|
);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
const buildReturnUrl = (action: PaymentReturnActionEnum) => {
|
|
77
|
+
const returnUrl = new URL(baseUrl);
|
|
78
|
+
returnUrl.searchParams.set('action', action);
|
|
79
|
+
if (journey) {
|
|
80
|
+
returnUrl.searchParams.set('journey', journey);
|
|
81
|
+
}
|
|
82
|
+
if (hashToken) {
|
|
83
|
+
returnUrl.searchParams.set('hashToken', hashToken);
|
|
84
|
+
}
|
|
85
|
+
return returnUrl;
|
|
86
|
+
};
|
|
87
|
+
const cancelUrl = buildReturnUrl(PaymentReturnActionEnum.CANCEL);
|
|
88
|
+
const confirmUrl = buildReturnUrl(PaymentReturnActionEnum.COMPLETE);
|
|
76
89
|
|
|
77
90
|
if (paymentMethod === PaymentMethodEnum.SwedbankPay) {
|
|
78
91
|
model.swedbankPay = {
|
|
@@ -105,7 +118,7 @@ export const prepareAgreementModel = ({
|
|
|
105
118
|
merchantAgreementUrl,
|
|
106
119
|
accountId,
|
|
107
120
|
phoneNumber,
|
|
108
|
-
merchantRedirectUrl:
|
|
121
|
+
merchantRedirectUrl: `${baseUrl}`,
|
|
109
122
|
};
|
|
110
123
|
|
|
111
124
|
return model;
|
package/tsconfig.json
CHANGED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { type FC, useEffect, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
import useIframe from '../../hooks/useIframe';
|
|
4
|
-
|
|
5
|
-
type Props = {
|
|
6
|
-
iframeSrc: string | null;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
const MainIframe: FC<Props> = ({ iframeSrc }) => {
|
|
10
|
-
const [loading, setLoading] = useState(false);
|
|
11
|
-
|
|
12
|
-
const { frameRef, messageReceiver } = useIframe();
|
|
13
|
-
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
if (frameRef && iframeSrc) {
|
|
16
|
-
setLoading(true);
|
|
17
|
-
frameRef?.current &&
|
|
18
|
-
(frameRef?.current as HTMLIFrameElement).addEventListener(
|
|
19
|
-
'load',
|
|
20
|
-
() => {
|
|
21
|
-
setLoading(false);
|
|
22
|
-
}
|
|
23
|
-
);
|
|
24
|
-
messageReceiver();
|
|
25
|
-
}
|
|
26
|
-
}, [frameRef, iframeSrc]);
|
|
27
|
-
|
|
28
|
-
return (
|
|
29
|
-
<div className="sdk-iframe-wrapper" data-testid="sdk-iframe-id">
|
|
30
|
-
{loading && <div>... Loading</div>}
|
|
31
|
-
{iframeSrc && (
|
|
32
|
-
<iframe
|
|
33
|
-
ref={frameRef}
|
|
34
|
-
src={iframeSrc}
|
|
35
|
-
style={{ minHeight: '830px', width: '100%' }}
|
|
36
|
-
></iframe>
|
|
37
|
-
)}
|
|
38
|
-
</div>
|
|
39
|
-
);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export default MainIframe;
|
package/src/hooks/useIframe.tsx
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
let currentMessageReceiver: (event: Event) => void;
|
|
4
|
-
|
|
5
|
-
const useIframe = () => {
|
|
6
|
-
const frameRef = useRef(null);
|
|
7
|
-
|
|
8
|
-
const messageReceiver = (
|
|
9
|
-
callback?: (type: string, value: string) => void
|
|
10
|
-
) => {
|
|
11
|
-
if (currentMessageReceiver) {
|
|
12
|
-
window.removeEventListener('message', currentMessageReceiver);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
currentMessageReceiver = (event: Event) => {
|
|
16
|
-
if (!frameRef.current) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const childWindow = (frameRef.current as HTMLIFrameElement)
|
|
21
|
-
.contentWindow;
|
|
22
|
-
|
|
23
|
-
if ((event as MessageEvent).source !== childWindow) {
|
|
24
|
-
return; // Skip message in this event listener
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
let { value, type } = (event as MessageEvent).data as unknown as {
|
|
28
|
-
value: string;
|
|
29
|
-
type: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
if (callback) {
|
|
33
|
-
callback(type, value);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
window.addEventListener('message', currentMessageReceiver, false);
|
|
38
|
-
|
|
39
|
-
return () => {
|
|
40
|
-
removeMessageReceiver();
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const removeMessageReceiver = () => {
|
|
45
|
-
if (currentMessageReceiver) {
|
|
46
|
-
window.removeEventListener(
|
|
47
|
-
'addEventListener',
|
|
48
|
-
currentMessageReceiver
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
frameRef,
|
|
55
|
-
messageReceiver,
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export default useIframe;
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { useEffect, useCallback } from 'react';
|
|
2
|
-
|
|
3
|
-
import { MessageEventTypeEnum, PaymentMethodEnum } from '../enums/general';
|
|
4
|
-
import type { CompleteOrderParamsType, OrderInfoType } from '../types/order';
|
|
5
|
-
import useQueryParams from './useQueryParams';
|
|
6
|
-
|
|
7
|
-
const MOLLIE_ORDER_TTL_MS = 20 * 60 * 1000;
|
|
8
|
-
|
|
9
|
-
// Reads and consumes the persisted Mollie order id. The key is always removed:
|
|
10
|
-
// the return leg gets one chance to use it, whether or not it is still valid.
|
|
11
|
-
const takeStoredMollieOrderId = (): string | null => {
|
|
12
|
-
const raw = sessionStorage.getItem('mollieOrderId');
|
|
13
|
-
if (!raw) {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
sessionStorage.removeItem('mollieOrderId');
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
const { id, ts } = JSON.parse(raw) as { id: string; ts: number };
|
|
21
|
-
|
|
22
|
-
return Date.now() - ts <= MOLLIE_ORDER_TTL_MS ? id : null;
|
|
23
|
-
} catch {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const useMessageEvent = (
|
|
29
|
-
messageCallback: (data: CompleteOrderParamsType) => Promise<void>,
|
|
30
|
-
handleMessageEvent: (type: MessageEventTypeEnum, orderId?: string) => void,
|
|
31
|
-
showIframe: boolean,
|
|
32
|
-
orderInfo: OrderInfoType | null
|
|
33
|
-
) => {
|
|
34
|
-
const queryParams = useQueryParams();
|
|
35
|
-
|
|
36
|
-
const handleMessage = useCallback(
|
|
37
|
-
async (event: MessageEvent) => {
|
|
38
|
-
// Vipps handler
|
|
39
|
-
if (event.data.type === MessageEventTypeEnum.VIPPS_COMPLETE) {
|
|
40
|
-
await messageCallback({
|
|
41
|
-
orderId: event.data.orderId || '',
|
|
42
|
-
agreementId: event.data.agreementId || '',
|
|
43
|
-
orderInfo: orderInfo || null,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// SwedbankPay handler
|
|
48
|
-
if (event.data.type === MessageEventTypeEnum.COMPLETE) {
|
|
49
|
-
await messageCallback({
|
|
50
|
-
orderId: event.data.orderId || '',
|
|
51
|
-
agreementId: event.data.agreementId || '',
|
|
52
|
-
orderInfo: orderInfo || null,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
if (event.data.type === MessageEventTypeEnum.CANCEL) {
|
|
56
|
-
handleMessageEvent(event.data.type);
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
[orderInfo]
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
useEffect(() => {
|
|
63
|
-
window.addEventListener('message', handleMessage, false);
|
|
64
|
-
|
|
65
|
-
return () => {
|
|
66
|
-
window.removeEventListener('message', handleMessage, false);
|
|
67
|
-
};
|
|
68
|
-
}, [handleMessage]);
|
|
69
|
-
|
|
70
|
-
// Vipps
|
|
71
|
-
useEffect(() => {
|
|
72
|
-
if (
|
|
73
|
-
queryParams.get('orderId') &&
|
|
74
|
-
queryParams.get('agreementId') &&
|
|
75
|
-
top &&
|
|
76
|
-
window !== top
|
|
77
|
-
) {
|
|
78
|
-
top?.postMessage(
|
|
79
|
-
{
|
|
80
|
-
type: MessageEventTypeEnum.VIPPS_COMPLETE,
|
|
81
|
-
orderId: queryParams.get('orderId'),
|
|
82
|
-
agreementId: queryParams.get('agreementId'),
|
|
83
|
-
},
|
|
84
|
-
top?.location?.origin || '*'
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
}, [queryParams.get('orderId'), queryParams.get('agreementId')]);
|
|
88
|
-
|
|
89
|
-
//Vipps
|
|
90
|
-
useEffect(() => {
|
|
91
|
-
if (
|
|
92
|
-
queryParams.get('orderId') &&
|
|
93
|
-
queryParams.get('agreementId')
|
|
94
|
-
// !showIframe
|
|
95
|
-
) {
|
|
96
|
-
messageCallback({
|
|
97
|
-
orderId: queryParams.get('orderId') || '',
|
|
98
|
-
agreementId: queryParams.get('agreementId') || '',
|
|
99
|
-
orderInfo: orderInfo || null,
|
|
100
|
-
paymentMethod: PaymentMethodEnum.Vipps,
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
}, [
|
|
104
|
-
queryParams.get('orderId'),
|
|
105
|
-
queryParams.get('agreementId'),
|
|
106
|
-
// showIframe,
|
|
107
|
-
]);
|
|
108
|
-
|
|
109
|
-
// SwedbankPay
|
|
110
|
-
useEffect(() => {
|
|
111
|
-
if (
|
|
112
|
-
queryParams.get('action') &&
|
|
113
|
-
queryParams.get('S4OrderId') &&
|
|
114
|
-
queryParams.get('TransactionId') &&
|
|
115
|
-
top &&
|
|
116
|
-
window !== top
|
|
117
|
-
) {
|
|
118
|
-
top?.postMessage(
|
|
119
|
-
{
|
|
120
|
-
type: queryParams.get('action'),
|
|
121
|
-
orderId: queryParams.get('S4OrderId'),
|
|
122
|
-
agreementId: queryParams.get('TransactionId'),
|
|
123
|
-
},
|
|
124
|
-
top?.location?.origin || '*'
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
}, [
|
|
128
|
-
queryParams.get('S4OrderId'),
|
|
129
|
-
queryParams.get('TransactionId'),
|
|
130
|
-
queryParams.get('action'),
|
|
131
|
-
]);
|
|
132
|
-
|
|
133
|
-
// SwedbankPay
|
|
134
|
-
useEffect(() => {
|
|
135
|
-
if (
|
|
136
|
-
queryParams.get('S4OrderId') &&
|
|
137
|
-
queryParams.get('TransactionId') &&
|
|
138
|
-
queryParams.get('action') === MessageEventTypeEnum.COMPLETE &&
|
|
139
|
-
!showIframe
|
|
140
|
-
) {
|
|
141
|
-
messageCallback({
|
|
142
|
-
orderId: queryParams.get('S4OrderId') || '',
|
|
143
|
-
agreementId: queryParams.get('TransactionId') || '',
|
|
144
|
-
orderInfo: orderInfo || null,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (
|
|
149
|
-
queryParams.get('action') === MessageEventTypeEnum.CANCEL &&
|
|
150
|
-
(!showIframe || !queryParams.get('TransactionId'))
|
|
151
|
-
) {
|
|
152
|
-
const cancelOrderId =
|
|
153
|
-
queryParams.get('S4OrderId') || takeStoredMollieOrderId();
|
|
154
|
-
handleMessageEvent(
|
|
155
|
-
queryParams.get('action') as MessageEventTypeEnum,
|
|
156
|
-
cancelOrderId || undefined
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
}, [
|
|
160
|
-
queryParams.get('action'),
|
|
161
|
-
queryParams.get('S4OrderId'),
|
|
162
|
-
queryParams.get('TransactionId'),
|
|
163
|
-
showIframe,
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
// Mollie: a completion return with no TransactionId (that one is SwedbankPay's).
|
|
167
|
-
useEffect(() => {
|
|
168
|
-
if (
|
|
169
|
-
queryParams.get('action') !== MessageEventTypeEnum.COMPLETE ||
|
|
170
|
-
queryParams.get('TransactionId')
|
|
171
|
-
) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const s4OrderId = queryParams.get('S4OrderId');
|
|
176
|
-
const storedOrderId = takeStoredMollieOrderId();
|
|
177
|
-
const mollieOrderId = s4OrderId || storedOrderId;
|
|
178
|
-
if (!mollieOrderId) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
messageCallback({
|
|
183
|
-
orderId: mollieOrderId,
|
|
184
|
-
agreementId: '',
|
|
185
|
-
orderInfo: orderInfo || null,
|
|
186
|
-
});
|
|
187
|
-
}, [
|
|
188
|
-
queryParams.get('action'),
|
|
189
|
-
queryParams.get('S4OrderId'),
|
|
190
|
-
queryParams.get('TransactionId'),
|
|
191
|
-
]);
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
export default useMessageEvent;
|