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

@ -107,12 +107,18 @@ class VaultPaymentTokenCreated implements RequestHandler {
$customer_id = null !== $request['resource'] && isset( $request['resource']['customer_id'] )
? $request['resource']['customer_id']
: '';
if ( ! $customer_id ) {
$message = 'No customer id was found.';
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 );
if ( ! is_null( $request['resource'] ) && isset( $request['resource']['id'] ) ) {
@ -149,4 +155,29 @@ class VaultPaymentTokenCreated implements RequestHandler {
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;
}
}