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 { } else {
errorHandler.clear(); errorHandler.clear();
if (data.data.refresh) {
jQuery( document.body ).trigger( 'update_checkout' );
}
if (data.data.errors?.length > 0) { if (data.data.errors?.length > 0) {
errorHandler.messages(data.data.errors); errorHandler.messages(data.data.errors);
} else if (data.data.details?.length > 0) { } else if (data.data.details?.length > 0) {

View file

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

View file

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

View file

@ -86,12 +86,15 @@ class ValidateCheckoutEndpoint implements EndpointInterface {
return true; return true;
} catch ( ValidationException $exception ) { } catch ( ValidationException $exception ) {
wp_send_json_error( $response = array(
array( 'message' => $exception->getMessage(),
'message' => $exception->getMessage(), 'errors' => $exception->errors(),
'errors' => $exception->errors(), 'refresh' => isset( WC()->session->refresh_totals ),
)
); );
unset( WC()->session->refresh_totals );
wp_send_json_error( $response );
return false; return false;
} catch ( Throwable $error ) { } catch ( Throwable $error ) {
$this->logger->error( "Form validation execution failed. {$error->getMessage()} {$error->getFile()}:{$error->getLine()}" ); $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\Button\Validation\CheckoutFormValidator;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect; use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class ValidateCheckoutEndpointTest extends TestCase class ValidateCheckoutEndpointTest extends TestCase
{ {
@ -21,6 +22,8 @@ class ValidateCheckoutEndpointTest extends TestCase
private $logger; private $logger;
private $sut; private $sut;
private $session = [];
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
@ -36,6 +39,10 @@ class ValidateCheckoutEndpointTest extends TestCase
); );
$this->requestData->expects('read_request')->andReturn(['form' => ['field1' => 'value']]); $this->requestData->expects('read_request')->andReturn(['form' => ['field1' => 'value']]);
when('WC')->alias(function () {
return (object) ['session' => (object) $this->session];
});
} }
public function testValid() public function testValid()
@ -54,7 +61,21 @@ class ValidateCheckoutEndpointTest extends TestCase
->andThrow($exception); ->andThrow($exception);
expect('wp_send_json_error')->once() 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(); $this->sut->handle_request();
} }