From e72b80ae7276182f978b15b099b22ed1a692d4b7 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 27 Apr 2022 10:45:05 +0300 Subject: [PATCH] Show error if PayPal already vaulted for another WC customer --- .../src/Endpoint/PaymentTokenEndpoint.php | 7 ++++++- .../src/Exception/AlreadyVaultedException.php | 16 ++++++++++++++++ .../src/CustomerApprovalListener.php | 9 +++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 modules/ppcp-api-client/src/Exception/AlreadyVaultedException.php diff --git a/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php b/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php index 10698763d..9e1b3a74b 100644 --- a/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/PaymentTokenEndpoint.php @@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentTokenActionLinks; +use WooCommerce\PayPalCommerce\ApiClient\Exception\AlreadyVaultedException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenActionLinksFactory; @@ -290,6 +291,7 @@ class PaymentTokenEndpoint { * @return string * @throws RuntimeException If the request fails. * @throws PayPalApiException If the request fails. + * @throws AlreadyVaultedException When new token was not created (for example, already vaulted with this merchant). */ public function create_from_approval_token( string $approval_token, int $user_id ): string { $bearer = $this->bearer->bearer(); @@ -313,7 +315,10 @@ class PaymentTokenEndpoint { $json = json_decode( $response['body'] ); $status_code = (int) wp_remote_retrieve_response_code( $response ); - if ( ! in_array( $status_code, array( 200, 201 ), true ) ) { + if ( 200 === $status_code ) { + throw new AlreadyVaultedException( 'Already vaulted.' ); + } + if ( 201 !== $status_code ) { throw new PayPalApiException( $json, $status_code diff --git a/modules/ppcp-api-client/src/Exception/AlreadyVaultedException.php b/modules/ppcp-api-client/src/Exception/AlreadyVaultedException.php new file mode 100644 index 000000000..4c9380daf --- /dev/null +++ b/modules/ppcp-api-client/src/Exception/AlreadyVaultedException.php @@ -0,0 +1,16 @@ +payment_token_endpoint->create_from_approval_token( $token, get_current_user_id() ); $this->redirect( $url ); + } catch ( AlreadyVaultedException $exception ) { + $this->logger->error( 'Failed to create payment token. ' . $exception->getMessage() ); + $this->add_wc_error_notice( + __( + 'This PayPal account is already saved on this site. Please check that you are logged in correctly.', + 'woocommerce-paypal-payments' + ) + ); } catch ( Exception $exception ) { $this->logger->error( 'Failed to create payment token. ' . $exception->getMessage() ); $this->add_wc_error_notice( $exception->getMessage() );