mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
Extract disabled funding sources logic into a new class
This commit is contained in:
parent
167c2c6883
commit
43f4d15cea
3 changed files with 124 additions and 47 deletions
|
@ -32,6 +32,7 @@ use WooCommerce\PayPalCommerce\Button\Endpoint\SimulateCartEndpoint;
|
|||
use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Button\Endpoint\ValidateCheckoutEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\DisabledFundingSources;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\PayLaterBlock\PayLaterBlockModule;
|
||||
|
@ -225,6 +226,13 @@ class SmartButton implements SmartButtonInterface {
|
|||
*/
|
||||
private $should_handle_shipping_in_paypal;
|
||||
|
||||
/**
|
||||
* List of funding sources to be disabled.
|
||||
*
|
||||
* @var DisabledFundingSources
|
||||
*/
|
||||
private $disabled_funding_sources;
|
||||
|
||||
/**
|
||||
* SmartButton constructor.
|
||||
*
|
||||
|
@ -251,6 +259,7 @@ class SmartButton implements SmartButtonInterface {
|
|||
* @param PaymentTokensEndpoint $payment_tokens_endpoint Payment tokens endpoint.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param bool $should_handle_shipping_in_paypal Whether the shipping should be handled in PayPal.
|
||||
* @param DisabledFundingSources $disabled_funding_sources List of funding sources to be disabled.
|
||||
*/
|
||||
public function __construct(
|
||||
string $module_url,
|
||||
|
@ -275,7 +284,8 @@ class SmartButton implements SmartButtonInterface {
|
|||
bool $vault_v3_enabled,
|
||||
PaymentTokensEndpoint $payment_tokens_endpoint,
|
||||
LoggerInterface $logger,
|
||||
bool $should_handle_shipping_in_paypal
|
||||
bool $should_handle_shipping_in_paypal,
|
||||
DisabledFundingSources $disabled_funding_sources
|
||||
) {
|
||||
|
||||
$this->module_url = $module_url;
|
||||
|
@ -301,6 +311,7 @@ class SmartButton implements SmartButtonInterface {
|
|||
$this->logger = $logger;
|
||||
$this->payment_tokens_endpoint = $payment_tokens_endpoint;
|
||||
$this->should_handle_shipping_in_paypal = $should_handle_shipping_in_paypal;
|
||||
$this->disabled_funding_sources = $disabled_funding_sources;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1400,54 +1411,18 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages
|
|||
}
|
||||
}
|
||||
|
||||
$disable_funding = $this->settings->has( 'disable_funding' )
|
||||
? $this->settings->get( 'disable_funding' )
|
||||
: array();
|
||||
|
||||
if ( ! is_checkout() ) {
|
||||
$disable_funding[] = 'card';
|
||||
}
|
||||
|
||||
$is_dcc_enabled = $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' );
|
||||
|
||||
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
|
||||
$is_separate_card_enabled = isset( $available_gateways[ CardButtonGateway::ID ] );
|
||||
|
||||
if ( is_checkout() && ( $is_dcc_enabled || $is_separate_card_enabled ) ) {
|
||||
$key = array_search( 'card', $disable_funding, true );
|
||||
if ( false !== $key ) {
|
||||
unset( $disable_funding[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( in_array( $context, array( 'checkout-block', 'cart-block' ), true ) ) {
|
||||
$disable_funding = array_merge(
|
||||
$disable_funding,
|
||||
array_diff(
|
||||
array_keys( $this->all_funding_sources ),
|
||||
array( 'venmo', 'paylater', 'paypal', 'card' )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $this->is_free_trial_cart() ) {
|
||||
$all_sources = array_keys( $this->all_funding_sources );
|
||||
if ( $is_dcc_enabled || $is_separate_card_enabled ) {
|
||||
$all_sources = array_diff( $all_sources, array( 'card' ) );
|
||||
}
|
||||
$disable_funding = $all_sources;
|
||||
}
|
||||
$disabled_funding_sources = $this->disabled_funding_sources->sources( $context );
|
||||
|
||||
$enable_funding = array( 'venmo' );
|
||||
|
||||
if ( $this->is_pay_later_button_enabled_for_location( $context ) ) {
|
||||
$enable_funding[] = 'paylater';
|
||||
} else {
|
||||
$disable_funding[] = 'paylater';
|
||||
$disabled_funding_sources[] = 'paylater';
|
||||
}
|
||||
|
||||
$disable_funding = array_filter(
|
||||
$disable_funding,
|
||||
$disabled_funding_sources = array_filter(
|
||||
$disabled_funding_sources,
|
||||
/**
|
||||
* Make sure paypal is not sent in disable funding.
|
||||
*
|
||||
|
@ -1460,8 +1435,8 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages
|
|||
}
|
||||
);
|
||||
|
||||
if ( count( $disable_funding ) > 0 ) {
|
||||
$params['disable-funding'] = implode( ',', array_unique( $disable_funding ) );
|
||||
if ( count( $disabled_funding_sources ) > 0 ) {
|
||||
$params['disable-funding'] = implode( ',', array_unique( $disabled_funding_sources ) );
|
||||
}
|
||||
|
||||
if ( $this->is_free_trial_cart() ) {
|
||||
|
|
97
modules/ppcp-button/src/Helper/DisabledFundingSources.php
Normal file
97
modules/ppcp-button/src/Helper/DisabledFundingSources.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
/**
|
||||
* Creates the list of disabled funding sources.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Button\Helper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Button\Helper;
|
||||
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\FreeTrialHandlerTrait;
|
||||
|
||||
/**
|
||||
* Class DisabledFundingSources
|
||||
*/
|
||||
class DisabledFundingSources {
|
||||
|
||||
use FreeTrialHandlerTrait;
|
||||
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* All existing funding sources.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $all_funding_sources;
|
||||
|
||||
/**
|
||||
* DisabledFundingSources constructor.
|
||||
*
|
||||
* @param Settings $settings The settings.
|
||||
* @param array $all_funding_sources All existing funding sources.
|
||||
*/
|
||||
public function __construct( Settings $settings, array $all_funding_sources ) {
|
||||
$this->settings = $settings;
|
||||
$this->all_funding_sources = $all_funding_sources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of funding sources to be disabled.
|
||||
*
|
||||
* @param string $context The context.
|
||||
* @return array|int[]|mixed|string[]
|
||||
* @throws NotFoundException When the setting is not found.
|
||||
*/
|
||||
public function sources( $context ) {
|
||||
$disable_funding = $this->settings->has( 'disable_funding' )
|
||||
? $this->settings->get( 'disable_funding' )
|
||||
: array();
|
||||
|
||||
if ( ! is_checkout() ) {
|
||||
$disable_funding[] = 'card';
|
||||
}
|
||||
|
||||
$is_dcc_enabled = $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' );
|
||||
|
||||
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
|
||||
$is_separate_card_enabled = isset( $available_gateways[ CardButtonGateway::ID ] );
|
||||
|
||||
if ( is_checkout() && ( $is_dcc_enabled || $is_separate_card_enabled ) ) {
|
||||
$key = array_search( 'card', $disable_funding, true );
|
||||
if ( false !== $key ) {
|
||||
unset( $disable_funding[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( in_array( $context, array( 'checkout-block', 'cart-block' ), true ) ) {
|
||||
$disable_funding = array_merge(
|
||||
$disable_funding,
|
||||
array_diff(
|
||||
array_keys( $this->all_funding_sources ),
|
||||
array( 'venmo', 'paylater', 'paypal', 'card' )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $this->is_free_trial_cart() ) {
|
||||
$all_sources = array_keys( $this->all_funding_sources );
|
||||
if ( $is_dcc_enabled || $is_separate_card_enabled ) {
|
||||
$all_sources = array_diff( $all_sources, array( 'card' ) );
|
||||
}
|
||||
$disable_funding = $all_sources;
|
||||
}
|
||||
|
||||
return $disable_funding;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue