mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Cache partner referrals signup links
This commit is contained in:
parent
12a8596261
commit
f813cf451a
5 changed files with 56 additions and 11 deletions
|
@ -211,16 +211,17 @@ return array(
|
|||
);
|
||||
},
|
||||
'onboarding.render' => static function ( ContainerInterface $container ) : OnboardingRenderer {
|
||||
|
||||
$partner_referrals = $container->get( 'api.endpoint.partner-referrals-production' );
|
||||
$partner_referrals_sandbox = $container->get( 'api.endpoint.partner-referrals-sandbox' );
|
||||
$partner_referrals_data = $container->get( 'api.repository.partner-referrals-data' );
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
$cache = new Cache( 'ppcp-paypal-signup-link' );
|
||||
return new OnboardingRenderer(
|
||||
$settings,
|
||||
$partner_referrals,
|
||||
$partner_referrals_sandbox,
|
||||
$partner_referrals_data
|
||||
$partner_referrals_data,
|
||||
$cache
|
||||
);
|
||||
},
|
||||
'onboarding.render-options' => static function ( ContainerInterface $container ) : OnboardingOptionsRenderer {
|
||||
|
|
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Onboarding\Render;
|
|||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\PartnerReferralsData;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
|
@ -47,6 +48,13 @@ class OnboardingRenderer {
|
|||
*/
|
||||
private $partner_referrals_data;
|
||||
|
||||
/**
|
||||
* The cache
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* OnboardingRenderer constructor.
|
||||
*
|
||||
|
@ -54,17 +62,20 @@ class OnboardingRenderer {
|
|||
* @param PartnerReferrals $production_partner_referrals The PartnerReferrals for production.
|
||||
* @param PartnerReferrals $sandbox_partner_referrals The PartnerReferrals for sandbox.
|
||||
* @param PartnerReferralsData $partner_referrals_data The default partner referrals data.
|
||||
* @param Cache $cache The cache.
|
||||
*/
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
PartnerReferrals $production_partner_referrals,
|
||||
PartnerReferrals $sandbox_partner_referrals,
|
||||
PartnerReferralsData $partner_referrals_data
|
||||
PartnerReferralsData $partner_referrals_data,
|
||||
Cache $cache
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->production_partner_referrals = $production_partner_referrals;
|
||||
$this->sandbox_partner_referrals = $sandbox_partner_referrals;
|
||||
$this->partner_referrals_data = $partner_referrals_data;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,9 +94,17 @@ class OnboardingRenderer {
|
|||
->with_products( $products )
|
||||
->data();
|
||||
|
||||
$environment = $is_production ? 'production' : 'sandbox';
|
||||
$product = isset( $data['products']['PPCP'] ) ? 'ppcp' : 'express-checkout';
|
||||
if ( $this->cache->has( $environment . '-' . $product ) ) {
|
||||
return $this->cache->get( $environment . '-' . $product );
|
||||
}
|
||||
|
||||
$url = $is_production ? $this->production_partner_referrals->signup_link( $data ) : $this->sandbox_partner_referrals->signup_link( $data );
|
||||
$url = add_query_arg( $args, $url );
|
||||
|
||||
$this->cache->set( $environment . '-' . $product, $url );
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
|
|
|
@ -219,7 +219,8 @@ return array(
|
|||
$cache = new Cache( 'ppcp-paypal-bearer' );
|
||||
$bearer = $container->get( 'api.bearer' );
|
||||
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
|
||||
return new SettingsListener( $settings, $fields, $webhook_registrar, $cache, $state, $bearer, $page_id );
|
||||
$signup_link_cache = new Cache( 'ppcp-paypal-signup-link' );
|
||||
return new SettingsListener( $settings, $fields, $webhook_registrar, $cache, $state, $bearer, $page_id, $signup_link_cache );
|
||||
},
|
||||
'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor {
|
||||
|
||||
|
@ -2215,7 +2216,7 @@ return array(
|
|||
);
|
||||
},
|
||||
|
||||
'wcgateway.helper.vaulting-scope' => static function ( ContainerInterface $container ): bool {
|
||||
'wcgateway.helper.vaulting-scope' => static function ( ContainerInterface $container ): bool {
|
||||
try {
|
||||
$token = $container->get( 'api.bearer' )->bearer();
|
||||
return $token->vaulting_available();
|
||||
|
@ -2224,7 +2225,7 @@ return array(
|
|||
}
|
||||
},
|
||||
|
||||
'button.helper.vaulting-label' => static function ( ContainerInterface $container ): string {
|
||||
'button.helper.vaulting-label' => static function ( ContainerInterface $container ): string {
|
||||
$vaulting_label = __( 'Enable saved cards and subscription features on your store.', 'woocommerce-paypal-payments' );
|
||||
|
||||
if ( ! $container->get( 'wcgateway.helper.vaulting-scope' ) ) {
|
||||
|
@ -2246,7 +2247,7 @@ return array(
|
|||
return $vaulting_label;
|
||||
},
|
||||
|
||||
'wcgateway.settings.fields.pay-later-label' => static function ( ContainerInterface $container ): string {
|
||||
'wcgateway.settings.fields.pay-later-label' => static function ( ContainerInterface $container ): string {
|
||||
$pay_later_label = '<span class="ppcp-pay-later-enabled-label">%s</span>';
|
||||
$pay_later_label .= '<span class="ppcp-pay-later-disabled-label">';
|
||||
$pay_later_label .= __( "You have PayPal vaulting enabled, that's why Pay Later Messaging options are unavailable now. You cannot use both features at the same time.", 'woocommerce-paypal-payments' );
|
||||
|
|
|
@ -81,6 +81,13 @@ class SettingsListener {
|
|||
*/
|
||||
protected $page_id;
|
||||
|
||||
/**
|
||||
* The signup link cache.
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $signup_link_cache;
|
||||
|
||||
/**
|
||||
* SettingsListener constructor.
|
||||
*
|
||||
|
@ -91,6 +98,7 @@ class SettingsListener {
|
|||
* @param State $state The state.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page.
|
||||
* @param Cache $signup_link_cache The signup link cache.
|
||||
*/
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
|
@ -99,7 +107,8 @@ class SettingsListener {
|
|||
Cache $cache,
|
||||
State $state,
|
||||
Bearer $bearer,
|
||||
string $page_id
|
||||
string $page_id,
|
||||
Cache $signup_link_cache
|
||||
) {
|
||||
|
||||
$this->settings = $settings;
|
||||
|
@ -109,6 +118,7 @@ class SettingsListener {
|
|||
$this->state = $state;
|
||||
$this->bearer = $bearer;
|
||||
$this->page_id = $page_id;
|
||||
$this->signup_link_cache = $signup_link_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -260,6 +270,18 @@ class SettingsListener {
|
|||
true
|
||||
) ) {
|
||||
$this->webhook_registrar->unregister();
|
||||
|
||||
$keys = array(
|
||||
'production-ppcp',
|
||||
'production-express-checkout',
|
||||
'sandbox-ppcp',
|
||||
'sandbox-express-checkout',
|
||||
);
|
||||
foreach ( $keys as $key ) {
|
||||
if ( $this->signup_link_cache->has( $key ) ) {
|
||||
$this->signup_link_cache->delete( $key );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,12 +32,11 @@ class SettingsListenerTest extends ModularTestCase
|
|||
$webhook_registrar = Mockery::mock(WebhookRegistrar::class);
|
||||
$webhook_registrar->shouldReceive('unregister')->andReturnTrue();
|
||||
$webhook_registrar->shouldReceive('register')->andReturnTrue();
|
||||
|
||||
$cache = Mockery::mock(Cache::class);
|
||||
|
||||
$state = Mockery::mock(State::class);
|
||||
$state->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
|
||||
$bearer = Mockery::mock(Bearer::class);
|
||||
$signup_link_cache = Mockery::mock(Cache::class);
|
||||
|
||||
$testee = new SettingsListener(
|
||||
$settings,
|
||||
|
@ -46,7 +45,8 @@ class SettingsListenerTest extends ModularTestCase
|
|||
$cache,
|
||||
$state,
|
||||
$bearer,
|
||||
PayPalGateway::ID
|
||||
PayPalGateway::ID,
|
||||
$signup_link_cache
|
||||
);
|
||||
|
||||
$_GET['section'] = PayPalGateway::ID;
|
||||
|
@ -74,6 +74,8 @@ class SettingsListenerTest extends ModularTestCase
|
|||
$settings->shouldReceive('persist');
|
||||
$cache->shouldReceive('has')
|
||||
->andReturn(false);
|
||||
$signup_link_cache->shouldReceive('has')
|
||||
->andReturn(false);
|
||||
|
||||
$testee->listen();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue