Extract funding source name/description generation

This commit is contained in:
Alex P 2021-12-09 18:31:21 +02:00
parent 222406aad6
commit 9ef61a2b81
4 changed files with 88 additions and 3 deletions

View file

@ -24,6 +24,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Admin\RenderAuthorizeAction;
use WooCommerce\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use WooCommerce\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset;
use WooCommerce\PayPalCommerce\WcGateway\Checkout\DisableGateways; use WooCommerce\PayPalCommerce\WcGateway\Checkout\DisableGateways;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
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 WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider; use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider;
@ -45,6 +46,7 @@ return array(
'wcgateway.paypal-gateway' => static function ( ContainerInterface $container ): PayPalGateway { 'wcgateway.paypal-gateway' => static function ( ContainerInterface $container ): PayPalGateway {
$order_processor = $container->get( 'wcgateway.order-processor' ); $order_processor = $container->get( 'wcgateway.order-processor' );
$settings_renderer = $container->get( 'wcgateway.settings.render' ); $settings_renderer = $container->get( 'wcgateway.settings.render' );
$funding_source_renderer = $container->get( 'wcgateway.funding-source.renderer' );
$authorized_payments = $container->get( 'wcgateway.processor.authorized-payments' ); $authorized_payments = $container->get( 'wcgateway.processor.authorized-payments' );
$settings = $container->get( 'wcgateway.settings' ); $settings = $container->get( 'wcgateway.settings' );
$session_handler = $container->get( 'session.handler' ); $session_handler = $container->get( 'session.handler' );
@ -60,6 +62,7 @@ return array(
$logger = $container->get( 'woocommerce.logger.woocommerce' ); $logger = $container->get( 'woocommerce.logger.woocommerce' );
return new PayPalGateway( return new PayPalGateway(
$settings_renderer, $settings_renderer,
$funding_source_renderer,
$order_processor, $order_processor,
$authorized_payments, $authorized_payments,
$settings, $settings,
@ -2030,4 +2033,10 @@ return array(
$container->get( 'api.shop.country' ) $container->get( 'api.shop.country' )
); );
}, },
'wcgateway.funding-source.renderer' => function ( ContainerInterface $container ) : FundingSourceRenderer {
return new FundingSourceRenderer(
$container->get( 'wcgateway.settings' )
);
},
); );

View file

@ -0,0 +1,61 @@
<?php
/**
* Renders info about funding sources like Venmo.
*
* @package WooCommerce\PayPalCommerce\WcGateway\FundingSource
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\FundingSource;
use Psr\Container\ContainerInterface;
/**
* Class FundingSourceRenderer
*/
class FundingSourceRenderer {
/**
* The settings.
*
* @var ContainerInterface
*/
protected $settings;
/**
* FundingSourceRenderer constructor.
*
* @param ContainerInterface $settings The settings.
*/
public function __construct( ContainerInterface $settings ) {
$this->settings = $settings;
}
/**
* Returns name of the funding source (suitable for displaying to user).
*
* @param string $id The ID of the funding source, such as 'venmo'.
*/
public function render_name( string $id ): string {
if ( 'venmo' === $id ) {
return __( 'Venmo', 'woocommerce-paypal-payments' );
}
return $this->settings->has( 'title' ) ?
$this->settings->get( 'title' )
: __( 'PayPal', 'woocommerce-paypal-payments' );
}
/**
* Returns description of the funding source (for checkout).
*
* @param string $id The ID of the funding source, such as 'venmo'.
*/
public function render_description( string $id ): string {
if ( 'venmo' === $id ) {
return __( 'Pay via Venmo.', 'woocommerce-paypal-payments' );
}
return $this->settings->has( 'description' ) ?
$this->settings->get( 'description' )
: __( 'Pay via PayPal.', 'woocommerce-paypal-payments' );
}
}

View file

@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
@ -44,6 +45,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/ */
protected $settings_renderer; protected $settings_renderer;
/**
* The funding source renderer.
*
* @var FundingSourceRenderer
*/
protected $funding_source_renderer;
/** /**
* The processor for orders. * The processor for orders.
* *
@ -153,6 +161,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
* PayPalGateway constructor. * PayPalGateway constructor.
* *
* @param SettingsRenderer $settings_renderer The Settings Renderer. * @param SettingsRenderer $settings_renderer The Settings Renderer.
* @param FundingSourceRenderer $funding_source_renderer The funding source renderer.
* @param OrderProcessor $order_processor The Order Processor. * @param OrderProcessor $order_processor The Order Processor.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor. * @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
* @param ContainerInterface $config The settings. * @param ContainerInterface $config The settings.
@ -170,6 +179,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/ */
public function __construct( public function __construct(
SettingsRenderer $settings_renderer, SettingsRenderer $settings_renderer,
FundingSourceRenderer $funding_source_renderer,
OrderProcessor $order_processor, OrderProcessor $order_processor,
AuthorizedPaymentsProcessor $authorized_payments_processor, AuthorizedPaymentsProcessor $authorized_payments_processor,
ContainerInterface $config, ContainerInterface $config,
@ -190,6 +200,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->order_processor = $order_processor; $this->order_processor = $order_processor;
$this->authorized_payments_processor = $authorized_payments_processor; $this->authorized_payments_processor = $authorized_payments_processor;
$this->settings_renderer = $settings_renderer; $this->settings_renderer = $settings_renderer;
$this->funding_source_renderer = $funding_source_renderer;
$this->config = $config; $this->config = $config;
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
$this->refund_processor = $refund_processor; $this->refund_processor = $refund_processor;
@ -242,9 +253,9 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->config->get( 'description' ) : $this->method_description; $this->config->get( 'description' ) : $this->method_description;
$funding_source = $this->session_handler->funding_source(); $funding_source = $this->session_handler->funding_source();
if ( 'venmo' === $funding_source ) { if ( $funding_source ) {
$this->title = 'Venmo'; $this->title = $this->funding_source_renderer->render_name( $funding_source );
$this->description = __( 'Pay via Venmo.', 'woocommerce-paypal-payments' ); $this->description = $this->funding_source_renderer->render_description( $funding_source );
} }
$this->init_form_fields(); $this->init_form_fields();

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
@ -33,6 +34,7 @@ class WcGatewayTest extends TestCase
private $fundingSource = null; private $fundingSource = null;
private $settingsRenderer; private $settingsRenderer;
private $funding_source_renderer;
private $orderProcessor; private $orderProcessor;
private $authorizedOrdersProcessor; private $authorizedOrdersProcessor;
private $settings; private $settings;
@ -67,6 +69,7 @@ class WcGatewayTest extends TestCase
$this->logger = Mockery::mock(LoggerInterface::class); $this->logger = Mockery::mock(LoggerInterface::class);
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class); $this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class); $this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->funding_source_renderer = new FundingSourceRenderer($this->settings);
$this->onboardingState->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED); $this->onboardingState->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
@ -85,6 +88,7 @@ class WcGatewayTest extends TestCase
{ {
return new PayPalGateway( return new PayPalGateway(
$this->settingsRenderer, $this->settingsRenderer,
$this->funding_source_renderer,
$this->orderProcessor, $this->orderProcessor,
$this->authorizedOrdersProcessor, $this->authorizedOrdersProcessor,
$this->settings, $this->settings,