Extract customer id retrieval

This commit is contained in:
dinamiko 2022-01-05 12:49:49 +01:00
parent d6e36e559b
commit 0ae7e6e9bc
6 changed files with 134 additions and 78 deletions

View file

@ -43,6 +43,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
@ -108,7 +109,7 @@ return array(
$container->get( 'api.bearer' ),
$container->get( 'api.factory.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.prefix' )
$container->get( 'api.repository.customer' )
);
},
'api.endpoint.webhook' => static function ( ContainerInterface $container ) : WebhookEndpoint {
@ -132,14 +133,14 @@ return array(
},
'api.endpoint.identity-token' => static function ( ContainerInterface $container ) : IdentityToken {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$prefix = $container->get( 'api.prefix' );
$settings = $container->get( 'wcgateway.settings' );
$customer_repository = $container->get( 'api.repository.customer' );
return new IdentityToken(
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$logger,
$prefix,
$settings
$settings,
$customer_repository
);
},
'api.endpoint.payments' => static function ( ContainerInterface $container ): PaymentsEndpoint {
@ -221,6 +222,10 @@ return array(
$merchant_id = $container->get( 'api.merchant_id' );
return new PayeeRepository( $merchant_email, $merchant_id );
},
'api.repository.customer' => static function( ContainerInterface $container ): CustomerRepository {
$prefix = $container->get( 'api.prefix' );
return new CustomerRepository( $prefix );
},
'api.factory.application-context' => static function ( ContainerInterface $container ) : ApplicationContextFactory {
return new ApplicationContextFactory();
},

View file

@ -14,6 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
@ -44,13 +45,6 @@ class IdentityToken {
*/
private $logger;
/**
* The prefix.
*
* @var string
*/
private $prefix;
/**
* The settings
*
@ -58,32 +52,45 @@ class IdentityToken {
*/
private $settings;
/**
* The customer repository.
*
* @var CustomerRepository
*/
protected $customer_repository;
/**
* IdentityToken constructor.
*
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
* @param string $prefix The prefix.
* @param Settings $settings The settings.
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param LoggerInterface $logger The logger.
* @param Settings $settings The settings.
* @param CustomerRepository $customer_repository The customer repository.
*/
public function __construct( string $host, Bearer $bearer, LoggerInterface $logger, string $prefix, Settings $settings ) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
$this->prefix = $prefix;
$this->settings = $settings;
public function __construct(
string $host,
Bearer $bearer,
LoggerInterface $logger,
Settings $settings,
CustomerRepository $customer_repository
) {
$this->host = $host;
$this->bearer = $bearer;
$this->logger = $logger;
$this->settings = $settings;
$this->customer_repository = $customer_repository;
}
/**
* Generates a token for a specific customer.
* Generates a token for a specific user.
*
* @param int $customer_id The id of the customer.
* @param int $user_id The id of the user.
*
* @return Token
* @throws RuntimeException If the request fails.
*/
public function generate_for_customer( int $customer_id ): Token {
public function generate_for_customer( int $user_id ): Token {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/identity/generate-token';
@ -98,17 +105,11 @@ class IdentityToken {
( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) )
&& defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION
) {
$guest_customer_id = get_user_meta( $customer_id, 'ppcp_guest_customer_id', true );
if ( $guest_customer_id ) {
$customer_id = $guest_customer_id;
}
if ( 0 === $customer_id ) {
$customer_id = uniqid();
WC()->session->set( 'ppcp_guest_customer_id', $customer_id );
}
$args['body'] = wp_json_encode( array( 'customer_id' => $this->prefix . (string) $customer_id ) );
$args['body'] = wp_json_encode(
array(
'customer_id' => $this->customer_repository->customer_id_for_user( ( $user_id ) ),
)
);
}
$response = $this->request( $url, $args );

View file

@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
/**
* Class PaymentTokenEndpoint
@ -50,12 +51,13 @@ class PaymentTokenEndpoint {
* @var LoggerInterface
*/
private $logger;
/**
* The prefix.
* The customer repository.
*
* @var string
* @var CustomerRepository
*/
private $prefix;
protected $customer_repository;
/**
* PaymentTokenEndpoint constructor.
@ -64,21 +66,21 @@ class PaymentTokenEndpoint {
* @param Bearer $bearer The bearer.
* @param PaymentTokenFactory $factory The payment token factory.
* @param LoggerInterface $logger The logger.
* @param string $prefix The prefix.
* @param CustomerRepository $customer_repository The customer repository.
*/
public function __construct(
string $host,
Bearer $bearer,
PaymentTokenFactory $factory,
LoggerInterface $logger,
string $prefix
CustomerRepository $customer_repository
) {
$this->host = $host;
$this->bearer = $bearer;
$this->factory = $factory;
$this->logger = $logger;
$this->prefix = $prefix;
$this->host = $host;
$this->bearer = $bearer;
$this->factory = $factory;
$this->logger = $logger;
$this->customer_repository = $customer_repository;
}
/**
@ -91,15 +93,8 @@ class PaymentTokenEndpoint {
*/
public function for_user( int $id ): array {
$bearer = $this->bearer->bearer();
$customer_id = $this->prefix . $id;
$guest_customer_id = get_user_meta( $id, 'ppcp_guest_customer_id', true );
if ( $guest_customer_id ) {
$customer_id = $this->prefix . $guest_customer_id;
}
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $customer_id;
$args = array(
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $this->customer_repository->customer_id_for_user( $id );
$args = array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),

View file

@ -0,0 +1,52 @@
<?php
/**
* The customer repository.
*
* @package WooCommerce\PayPalCommerce\ApiClient\Repository
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Repository;
/**
* Class CustomerRepository
*/
class CustomerRepository {
/**
* The prefix.
*
* @var string
*/
protected $prefix;
/**
* CustomerRepository constructor.
*
* @param string $prefix The prefix.
*/
public function __construct( string $prefix ) {
$this->prefix = $prefix;
}
/**
* Returns a unique ID for the given user ID.
*
* @param int $user_id The user ID.
* @return string
*/
public function customer_id_for_user( int $user_id ): string {
$guest_customer_id = get_user_meta( $user_id, 'ppcp_guest_customer_id', true );
if ( $guest_customer_id ) {
$user_id = $guest_customer_id;
}
if ( 0 === $user_id ) {
$user_id = uniqid();
WC()->session->set( 'ppcp_guest_customer_id', $user_id );
}
return $this->prefix . $user_id;
}
}