PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.14.3
GiveWP – Donation Plugin and Fundraising Platform v4.14.3
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / DonationForms / resources / app / utilities / handleValidationRequest.ts
handleValidationRequest.ts
50 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {Gateway, isFormResponseGatewayError, isFormResponseValidationError} from '@givewp/forms/types';
2 import generateRequestErrors from '../utilities/generateRequestErrors';
3 import FormRequestError from '../errors/FormRequestError';
4
5 import {__} from '@wordpress/i18n';
6 import {FieldValues, UseFormSetError} from 'react-hook-form';
7 import postFormData from '@givewp/forms/app/utilities/postFormData';
8 import convertValuesToFormData from '@givewp/forms/app/utilities/convertValuesToFormData';
9
10 /**
11 * @since 3.0.0
12 */
13 export default async function handleValidationRequest(
14 validateUrl: string,
15 values: FieldValues,
16 setError: UseFormSetError<FieldValues>,
17 gateway?: Gateway
18 ) {
19 if (gateway !== undefined && values?.donationType === 'subscription' && !gateway.supportsSubscriptions) {
20 return setError('FORM_ERROR', {
21 message: __(
22 'This payment gateway does not support recurring payments, please try selecting another payment gateway.',
23 'give'
24 ),
25 });
26 }
27
28 try {
29 const formData = convertValuesToFormData(values);
30
31 const {response} = await postFormData(validateUrl, formData);
32
33 const formResponse = await response.json();
34
35 if (isFormResponseGatewayError(formResponse) || isFormResponseValidationError(formResponse)) {
36 throw new FormRequestError(formResponse.data.errors.errors);
37 } else {
38 return true;
39 }
40 } catch (error) {
41 if (error instanceof FormRequestError) {
42 return generateRequestErrors(values, error.errors, setError);
43 }
44
45 return setError('FORM_ERROR', {
46 message: error?.message ?? __('Something went wrong, please try again or contact support.', 'give'),
47 });
48 }
49 }
50