mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
♻️ Use EnvironmentConfig in ConnectionUrlGenerator
This commit is contained in:
parent
a1f80f1d3d
commit
3f691bea01
6 changed files with 54 additions and 85 deletions
|
@ -896,4 +896,12 @@ return array(
|
||||||
$container->get( 'api.endpoint.login-seller-sandbox' )
|
$container->get( 'api.endpoint.login-seller-sandbox' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
'api.env.endpoint.partner-referrals' => static function ( ContainerInterface $container ) : EnvironmentConfig {
|
||||||
|
/** @type EnvironmentConfig<PartnerReferrals> Configuration object */
|
||||||
|
return EnvironmentConfig::create(
|
||||||
|
PartnerReferrals::class,
|
||||||
|
$container->get( 'api.endpoint.partner-referrals-production' ),
|
||||||
|
$container->get( 'api.endpoint.partner-referrals-sandbox' )
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -153,7 +153,7 @@ export const persist = function* () {
|
||||||
export const sandboxOnboardingUrl = function* () {
|
export const sandboxOnboardingUrl = function* () {
|
||||||
return yield {
|
return yield {
|
||||||
type: ACTION_TYPES.DO_GENERATE_ONBOARDING_URL,
|
type: ACTION_TYPES.DO_GENERATE_ONBOARDING_URL,
|
||||||
environment: 'sandbox',
|
useSandbox: true,
|
||||||
products: [ 'EXPRESS_CHECKOUT' ],
|
products: [ 'EXPRESS_CHECKOUT' ],
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -167,7 +167,7 @@ export const sandboxOnboardingUrl = function* () {
|
||||||
export const productionOnboardingUrl = function* ( products = [] ) {
|
export const productionOnboardingUrl = function* ( products = [] ) {
|
||||||
return yield {
|
return yield {
|
||||||
type: ACTION_TYPES.DO_GENERATE_ONBOARDING_URL,
|
type: ACTION_TYPES.DO_GENERATE_ONBOARDING_URL,
|
||||||
environment: 'production',
|
useSandbox: false,
|
||||||
products,
|
products,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,13 +36,13 @@ export const controls = {
|
||||||
|
|
||||||
async [ ACTION_TYPES.DO_GENERATE_ONBOARDING_URL ]( {
|
async [ ACTION_TYPES.DO_GENERATE_ONBOARDING_URL ]( {
|
||||||
products,
|
products,
|
||||||
environment,
|
useSandbox,
|
||||||
} ) {
|
} ) {
|
||||||
try {
|
try {
|
||||||
return apiFetch( {
|
return apiFetch( {
|
||||||
path: REST_CONNECTION_URL_PATH,
|
path: REST_CONNECTION_URL_PATH,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: { environment, products },
|
data: { useSandbox, products },
|
||||||
} );
|
} );
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -87,7 +87,7 @@ return array(
|
||||||
},
|
},
|
||||||
'settings.rest.login_link' => static function ( ContainerInterface $container ) : LoginLinkRestEndpoint {
|
'settings.rest.login_link' => static function ( ContainerInterface $container ) : LoginLinkRestEndpoint {
|
||||||
return new LoginLinkRestEndpoint(
|
return new LoginLinkRestEndpoint(
|
||||||
$container->get( 'settings.service.connection-url-generators' ),
|
$container->get( 'settings.service.connection-url-generator' ),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'settings.rest.webhooks' => static function ( ContainerInterface $container ) : WebhookSettingsEndpoint {
|
'settings.rest.webhooks' => static function ( ContainerInterface $container ) : WebhookSettingsEndpoint {
|
||||||
|
@ -172,31 +172,13 @@ return array(
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$container->get( 'woocommerce.logger.woocommerce' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'settings.service.connection-url-generators' => static function ( ContainerInterface $container ) : array {
|
'settings.service.connection-url-generator' => static function ( ContainerInterface $container ) : ConnectionUrlGenerator {
|
||||||
// Define available environments.
|
return new ConnectionUrlGenerator(
|
||||||
$environments = array(
|
$container->get( 'api.env.endpoint.partner-referrals' ),
|
||||||
'production' => array(
|
$container->get( 'api.repository.partner-referrals-data' ),
|
||||||
'partner_referrals' => $container->get( 'api.endpoint.partner-referrals-production' ),
|
$container->get( 'settings.service.onboarding-url-manager' ),
|
||||||
),
|
$container->get( 'woocommerce.logger.woocommerce' )
|
||||||
'sandbox' => array(
|
|
||||||
'partner_referrals' => $container->get( 'api.endpoint.partner-referrals-sandbox' ),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$generators = array();
|
|
||||||
|
|
||||||
// Instantiate URL generators for each environment.
|
|
||||||
foreach ( $environments as $environment => $config ) {
|
|
||||||
$generators[ $environment ] = new ConnectionUrlGenerator(
|
|
||||||
$config['partner_referrals'],
|
|
||||||
$container->get( 'api.repository.partner-referrals-data' ),
|
|
||||||
$environment,
|
|
||||||
$container->get( 'settings.service.onboarding-url-manager' ),
|
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $generators;
|
|
||||||
},
|
},
|
||||||
'settings.service.connection_manager' => static function ( ContainerInterface $container ) : ConnectionManager {
|
'settings.service.connection_manager' => static function ( ContainerInterface $container ) : ConnectionManager {
|
||||||
return new ConnectionManager(
|
return new ConnectionManager(
|
||||||
|
|
|
@ -33,25 +33,25 @@ class LoginLinkRestEndpoint extends RestEndpoint {
|
||||||
protected $rest_base = 'login_link';
|
protected $rest_base = 'login_link';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Link generator list, with environment name as array key.
|
* Login-URL generator.
|
||||||
*
|
*
|
||||||
* @var ConnectionUrlGenerator[]
|
* @var ConnectionUrlGenerator
|
||||||
*/
|
*/
|
||||||
protected array $url_generators;
|
protected ConnectionUrlGenerator $url_generator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param ConnectionUrlGenerator[] $url_generators Array of environment-specific URL generators.
|
* @param ConnectionUrlGenerator $url_generator Login-URL generator.
|
||||||
*/
|
*/
|
||||||
public function __construct( array $url_generators ) {
|
public function __construct( ConnectionUrlGenerator $url_generator ) {
|
||||||
$this->url_generators = $url_generators;
|
$this->url_generator = $url_generator;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure REST API routes.
|
* Configure REST API routes.
|
||||||
*/
|
*/
|
||||||
public function register_routes() {
|
public function register_routes() : void {
|
||||||
register_rest_route(
|
register_rest_route(
|
||||||
$this->namespace,
|
$this->namespace,
|
||||||
'/' . $this->rest_base,
|
'/' . $this->rest_base,
|
||||||
|
@ -60,11 +60,12 @@ class LoginLinkRestEndpoint extends RestEndpoint {
|
||||||
'callback' => array( $this, 'get_login_url' ),
|
'callback' => array( $this, 'get_login_url' ),
|
||||||
'permission_callback' => array( $this, 'check_permission' ),
|
'permission_callback' => array( $this, 'check_permission' ),
|
||||||
'args' => array(
|
'args' => array(
|
||||||
'environment' => array(
|
'useSandbox' => array(
|
||||||
'required' => true,
|
'default' => 0,
|
||||||
'type' => 'string',
|
'type' => 'boolean',
|
||||||
|
'sanitize_callback' => array( $this, 'to_boolean' ),
|
||||||
),
|
),
|
||||||
'products' => array(
|
'products' => array(
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'type' => 'array',
|
'type' => 'array',
|
||||||
'items' => array(
|
'items' => array(
|
||||||
|
@ -87,20 +88,11 @@ class LoginLinkRestEndpoint extends RestEndpoint {
|
||||||
* @return WP_REST_Response The login URL or an error response.
|
* @return WP_REST_Response The login URL or an error response.
|
||||||
*/
|
*/
|
||||||
public function get_login_url( WP_REST_Request $request ) : WP_REST_Response {
|
public function get_login_url( WP_REST_Request $request ) : WP_REST_Response {
|
||||||
$environment = $request->get_param( 'environment' );
|
$use_sandbox = $request->get_param( 'useSandbox' );
|
||||||
$products = $request->get_param( 'products' );
|
$products = $request->get_param( 'products' );
|
||||||
|
|
||||||
if ( ! isset( $this->url_generators[ $environment ] ) ) {
|
|
||||||
return new WP_REST_Response(
|
|
||||||
array( 'error' => 'Invalid environment specified.' ),
|
|
||||||
400
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$url_generator = $this->url_generators[ $environment ];
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$url = $url_generator->generate( $products );
|
$url = $this->url_generator->generate( $products, $use_sandbox );
|
||||||
|
|
||||||
return $this->return_success( $url );
|
return $this->return_success( $url );
|
||||||
} catch ( \Exception $e ) {
|
} catch ( \Exception $e ) {
|
||||||
|
|
|
@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
|
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
|
||||||
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
|
use WooCommerce\WooCommerce\Logging\Logger\NullLogger;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Helper\EnvironmentConfig;
|
||||||
|
|
||||||
// TODO: Replace the OnboardingUrl with a new implementation for this module.
|
// TODO: Replace the OnboardingUrl with a new implementation for this module.
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Helper\OnboardingUrl;
|
use WooCommerce\PayPalCommerce\Onboarding\Helper\OnboardingUrl;
|
||||||
|
@ -25,9 +26,9 @@ class ConnectionUrlGenerator {
|
||||||
/**
|
/**
|
||||||
* The partner referrals endpoint.
|
* The partner referrals endpoint.
|
||||||
*
|
*
|
||||||
* @var PartnerReferrals
|
* @var EnvironmentConfig<PartnerReferrals>
|
||||||
*/
|
*/
|
||||||
protected PartnerReferrals $partner_referrals;
|
protected EnvironmentConfig $partner_referrals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default partner referrals data.
|
* The default partner referrals data.
|
||||||
|
@ -43,13 +44,6 @@ class ConnectionUrlGenerator {
|
||||||
*/
|
*/
|
||||||
protected OnboardingUrlManager $url_manager;
|
protected OnboardingUrlManager $url_manager;
|
||||||
|
|
||||||
/**
|
|
||||||
* Which environment is used for the connection URL.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected string $environment = '';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger
|
* The logger
|
||||||
*
|
*
|
||||||
|
@ -62,36 +56,23 @@ class ConnectionUrlGenerator {
|
||||||
*
|
*
|
||||||
* Initializes the cache and logger properties of the class.
|
* Initializes the cache and logger properties of the class.
|
||||||
*
|
*
|
||||||
* @param PartnerReferrals $partner_referrals PartnerReferrals for URL generation.
|
* @param EnvironmentConfig $partner_referrals PartnerReferrals for URL generation.
|
||||||
* @param PartnerReferralsData $referrals_data Default partner referrals data.
|
* @param PartnerReferralsData $referrals_data Default partner referrals data.
|
||||||
* @param string $environment Environment that is used to generate the URL.
|
|
||||||
* ['production'|'sandbox'].
|
|
||||||
* @param OnboardingUrlManager $url_manager Manages access to OnboardingUrl instances.
|
* @param OnboardingUrlManager $url_manager Manages access to OnboardingUrl instances.
|
||||||
* @param ?LoggerInterface $logger The logger object for logging messages.
|
* @param ?LoggerInterface $logger The logger object for logging messages.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PartnerReferrals $partner_referrals,
|
EnvironmentConfig $partner_referrals,
|
||||||
PartnerReferralsData $referrals_data,
|
PartnerReferralsData $referrals_data,
|
||||||
string $environment,
|
|
||||||
OnboardingUrlManager $url_manager,
|
OnboardingUrlManager $url_manager,
|
||||||
?LoggerInterface $logger = null
|
?LoggerInterface $logger = null
|
||||||
) {
|
) {
|
||||||
$this->partner_referrals = $partner_referrals;
|
$this->partner_referrals = $partner_referrals;
|
||||||
$this->referrals_data = $referrals_data;
|
$this->referrals_data = $referrals_data;
|
||||||
$this->environment = $environment;
|
|
||||||
$this->url_manager = $url_manager;
|
$this->url_manager = $url_manager;
|
||||||
$this->logger = $logger ?: new NullLogger();
|
$this->logger = $logger ?: new NullLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the environment for which the URL is being generated.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function environment() : string {
|
|
||||||
return $this->environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a PayPal onboarding URL for merchant sign-up.
|
* Generates a PayPal onboarding URL for merchant sign-up.
|
||||||
*
|
*
|
||||||
|
@ -99,13 +80,14 @@ class ConnectionUrlGenerator {
|
||||||
* It handles caching of the URL, generation of new URLs when necessary,
|
* It handles caching of the URL, generation of new URLs when necessary,
|
||||||
* and works for both production and sandbox environments.
|
* and works for both production and sandbox environments.
|
||||||
*
|
*
|
||||||
* @param array $products An array of product identifiers to include in the sign-up process.
|
* @param array $products An array of product identifiers to include in the sign-up process.
|
||||||
* These determine the PayPal onboarding experience.
|
* These determine the PayPal onboarding experience.
|
||||||
|
* @param bool $use_sandbox Whether to generate a sandbox URL.
|
||||||
*
|
*
|
||||||
* @return string The generated PayPal onboarding URL.
|
* @return string The generated PayPal onboarding URL.
|
||||||
*/
|
*/
|
||||||
public function generate( array $products = array() ) : string {
|
public function generate( array $products = array(), bool $use_sandbox = false ) : string {
|
||||||
$cache_key = $this->cache_key( $products );
|
$cache_key = $this->cache_key( $products, $use_sandbox );
|
||||||
$user_id = get_current_user_id();
|
$user_id = get_current_user_id();
|
||||||
$onboarding_url = $this->url_manager->get( $cache_key, $user_id );
|
$onboarding_url = $this->url_manager->get( $cache_key, $user_id );
|
||||||
$cached_url = $this->try_get_from_cache( $onboarding_url, $cache_key );
|
$cached_url = $this->try_get_from_cache( $onboarding_url, $cache_key );
|
||||||
|
@ -118,7 +100,7 @@ class ConnectionUrlGenerator {
|
||||||
|
|
||||||
$this->logger->info( 'Generating onboarding URL for: ' . $cache_key );
|
$this->logger->info( 'Generating onboarding URL for: ' . $cache_key );
|
||||||
|
|
||||||
$url = $this->generate_new_url( $products, $onboarding_url, $cache_key );
|
$url = $this->generate_new_url( $use_sandbox, $products, $onboarding_url, $cache_key );
|
||||||
|
|
||||||
if ( $url ) {
|
if ( $url ) {
|
||||||
$this->persist_url( $onboarding_url, $url );
|
$this->persist_url( $onboarding_url, $url );
|
||||||
|
@ -130,15 +112,18 @@ class ConnectionUrlGenerator {
|
||||||
/**
|
/**
|
||||||
* Generates a cache key from the environment and sorted product array.
|
* Generates a cache key from the environment and sorted product array.
|
||||||
*
|
*
|
||||||
* @param array $products Product identifiers that are part of the cache key.
|
* @param array $products Product identifiers that are part of the cache key.
|
||||||
|
* @param bool $for_sandbox Whether the cache contains a sandbox URL.
|
||||||
*
|
*
|
||||||
* @return string The cache key, defining the product list and environment.
|
* @return string The cache key, defining the product list and environment.
|
||||||
*/
|
*/
|
||||||
protected function cache_key( array $products = array() ) : string {
|
protected function cache_key( array $products, bool $for_sandbox ) : string {
|
||||||
|
$environment = $for_sandbox ? 'sandbox' : 'production';
|
||||||
|
|
||||||
// Sort products alphabetically, to improve cache implementation.
|
// Sort products alphabetically, to improve cache implementation.
|
||||||
sort( $products );
|
sort( $products );
|
||||||
|
|
||||||
return $this->environment() . '-' . implode( '-', $products );
|
return $environment . '-' . implode( '-', $products );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -167,13 +152,14 @@ class ConnectionUrlGenerator {
|
||||||
/**
|
/**
|
||||||
* Generates a new URL.
|
* Generates a new URL.
|
||||||
*
|
*
|
||||||
|
* @param bool $for_sandbox Whether to generate a sandbox URL.
|
||||||
* @param array $products The products array.
|
* @param array $products The products array.
|
||||||
* @param OnboardingUrl $onboarding_url The OnboardingUrl object.
|
* @param OnboardingUrl $onboarding_url The OnboardingUrl object.
|
||||||
* @param string $cache_key The cache key.
|
* @param string $cache_key The cache key.
|
||||||
*
|
*
|
||||||
* @return string The generated URL or an empty string on failure.
|
* @return string The generated URL or an empty string on failure.
|
||||||
*/
|
*/
|
||||||
protected function generate_new_url( array $products, OnboardingUrl $onboarding_url, string $cache_key ) : string {
|
protected function generate_new_url( bool $for_sandbox, array $products, OnboardingUrl $onboarding_url, string $cache_key ) : string {
|
||||||
$query_args = array( 'displayMode' => 'minibrowser' );
|
$query_args = array( 'displayMode' => 'minibrowser' );
|
||||||
$onboarding_url->init();
|
$onboarding_url->init();
|
||||||
|
|
||||||
|
@ -188,7 +174,8 @@ class ConnectionUrlGenerator {
|
||||||
$data = $this->prepare_referral_data( $products, $onboarding_token );
|
$data = $this->prepare_referral_data( $products, $onboarding_token );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$url = $this->partner_referrals->signup_link( $data );
|
$referral = $this->partner_referrals->get_value( $for_sandbox );
|
||||||
|
$url = $referral->signup_link( $data );
|
||||||
} catch ( Exception $e ) {
|
} catch ( Exception $e ) {
|
||||||
$this->logger->warning( 'Could not generate an onboarding URL for: ' . $cache_key );
|
$this->logger->warning( 'Could not generate an onboarding URL for: ' . $cache_key );
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue