mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Extract customer id retrieval
This commit is contained in:
parent
d6e36e559b
commit
0ae7e6e9bc
6 changed files with 134 additions and 78 deletions
|
@ -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();
|
||||
},
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
@ -20,8 +21,8 @@ class IdentityTokenTest extends TestCase
|
|||
private $host;
|
||||
private $bearer;
|
||||
private $logger;
|
||||
private $prefix;
|
||||
private $settings;
|
||||
private $customer_repository;
|
||||
private $sut;
|
||||
|
||||
public function setUp(): void
|
||||
|
@ -31,10 +32,16 @@ class IdentityTokenTest extends TestCase
|
|||
$this->host = 'https://example.com/';
|
||||
$this->bearer = Mockery::mock(Bearer::class);
|
||||
$this->logger = Mockery::mock(LoggerInterface::class);
|
||||
$this->prefix = 'prefix';
|
||||
$this->settings = Mockery::mock(Settings::class);
|
||||
$this->customer_repository = Mockery::mock(CustomerRepository::class);
|
||||
|
||||
$this->sut = new IdentityToken($this->host, $this->bearer, $this->logger, $this->prefix, $this->settings);
|
||||
$this->sut = new IdentityToken(
|
||||
$this->host,
|
||||
$this->bearer,
|
||||
$this->logger,
|
||||
$this->settings,
|
||||
$this->customer_repository
|
||||
);
|
||||
}
|
||||
|
||||
public function testGenerateForCustomerReturnsToken()
|
||||
|
@ -52,6 +59,7 @@ class IdentityTokenTest extends TestCase
|
|||
$this->logger->shouldReceive('debug');
|
||||
$this->settings->shouldReceive('has')->andReturn(true);
|
||||
$this->settings->shouldReceive('get')->andReturn(true);
|
||||
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('prefix1');
|
||||
|
||||
$rawResponse = [
|
||||
'body' => '{"client_token":"abc123", "expires_in":3600}',
|
||||
|
@ -105,7 +113,7 @@ class IdentityTokenTest extends TestCase
|
|||
$this->logger->shouldReceive('debug');
|
||||
$this->settings->shouldReceive('has')->andReturn(true);
|
||||
$this->settings->shouldReceive('get')->andReturn(true);
|
||||
when('get_user_meta')->justReturn('');
|
||||
$this->customer_repository->shouldReceive('customer_id_for_user');
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->sut->generate_for_customer(1);
|
||||
|
@ -128,11 +136,11 @@ class IdentityTokenTest extends TestCase
|
|||
expect('is_wp_error')->andReturn(false);
|
||||
expect('wp_remote_retrieve_response_code')->andReturn(500);
|
||||
when('wc_print_r')->returnArg();
|
||||
when('get_user_meta')->justReturn('');
|
||||
$this->logger->shouldReceive('log');
|
||||
$this->logger->shouldReceive('debug');
|
||||
$this->settings->shouldReceive('has')->andReturn(true);
|
||||
$this->settings->shouldReceive('get')->andReturn(true);
|
||||
$this->customer_repository->shouldReceive('customer_id_for_user');
|
||||
|
||||
$this->expectException(PayPalApiException::class);
|
||||
$this->sut->generate_for_customer(1);
|
||||
|
|
|
@ -11,11 +11,11 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
||||
use Mockery;
|
||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||
use function Brain\Monkey\Functions\expect;
|
||||
use function Brain\Monkey\Functions\when;
|
||||
|
||||
class PaymentTokenEndpointTest extends TestCase
|
||||
{
|
||||
|
@ -25,7 +25,7 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
private $bearer;
|
||||
private $factory;
|
||||
private $logger;
|
||||
private $prefix;
|
||||
private $customer_repository;
|
||||
private $sut;
|
||||
|
||||
public function setUp(): void
|
||||
|
@ -36,13 +36,13 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
$this->bearer = Mockery::mock(Bearer::class);
|
||||
$this->factory = Mockery::mock(PaymentTokenFactory::class);
|
||||
$this->logger = Mockery::mock(LoggerInterface::class);
|
||||
$this->prefix = 'prefix';
|
||||
$this->customer_repository = Mockery::mock(CustomerRepository::class);
|
||||
$this->sut = new PaymentTokenEndpoint(
|
||||
$this->host,
|
||||
$this->bearer,
|
||||
$this->factory,
|
||||
$this->logger,
|
||||
$this->prefix
|
||||
$this->customer_repository
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
$token->shouldReceive('token')
|
||||
->andReturn('bearer');
|
||||
|
||||
$this->ensureRequestForUser($rawResponse, $id);
|
||||
$this->ensureRequestForUser($rawResponse, '123abc');
|
||||
expect('is_wp_error')->with($rawResponse)->andReturn(false);
|
||||
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
|
||||
|
||||
|
@ -74,7 +74,7 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
|
||||
$this->logger->shouldReceive('debug');
|
||||
|
||||
when('get_user_meta')->justReturn('');
|
||||
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('123abc');
|
||||
|
||||
$result = $this->sut->for_user($id);
|
||||
$this->assertInstanceOf(PaymentToken::class, $result[0]);
|
||||
|
@ -92,14 +92,14 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
->andReturn($token);
|
||||
$token->shouldReceive('token')
|
||||
->andReturn('bearer');
|
||||
$this->ensureRequestForUser($rawResponse, $id);
|
||||
$this->ensureRequestForUser($rawResponse, '123abc');
|
||||
|
||||
expect('wp_remote_get')->andReturn($rawResponse);
|
||||
expect('is_wp_error')->with($rawResponse)->andReturn(true);
|
||||
$this->logger->shouldReceive('log');
|
||||
$this->logger->shouldReceive('debug');
|
||||
|
||||
when('get_user_meta')->justReturn('');
|
||||
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('123abc');
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->sut->for_user($id);
|
||||
|
@ -119,7 +119,7 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
->andReturn($token);
|
||||
$token->shouldReceive('token')
|
||||
->andReturn('bearer');
|
||||
$this->ensureRequestForUser($rawResponse, $id);
|
||||
$this->ensureRequestForUser($rawResponse, '123abc');
|
||||
|
||||
|
||||
expect('wp_remote_get')->andReturn($rawResponse);
|
||||
|
@ -128,7 +128,7 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
$this->logger->shouldReceive('log');
|
||||
$this->logger->shouldReceive('debug');
|
||||
|
||||
when('get_user_meta')->justReturn('');
|
||||
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('123abc');
|
||||
|
||||
$this->expectException(PayPalApiException::class);
|
||||
$this->sut->for_user($id);
|
||||
|
@ -186,17 +186,12 @@ class PaymentTokenEndpointTest extends TestCase
|
|||
* @param int $id
|
||||
* @throws \Brain\Monkey\Expectation\Exception\ExpectationArgsRequired
|
||||
*/
|
||||
private function ensureRequestForUser(array $rawResponse, int $id): void
|
||||
private function ensureRequestForUser(array $rawResponse, string $id): void
|
||||
{
|
||||
$host = $this->host;
|
||||
$prefix = $this->prefix;
|
||||
expect('wp_remote_get')->andReturnUsing(
|
||||
function ($url, $args) use ($rawResponse, $host, $prefix, $id) {
|
||||
// echo $url; // https://example.com/v2/vault/payment-tokens/?customer_id=prefixabc123
|
||||
// https://example.com/v2/vault/payment-tokens/?customer_id=prefix1
|
||||
// https://example.com/v2/vault/payment-tokens/?customer_id=prefixabc123
|
||||
echo $host . 'v2/vault/payment-tokens/?customer_id=' . $prefix . $id;
|
||||
if ($url !== $host . 'v2/vault/payment-tokens/?customer_id=' . $prefix . $id) {
|
||||
function ($url, $args) use ($rawResponse, $host, $id) {
|
||||
if ($url !== $host . 'v2/vault/payment-tokens/?customer_id=' . $id) {
|
||||
return false;
|
||||
}
|
||||
if ($args['headers']['Authorization'] !== 'Bearer bearer') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue