Show error if PayPal already vaulted for another WC customer

This commit is contained in:
Alex P 2022-04-27 10:45:05 +03:00
parent b2e5a80f3f
commit e72b80ae72
3 changed files with 31 additions and 1 deletions

View file

@ -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

View file

@ -0,0 +1,16 @@
<?php
/**
* AlreadyVaultedException.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Exception
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Exception;
/**
* Class AlreadyVaultedException
*/
class AlreadyVaultedException extends RuntimeException {
}

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Vaulting;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\AlreadyVaultedException;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
/**
@ -69,6 +70,14 @@ class CustomerApprovalListener {
$this->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() );