Merge pull request #1529 from woocommerce/PCP-1899-high-rate-of-auth-voids

High rate of auth voids on vaulted subscriptions for guest users (1899)
This commit is contained in:
Emili Castells 2023-08-17 11:28:11 +02:00 committed by GitHub
commit 0e97af9122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 139 additions and 5 deletions

View file

@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenActionLinksFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
use WP_Error;
/** /**
* Class PaymentTokenEndpoint * Class PaymentTokenEndpoint
@ -97,7 +98,7 @@ class PaymentTokenEndpoint {
} }
/** /**
* Returns the payment tokens for a user. * Returns the payment tokens for the given user id.
* *
* @param int $id The user id. * @param int $id The user id.
* *
@ -118,7 +119,67 @@ class PaymentTokenEndpoint {
$response = $this->request( $url, $args ); $response = $this->request( $url, $args );
if ( is_wp_error( $response ) ) { if ( is_wp_error( $response ) ) {
$error = new RuntimeException( $error = new RuntimeException(
__( 'Could not fetch payment token.', 'woocommerce-paypal-payments' ) __( 'Could not fetch payment token for customer id.', 'woocommerce-paypal-payments' )
);
$this->logger->log(
'warning',
$error->getMessage(),
array(
'args' => $args,
'response' => $response,
)
);
throw $error;
}
$json = json_decode( $response['body'] );
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code ) {
$error = new PayPalApiException(
$json,
$status_code
);
$this->logger->log(
'warning',
$error->getMessage(),
array(
'args' => $args,
'response' => $response,
)
);
throw $error;
}
$tokens = array();
foreach ( $json->payment_tokens as $token_value ) {
$tokens[] = $this->factory->from_paypal_response( $token_value );
}
return $tokens;
}
/**
* Returns the payment tokens for the given guest customer id.
*
* @param string $customer_id The guest customer id.
*
* @return PaymentToken[]
* @throws RuntimeException If the request fails.
*/
public function for_guest( string $customer_id ): array {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $customer_id;
$args = array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
),
);
$response = $this->request( $url, $args );
if ( $response instanceof WP_Error ) {
$error = new RuntimeException(
__( 'Could not fetch payment token for guest customer id.', 'woocommerce-paypal-payments' )
); );
$this->logger->log( $this->logger->log(
'warning', 'warning',

View file

@ -30,6 +30,7 @@ return array(
$container->get( 'wcgateway.settings' ), $container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.processor.authorized-payments' ), $container->get( 'wcgateway.processor.authorized-payments' ),
$container->get( 'api.endpoint.payments' ), $container->get( 'api.endpoint.payments' ),
$container->get( 'api.endpoint.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' ) $container->get( 'woocommerce.logger.woocommerce' )
); );
}, },

View file

@ -14,6 +14,8 @@ use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
use WC_Order; use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository; use WooCommerce\PayPalCommerce\ApiClient\Repository\OrderRepository;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait; use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
@ -67,6 +69,13 @@ class PaymentTokenChecker {
*/ */
protected $payments_endpoint; protected $payments_endpoint;
/**
* The payment token endpoint.
*
* @var PaymentTokenEndpoint
*/
protected $payment_token_endpoint;
/** /**
* The logger. * The logger.
* *
@ -82,6 +91,7 @@ class PaymentTokenChecker {
* @param Settings $settings The settings. * @param Settings $settings The settings.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The authorized payments processor. * @param AuthorizedPaymentsProcessor $authorized_payments_processor The authorized payments processor.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint. * @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @param PaymentTokenEndpoint $payment_token_endpoint The payment token endpoint.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
@ -90,6 +100,7 @@ class PaymentTokenChecker {
Settings $settings, Settings $settings,
AuthorizedPaymentsProcessor $authorized_payments_processor, AuthorizedPaymentsProcessor $authorized_payments_processor,
PaymentsEndpoint $payments_endpoint, PaymentsEndpoint $payments_endpoint,
PaymentTokenEndpoint $payment_token_endpoint,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->payment_token_repository = $payment_token_repository; $this->payment_token_repository = $payment_token_repository;
@ -97,6 +108,7 @@ class PaymentTokenChecker {
$this->settings = $settings; $this->settings = $settings;
$this->authorized_payments_processor = $authorized_payments_processor; $this->authorized_payments_processor = $authorized_payments_processor;
$this->payments_endpoint = $payments_endpoint; $this->payments_endpoint = $payments_endpoint;
$this->payment_token_endpoint = $payment_token_endpoint;
$this->logger = $logger; $this->logger = $logger;
} }
@ -130,7 +142,7 @@ class PaymentTokenChecker {
return; return;
} }
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id ); $tokens = $this->tokens_for_user( $customer_id );
if ( $tokens ) { if ( $tokens ) {
try { try {
$this->capture_authorized_payment( $wc_order ); $this->capture_authorized_payment( $wc_order );
@ -231,4 +243,32 @@ class PaymentTokenChecker {
} }
} }
} }
/**
* Returns customer tokens either from guest or customer id.
*
* @param int $customer_id The customer id.
* @return PaymentToken[]
*/
private function tokens_for_user( int $customer_id ): array {
$tokens = array();
$guest_customer_id = get_user_meta( $customer_id, 'ppcp_guest_customer_id', true );
if ( $guest_customer_id ) {
$tokens = $this->payment_token_endpoint->for_guest( $guest_customer_id );
}
if ( ! $tokens ) {
$guest_customer_id = get_user_meta( $customer_id, 'ppcp_customer_id', true );
if ( $guest_customer_id ) {
$tokens = $this->payment_token_endpoint->for_guest( $guest_customer_id );
}
}
if ( ! $tokens ) {
$tokens = $this->payment_token_repository->all_for_user_id( $customer_id );
}
return $tokens;
}
} }

