Skip to content

Commit

Permalink
Merge pull request #3021 from woocommerce/PCP-4077-conditionally-disp…
Browse files Browse the repository at this point in the history
…lay-payment-methods

Conditionally display payment methods (4077)
  • Loading branch information
stracker-phil authored Jan 24, 2025
2 parents f5be748 + b08c6f6 commit f47487d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
7 changes: 4 additions & 3 deletions modules/ppcp-settings/resources/js/data/payment/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@ export const usePaymentMethodsModal = () => {

export const usePaymentMethodsPayPalCheckout = () => {
const { paypal, venmo, payLater, creditCard } = useHooks();

const paymentMethodsPayPalCheckout = [
paypal,
venmo,
payLater,
creditCard,
];
].filter( ( item ) => Object.keys( item ).length !== 0 );

return {
paymentMethodsPayPalCheckout,
Expand All @@ -185,7 +186,7 @@ export const usePaymentMethodsOnlineCardPayments = () => {
fastlane,
applePay,
googlePay,
];
].filter( ( item ) => Object.keys( item ).length !== 0 );

return {
paymentMethodsOnlineCardPayments,
Expand Down Expand Up @@ -217,7 +218,7 @@ export const usePaymentMethodsAlternative = () => {
multibanco,
pui,
oxxo,
];
].filter( ( item ) => Object.keys( item ).length !== 0 );

return {
paymentMethodsAlternative,
Expand Down
2 changes: 1 addition & 1 deletion modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public function get_details() : WP_REST_Response {
$gateway_settings['fastlaneCardholderName'] = $this->settings->get_fastlane_cardholder_name();
$gateway_settings['fastlaneDisplayWatermark'] = $this->settings->get_fastlane_display_watermark();

return $this->return_success( $gateway_settings );
return $this->return_success( apply_filters( 'woocommerce_paypal_payments_payment_methods', $gateway_settings ) );
}

/**
Expand Down
79 changes: 79 additions & 0 deletions modules/ppcp-settings/src/SettingsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

namespace WooCommerce\PayPalCommerce\Settings;

use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\BancontactGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\BlikGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\EPSGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\IDealGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MultibancoGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\MyBankGateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\P24Gateway;
use WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods\TrustlyGateway;
use WooCommerce\PayPalCommerce\Settings\Ajax\SwitchSettingsUiEndpoint;
use WooCommerce\PayPalCommerce\Settings\Data\OnboardingProfile;
use WooCommerce\PayPalCommerce\Settings\Endpoint\RestEndpoint;
Expand All @@ -17,6 +28,11 @@
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\OXXO\OXXO;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;

/**
Expand Down Expand Up @@ -263,6 +279,69 @@ static function () use ( $container ) : void {
}
);

add_filter(
'woocommerce_paypal_payments_payment_methods',
function( array $payment_methods ) use ( $container ) : array {
$all_payment_methods = $payment_methods;

$dcc_product_status = $container->get( 'wcgateway.helper.dcc-product-status' );
assert( $dcc_product_status instanceof DCCProductStatus );

$googlepay_product_status = $container->get( 'googlepay.helpers.apm-product-status' );
assert( $googlepay_product_status instanceof ApmProductStatus );

$applepay_product_status = $container->get( 'applepay.apple-product-status' );
assert( $applepay_product_status instanceof AppleProductStatus );

$dcc_applies = $container->get( 'api.helpers.dccapplies' );
assert( $dcc_applies instanceof DCCApplies );

// Unset BCDC if merchant is eligible for ACDC.
if ( $dcc_product_status->dcc_is_active() && ! $container->get( 'wcgateway.settings.allow_card_button_gateway' ) ) {
unset( $payment_methods[ CardButtonGateway::ID ] );
}

// Unset Venmo when store location is not United States.
if ( $container->get( 'api.shop.country' ) !== 'US' ) {
unset( $payment_methods['venmo'] );
}

// Unset if not eligible for Google Pay.
if ( ! $googlepay_product_status->is_active() ) {
unset( $payment_methods['ppcp-googlepay'] );
}

// Unset if not eligible for Apple Pay.
if ( ! $applepay_product_status->is_active() ) {
unset( $payment_methods['ppcp-applepay'] );
}

// Unset Fastlane if store location is not United States or merchant is not eligible for ACDC.
if ( $container->get( 'api.shop.country' ) !== 'US' || ! $dcc_product_status->dcc_is_active() ) {
unset( $payment_methods['ppcp-axo-gateway'] );
}

// For non-ACDC regions unset ACDC, local APMs and set BCDC.
if ( ! $dcc_applies ) {
unset( $payment_methods[ CreditCardGateway::ID ] );
unset( $payment_methods[ BancontactGateway::ID ] );
unset( $payment_methods[ BlikGateway::ID ] );
unset( $payment_methods[ EPSGateway::ID ] );
unset( $payment_methods[ IDealGateway::ID ] );
unset( $payment_methods[ MyBankGateway::ID ] );
unset( $payment_methods[ P24Gateway::ID ] );
unset( $payment_methods[ TrustlyGateway::ID ] );
unset( $payment_methods[ MultibancoGateway::ID ] );
unset( $payment_methods[ PayUponInvoiceGateway::ID ] );
unset( $payment_methods[ OXXO::ID ] );

$payment_methods[ CardButtonGateway::ID ] = $all_payment_methods[ CardButtonGateway::ID ];
}

return $payment_methods;
}
);

return true;
}

Expand Down
7 changes: 5 additions & 2 deletions modules/ppcp-wc-gateway/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1659,9 +1659,12 @@ static function ( ContainerInterface $container ): string {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );

return $settings->has( 'allow_card_button_gateway' ) ?
return apply_filters(
'woocommerce_paypal_payments_enable_standard_card_button_gateway_settings',
$settings->has( 'allow_card_button_gateway' ) ?
(bool) $settings->get( 'allow_card_button_gateway' ) :
$container->get( 'wcgateway.settings.allow_card_button_gateway.default' );
$container->get( 'wcgateway.settings.allow_card_button_gateway.default' )
);
},
'wcgateway.settings.has_enabled_separate_button_gateways' => static function ( ContainerInterface $container ): bool {
return (bool) $container->get( 'wcgateway.settings.allow_card_button_gateway' );
Expand Down

0 comments on commit f47487d

Please sign in to comment.