diff --git a/changelog/refactor-afterpay-clearpay-payment-method-definition b/changelog/refactor-afterpay-clearpay-payment-method-definition new file mode 100644 index 00000000000..f652579b89a --- /dev/null +++ b/changelog/refactor-afterpay-clearpay-payment-method-definition @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +refactor: remove Afterpay_Payment_Method in favor of AfterpayDefinition diff --git a/client/checkout/blocks/style.scss b/client/checkout/blocks/style.scss index 3d42bc82da5..094b7b78526 100644 --- a/client/checkout/blocks/style.scss +++ b/client/checkout/blocks/style.scss @@ -102,11 +102,13 @@ button.wcpay-stripelink-modal-trigger:hover { #radio-control-wc-payment-method-options-woocommerce_payments_affirm__label img { min-width: 50px; + height: auto; } /* stylelint-disable-next-line selector-id-pattern */ #radio-control-wc-payment-method-options-woocommerce_payments_afterpay_clearpay__label img { min-width: 64px; + height: auto; } } diff --git a/includes/class-duplicates-detection-service.php b/includes/class-duplicates-detection-service.php index e2fbf78bebb..d7ffb5c7a72 100644 --- a/includes/class-duplicates-detection-service.php +++ b/includes/class-duplicates-detection-service.php @@ -12,7 +12,6 @@ } use WC_Payments; -use WCPay\Payment_Methods\Afterpay_Payment_Method; use WCPay\Payment_Methods\Becs_Payment_Method; use WCPay\Payment_Methods\CC_Payment_Method; use WCPay\Payment_Methods\Klarna_Payment_Method; @@ -93,12 +92,10 @@ private function search_for_additional_payment_methods() { * As payment methods are converted to use definitions, they need to be removed from the list below. */ $keywords = [ - 'sepa' => Sepa_Payment_Method::PAYMENT_METHOD_STRIPE_ID, - 'becs' => Becs_Payment_Method::PAYMENT_METHOD_STRIPE_ID, - 'afterpay' => Afterpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID, - 'clearpay' => Afterpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID, - 'klarna' => Klarna_Payment_Method::PAYMENT_METHOD_STRIPE_ID, - 'grabpay' => Grabpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID, + 'sepa' => Sepa_Payment_Method::PAYMENT_METHOD_STRIPE_ID, + 'becs' => Becs_Payment_Method::PAYMENT_METHOD_STRIPE_ID, + 'klarna' => Klarna_Payment_Method::PAYMENT_METHOD_STRIPE_ID, + 'grabpay' => Grabpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID, ]; // Get all payment method definitions. diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index a4278f234f2..f5eca9919c2 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -55,7 +55,6 @@ use WCPay\Tracker; use WCPay\Internal\Service\Level3Service; use WCPay\Internal\Service\OrderService; -use WCPay\Payment_Methods\Afterpay_Payment_Method; use WCPay\Payment_Methods\Becs_Payment_Method; use WCPay\Payment_Methods\CC_Payment_Method; use WCPay\Payment_Methods\Klarna_Payment_Method; @@ -378,7 +377,7 @@ public function __construct( */ public function get_title() { if ( ! $this->title ) { - $this->title = $this->payment_method->get_title(); + $this->title = $this->payment_method->get_title( $this->get_account_country() ); $this->method_title = "WooPayments ($this->title)"; } return parent::get_title(); @@ -568,7 +567,6 @@ public function get_form_fields() { * @return void */ public function init_hooks() { - add_action( 'init', [ $this, 'maybe_update_properties_with_country' ] ); // Only add certain actions/filter if this is the main gateway (i.e. not split UPE). if ( self::GATEWAY_ID === $this->id ) { add_action( 'woocommerce_order_actions', [ $this, 'add_order_actions' ] ); @@ -601,22 +599,6 @@ public function init_hooks() { $this->maybe_init_subscriptions_hooks(); } - /** - * Updates icon and title using the account country. - * This method runs on init is not in the controller because get_account_country might - * make a request to the API if the account data is not cached. - * - * @return void - */ - public function maybe_update_properties_with_country(): void { - if ( Afterpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID !== $this->stripe_id ) { - return; - } - $account_country = $this->get_account_country(); - $this->icon = $this->payment_method->get_icon( $account_country ); - $this->title = $this->payment_method->get_title( $account_country ); - } - /** * Displays HTML tags for WC payment gateway radio button content. */ @@ -4085,7 +4067,6 @@ public function get_upe_available_payment_methods() { $available_methods[] = Becs_Payment_Method::PAYMENT_METHOD_STRIPE_ID; $available_methods[] = Sepa_Payment_Method::PAYMENT_METHOD_STRIPE_ID; $available_methods[] = Link_Payment_Method::PAYMENT_METHOD_STRIPE_ID; - $available_methods[] = Afterpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID; $available_methods[] = Klarna_Payment_Method::PAYMENT_METHOD_STRIPE_ID; $available_methods[] = Multibanco_Payment_Method::PAYMENT_METHOD_STRIPE_ID; $available_methods[] = Grabpay_Payment_Method::PAYMENT_METHOD_STRIPE_ID; diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index aefbf32c0a1..59360981ec9 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -24,7 +24,6 @@ use WCPay\WooPay\WooPay_Utilities; use WCPay\WooPay\WooPay_Order_Status_Sync; use WCPay\Payment_Methods\Link_Payment_Method; -use WCPay\Payment_Methods\Afterpay_Payment_Method; use WCPay\Session_Rate_Limiter; use WCPay\Database_Cache; use WCPay\WC_Payments_Checkout; @@ -432,7 +431,6 @@ public static function init() { include_once __DIR__ . '/payment-methods/class-sepa-payment-method.php'; include_once __DIR__ . '/payment-methods/class-becs-payment-method.php'; include_once __DIR__ . '/payment-methods/class-link-payment-method.php'; - include_once __DIR__ . '/payment-methods/class-afterpay-payment-method.php'; include_once __DIR__ . '/payment-methods/class-klarna-payment-method.php'; include_once __DIR__ . '/payment-methods/class-multibanco-payment-method.php'; include_once __DIR__ . '/payment-methods/class-grabpay-payment-method.php'; @@ -574,7 +572,6 @@ public static function init() { Sepa_Payment_Method::class, Becs_Payment_Method::class, Link_Payment_Method::class, - Afterpay_Payment_Method::class, Klarna_Payment_Method::class, Multibanco_Payment_Method::class, Grabpay_Payment_Method::class, diff --git a/includes/payment-methods/Configs/Definitions/AfterpayDefinition.php b/includes/payment-methods/Configs/Definitions/AfterpayDefinition.php new file mode 100644 index 00000000000..2c8b6ee0040 --- /dev/null +++ b/includes/payment-methods/Configs/Definitions/AfterpayDefinition.php @@ -0,0 +1,259 @@ +> + */ + public static function get_limits_per_currency(): array { + return WC_Payments_Utils::get_bnpl_limits_per_currency( self::get_stripe_id() ); + } + + /** + * Whether this payment method is available for the given currency and country + * + * @param string $currency The currency code to check. + * @param string $account_country The merchant's account country. + * + * @return bool + */ + public static function is_available_for( string $currency, string $account_country ): bool { + return PaymentMethodUtils::is_available_for( self::get_supported_currencies(), self::get_supported_countries(), $currency, $account_country ); + } + + /** + * Get the minimum amount for this payment method for a given currency and country + * + * @param string $currency The currency code. + * @param string $country The country code. + * + * @return int|null The minimum amount or null if no minimum. + */ + public static function get_minimum_amount( string $currency, string $country ): ?int { + $limits = self::get_limits_per_currency(); + + if ( isset( $limits[ $currency ][ $country ]['min'] ) ) { + return $limits[ $currency ][ $country ]['min']; + } + + return null; + } + + /** + * Get the maximum amount for this payment method for a given currency and country + * + * @param string $currency The currency code. + * @param string $country The country code. + * + * @return int|null The maximum amount or null if no maximum. + */ + public static function get_maximum_amount( string $currency, string $country ): ?int { + $limits = self::get_limits_per_currency(); + + if ( isset( $limits[ $currency ][ $country ]['max'] ) ) { + return $limits[ $currency ][ $country ]['max']; + } + + return null; + } +} diff --git a/includes/payment-methods/Configs/Registry/PaymentMethodDefinitionRegistry.php b/includes/payment-methods/Configs/Registry/PaymentMethodDefinitionRegistry.php index bebb96e8ca6..1b04b3f44e2 100644 --- a/includes/payment-methods/Configs/Registry/PaymentMethodDefinitionRegistry.php +++ b/includes/payment-methods/Configs/Registry/PaymentMethodDefinitionRegistry.php @@ -8,6 +8,7 @@ namespace WCPay\PaymentMethods\Configs\Registry; use WCPay\PaymentMethods\Configs\Definitions\AffirmDefinition; +use WCPay\PaymentMethods\Configs\Definitions\AfterpayDefinition; use WCPay\PaymentMethods\Configs\Definitions\AlipayDefinition; use WCPay\PaymentMethods\Configs\Definitions\ApplePayDefinition; use WCPay\PaymentMethods\Configs\Definitions\GooglePayDefinition; @@ -40,6 +41,7 @@ class PaymentMethodDefinitionRegistry { private $available_definitions = [ // Add new payment method definitions here. AffirmDefinition::class, + AfterpayDefinition::class, AlipayDefinition::class, BancontactDefinition::class, EpsDefinition::class, diff --git a/includes/payment-methods/class-afterpay-payment-method.php b/includes/payment-methods/class-afterpay-payment-method.php deleted file mode 100644 index 41dbb955e3e..00000000000 --- a/includes/payment-methods/class-afterpay-payment-method.php +++ /dev/null @@ -1,144 +0,0 @@ -stripe_id = self::PAYMENT_METHOD_STRIPE_ID; - $this->is_reusable = false; - $this->is_bnpl = true; - $this->icon_url = plugins_url( 'assets/images/payment-methods/afterpay-logo.svg', WCPAY_PLUGIN_FILE ); - $this->currencies = [ Currency_Code::UNITED_STATES_DOLLAR, Currency_Code::CANADIAN_DOLLAR, Currency_Code::AUSTRALIAN_DOLLAR, Currency_Code::NEW_ZEALAND_DOLLAR, Currency_Code::POUND_STERLING ]; - $this->countries = [ Country_Code::UNITED_STATES, Country_Code::CANADA, Country_Code::AUSTRALIA, Country_Code::NEW_ZEALAND, Country_Code::UNITED_KINGDOM ]; - $this->accept_only_domestic_payment = true; - $this->limits_per_currency = WC_Payments_Utils::get_bnpl_limits_per_currency( self::PAYMENT_METHOD_STRIPE_ID ); - } - - /** - * Returns payment method title. - * - * @param string|null $account_country Country of merchants account. - * @param array|false $payment_details Payment details from charge object. Not used by this class. - * @return string|null - * - * @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - */ - public function get_title( ?string $account_country = null, $payment_details = false ) { - if ( Country_Code::UNITED_KINGDOM === $account_country ) { - return __( 'Clearpay', 'woocommerce-payments' ); - } - - if ( Country_Code::UNITED_STATES === $account_country ) { - return __( 'Cash App Afterpay', 'woocommerce-payments' ); - } - - return __( 'Afterpay', 'woocommerce-payments' ); - } - - /** - * Returns payment method icon. - * - * @param string|null $account_country Country of merchants account. - * @return string|null - */ - public function get_icon( ?string $account_country = null ) { - if ( Country_Code::UNITED_KINGDOM === $account_country ) { - return plugins_url( 'assets/images/payment-methods/clearpay.svg', WCPAY_PLUGIN_FILE ); - } - - if ( Country_Code::UNITED_STATES === $account_country ) { - return plugins_url( 'assets/images/payment-methods/afterpay-cashapp-logo.svg', WCPAY_PLUGIN_FILE ); - } - - return plugins_url( 'assets/images/payment-methods/afterpay-badge.svg', WCPAY_PLUGIN_FILE ); - } - - /** - * Returns payment method dark icon. - * - * @param string|null $account_country Country of merchants account. - * @return string|null - */ - public function get_dark_icon( ?string $account_country = null ) { - if ( Country_Code::UNITED_KINGDOM === $account_country ) { - return plugins_url( 'assets/images/payment-methods/clearpay-dark.svg', WCPAY_PLUGIN_FILE ); - } - - if ( Country_Code::UNITED_STATES === $account_country ) { - return plugins_url( 'assets/images/payment-methods/afterpay-cashapp-logo-dark.svg', WCPAY_PLUGIN_FILE ); - } - - return plugins_url( 'assets/images/payment-methods/afterpay-badge-dark.svg', WCPAY_PLUGIN_FILE ); - } - - /** - * Returns testing credentials to be printed at checkout in test mode. - * - * @param string $account_country The country of the account. - * @return string - */ - public function get_testing_instructions( string $account_country ) { - return ''; - } - - /** - * Returns payment method description for the settings page. - * - * @param string|null $account_country Country of merchants account. - * - * @return string - */ - public function get_description( ?string $account_country = null ) { - if ( Country_Code::UNITED_KINGDOM === $account_country ) { - return __( - 'Allow customers to pay over time with Clearpay.', - 'woocommerce-payments' - ); - } - - return __( - 'Allow customers to pay over time with Afterpay.', - 'woocommerce-payments' - ); - } - - /** - * Returns payment method settings icon. - * - * @param string|null $account_country Country of merchants account. - * @return string - */ - public function get_settings_icon_url( ?string $account_country = null ) { - if ( Country_Code::UNITED_KINGDOM === $account_country ) { - return plugins_url( 'assets/images/payment-methods/clearpay.svg', WCPAY_PLUGIN_FILE ); - } - - if ( Country_Code::UNITED_STATES === $account_country ) { - return plugins_url( 'assets/images/payment-methods/afterpay-cashapp-badge.svg', WCPAY_PLUGIN_FILE ); - } - - return plugins_url( 'assets/images/payment-methods/afterpay-logo.svg', WCPAY_PLUGIN_FILE ); - } -} diff --git a/tests/unit/payment-methods/test-class-upe-payment-gateway.php b/tests/unit/payment-methods/test-class-upe-payment-gateway.php index d7733c82d8d..548f99adc81 100644 --- a/tests/unit/payment-methods/test-class-upe-payment-gateway.php +++ b/tests/unit/payment-methods/test-class-upe-payment-gateway.php @@ -247,6 +247,7 @@ public function set_up() { $payment_method_definitions = [ \WCPay\PaymentMethods\Configs\Definitions\AffirmDefinition::class, + \WCPay\PaymentMethods\Configs\Definitions\AfterpayDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\GiropayDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\SofortDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\BancontactDefinition::class, @@ -260,7 +261,6 @@ public function set_up() { Sepa_Payment_Method::class, Becs_Payment_Method::class, Link_Payment_Method::class, - Afterpay_Payment_Method::class, ]; $this->mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class ); diff --git a/tests/unit/payment-methods/test-class-upe-payment-method.php b/tests/unit/payment-methods/test-class-upe-payment-method.php index bff90162d0c..407c425612a 100644 --- a/tests/unit/payment-methods/test-class-upe-payment-method.php +++ b/tests/unit/payment-methods/test-class-upe-payment-method.php @@ -73,6 +73,7 @@ public function set_up() { $payment_method_definitions = [ \WCPay\PaymentMethods\Configs\Definitions\AffirmDefinition::class, + \WCPay\PaymentMethods\Configs\Definitions\AfterpayDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\GiropayDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\SofortDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\BancontactDefinition::class, @@ -86,7 +87,6 @@ public function set_up() { Sepa_Payment_Method::class, Becs_Payment_Method::class, Link_Payment_Method::class, - Afterpay_Payment_Method::class, Klarna_Payment_Method::class, ]; diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay.php b/tests/unit/test-class-wc-payment-gateway-wcpay.php index f7b2133e1e9..893191ce407 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay.php @@ -28,7 +28,6 @@ use WCPay\Internal\Service\OrderService; use WCPay\Payment_Information; use WCPay\Payment_Methods\UPE_Payment_Method; -use WCPay\Payment_Methods\Afterpay_Payment_Method; use WCPay\Payment_Methods\Becs_Payment_Method; use WCPay\Payment_Methods\CC_Payment_Method; use WCPay\Payment_Methods\Grabpay_Payment_Method; @@ -4157,6 +4156,7 @@ private function init_payment_methods() { $payment_method_definitions = [ \WCPay\PaymentMethods\Configs\Definitions\AffirmDefinition::class, + \WCPay\PaymentMethods\Configs\Definitions\AfterpayDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\BancontactDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\EpsDefinition::class, \WCPay\PaymentMethods\Configs\Definitions\GiropayDefinition::class, @@ -4170,7 +4170,6 @@ private function init_payment_methods() { Sepa_Payment_Method::class, Becs_Payment_Method::class, Link_Payment_Method::class, - Afterpay_Payment_Method::class, Klarna_Payment_Method::class, Grabpay_Payment_Method::class, ];