View file

@ -72,6 +72,7 @@ class VaultingModule implements ModuleInterface {
'woocommerce_paypal_payments_check_saved_payment', 'woocommerce_paypal_payments_check_saved_payment',
function ( int $order_id, int $customer_id, string $intent ) use ( $container ) { function ( int $order_id, int $customer_id, string $intent ) use ( $container ) {
$payment_token_checker = $container->get( 'vaulting.payment-token-checker' ); $payment_token_checker = $container->get( 'vaulting.payment-token-checker' );
assert( $payment_token_checker instanceof PaymentTokenChecker );
$payment_token_checker->check_and_update( $order_id, $customer_id, $intent ); $payment_token_checker->check_and_update( $order_id, $customer_id, $intent );
}, },
10, 10,

View file

@ -40,7 +40,7 @@ trait ProcessPaymentTrait {
* @param int $customer_id The customer ID. * @param int $customer_id The customer ID.
*/ */
protected function schedule_saved_payment_check( int $wc_order_id, int $customer_id ): void { protected function schedule_saved_payment_check( int $wc_order_id, int $customer_id ): void {
$timestamp = 1 * MINUTE_IN_SECONDS; $timestamp = 3 * MINUTE_IN_SECONDS;
if ( if (
$this->config->has( 'subscription_behavior_when_vault_fails' ) $this->config->has( 'subscription_behavior_when_vault_fails' )
&& $this->config->get( 'subscription_behavior_when_vault_fails' ) === 'capture_auth' && $this->config->get( 'subscription_behavior_when_vault_fails' ) === 'capture_auth'

View file

@ -107,12 +107,18 @@ class VaultPaymentTokenCreated implements RequestHandler {
$customer_id = null !== $request['resource'] && isset( $request['resource']['customer_id'] ) $customer_id = null !== $request['resource'] && isset( $request['resource']['customer_id'] )
? $request['resource']['customer_id'] ? $request['resource']['customer_id']
: ''; : '';
if ( ! $customer_id ) { if ( ! $customer_id ) {
$message = 'No customer id was found.'; $message = 'No customer id was found.';
return $this->failure_response( $message ); return $this->failure_response( $message );
} }
$wc_customer_id = (int) str_replace( $this->prefix, '', $customer_id ); $wc_customer_id = $this->wc_customer_id_from( $customer_id );
if ( ! $wc_customer_id ) {
$message = "No WC customer id was found from PayPal customer id {$customer_id}";
return $this->failure_response( $message );
}
$this->authorized_payments_processor->capture_authorized_payments_for_customer( $wc_customer_id ); $this->authorized_payments_processor->capture_authorized_payments_for_customer( $wc_customer_id );
if ( ! is_null( $request['resource'] ) && isset( $request['resource']['id'] ) ) { if ( ! is_null( $request['resource'] ) && isset( $request['resource']['id'] ) ) {
@ -149,4 +155,29 @@ class VaultPaymentTokenCreated implements RequestHandler {
return $this->success_response(); return $this->success_response();
} }
/**
* Returns WC customer id from PayPal customer id.
*
* @param string $customer_id The customer ID from PayPal.
* @return int
*/
private function wc_customer_id_from( string $customer_id ): int {
$customers = get_users(
array(
'meta_key' => 'ppcp_guest_customer_id', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $customer_id, //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'fields' => 'ids',
'number' => 1,
)
);
$wc_customer_id = $customers[0] ?? '';
if ( $wc_customer_id ) {
return (int) $wc_customer_id;
}
$id = str_replace( $this->prefix, '', $customer_id );
return is_numeric( $id ) ? (int) $id : 0;
}
} }