Merge branch 'trunk' into PCP-591-save-and-display-vaulted-payment-methods-in-woo-commerce-native-endpoint

This commit is contained in:
Emili Castells Guasch 2023-04-12 09:45:13 +02:00
commit 524be09bd1
5 changed files with 47 additions and 11 deletions

View file

@ -62,6 +62,11 @@ class CheckoutActionHandler {
);
} else {
errorHandler.clear();
if (data.data.refresh) {
jQuery( document.body ).trigger( 'update_checkout' );
}
if (data.data.errors?.length > 0) {
errorHandler.messages(data.data.errors);
} else if (data.data.details?.length > 0) {

View file

@ -23,6 +23,10 @@ export default class FormValidator {
const data = await res.json();
if (!data.success) {
if (data.data.refresh) {
jQuery( document.body ).trigger( 'update_checkout' );
}
if (data.data.errors) {
return data.data.errors;
}

View file

@ -288,12 +288,15 @@ class CreateOrderEndpoint implements EndpointInterface {
return true;
} catch ( ValidationException $error ) {
wp_send_json_error(
array(
'message' => $error->getMessage(),
'errors' => $error->errors(),
)
$response = array(
'message' => $error->getMessage(),
'errors' => $error->errors(),
'refresh' => isset( WC()->session->refresh_totals ),
);
unset( WC()->session->refresh_totals );
wp_send_json_error( $response );
} catch ( \RuntimeException $error ) {
$this->logger->error( 'Order creation failed: ' . $error->getMessage() );

View file

@ -86,12 +86,15 @@ class ValidateCheckoutEndpoint implements EndpointInterface {
return true;
} catch ( ValidationException $exception ) {
wp_send_json_error(
array(
'message' => $exception->getMessage(),
'errors' => $exception->errors(),
)
$response = array(
'message' => $exception->getMessage(),
'errors' => $exception->errors(),
'refresh' => isset( WC()->session->refresh_totals ),
);
unset( WC()->session->refresh_totals );
wp_send_json_error( $response );
return false;
} catch ( Throwable $error ) {
$this->logger->error( "Form validation execution failed. {$error->getMessage()} {$error->getFile()}:{$error->getLine()}" );

View file

@ -11,6 +11,7 @@ use WooCommerce\PayPalCommerce\Button\Exception\ValidationException;
use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class ValidateCheckoutEndpointTest extends TestCase
{
@ -21,6 +22,8 @@ class ValidateCheckoutEndpointTest extends TestCase
private $logger;
private $sut;
private $session = [];
public function setUp(): void
{
parent::setUp();
@ -36,6 +39,10 @@ class ValidateCheckoutEndpointTest extends TestCase
);
$this->requestData->expects('read_request')->andReturn(['form' => ['field1' => 'value']]);
when('WC')->alias(function () {
return (object) ['session' => (object) $this->session];
});
}
public function testValid()
@ -54,7 +61,21 @@ class ValidateCheckoutEndpointTest extends TestCase
->andThrow($exception);
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value']]);
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value'], 'refresh' => false]);
$this->sut->handle_request();
}
public function testInvalidAndRefresh()
{
$exception = new ValidationException(['Invalid value']);
$this->formValidator->expects('validate')->once()
->andThrow($exception);
$this->session['refresh_totals'] = true;
expect('wp_send_json_error')->once()
->with(['message' => $exception->getMessage(), 'errors' => ['Invalid value'], 'refresh' => true]);
$this->sut->handle_request();
}