Skip to content

Commit

Permalink
feature: add support to server validations for PayPal Donations gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva committed Dec 6, 2024
1 parent 4fb99af commit 6dae348
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Give\PaymentGateways\Gateways\PayPalCommerce;

use Exception;
use Give\DonationForms\Actions\GenerateDonationFormValidationRouteUrl;
use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile;
use Give\Helpers\Language;
use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail;
Expand Down Expand Up @@ -51,7 +53,9 @@ public function enqueueScript(int $formId)
}

/**
* @throws \Exception
* @unreleased Add validate URL
*
* @throws Exception
*/
public function formSettings(int $formId): array
{
Expand All @@ -60,14 +64,15 @@ public function formSettings(int $formId): array
'donationFormId' => $formId,
'donationFormNonce' => wp_create_nonce("give_donation_form_nonce_{$formId}"),
'sdkOptions' => $this->getPayPalSDKOptions($formId),
'validateUrl' => (new GenerateDonationFormValidationRouteUrl())(),
];
}

/**
* List of PayPal query parameters: https://developer.paypal.com/docs/checkout/reference/customize-sdk/#query-parameters
*
* @since 3.0.0
* @throws \Exception
* @throws Exception
*/
private function getPayPalSDKOptions(int $formId): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {debounce} from 'react-ace/lib/editorOptions';
import {Flex, TextControl} from '@wordpress/components';
import {CSSProperties, useEffect, useState} from 'react';
import {PayPalSubscriber} from './types';
import handleValidationRequest from '@givewp/forms/app/utilities/handleValidationRequest';

(() => {
/**
Expand Down Expand Up @@ -384,9 +385,9 @@ import {PayPalSubscriber} from './types';
return actions.reject();
}

// Validate the form values before proceeding.
const result = await trigger();
if (result === false) {
// Validate the form values in the client side before proceeding.
const isClientValidationSuccessful = await trigger();
if (!isClientValidationSuccessful) {
// Set focus on first invalid field.
for (const fieldName in getValues()) {
if (getFieldState(fieldName).invalid) {
Expand All @@ -396,6 +397,18 @@ import {PayPalSubscriber} from './types';
return actions.reject();
}

// Validate the form values in the server side before proceeding.
const isServerValidationSuccessful = await handleValidationRequest(
payPalDonationsSettings.validateUrl,
getValues(),
setError,
gateway
);

if (!isServerValidationSuccessful) {
return actions.reject();
}

/**
* Depending on the selected country, the city, state, and zip fields can be required or not and there are custom
* validation rules on the server side that check that. However, in this case, these validations are reached later
Expand Down

0 comments on commit 6dae348

Please sign in to comment.