mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Merge pull request #942 from woocommerce/pcp-817-validation
Execute server-side WC validation when clicking button
This commit is contained in:
commit
77e18ea2b3
12 changed files with 200 additions and 44 deletions
|
@ -100,7 +100,8 @@ const bootstrap = () => {
|
||||||
if (PayPalCommerceGateway.mini_cart_buttons_enabled === '1') {
|
if (PayPalCommerceGateway.mini_cart_buttons_enabled === '1') {
|
||||||
const miniCartBootstrap = new MiniCartBootstap(
|
const miniCartBootstrap = new MiniCartBootstap(
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
renderer
|
renderer,
|
||||||
|
errorHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
miniCartBootstrap.init();
|
miniCartBootstrap.init();
|
||||||
|
@ -112,6 +113,7 @@ const bootstrap = () => {
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
renderer,
|
renderer,
|
||||||
messageRenderer,
|
messageRenderer,
|
||||||
|
errorHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
singleProductBootstrap.init();
|
singleProductBootstrap.init();
|
||||||
|
@ -121,6 +123,7 @@ const bootstrap = () => {
|
||||||
const cartBootstrap = new CartBootstrap(
|
const cartBootstrap = new CartBootstrap(
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
renderer,
|
renderer,
|
||||||
|
errorHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
cartBootstrap.init();
|
cartBootstrap.init();
|
||||||
|
@ -131,7 +134,8 @@ const bootstrap = () => {
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
renderer,
|
renderer,
|
||||||
messageRenderer,
|
messageRenderer,
|
||||||
spinner
|
spinner,
|
||||||
|
errorHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
checkoutBootstap.init();
|
checkoutBootstap.init();
|
||||||
|
@ -142,7 +146,8 @@ const bootstrap = () => {
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
renderer,
|
renderer,
|
||||||
messageRenderer,
|
messageRenderer,
|
||||||
spinner
|
spinner,
|
||||||
|
errorHandler,
|
||||||
);
|
);
|
||||||
payNowBootstrap.init();
|
payNowBootstrap.init();
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,14 +59,16 @@ class CheckoutActionHandler {
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
errorHandler.clear();
|
errorHandler.clear();
|
||||||
if (data.data.details.length > 0) {
|
if (data.data.errors.length > 0) {
|
||||||
|
errorHandler.messages(data.data.errors);
|
||||||
|
} else if (data.data.details.length > 0) {
|
||||||
errorHandler.message(data.data.details.map(d => `${d.issue} ${d.description}`).join('<br/>'), true);
|
errorHandler.message(data.data.details.map(d => `${d.issue} ${d.description}`).join('<br/>'), true);
|
||||||
} else {
|
} else {
|
||||||
errorHandler.message(data.data.message, true);
|
errorHandler.message(data.data.message, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(data.data.message);
|
throw {type: 'create-order-error', data: data.data};
|
||||||
}
|
}
|
||||||
const input = document.createElement('input');
|
const input = document.createElement('input');
|
||||||
input.setAttribute('type', 'hidden');
|
input.setAttribute('type', 'hidden');
|
||||||
|
@ -82,9 +84,15 @@ class CheckoutActionHandler {
|
||||||
onCancel: () => {
|
onCancel: () => {
|
||||||
spinner.unblock();
|
spinner.unblock();
|
||||||
},
|
},
|
||||||
onError: () => {
|
onError: (err) => {
|
||||||
this.errorHandler.genericError();
|
console.error(err);
|
||||||
spinner.unblock();
|
spinner.unblock();
|
||||||
|
|
||||||
|
if (err && err.type === 'create-order-error') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.errorHandler.genericError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
||||||
import ErrorHandler from '../ErrorHandler';
|
|
||||||
|
|
||||||
class CartBootstrap {
|
class CartBootstrap {
|
||||||
constructor(gateway, renderer) {
|
constructor(gateway, renderer, errorHandler) {
|
||||||
this.gateway = gateway;
|
this.gateway = gateway;
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
|
this.errorHandler = errorHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
@ -28,7 +28,7 @@ class CartBootstrap {
|
||||||
render() {
|
render() {
|
||||||
const actionHandler = new CartActionHandler(
|
const actionHandler = new CartActionHandler(
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
new ErrorHandler(this.gateway.labels.error.generic),
|
this.errorHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.renderer.render(
|
this.renderer.render(
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import ErrorHandler from '../ErrorHandler';
|
|
||||||
import CheckoutActionHandler from '../ActionHandler/CheckoutActionHandler';
|
import CheckoutActionHandler from '../ActionHandler/CheckoutActionHandler';
|
||||||
import {setVisible, setVisibleByClass} from '../Helper/Hiding';
|
import {setVisible, setVisibleByClass} from '../Helper/Hiding';
|
||||||
import {
|
import {
|
||||||
|
@ -8,11 +7,12 @@ import {
|
||||||
} from "../Helper/CheckoutMethodState";
|
} from "../Helper/CheckoutMethodState";
|
||||||
|
|
||||||
class CheckoutBootstap {
|
class CheckoutBootstap {
|
||||||
constructor(gateway, renderer, messages, spinner) {
|
constructor(gateway, renderer, messages, spinner, errorHandler) {
|
||||||
this.gateway = gateway;
|
this.gateway = gateway;
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
this.spinner = spinner;
|
this.spinner = spinner;
|
||||||
|
this.errorHandler = errorHandler;
|
||||||
|
|
||||||
this.standardOrderButtonSelector = ORDER_BUTTON_SELECTOR;
|
this.standardOrderButtonSelector = ORDER_BUTTON_SELECTOR;
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ class CheckoutBootstap {
|
||||||
}
|
}
|
||||||
const actionHandler = new CheckoutActionHandler(
|
const actionHandler = new CheckoutActionHandler(
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
new ErrorHandler(this.gateway.labels.error.generic),
|
this.errorHandler,
|
||||||
this.spinner
|
this.spinner
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import ErrorHandler from '../ErrorHandler';
|
|
||||||
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
import CartActionHandler from '../ActionHandler/CartActionHandler';
|
||||||
|
|
||||||
class MiniCartBootstap {
|
class MiniCartBootstap {
|
||||||
constructor(gateway, renderer) {
|
constructor(gateway, renderer, errorHandler) {
|
||||||
this.gateway = gateway;
|
this.gateway = gateway;
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
|
this.errorHandler = errorHandler;
|
||||||
this.actionHandler = null;
|
this.actionHandler = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ class MiniCartBootstap {
|
||||||
|
|
||||||
this.actionHandler = new CartActionHandler(
|
this.actionHandler = new CartActionHandler(
|
||||||
PayPalCommerceGateway,
|
PayPalCommerceGateway,
|
||||||
new ErrorHandler(this.gateway.labels.error.generic),
|
this.errorHandler,
|
||||||
);
|
);
|
||||||
this.render();
|
this.render();
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ import CheckoutBootstap from './CheckoutBootstap'
|
||||||
import {isChangePaymentPage} from "../Helper/Subscriptions";
|
import {isChangePaymentPage} from "../Helper/Subscriptions";
|
||||||
|
|
||||||
class PayNowBootstrap extends CheckoutBootstap {
|
class PayNowBootstrap extends CheckoutBootstap {
|
||||||
constructor(gateway, renderer, messages, spinner) {
|
constructor(gateway, renderer, messages, spinner, errorHandler) {
|
||||||
super(gateway, renderer, messages, spinner)
|
super(gateway, renderer, messages, spinner, errorHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUi() {
|
updateUi() {
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import ErrorHandler from '../ErrorHandler';
|
|
||||||
import UpdateCart from "../Helper/UpdateCart";
|
import UpdateCart from "../Helper/UpdateCart";
|
||||||
import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler";
|
import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler";
|
||||||
|
|
||||||
class SingleProductBootstap {
|
class SingleProductBootstap {
|
||||||
constructor(gateway, renderer, messages) {
|
constructor(gateway, renderer, messages, errorHandler) {
|
||||||
this.gateway = gateway;
|
this.gateway = gateway;
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
|
this.errorHandler = errorHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ class SingleProductBootstap {
|
||||||
this.messages.hideMessages();
|
this.messages.hideMessages();
|
||||||
},
|
},
|
||||||
document.querySelector('form.cart'),
|
document.querySelector('form.cart'),
|
||||||
new ErrorHandler(this.gateway.labels.error.generic),
|
this.errorHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.renderer.render(
|
this.renderer.render(
|
||||||
|
|
|
@ -134,6 +134,7 @@ return array(
|
||||||
$early_order_handler,
|
$early_order_handler,
|
||||||
$registration_needed,
|
$registration_needed,
|
||||||
$container->get( 'wcgateway.settings.card_billing_data_mode' ),
|
$container->get( 'wcgateway.settings.card_billing_data_mode' ),
|
||||||
|
$container->get( 'button.early-wc-checkout-validation-enabled' ),
|
||||||
$logger
|
$logger
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -208,6 +209,14 @@ return array(
|
||||||
* The filter allowing to disable the basic client-side validation of the checkout form
|
* The filter allowing to disable the basic client-side validation of the checkout form
|
||||||
* when the PayPal button is clicked.
|
* when the PayPal button is clicked.
|
||||||
*/
|
*/
|
||||||
return (bool) apply_filters( 'woocommerce_paypal_payments_basic_checkout_validation_enabled', true );
|
return (bool) apply_filters( 'woocommerce_paypal_payments_basic_checkout_validation_enabled', false );
|
||||||
|
},
|
||||||
|
'button.early-wc-checkout-validation-enabled' => static function ( ContainerInterface $container ): bool {
|
||||||
|
/**
|
||||||
|
* The filter allowing to disable the WC validation of the checkout form
|
||||||
|
* when the PayPal button is clicked.
|
||||||
|
* The validation is triggered in a non-standard way and may cause issues on some sites.
|
||||||
|
*/
|
||||||
|
return (bool) apply_filters( 'woocommerce_paypal_payments_early_wc_checkout_validation_enabled', true );
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
use Throwable;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Amount;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
||||||
|
@ -25,6 +26,8 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Exception\ValidationException;
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator;
|
||||||
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
||||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||||
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
|
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
|
||||||
|
@ -128,6 +131,13 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
*/
|
*/
|
||||||
protected $card_billing_data_mode;
|
protected $card_billing_data_mode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to execute WC validation of the checkout form.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $early_validation_enabled;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger.
|
* The logger.
|
||||||
*
|
*
|
||||||
|
@ -148,6 +158,7 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
* @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object.
|
* @param EarlyOrderHandler $early_order_handler The EarlyOrderHandler object.
|
||||||
* @param bool $registration_needed Whether a new user must be registered during checkout.
|
* @param bool $registration_needed Whether a new user must be registered during checkout.
|
||||||
* @param string $card_billing_data_mode The value of card_billing_data_mode from the settings.
|
* @param string $card_billing_data_mode The value of card_billing_data_mode from the settings.
|
||||||
|
* @param bool $early_validation_enabled Whether to execute WC validation of the checkout form.
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -161,6 +172,7 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
EarlyOrderHandler $early_order_handler,
|
EarlyOrderHandler $early_order_handler,
|
||||||
bool $registration_needed,
|
bool $registration_needed,
|
||||||
string $card_billing_data_mode,
|
string $card_billing_data_mode,
|
||||||
|
bool $early_validation_enabled,
|
||||||
LoggerInterface $logger
|
LoggerInterface $logger
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
@ -174,6 +186,7 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
$this->early_order_handler = $early_order_handler;
|
$this->early_order_handler = $early_order_handler;
|
||||||
$this->registration_needed = $registration_needed;
|
$this->registration_needed = $registration_needed;
|
||||||
$this->card_billing_data_mode = $card_billing_data_mode;
|
$this->card_billing_data_mode = $card_billing_data_mode;
|
||||||
|
$this->early_validation_enabled = $early_validation_enabled;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,8 +246,14 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
|
|
||||||
$this->set_bn_code( $data );
|
$this->set_bn_code( $data );
|
||||||
|
|
||||||
if ( 'pay-now' === $data['context'] && get_option( 'woocommerce_terms_page_id', '' ) !== '' ) {
|
$form_fields = $data['form'] ?? null;
|
||||||
$this->validate_paynow_form( $data['form'] );
|
|
||||||
|
if ( $this->early_validation_enabled && is_array( $form_fields ) ) {
|
||||||
|
$this->validate_form( $form_fields );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 'pay-now' === $data['context'] && is_array( $form_fields ) && get_option( 'woocommerce_terms_page_id', '' ) !== '' ) {
|
||||||
|
$this->validate_paynow_form( $form_fields );
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -264,6 +283,13 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
wp_send_json_success( $order->to_array() );
|
wp_send_json_success( $order->to_array() );
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
} catch ( ValidationException $error ) {
|
||||||
|
wp_send_json_error(
|
||||||
|
array(
|
||||||
|
'message' => $error->getMessage(),
|
||||||
|
'errors' => $error->errors(),
|
||||||
|
)
|
||||||
|
);
|
||||||
} catch ( \RuntimeException $error ) {
|
} catch ( \RuntimeException $error ) {
|
||||||
$this->logger->error( 'Order creation failed: ' . $error->getMessage() );
|
$this->logger->error( 'Order creation failed: ' . $error->getMessage() );
|
||||||
|
|
||||||
|
@ -481,16 +507,33 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
return $payment_method;
|
return $payment_method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the form fields are valid.
|
||||||
|
*
|
||||||
|
* @param array $form_fields The form fields.
|
||||||
|
* @throws ValidationException When fields are not valid.
|
||||||
|
*/
|
||||||
|
private function validate_form( array $form_fields ): void {
|
||||||
|
try {
|
||||||
|
$v = new CheckoutFormValidator();
|
||||||
|
$v->validate( $form_fields );
|
||||||
|
} catch ( ValidationException $exception ) {
|
||||||
|
throw $exception;
|
||||||
|
} catch ( Throwable $exception ) {
|
||||||
|
$this->logger->error( "Form validation execution failed. {$exception->getMessage()} {$exception->getFile()}:{$exception->getLine()}" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the terms input field is checked.
|
* Checks whether the terms input field is checked.
|
||||||
*
|
*
|
||||||
* @param array $form_fields The form fields.
|
* @param array $form_fields The form fields.
|
||||||
* @throws \RuntimeException When field is not checked.
|
* @throws ValidationException When field is not checked.
|
||||||
*/
|
*/
|
||||||
private function validate_paynow_form( array $form_fields ) {
|
private function validate_paynow_form( array $form_fields ): void {
|
||||||
if ( isset( $form_fields['terms-field'] ) && ! isset( $form_fields['terms'] ) ) {
|
if ( isset( $form_fields['terms-field'] ) && ! isset( $form_fields['terms'] ) ) {
|
||||||
throw new \RuntimeException(
|
throw new ValidationException(
|
||||||
__( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce-paypal-payments' )
|
array( __( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce-paypal-payments' ) )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
47
modules/ppcp-button/src/Exception/ValidationException.php
Normal file
47
modules/ppcp-button/src/Exception/ValidationException.php
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ValidationException.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\Button\Exception
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\Button\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ValidationException
|
||||||
|
*/
|
||||||
|
class ValidationException extends RuntimeException {
|
||||||
|
/**
|
||||||
|
* The error messages.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $errors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ValidationException constructor.
|
||||||
|
*
|
||||||
|
* @param string[] $errors The validation error messages.
|
||||||
|
* @param string $message The error message.
|
||||||
|
*/
|
||||||
|
public function __construct( array $errors, string $message = '' ) {
|
||||||
|
$this->errors = $errors;
|
||||||
|
|
||||||
|
if ( ! $message ) {
|
||||||
|
$message = implode( ' ', $errors );
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct( $message );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The error messages.
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function errors(): array {
|
||||||
|
return $this->errors;
|
||||||
|
}
|
||||||
|
}
|
43
modules/ppcp-button/src/Validation/CheckoutFormValidator.php
Normal file
43
modules/ppcp-button/src/Validation/CheckoutFormValidator.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Executes WC checkout validation.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\Button\Endpoint
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\Button\Validation;
|
||||||
|
|
||||||
|
use WC_Checkout;
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Exception\ValidationException;
|
||||||
|
use WP_Error;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class FormValidator
|
||||||
|
*/
|
||||||
|
class CheckoutFormValidator extends WC_Checkout {
|
||||||
|
/**
|
||||||
|
* Validates the form data.
|
||||||
|
*
|
||||||
|
* @param array $data The form data.
|
||||||
|
* @return void
|
||||||
|
* @throws ValidationException When validation fails.
|
||||||
|
*/
|
||||||
|
public function validate( array $data ) {
|
||||||
|
$errors = new WP_Error();
|
||||||
|
|
||||||
|
if ( isset( $data['terms-field'] ) ) {
|
||||||
|
// WC checks this field via $_POST https://github.com/woocommerce/woocommerce/issues/35328 .
|
||||||
|
$_POST['terms-field'] = $data['terms-field'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// It throws some notices when checking fields etc., also from other plugins via hooks.
|
||||||
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
|
||||||
|
@$this->validate_checkout( $data, $errors );
|
||||||
|
|
||||||
|
if ( $errors->has_errors() ) {
|
||||||
|
throw new ValidationException( $errors->get_error_messages() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -166,6 +166,7 @@ class CreateOrderEndpointTest extends TestCase
|
||||||
$early_order_handler,
|
$early_order_handler,
|
||||||
false,
|
false,
|
||||||
CardBillingMode::MINIMAL_INPUT,
|
CardBillingMode::MINIMAL_INPUT,
|
||||||
|
false,
|
||||||
new NullLogger()
|
new NullLogger()
|
||||||
);
|
);
|
||||||
return array($payer_factory, $testee);
|
return array($payer_factory, $testee);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue