mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Merge pull request #423 from woocommerce/PCP-434-allow-guest-users-to-purchase-su
Add customer id for guest users purchasing subscriptions
This commit is contained in:
commit
0fee10931a
13 changed files with 183 additions and 89 deletions
|
@ -43,6 +43,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
|
||||||
|
@ -108,7 +109,7 @@ return array(
|
||||||
$container->get( 'api.bearer' ),
|
$container->get( 'api.bearer' ),
|
||||||
$container->get( 'api.factory.payment-token' ),
|
$container->get( 'api.factory.payment-token' ),
|
||||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||||
$container->get( 'api.prefix' )
|
$container->get( 'api.repository.customer' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'api.endpoint.webhook' => static function ( ContainerInterface $container ) : WebhookEndpoint {
|
'api.endpoint.webhook' => static function ( ContainerInterface $container ) : WebhookEndpoint {
|
||||||
|
@ -132,14 +133,14 @@ return array(
|
||||||
},
|
},
|
||||||
'api.endpoint.identity-token' => static function ( ContainerInterface $container ) : IdentityToken {
|
'api.endpoint.identity-token' => static function ( ContainerInterface $container ) : IdentityToken {
|
||||||
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
||||||
$prefix = $container->get( 'api.prefix' );
|
|
||||||
$settings = $container->get( 'wcgateway.settings' );
|
$settings = $container->get( 'wcgateway.settings' );
|
||||||
|
$customer_repository = $container->get( 'api.repository.customer' );
|
||||||
return new IdentityToken(
|
return new IdentityToken(
|
||||||
$container->get( 'api.host' ),
|
$container->get( 'api.host' ),
|
||||||
$container->get( 'api.bearer' ),
|
$container->get( 'api.bearer' ),
|
||||||
$logger,
|
$logger,
|
||||||
$prefix,
|
$settings,
|
||||||
$settings
|
$customer_repository
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'api.endpoint.payments' => static function ( ContainerInterface $container ): PaymentsEndpoint {
|
'api.endpoint.payments' => static function ( ContainerInterface $container ): PaymentsEndpoint {
|
||||||
|
@ -221,6 +222,10 @@ return array(
|
||||||
$merchant_id = $container->get( 'api.merchant_id' );
|
$merchant_id = $container->get( 'api.merchant_id' );
|
||||||
return new PayeeRepository( $merchant_email, $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 {
|
'api.factory.application-context' => static function ( ContainerInterface $container ) : ApplicationContextFactory {
|
||||||
return new 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\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,13 +45,6 @@ class IdentityToken {
|
||||||
*/
|
*/
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* The prefix.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $prefix;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The settings
|
* The settings
|
||||||
*
|
*
|
||||||
|
@ -58,32 +52,45 @@ class IdentityToken {
|
||||||
*/
|
*/
|
||||||
private $settings;
|
private $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The customer repository.
|
||||||
|
*
|
||||||
|
* @var CustomerRepository
|
||||||
|
*/
|
||||||
|
protected $customer_repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IdentityToken constructor.
|
* IdentityToken constructor.
|
||||||
*
|
*
|
||||||
* @param string $host The host.
|
* @param string $host The host.
|
||||||
* @param Bearer $bearer The bearer.
|
* @param Bearer $bearer The bearer.
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
* @param string $prefix The prefix.
|
|
||||||
* @param Settings $settings The settings.
|
* @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 ) {
|
public function __construct(
|
||||||
|
string $host,
|
||||||
|
Bearer $bearer,
|
||||||
|
LoggerInterface $logger,
|
||||||
|
Settings $settings,
|
||||||
|
CustomerRepository $customer_repository
|
||||||
|
) {
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->bearer = $bearer;
|
$this->bearer = $bearer;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->prefix = $prefix;
|
|
||||||
$this->settings = $settings;
|
$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
|
* @return Token
|
||||||
* @throws RuntimeException If the request fails.
|
* @throws RuntimeException If the request fails.
|
||||||
*/
|
*/
|
||||||
public function generate_for_customer( int $customer_id ): Token {
|
public function generate_for_user( int $user_id ): Token {
|
||||||
|
|
||||||
$bearer = $this->bearer->bearer();
|
$bearer = $this->bearer->bearer();
|
||||||
$url = trailingslashit( $this->host ) . 'v1/identity/generate-token';
|
$url = trailingslashit( $this->host ) . 'v1/identity/generate-token';
|
||||||
|
@ -95,11 +102,16 @@ class IdentityToken {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if (
|
if (
|
||||||
$customer_id
|
( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) )
|
||||||
&& ( $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ) )
|
|
||||||
&& defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION
|
&& defined( 'PPCP_FLAG_SUBSCRIPTION' ) && PPCP_FLAG_SUBSCRIPTION
|
||||||
) {
|
) {
|
||||||
$args['body'] = wp_json_encode( array( 'customer_id' => $this->prefix . $customer_id ) );
|
$customer_id = $this->customer_repository->customer_id_for_user( ( $user_id ) );
|
||||||
|
|
||||||
|
$args['body'] = wp_json_encode(
|
||||||
|
array(
|
||||||
|
'customer_id' => $customer_id,
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $this->request( $url, $args );
|
$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\Exception\RuntimeException;
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PaymentTokenEndpoint
|
* Class PaymentTokenEndpoint
|
||||||
|
@ -50,12 +51,13 @@ class PaymentTokenEndpoint {
|
||||||
* @var LoggerInterface
|
* @var LoggerInterface
|
||||||
*/
|
*/
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The prefix.
|
* The customer repository.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var CustomerRepository
|
||||||
*/
|
*/
|
||||||
private $prefix;
|
protected $customer_repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PaymentTokenEndpoint constructor.
|
* PaymentTokenEndpoint constructor.
|
||||||
|
@ -64,21 +66,21 @@ class PaymentTokenEndpoint {
|
||||||
* @param Bearer $bearer The bearer.
|
* @param Bearer $bearer The bearer.
|
||||||
* @param PaymentTokenFactory $factory The payment token factory.
|
* @param PaymentTokenFactory $factory The payment token factory.
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
* @param string $prefix The prefix.
|
* @param CustomerRepository $customer_repository The customer repository.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $host,
|
string $host,
|
||||||
Bearer $bearer,
|
Bearer $bearer,
|
||||||
PaymentTokenFactory $factory,
|
PaymentTokenFactory $factory,
|
||||||
LoggerInterface $logger,
|
LoggerInterface $logger,
|
||||||
string $prefix
|
CustomerRepository $customer_repository
|
||||||
) {
|
) {
|
||||||
|
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->bearer = $bearer;
|
$this->bearer = $bearer;
|
||||||
$this->factory = $factory;
|
$this->factory = $factory;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->prefix = $prefix;
|
$this->customer_repository = $customer_repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,9 +93,7 @@ class PaymentTokenEndpoint {
|
||||||
*/
|
*/
|
||||||
public function for_user( int $id ): array {
|
public function for_user( int $id ): array {
|
||||||
$bearer = $this->bearer->bearer();
|
$bearer = $this->bearer->bearer();
|
||||||
|
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $this->customer_repository->customer_id_for_user( $id );
|
||||||
$customer_id = $this->prefix . $id;
|
|
||||||
$url = trailingslashit( $this->host ) . 'v2/vault/payment-tokens/?customer_id=' . $customer_id;
|
|
||||||
$args = array(
|
$args = array(
|
||||||
'method' => 'GET',
|
'method' => 'GET',
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?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 the customer 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 {
|
||||||
|
if ( 0 === $user_id ) {
|
||||||
|
$guest_customer_id = WC()->session->get( 'ppcp_guest_customer_id' );
|
||||||
|
if ( is_string( $guest_customer_id ) && $guest_customer_id ) {
|
||||||
|
return $guest_customer_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$unique_id = $this->prefix . uniqid();
|
||||||
|
WC()->session->set( 'ppcp_guest_customer_id', $unique_id );
|
||||||
|
|
||||||
|
return $unique_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$guest_customer_id = get_user_meta( $user_id, 'ppcp_guest_customer_id', true );
|
||||||
|
if ( $guest_customer_id ) {
|
||||||
|
return $guest_customer_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->prefix . (string) $user_id;
|
||||||
|
}
|
||||||
|
}
|
|
@ -82,8 +82,16 @@ document.addEventListener(
|
||||||
console.error('PayPal button could not be configured.');
|
console.error('PayPal button could not be configured.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const script = document.createElement('script');
|
|
||||||
|
|
||||||
|
if (
|
||||||
|
PayPalCommerceGateway.context !== 'checkout'
|
||||||
|
&& PayPalCommerceGateway.data_client_id.user === 0
|
||||||
|
&& PayPalCommerceGateway.data_client_id.has_subscriptions
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const script = document.createElement('script');
|
||||||
script.addEventListener('load', (event) => {
|
script.addEventListener('load', (event) => {
|
||||||
bootstrap();
|
bootstrap();
|
||||||
});
|
});
|
||||||
|
|
|
@ -392,9 +392,6 @@ class SmartButton implements SmartButtonInterface {
|
||||||
if ( ! is_checkout() && ! $buttons_enabled ) {
|
if ( ! is_checkout() && ! $buttons_enabled ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ( ! $this->can_save_vault_token() && $this->has_subscriptions() ) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$load_script = false;
|
$load_script = false;
|
||||||
if ( is_checkout() && $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) {
|
if ( is_checkout() && $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' ) ) {
|
||||||
|
@ -599,7 +596,7 @@ class SmartButton implements SmartButtonInterface {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return is_user_logged_in();
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -650,10 +647,11 @@ class SmartButton implements SmartButtonInterface {
|
||||||
$localize = array(
|
$localize = array(
|
||||||
'script_attributes' => $this->attributes(),
|
'script_attributes' => $this->attributes(),
|
||||||
'data_client_id' => array(
|
'data_client_id' => array(
|
||||||
'set_attribute' => ( is_checkout() && $this->dcc_is_enabled() ) || $this->can_save_vault_token(),
|
'set_attribute' => $this->can_save_vault_token(),
|
||||||
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
|
'endpoint' => home_url( \WC_AJAX::get_endpoint( DataClientIdEndpoint::ENDPOINT ) ),
|
||||||
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
|
'nonce' => wp_create_nonce( DataClientIdEndpoint::nonce() ),
|
||||||
'user' => get_current_user_id(),
|
'user' => get_current_user_id(),
|
||||||
|
'has_subscriptions' => $this->has_subscriptions(),
|
||||||
),
|
),
|
||||||
'redirect' => wc_get_checkout_url(),
|
'redirect' => wc_get_checkout_url(),
|
||||||
'context' => $this->context(),
|
'context' => $this->context(),
|
||||||
|
|
|
@ -80,7 +80,7 @@ class DataClientIdEndpoint implements EndpointInterface {
|
||||||
try {
|
try {
|
||||||
$this->request_data->read_request( $this->nonce() );
|
$this->request_data->read_request( $this->nonce() );
|
||||||
$user_id = get_current_user_id();
|
$user_id = get_current_user_id();
|
||||||
$token = $this->identity_token->generate_for_customer( $user_id );
|
$token = $this->identity_token->generate_for_user( $user_id );
|
||||||
wp_send_json(
|
wp_send_json(
|
||||||
array(
|
array(
|
||||||
'token' => $token->token(),
|
'token' => $token->token(),
|
||||||
|
|
|
@ -98,6 +98,17 @@ class VaultingModule implements ModuleInterface {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$subscription_helper = $container->get( 'subscription.helper' );
|
||||||
|
add_action(
|
||||||
|
'woocommerce_created_customer',
|
||||||
|
function( int $customer_id ) use ( $subscription_helper ) {
|
||||||
|
$guest_customer_id = WC()->session->get( 'ppcp_guest_customer_id' );
|
||||||
|
if ( $guest_customer_id && $subscription_helper->cart_contains_subscription() ) {
|
||||||
|
update_user_meta( $customer_id, 'ppcp_guest_customer_id', $guest_customer_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
$asset_loader = $container->get( 'vaulting.assets.myaccount-payments' );
|
$asset_loader = $container->get( 'vaulting.assets.myaccount-payments' );
|
||||||
add_action(
|
add_action(
|
||||||
'wp_enqueue_scripts',
|
'wp_enqueue_scripts',
|
||||||
|
|
|
@ -120,8 +120,7 @@ return array(
|
||||||
'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways {
|
'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways {
|
||||||
$session_handler = $container->get( 'session.handler' );
|
$session_handler = $container->get( 'session.handler' );
|
||||||
$settings = $container->get( 'wcgateway.settings' );
|
$settings = $container->get( 'wcgateway.settings' );
|
||||||
$subscription_helper = $container->get( 'subscription.helper' );
|
return new DisableGateways( $session_handler, $settings );
|
||||||
return new DisableGateways( $session_handler, $settings, $subscription_helper );
|
|
||||||
},
|
},
|
||||||
'wcgateway.is-wc-payments-page' => static function ( ContainerInterface $container ): bool {
|
'wcgateway.is-wc-payments-page' => static function ( ContainerInterface $container ): bool {
|
||||||
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
|
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
|
||||||
|
|
|
@ -10,7 +10,6 @@ declare(strict_types=1);
|
||||||
namespace WooCommerce\PayPalCommerce\WcGateway\Checkout;
|
namespace WooCommerce\PayPalCommerce\WcGateway\Checkout;
|
||||||
|
|
||||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
@ -34,29 +33,19 @@ class DisableGateways {
|
||||||
*/
|
*/
|
||||||
private $settings;
|
private $settings;
|
||||||
|
|
||||||
/**
|
|
||||||
* The subscription helper
|
|
||||||
*
|
|
||||||
* @var SubscriptionHelper
|
|
||||||
*/
|
|
||||||
private $subscription_helper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DisableGateways constructor.
|
* DisableGateways constructor.
|
||||||
*
|
*
|
||||||
* @param SessionHandler $session_handler The Session Handler.
|
* @param SessionHandler $session_handler The Session Handler.
|
||||||
* @param ContainerInterface $settings The Settings.
|
* @param ContainerInterface $settings The Settings.
|
||||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SessionHandler $session_handler,
|
SessionHandler $session_handler,
|
||||||
ContainerInterface $settings,
|
ContainerInterface $settings
|
||||||
SubscriptionHelper $subscription_helper
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
$this->session_handler = $session_handler;
|
$this->session_handler = $session_handler;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->subscription_helper = $subscription_helper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -110,10 +99,6 @@ class DisableGateways {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $this->subscription_helper->cart_contains_subscription() && ! is_user_logged_in() ) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
@ -20,8 +21,8 @@ class IdentityTokenTest extends TestCase
|
||||||
private $host;
|
private $host;
|
||||||
private $bearer;
|
private $bearer;
|
||||||
private $logger;
|
private $logger;
|
||||||
private $prefix;
|
|
||||||
private $settings;
|
private $settings;
|
||||||
|
private $customer_repository;
|
||||||
private $sut;
|
private $sut;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
|
@ -31,10 +32,16 @@ class IdentityTokenTest extends TestCase
|
||||||
$this->host = 'https://example.com/';
|
$this->host = 'https://example.com/';
|
||||||
$this->bearer = Mockery::mock(Bearer::class);
|
$this->bearer = Mockery::mock(Bearer::class);
|
||||||
$this->logger = Mockery::mock(LoggerInterface::class);
|
$this->logger = Mockery::mock(LoggerInterface::class);
|
||||||
$this->prefix = 'prefix';
|
|
||||||
$this->settings = Mockery::mock(Settings::class);
|
$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()
|
public function testGenerateForCustomerReturnsToken()
|
||||||
|
@ -52,6 +59,7 @@ class IdentityTokenTest extends TestCase
|
||||||
$this->logger->shouldReceive('debug');
|
$this->logger->shouldReceive('debug');
|
||||||
$this->settings->shouldReceive('has')->andReturn(true);
|
$this->settings->shouldReceive('has')->andReturn(true);
|
||||||
$this->settings->shouldReceive('get')->andReturn(true);
|
$this->settings->shouldReceive('get')->andReturn(true);
|
||||||
|
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('prefix1');
|
||||||
|
|
||||||
$rawResponse = [
|
$rawResponse = [
|
||||||
'body' => '{"client_token":"abc123", "expires_in":3600}',
|
'body' => '{"client_token":"abc123", "expires_in":3600}',
|
||||||
|
@ -82,8 +90,9 @@ class IdentityTokenTest extends TestCase
|
||||||
expect('is_wp_error')->with($rawResponse)->andReturn(false);
|
expect('is_wp_error')->with($rawResponse)->andReturn(false);
|
||||||
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
|
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
|
||||||
when('wc_print_r')->returnArg();
|
when('wc_print_r')->returnArg();
|
||||||
|
when('get_user_meta')->justReturn('');
|
||||||
|
|
||||||
$result = $this->sut->generate_for_customer(1);
|
$result = $this->sut->generate_for_user(1);
|
||||||
$this->assertInstanceOf(Token::class, $result);
|
$this->assertInstanceOf(Token::class, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,9 +113,10 @@ class IdentityTokenTest extends TestCase
|
||||||
$this->logger->shouldReceive('debug');
|
$this->logger->shouldReceive('debug');
|
||||||
$this->settings->shouldReceive('has')->andReturn(true);
|
$this->settings->shouldReceive('has')->andReturn(true);
|
||||||
$this->settings->shouldReceive('get')->andReturn(true);
|
$this->settings->shouldReceive('get')->andReturn(true);
|
||||||
|
$this->customer_repository->shouldReceive('customer_id_for_user');
|
||||||
|
|
||||||
$this->expectException(RuntimeException::class);
|
$this->expectException(RuntimeException::class);
|
||||||
$this->sut->generate_for_customer(1);
|
$this->sut->generate_for_user(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGenerateForCustomerFailsBecauseResponseCodeIsNot200()
|
public function testGenerateForCustomerFailsBecauseResponseCodeIsNot200()
|
||||||
|
@ -130,8 +140,9 @@ class IdentityTokenTest extends TestCase
|
||||||
$this->logger->shouldReceive('debug');
|
$this->logger->shouldReceive('debug');
|
||||||
$this->settings->shouldReceive('has')->andReturn(true);
|
$this->settings->shouldReceive('has')->andReturn(true);
|
||||||
$this->settings->shouldReceive('get')->andReturn(true);
|
$this->settings->shouldReceive('get')->andReturn(true);
|
||||||
|
$this->customer_repository->shouldReceive('customer_id_for_user');
|
||||||
|
|
||||||
$this->expectException(PayPalApiException::class);
|
$this->expectException(PayPalApiException::class);
|
||||||
$this->sut->generate_for_customer(1);
|
$this->sut->generate_for_user(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\CustomerRepository;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
use WooCommerce\PayPalCommerce\ApiClient\TestCase;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
||||||
|
@ -24,7 +25,7 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
private $bearer;
|
private $bearer;
|
||||||
private $factory;
|
private $factory;
|
||||||
private $logger;
|
private $logger;
|
||||||
private $prefix;
|
private $customer_repository;
|
||||||
private $sut;
|
private $sut;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
|
@ -35,13 +36,13 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
$this->bearer = Mockery::mock(Bearer::class);
|
$this->bearer = Mockery::mock(Bearer::class);
|
||||||
$this->factory = Mockery::mock(PaymentTokenFactory::class);
|
$this->factory = Mockery::mock(PaymentTokenFactory::class);
|
||||||
$this->logger = Mockery::mock(LoggerInterface::class);
|
$this->logger = Mockery::mock(LoggerInterface::class);
|
||||||
$this->prefix = 'prefix';
|
$this->customer_repository = Mockery::mock(CustomerRepository::class);
|
||||||
$this->sut = new PaymentTokenEndpoint(
|
$this->sut = new PaymentTokenEndpoint(
|
||||||
$this->host,
|
$this->host,
|
||||||
$this->bearer,
|
$this->bearer,
|
||||||
$this->factory,
|
$this->factory,
|
||||||
$this->logger,
|
$this->logger,
|
||||||
$this->prefix
|
$this->customer_repository
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
$token->shouldReceive('token')
|
$token->shouldReceive('token')
|
||||||
->andReturn('bearer');
|
->andReturn('bearer');
|
||||||
|
|
||||||
$this->ensureRequestForUser($rawResponse, $id);
|
$this->ensureRequestForUser($rawResponse, '123abc');
|
||||||
expect('is_wp_error')->with($rawResponse)->andReturn(false);
|
expect('is_wp_error')->with($rawResponse)->andReturn(false);
|
||||||
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
|
expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(200);
|
||||||
|
|
||||||
|
@ -73,6 +74,8 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
|
|
||||||
$this->logger->shouldReceive('debug');
|
$this->logger->shouldReceive('debug');
|
||||||
|
|
||||||
|
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('123abc');
|
||||||
|
|
||||||
$result = $this->sut->for_user($id);
|
$result = $this->sut->for_user($id);
|
||||||
$this->assertInstanceOf(PaymentToken::class, $result[0]);
|
$this->assertInstanceOf(PaymentToken::class, $result[0]);
|
||||||
|
|
||||||
|
@ -89,13 +92,15 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
->andReturn($token);
|
->andReturn($token);
|
||||||
$token->shouldReceive('token')
|
$token->shouldReceive('token')
|
||||||
->andReturn('bearer');
|
->andReturn('bearer');
|
||||||
$this->ensureRequestForUser($rawResponse, $id);
|
$this->ensureRequestForUser($rawResponse, '123abc');
|
||||||
|
|
||||||
expect('wp_remote_get')->andReturn($rawResponse);
|
expect('wp_remote_get')->andReturn($rawResponse);
|
||||||
expect('is_wp_error')->with($rawResponse)->andReturn(true);
|
expect('is_wp_error')->with($rawResponse)->andReturn(true);
|
||||||
$this->logger->shouldReceive('log');
|
$this->logger->shouldReceive('log');
|
||||||
$this->logger->shouldReceive('debug');
|
$this->logger->shouldReceive('debug');
|
||||||
|
|
||||||
|
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('123abc');
|
||||||
|
|
||||||
$this->expectException(RuntimeException::class);
|
$this->expectException(RuntimeException::class);
|
||||||
$this->sut->for_user($id);
|
$this->sut->for_user($id);
|
||||||
}
|
}
|
||||||
|
@ -114,7 +119,7 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
->andReturn($token);
|
->andReturn($token);
|
||||||
$token->shouldReceive('token')
|
$token->shouldReceive('token')
|
||||||
->andReturn('bearer');
|
->andReturn('bearer');
|
||||||
$this->ensureRequestForUser($rawResponse, $id);
|
$this->ensureRequestForUser($rawResponse, '123abc');
|
||||||
|
|
||||||
|
|
||||||
expect('wp_remote_get')->andReturn($rawResponse);
|
expect('wp_remote_get')->andReturn($rawResponse);
|
||||||
|
@ -123,6 +128,8 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
$this->logger->shouldReceive('log');
|
$this->logger->shouldReceive('log');
|
||||||
$this->logger->shouldReceive('debug');
|
$this->logger->shouldReceive('debug');
|
||||||
|
|
||||||
|
$this->customer_repository->shouldReceive('customer_id_for_user')->andReturn('123abc');
|
||||||
|
|
||||||
$this->expectException(PayPalApiException::class);
|
$this->expectException(PayPalApiException::class);
|
||||||
$this->sut->for_user($id);
|
$this->sut->for_user($id);
|
||||||
}
|
}
|
||||||
|
@ -179,13 +186,12 @@ class PaymentTokenEndpointTest extends TestCase
|
||||||
* @param int $id
|
* @param int $id
|
||||||
* @throws \Brain\Monkey\Expectation\Exception\ExpectationArgsRequired
|
* @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;
|
$host = $this->host;
|
||||||
$prefix = $this->prefix;
|
|
||||||
expect('wp_remote_get')->andReturnUsing(
|
expect('wp_remote_get')->andReturnUsing(
|
||||||
function ($url, $args) use ($rawResponse, $host, $prefix, $id) {
|
function ($url, $args) use ($rawResponse, $host, $id) {
|
||||||
if ($url !== $host . 'v2/vault/payment-tokens/?customer_id=' . $prefix . $id) {
|
if ($url !== $host . 'v2/vault/payment-tokens/?customer_id=' . $id) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ($args['headers']['Authorization'] !== 'Bearer bearer') {
|
if ($args['headers']['Authorization'] !== 'Bearer bearer') {
|
||||||
|
|
|
@ -34,7 +34,7 @@ class DataClientIdEndpointTest extends TestCase
|
||||||
$this->requestData->shouldReceive('read_request')
|
$this->requestData->shouldReceive('read_request')
|
||||||
->with($this->sut::nonce());
|
->with($this->sut::nonce());
|
||||||
when('get_current_user_id')->justReturn($userId);
|
when('get_current_user_id')->justReturn($userId);
|
||||||
$this->identityToken->shouldReceive('generate_for_customer')
|
$this->identityToken->shouldReceive('generate_for_user')
|
||||||
->with($userId)
|
->with($userId)
|
||||||
->andReturn($token);
|
->andReturn($token);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue