Add new setting if not eligible

This commit is contained in:
carmenmaymo 2023-09-18 17:36:40 +02:00
parent 7cf1105b51
commit 09bf938a6b
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
3 changed files with 127 additions and 0 deletions

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
'wcgateway.settings.fields' => function ( ContainerInterface $container, array $fields ): array {
$insert_after = function ( array $array, string $key, array $new ): array {
$keys = array_keys( $array );
$index = array_search( $key, $keys, true );
@ -24,6 +25,35 @@ return array(
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
};
if ( ! $container->has( 'applepay.eligible' ) || ! $container->get( 'applepay.eligible' ) ) {
$connection_url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway&ppcp-tab=ppcp-connection#field-credentials_feature_onboarding_heading' );
$connection_link = '<a href="' . $connection_url . '" target="_blank">';
return $insert_after(
$fields,
'allow_card_button_gateway',
array(
'applepay_button_enabled' => array(
'title' => __('Apple Pay Button', 'woocommerce-paypal-payments'),
'type' => 'checkbox',
'class' => array('ppcp-grayed-out-text'),
'input_class' => array('ppcp-disabled-checkbox'),
'label' => __('Enable Apple Pay button', 'woocommerce-paypal-payments')
. '<p class="description">'
. sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__('Your PayPal account %1$srequires additional permissions%2$s to enable Apple Pay.', 'woocommerce-paypal-payments'),
$connection_link,
'</a>'
)
. '</p>',
'default' => 'yes',
'screens' => array(State::STATE_ONBOARDED),
'gateway' => 'paypal',
'requirements' => array(),
),
)
);
}
return $insert_after(
$fields,
'allow_card_button_gateway',

View file

@ -15,10 +15,24 @@ use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
use WooCommerce\PayPalCommerce\Applepay\Assets\DataToAppleButtonScripts;
use WooCommerce\PayPalCommerce\Applepay\Assets\BlocksPaymentMethod;
use WooCommerce\PayPalCommerce\Applepay\Helper\ApmApplies;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
return array(
'applepay.eligible' => static function ( ContainerInterface $container ): bool {
$apm_applies = $container->get( 'applepay.helpers.apm-applies' );
assert( $apm_applies instanceof ApmApplies );
return $apm_applies->for_country_currency();
},
'applepay.helpers.apm-applies' => static function ( ContainerInterface $container ) : ApmApplies {
return new ApmApplies(
$container->get( 'applepay.supported-country-currency-matrix' ),
$container->get( 'api.shop.currency' ),
$container->get( 'api.shop.country' )
);
},
'applepay.status-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-paypal-apple-status-cache' );
},
@ -81,4 +95,20 @@ return array(
$container->get( 'blocks.method' )
);
},
/**
* The matrix which countries and currency combinations can be used for ApplePay.
*/
'applepay.supported-country-currency-matrix' => static function ( ContainerInterface $container ) : array {
/**
* Returns which countries and currency combinations can be used for ApplePay.
*/
return apply_filters(
'woocommerce_paypal_payments_applepay_supported_country_currency_matrix',
array(
'US' => array(
'USD',
),
)
);
},
);

View file

@ -0,0 +1,67 @@
<?php
/**
* Properties of the ApplePay module.
*
* @package WooCommerce\PayPalCommerce\ApplePay\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay\Helper;
/**
* Class ApmApplies
*/
class ApmApplies {
/**
* The matrix which countries and currency combinations can be used for DCC.
*
* @var array
*/
private $allowed_country_currency_matrix;
/**
* 3-letter currency code of the shop.
*
* @var string
*/
private $currency;
/**
* 2-letter country code of the shop.
*
* @var string
*/
private $country;
/**
* DccApplies constructor.
*
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used for DCC.
* @param string $currency 3-letter currency code of the shop.
* @param string $country 2-letter country code of the shop.
*/
public function __construct(
array $allowed_country_currency_matrix,
string $currency,
string $country
) {
$this->allowed_country_currency_matrix = $allowed_country_currency_matrix;
$this->currency = $currency;
$this->country = $country;
}
/**
* Returns whether ApplePay can be used in the current country and the current currency used.
*
* @return bool
*/
public function for_country_currency(): bool {
if ( ! in_array( $this->country, array_keys( $this->allowed_country_currency_matrix ), true ) ) {
return false;
}
return in_array( $this->currency, $this->allowed_country_currency_matrix[ $this->country ], true );
}
}