@infosoftas/s4-orders-js-sdk 0.1.21 → 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 +0 -0
- package/.release-it.json +0 -0
- package/README.md +572 -471
- package/build/postcss-scope-tailwind.cjs +0 -0
- package/components.json +0 -0
- package/package.json +62 -58
- package/src/App.completion.test.tsx +677 -0
- package/src/App.css +0 -14
- package/src/App.paymentReturn.test.tsx +341 -0
- package/src/App.tsx +722 -639
- package/src/ErrorBoundary.tsx +0 -0
- package/src/api/InvoiceApi.ts +0 -0
- package/src/api/OrdersApi.ts +74 -59
- package/src/api/SubscribeApi.ts +0 -0
- package/src/bundle.ts +0 -0
- package/src/components/Alert/Alert.tsx +0 -0
- package/src/components/Button/Button.tsx +0 -0
- package/src/components/EHFForm/EHFForm.tsx +250 -253
- package/src/components/FormFields/AddressField.tsx +0 -0
- package/src/components/FormFields/EmailField.tsx +0 -0
- package/src/components/FormFields/FormFields.helper.ts +0 -0
- package/src/components/FormFields/FormFieldsMapper.ts +0 -0
- package/src/components/FormFields/InputField.tsx +0 -0
- package/src/components/FormFields/NameField.tsx +0 -0
- package/src/components/FormFields/PhoneField.tsx +0 -0
- package/src/components/FormFields/TermsCheckbox.tsx +0 -0
- package/src/components/FormFields/ToggleFiled.tsx +0 -0
- package/src/components/FormFields/fieldValidators.ts +0 -0
- package/src/components/OIOForm/OIOForm.tsx +304 -307
- package/src/components/OrderDenialModal/OrderDenialModal.tsx +0 -0
- package/src/components/OrderForm/ContactDetailsSection.tsx +0 -0
- package/src/components/OrderForm/OrderForm.tsx +417 -420
- package/src/components/OrderForm/PaymentMethodContent.tsx +0 -0
- package/src/components/OrderForm/PaymentMethodSection.tsx +0 -0
- package/src/components/OrderForm/SubmitConfirmationDialog.tsx +0 -0
- package/src/components/OrderForm/orderFormState.ts +0 -0
- package/src/components/OrderForm/useSlotElement.tsx +0 -0
- 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 +0 -0
- package/src/custom.d.ts +0 -0
- package/src/enums/api.ts +0 -0
- package/src/enums/general.ts +42 -40
- package/src/hooks/useOrderForm.tsx +400 -403
- package/src/hooks/usePaymentReturn.tsx +139 -0
- package/src/hooks/useQueryParams.ts +0 -0
- package/src/index.tsx +0 -0
- package/src/lib/utils.ts +0 -0
- package/src/types/api.ts +0 -0
- package/src/types/general.ts +153 -136
- package/src/types/order.ts +91 -70
- package/src/utils/fetcher.ts +131 -113
- package/src/utils/helper.ts +0 -0
- package/src/utils/order.helper.test.ts +81 -0
- package/src/utils/order.helper.ts +285 -272
- package/src/utils/waitForOrderCompletion.ts +125 -0
- package/tsconfig.json +39 -38
- package/src/components/MainIframe/MainIframe.tsx +0 -42
- package/src/hooks/useIframe.tsx +0 -59
- package/src/hooks/useMessageEvent.tsx +0 -194
|
@@ -1,403 +1,400 @@
|
|
|
1
|
-
import { useRef, useState } from 'react';
|
|
2
|
-
import type { SubmitHandler } from 'react-hook-form';
|
|
3
|
-
|
|
4
|
-
import { createSubscriber } from '../api/SubscribeApi';
|
|
5
|
-
import { orderStart } from '../api/OrdersApi';
|
|
6
|
-
import { invoiceLookup } from '../api/InvoiceApi';
|
|
7
|
-
import {
|
|
8
|
-
InvoiceLookupNetworkEnum,
|
|
9
|
-
PaymentMethodEnum,
|
|
10
|
-
UserActionEnum,
|
|
11
|
-
} from '../enums/general';
|
|
12
|
-
import { WRONG_MSG, PAYMENT_METHOD_DEFAULT } from '../constants/index';
|
|
13
|
-
import type { OrderFormInputsType, OrderInfoType } from '../types/order';
|
|
14
|
-
import type {
|
|
15
|
-
ErrorsMsg,
|
|
16
|
-
OrderFormFieldType,
|
|
17
|
-
OrderDenialFallbackOfferType,
|
|
18
|
-
PaymentMethodSettingsType,
|
|
19
|
-
ContactRequestType,
|
|
20
|
-
} from '../types/general';
|
|
21
|
-
import {
|
|
22
|
-
prepareAgreementModel,
|
|
23
|
-
prepareContactModel,
|
|
24
|
-
prepareInvoiceModel,
|
|
25
|
-
} from '../utils/order.helper';
|
|
26
|
-
import {
|
|
27
|
-
prepareErrorMessage,
|
|
28
|
-
prepareErrorsArrayMessage,
|
|
29
|
-
type ErrorMessages,
|
|
30
|
-
} from '../utils/helper';
|
|
31
|
-
|
|
32
|
-
type Props = {
|
|
33
|
-
callback: (url: string | null, orderInfo?: OrderInfoType | null) => void;
|
|
34
|
-
submitStartCallback?: (id: string) => void;
|
|
35
|
-
userActionCallback?: (
|
|
36
|
-
action: UserActionEnum,
|
|
37
|
-
args: object | null | undefined
|
|
38
|
-
) => void;
|
|
39
|
-
setContactCallback?: (contactInfo: ContactRequestType) => void;
|
|
40
|
-
templatePackageId: string;
|
|
41
|
-
subscriberId?: string;
|
|
42
|
-
userId?: string;
|
|
43
|
-
identityProviderId?: string;
|
|
44
|
-
organizationId: string;
|
|
45
|
-
redirectUrl?: string;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const [
|
|
135
|
-
const [
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
id
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
})
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
);
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
};
|
|
402
|
-
|
|
403
|
-
export default useOrderForm;
|
|
1
|
+
import { useRef, useState } from 'react';
|
|
2
|
+
import type { SubmitHandler } from 'react-hook-form';
|
|
3
|
+
|
|
4
|
+
import { createSubscriber } from '../api/SubscribeApi';
|
|
5
|
+
import { orderStart } from '../api/OrdersApi';
|
|
6
|
+
import { invoiceLookup } from '../api/InvoiceApi';
|
|
7
|
+
import {
|
|
8
|
+
InvoiceLookupNetworkEnum,
|
|
9
|
+
PaymentMethodEnum,
|
|
10
|
+
UserActionEnum,
|
|
11
|
+
} from '../enums/general';
|
|
12
|
+
import { WRONG_MSG, PAYMENT_METHOD_DEFAULT } from '../constants/index';
|
|
13
|
+
import type { OrderFormInputsType, OrderInfoType } from '../types/order';
|
|
14
|
+
import type {
|
|
15
|
+
ErrorsMsg,
|
|
16
|
+
OrderFormFieldType,
|
|
17
|
+
OrderDenialFallbackOfferType,
|
|
18
|
+
PaymentMethodSettingsType,
|
|
19
|
+
ContactRequestType,
|
|
20
|
+
} from '../types/general';
|
|
21
|
+
import {
|
|
22
|
+
prepareAgreementModel,
|
|
23
|
+
prepareContactModel,
|
|
24
|
+
prepareInvoiceModel,
|
|
25
|
+
} from '../utils/order.helper';
|
|
26
|
+
import {
|
|
27
|
+
prepareErrorMessage,
|
|
28
|
+
prepareErrorsArrayMessage,
|
|
29
|
+
type ErrorMessages,
|
|
30
|
+
} from '../utils/helper';
|
|
31
|
+
|
|
32
|
+
type Props = {
|
|
33
|
+
callback: (url: string | null, orderInfo?: OrderInfoType | null) => void;
|
|
34
|
+
submitStartCallback?: (id: string) => void;
|
|
35
|
+
userActionCallback?: (
|
|
36
|
+
action: UserActionEnum,
|
|
37
|
+
args: object | null | undefined
|
|
38
|
+
) => void;
|
|
39
|
+
setContactCallback?: (contactInfo: ContactRequestType) => void;
|
|
40
|
+
templatePackageId: string;
|
|
41
|
+
subscriberId?: string;
|
|
42
|
+
userId?: string;
|
|
43
|
+
identityProviderId?: string;
|
|
44
|
+
organizationId: string;
|
|
45
|
+
redirectUrl?: string;
|
|
46
|
+
language: string;
|
|
47
|
+
merchantAgreementUrl: string;
|
|
48
|
+
paymentMethodsOptions?: PaymentMethodSettingsType;
|
|
49
|
+
orderFields: OrderFormFieldType[];
|
|
50
|
+
invoiceAddressToggle?: boolean;
|
|
51
|
+
invoiceOrderFields: OrderFormFieldType[];
|
|
52
|
+
invoiceLookupNotFoundText?: string;
|
|
53
|
+
errorValidationTitleMsg?: string;
|
|
54
|
+
errorValidationDenialOrderBlockingMsg?: string;
|
|
55
|
+
errorValidationBlockingOffersMsg?: string;
|
|
56
|
+
orderDenialOfferBaseText?: string;
|
|
57
|
+
orderDenialOfferWithFallbackText?: string;
|
|
58
|
+
orderDenialAmountText?: string;
|
|
59
|
+
fetchDenialFallbackOffer?: (
|
|
60
|
+
organizationId: string
|
|
61
|
+
) => Promise<OrderDenialFallbackOfferType | undefined>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type DenialErrorType = {
|
|
65
|
+
status?: number;
|
|
66
|
+
errors?: Record<string, string[] | undefined>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const OFFER_DENIAL_BASE_TEXT =
|
|
70
|
+
'Hello again!\n\nLooks like you already tried this offer.';
|
|
71
|
+
const OFFER_DENIAL_WITH_FALLBACK_TEXT =
|
|
72
|
+
'Hello again!\n\nLooks like you already tried this offer, but we would like to keep you with us.\n\nTherefore we got a new great offer for you:';
|
|
73
|
+
|
|
74
|
+
const getDenialErrorType = (
|
|
75
|
+
error: DenialErrorType
|
|
76
|
+
): 'offer' | 'amount' | null => {
|
|
77
|
+
if (error?.status !== 400 || !error?.errors) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const normalizedEntries = Object.entries(error.errors).map(
|
|
82
|
+
([key, value]) => [key.toLowerCase(), value] as const
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const hasOffer = normalizedEntries.some(
|
|
86
|
+
([key, value]) =>
|
|
87
|
+
key === 'offer' && Array.isArray(value) && value.length > 0
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
if (hasOffer) {
|
|
91
|
+
return 'offer';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const hasAmount = normalizedEntries.some(
|
|
95
|
+
([key, value]) =>
|
|
96
|
+
key === 'amount' && Array.isArray(value) && value.length > 0
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (hasAmount) {
|
|
100
|
+
return 'amount';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const useOrderForm = ({
|
|
107
|
+
callback,
|
|
108
|
+
submitStartCallback,
|
|
109
|
+
userActionCallback,
|
|
110
|
+
setContactCallback,
|
|
111
|
+
organizationId,
|
|
112
|
+
subscriberId,
|
|
113
|
+
userId,
|
|
114
|
+
identityProviderId,
|
|
115
|
+
orderFields,
|
|
116
|
+
redirectUrl,
|
|
117
|
+
language,
|
|
118
|
+
merchantAgreementUrl,
|
|
119
|
+
paymentMethodsOptions,
|
|
120
|
+
templatePackageId,
|
|
121
|
+
invoiceAddressToggle,
|
|
122
|
+
invoiceOrderFields,
|
|
123
|
+
invoiceLookupNotFoundText,
|
|
124
|
+
errorValidationTitleMsg,
|
|
125
|
+
errorValidationDenialOrderBlockingMsg,
|
|
126
|
+
errorValidationBlockingOffersMsg,
|
|
127
|
+
orderDenialOfferBaseText,
|
|
128
|
+
orderDenialOfferWithFallbackText,
|
|
129
|
+
orderDenialAmountText,
|
|
130
|
+
fetchDenialFallbackOffer,
|
|
131
|
+
}: Props) => {
|
|
132
|
+
const [loading, setLoading] = useState<boolean>(false);
|
|
133
|
+
const [apiErrorMsg, setApiErrorMsg] = useState<string>('');
|
|
134
|
+
const [errorsMsg, setErrorsMsg] = useState<string[]>([]);
|
|
135
|
+
const [orderDenialType, setOrderDenialType] = useState<
|
|
136
|
+
'offer' | 'amount' | null
|
|
137
|
+
>(null);
|
|
138
|
+
const [orderDenialFallbackOffer, setOrderDenialFallbackOffer] = useState<
|
|
139
|
+
OrderDenialFallbackOfferType | undefined
|
|
140
|
+
>();
|
|
141
|
+
const [submittedOrderData, setSubmittedOrderData] =
|
|
142
|
+
useState<OrderFormInputsType | null>(null);
|
|
143
|
+
const fallbackOfferTemplatePackageIdRef = useRef<string | null>(null);
|
|
144
|
+
|
|
145
|
+
const dismissOrderDenial = () => {
|
|
146
|
+
setOrderDenialType(null);
|
|
147
|
+
setOrderDenialFallbackOffer(undefined);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const getOrderDenialMessage = () => {
|
|
151
|
+
if (orderDenialType === 'offer') {
|
|
152
|
+
if (orderDenialFallbackOffer?.templatePackageId) {
|
|
153
|
+
return (
|
|
154
|
+
orderDenialOfferWithFallbackText ||
|
|
155
|
+
OFFER_DENIAL_WITH_FALLBACK_TEXT
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return orderDenialOfferBaseText || OFFER_DENIAL_BASE_TEXT;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (orderDenialType === 'amount') {
|
|
163
|
+
return (
|
|
164
|
+
orderDenialAmountText ||
|
|
165
|
+
errorValidationDenialOrderBlockingMsg ||
|
|
166
|
+
WRONG_MSG
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return '';
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const orderSubmit: SubmitHandler<OrderFormInputsType> = async (
|
|
174
|
+
data
|
|
175
|
+
): Promise<void> => {
|
|
176
|
+
setApiErrorMsg('');
|
|
177
|
+
setErrorsMsg([]);
|
|
178
|
+
setOrderDenialType(null);
|
|
179
|
+
setLoading(true);
|
|
180
|
+
setSubmittedOrderData(data);
|
|
181
|
+
try {
|
|
182
|
+
let id: string | undefined = subscriberId;
|
|
183
|
+
let invoiceResponse = null;
|
|
184
|
+
if (data.paymentMethod === PaymentMethodEnum.OIO) {
|
|
185
|
+
invoiceResponse = await invoiceLookup({
|
|
186
|
+
network: data.gln
|
|
187
|
+
? InvoiceLookupNetworkEnum.OIO_GLN
|
|
188
|
+
: InvoiceLookupNetworkEnum.OIO_Danish_CVR,
|
|
189
|
+
value: data.gln || data.cvr || '',
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (data.paymentMethod === PaymentMethodEnum.EHF) {
|
|
193
|
+
invoiceResponse = await invoiceLookup({
|
|
194
|
+
network: InvoiceLookupNetworkEnum.EHF,
|
|
195
|
+
value: data.organizationNumber || '',
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// TODO: "recievesInvoice" should be deleted in the future
|
|
200
|
+
const receivesInvoice =
|
|
201
|
+
invoiceResponse?.receivesInvoice ||
|
|
202
|
+
invoiceResponse?.recievesInvoice;
|
|
203
|
+
|
|
204
|
+
if (invoiceResponse) {
|
|
205
|
+
userActionCallback?.(
|
|
206
|
+
UserActionEnum.SEARCH_ORGANIZATION_NUMBER,
|
|
207
|
+
{
|
|
208
|
+
organizationNumber: data.organizationNumber,
|
|
209
|
+
cvr: data.cvr,
|
|
210
|
+
gln: data.gln,
|
|
211
|
+
found: !!receivesInvoice,
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (invoiceResponse && !receivesInvoice) {
|
|
217
|
+
setLoading(false);
|
|
218
|
+
const value =
|
|
219
|
+
data.paymentMethod === PaymentMethodEnum.OIO
|
|
220
|
+
? data.gln || data.cvr || ''
|
|
221
|
+
: data.organizationNumber || '';
|
|
222
|
+
setApiErrorMsg(`${invoiceLookupNotFoundText || ''} (${value})`);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const contactModel = prepareContactModel({ data, orderFields });
|
|
227
|
+
if (!subscriberId) {
|
|
228
|
+
const response = await createSubscriber({
|
|
229
|
+
...contactModel,
|
|
230
|
+
gln: data?.gln,
|
|
231
|
+
organizationNumber:
|
|
232
|
+
data.paymentMethod === PaymentMethodEnum.OIO
|
|
233
|
+
? data?.cvr
|
|
234
|
+
: data.organizationNumber || undefined,
|
|
235
|
+
...(userId &&
|
|
236
|
+
identityProviderId && { userId, identityProviderId }),
|
|
237
|
+
});
|
|
238
|
+
id = response.id;
|
|
239
|
+
|
|
240
|
+
sessionStorage.setItem('subscriberId', id); // For avoiding attempt of creating subscriber again
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (setContactCallback) {
|
|
244
|
+
setContactCallback(contactModel);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
submitStartCallback?.(id as string);
|
|
248
|
+
|
|
249
|
+
if (id) {
|
|
250
|
+
const paymentMethod =
|
|
251
|
+
data.paymentMethod || PAYMENT_METHOD_DEFAULT;
|
|
252
|
+
const selectedTemplateSubscriptionPlanId =
|
|
253
|
+
fallbackOfferTemplatePackageIdRef.current ||
|
|
254
|
+
templatePackageId;
|
|
255
|
+
fallbackOfferTemplatePackageIdRef.current = null;
|
|
256
|
+
|
|
257
|
+
const agreements = prepareAgreementModel({
|
|
258
|
+
paymentMethod,
|
|
259
|
+
redirectUrl,
|
|
260
|
+
phoneNumber: orderFields.find(
|
|
261
|
+
(i) => i.name === 'phoneNumber'
|
|
262
|
+
)
|
|
263
|
+
? data.phoneNumber?.trim() || undefined
|
|
264
|
+
: undefined,
|
|
265
|
+
language,
|
|
266
|
+
merchantAgreementUrl,
|
|
267
|
+
generateSubscriberContact:
|
|
268
|
+
paymentMethodsOptions?.[
|
|
269
|
+
data.paymentMethod as PaymentMethodEnum
|
|
270
|
+
]?.generateSubscriberContact || false,
|
|
271
|
+
accountId:
|
|
272
|
+
paymentMethodsOptions?.[
|
|
273
|
+
data.paymentMethod as PaymentMethodEnum
|
|
274
|
+
]?.accountId,
|
|
275
|
+
});
|
|
276
|
+
const responseOrder = await orderStart({
|
|
277
|
+
subscriberId: id,
|
|
278
|
+
subscriptionPlan: {
|
|
279
|
+
organizationId,
|
|
280
|
+
templateSubscriptionPlanId:
|
|
281
|
+
selectedTemplateSubscriptionPlanId,
|
|
282
|
+
},
|
|
283
|
+
paymentAgreement: { ...agreements },
|
|
284
|
+
invoiceContact: invoiceAddressToggle
|
|
285
|
+
? prepareInvoiceModel({
|
|
286
|
+
data,
|
|
287
|
+
orderFields: invoiceOrderFields,
|
|
288
|
+
})
|
|
289
|
+
: undefined,
|
|
290
|
+
orderReference: data.orderReference || undefined,
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
if (!responseOrder.id) {
|
|
294
|
+
callback(null);
|
|
295
|
+
setApiErrorMsg(WRONG_MSG);
|
|
296
|
+
setErrorsMsg([]);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
callback(responseOrder.terminalRedirectUrl, {
|
|
301
|
+
...(data || {}),
|
|
302
|
+
orderId: responseOrder.id,
|
|
303
|
+
subscriberId: id,
|
|
304
|
+
paymentMethod: paymentMethod,
|
|
305
|
+
});
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
callback(null);
|
|
309
|
+
setApiErrorMsg(WRONG_MSG);
|
|
310
|
+
setErrorsMsg([]);
|
|
311
|
+
} catch (error) {
|
|
312
|
+
const translations = {
|
|
313
|
+
errorValidationTitleMsg,
|
|
314
|
+
errorValidationBlockingOffersMsg,
|
|
315
|
+
errorValidationDenialOrderBlockingMsg,
|
|
316
|
+
} as ErrorMessages;
|
|
317
|
+
|
|
318
|
+
const detectedDenialType = getDenialErrorType(
|
|
319
|
+
error as DenialErrorType
|
|
320
|
+
);
|
|
321
|
+
if (detectedDenialType) {
|
|
322
|
+
setApiErrorMsg('');
|
|
323
|
+
setErrorsMsg([]);
|
|
324
|
+
setOrderDenialFallbackOffer(undefined);
|
|
325
|
+
setOrderDenialType(detectedDenialType);
|
|
326
|
+
callback(null);
|
|
327
|
+
if (
|
|
328
|
+
detectedDenialType === 'offer' &&
|
|
329
|
+
fetchDenialFallbackOffer
|
|
330
|
+
) {
|
|
331
|
+
void fetchDenialFallbackOffer(organizationId)
|
|
332
|
+
.then((offer) => {
|
|
333
|
+
setOrderDenialFallbackOffer(offer);
|
|
334
|
+
if (offer?.templatePackageId) {
|
|
335
|
+
fallbackOfferTemplatePackageIdRef.current =
|
|
336
|
+
offer.templatePackageId;
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
.catch(() => {
|
|
340
|
+
setOrderDenialFallbackOffer(undefined);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
console.error(error);
|
|
347
|
+
setApiErrorMsg(
|
|
348
|
+
prepareErrorMessage(error as Error, undefined, translations)
|
|
349
|
+
);
|
|
350
|
+
setErrorsMsg(
|
|
351
|
+
prepareErrorsArrayMessage(
|
|
352
|
+
(
|
|
353
|
+
error as {
|
|
354
|
+
errors: ErrorsMsg;
|
|
355
|
+
}
|
|
356
|
+
)?.errors,
|
|
357
|
+
translations
|
|
358
|
+
)
|
|
359
|
+
);
|
|
360
|
+
callback(null);
|
|
361
|
+
} finally {
|
|
362
|
+
setLoading(false);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const continueWithFallbackOffer = async () => {
|
|
367
|
+
if (orderDenialType !== 'offer' || !submittedOrderData) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const fallbackTemplatePackageId =
|
|
372
|
+
orderDenialFallbackOffer?.templatePackageId;
|
|
373
|
+
|
|
374
|
+
if (!fallbackTemplatePackageId) {
|
|
375
|
+
dismissOrderDenial();
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
fallbackOfferTemplatePackageIdRef.current = fallbackTemplatePackageId;
|
|
380
|
+
userActionCallback?.(UserActionEnum.SELECT_FALLBACK_OFFER, {
|
|
381
|
+
templatePackageId: fallbackTemplatePackageId,
|
|
382
|
+
});
|
|
383
|
+
dismissOrderDenial();
|
|
384
|
+
await orderSubmit(submittedOrderData);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
orderSubmit,
|
|
389
|
+
continueWithFallbackOffer,
|
|
390
|
+
loading,
|
|
391
|
+
apiErrorMsg,
|
|
392
|
+
errorsMsg,
|
|
393
|
+
orderDenialType,
|
|
394
|
+
orderDenialMessage: getOrderDenialMessage(),
|
|
395
|
+
orderDenialFallbackOffer,
|
|
396
|
+
dismissOrderDenial,
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
export default useOrderForm;
|