enable loginseller endpoint to switch between sandbox and live

This commit is contained in:
David Remer 2020-09-24 10:29:09 +03:00
parent 6f870ec7a5
commit 09a5f0e466
3 changed files with 63 additions and 18 deletions

View file

@ -1,4 +1,5 @@
function onboardingCallback(authCode, sharedId) {
const sandboxSwitchElement = document.querySelector('#ppcp-sandbox_on')
fetch(
PayPalCommerceGatewayOnboarding.endpoint,
{
@ -10,7 +11,8 @@ function onboardingCallback(authCode, sharedId) {
{
authCode: authCode,
sharedId: sharedId,
nonce: PayPalCommerceGatewayOnboarding.nonce
nonce: PayPalCommerceGatewayOnboarding.nonce,
env: sandboxSwitchElement && sandboxSwitchElement.checked ? 'sandbox' : 'production'
}
)
}

View file

@ -13,6 +13,7 @@ use Dhii\Data\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\ConnectBearer;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Onboarding\Assets\OnboardingAssets;
@ -65,6 +66,12 @@ return array(
? (string) $container->get( 'api.sandbox-host' ) : (string) $container->get( 'api.production-host' );
},
'api.paypal-host-production' => static function( $container ) : string {
return 'https://api.paypal.com';
},
'api.paypal-host-sandbox' => static function( $container ) : string {
return 'https://api.sandbox.paypal.com';
},
'api.paypal-host' => function( $container ) : string {
$environment = $container->get( 'onboarding.environment' );
/**
@ -73,9 +80,10 @@ return array(
* @var Environment $environment
*/
if ( $environment->current_environment_is( Environment::SANDBOX ) ) {
return 'https://api.sandbox.paypal.com';
return $container->get( 'api.paypal-host-sandbox' );
}
return 'https://api.paypal.com';
return $container->get( 'api.paypal-host-production' );
},
'api.bearer' => static function ( $container ): Bearer {
@ -131,17 +139,39 @@ return array(
);
},
'api.endpoint.login-seller-production' => static function ( $container ) : LoginSeller {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new LoginSeller(
$container->get( 'api.paypal-host-production' ),
$container->get( 'api.partner_merchant_id' ),
$logger
);
},
'api.endpoint.login-seller-sandbox' => static function ( $container ) : LoginSeller {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new LoginSeller(
$container->get( 'api.paypal-host-sandbox' ),
$container->get( 'api.partner_merchant_id' ),
$logger
);
},
'onboarding.endpoint.login-seller' => static function ( $container ) : LoginSellerEndpoint {
$request_data = $container->get( 'button.request-data' );
$login_seller = $container->get( 'api.endpoint.login-seller' );
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
$settings = $container->get( 'wcgateway.settings' );
$request_data = $container->get( 'button.request-data' );
$login_seller_production = $container->get( 'api.endpoint.login-seller-production' );
$login_seller_sandbox = $container->get( 'api.endpoint.login-seller-sandbox' );
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
$settings = $container->get( 'wcgateway.settings' );
$cache = new Cache( 'ppcp-paypal-bearer' );
return new LoginSellerEndpoint(
$request_data,
$login_seller,
$login_seller_production,
$login_seller_sandbox,
$partner_referrals_data,
$settings,
$cache

View file

@ -33,11 +33,18 @@ class LoginSellerEndpoint implements EndpointInterface {
private $request_data;
/**
* The Login Seller Endpoint.
* The Login Seller Endpoint for the production environment
*
* @var LoginSeller
*/
private $login_seller_endpoint;
private $login_seller_production;
/**
* The Login Seller Endpoint for the sandbox environment
*
* @var LoginSeller
*/
private $login_seller_sandbox;
/**
* The Partner Referrals Data.
@ -64,24 +71,27 @@ class LoginSellerEndpoint implements EndpointInterface {
* LoginSellerEndpoint constructor.
*
* @param RequestData $request_data The Request Data.
* @param LoginSeller $login_seller The Login Seller.
* @param LoginSeller $login_seller_production The Login Seller for the production environment.
* @param LoginSeller $login_seller_sandbox The Login Seller for the sandbox environment.
* @param PartnerReferralsData $partner_referrals_data The Partner Referrals Data.
* @param Settings $settings The Settings.
* @param Cache $cache The Cache.
*/
public function __construct(
RequestData $request_data,
LoginSeller $login_seller,
LoginSeller $login_seller_production,
LoginSeller $login_seller_sandbox,
PartnerReferralsData $partner_referrals_data,
Settings $settings,
Cache $cache
) {
$this->request_data = $request_data;
$this->login_seller_endpoint = $login_seller;
$this->partner_referrals_data = $partner_referrals_data;
$this->settings = $settings;
$this->cache = $cache;
$this->request_data = $request_data;
$this->login_seller_production = $login_seller_production;
$this->login_seller_sandbox = $login_seller_sandbox;
$this->partner_referrals_data = $partner_referrals_data;
$this->settings = $settings;
$this->cache = $cache;
}
/**
@ -102,13 +112,16 @@ class LoginSellerEndpoint implements EndpointInterface {
try {
$data = $this->request_data->read_request( $this->nonce() );
$credentials = $this->login_seller_endpoint->credentials_for(
$is_sandbox = isset( $data['env'] ) && 'sandbox' === $data['env'];
$endpoint = $is_sandbox ? $this->login_seller_sandbox : $this->login_seller_production;
$credentials = $endpoint->credentials_for(
$data['sharedId'],
$data['authCode'],
$this->partner_referrals_data->nonce()
);
$this->settings->set( 'client_secret', $credentials->client_secret );
$this->settings->set( 'client_id', $credentials->client_id );
$this->settings->set( 'sandbox_on', $is_sandbox );
$this->settings->persist();
if ( $this->cache->has( PayPalBearer::CACHE_KEY ) ) {
$this->cache->delete( PayPalBearer::CACHE_KEY );