-
Notifications
You must be signed in to change notification settings - Fork 72
refactor: Afterpay payment method definition #11174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
28ef3d4
6e3ed9a
d589eff
30d6246
1bee3e9
0c7c2c1
2c7881a
dfbc248
7e3d2cf
834fe07
faecda6
18abe99
7883226
8752e56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: update | ||
|
|
||
| refactor: remove Afterpay_Payment_Method in favor of AfterpayDefinition |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,14 +55,14 @@ | |
| 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; | ||
| use WCPay\Payment_Methods\Sepa_Payment_Method; | ||
| use WCPay\Payment_Methods\UPE_Payment_Method; | ||
| use WCPay\Payment_Methods\Multibanco_Payment_Method; | ||
| use WCPay\Payment_Methods\Grabpay_Payment_Method; | ||
| use WCPay\PaymentMethods\Configs\Definitions\AfterpayDefinition; | ||
frosso marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| use WCPay\PaymentMethods\Configs\Registry\PaymentMethodDefinitionRegistry; | ||
|
|
||
| /** | ||
|
|
@@ -378,7 +378,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 +568,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 +600,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 ); | ||
| } | ||
|
Comment on lines
-611
to
-618
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was being executed only for Afterpay. |
||
|
|
||
| /** | ||
| * Displays HTML tags for WC payment gateway radio button content. | ||
| */ | ||
|
|
@@ -4085,7 +4068,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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| <?php | ||
| /** | ||
| * Afterpay Payment Method Definition | ||
| * | ||
| * @package WCPay\PaymentMethods\Configs\Definitions | ||
| */ | ||
|
|
||
| namespace WCPay\PaymentMethods\Configs\Definitions; | ||
|
|
||
| use WC_Payments_Utils; | ||
| use WCPay\PaymentMethods\Configs\Interfaces\PaymentMethodDefinitionInterface; | ||
| use WCPay\PaymentMethods\Configs\Constants\PaymentMethodCapability; | ||
| use WCPay\Constants\Country_Code; | ||
| use WCPay\Constants\Currency_Code; | ||
| use WCPay\PaymentMethods\Configs\Utils\PaymentMethodUtils; | ||
|
|
||
| /** | ||
| * Class implementing the Afterpay payment method definition. | ||
| */ | ||
| class AfterpayDefinition implements PaymentMethodDefinitionInterface { | ||
|
|
||
| /** | ||
| * Get the internal ID for the payment method | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function get_id(): string { | ||
| return 'afterpay_clearpay'; | ||
| } | ||
|
|
||
| /** | ||
| * Get the keywords for the payment method. These are used by the duplicate detection service. | ||
| * | ||
| * @return string[] | ||
| */ | ||
| public static function get_keywords(): array { | ||
| return [ 'afterpay', 'clearpay' ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the Stripe payment method ID | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function get_stripe_id(): string { | ||
| return self::get_id(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the customer-facing title of the payment method | ||
| * | ||
| * @param string|null $account_country Optional. The merchant's account country. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function get_title( ?string $account_country = null ): string { | ||
| 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' ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the title of the payment method for the settings page. | ||
| * | ||
| * @param string|null $account_country Optional. The merchant's account country. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function get_settings_label( ?string $account_country = null ): string { | ||
| return self::get_title( $account_country ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the customer-facing description of the payment method | ||
| * | ||
| * @param string|null $account_country Optional. The merchant's account country. | ||
| * @return string | ||
| */ | ||
| public static function get_description( ?string $account_country = null ): string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this method doesn't account for US accounts where the title is "Cash App Afterpay". Should the description say "Allow customers to pay over time with Cash App Afterpay" in the US? I realize this matches the old behavior and it may be intended though |
||
| 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' ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of supported currencies | ||
| * | ||
| * @return string[] Array of currency codes | ||
| */ | ||
| public static function get_supported_currencies(): array { | ||
| return [ | ||
| Currency_Code::UNITED_STATES_DOLLAR, | ||
| Currency_Code::CANADIAN_DOLLAR, | ||
| Currency_Code::AUSTRALIAN_DOLLAR, | ||
| Currency_Code::NEW_ZEALAND_DOLLAR, | ||
| Currency_Code::POUND_STERLING, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of supported countries | ||
| * | ||
| * @return string[] Array of country codes | ||
| */ | ||
| public static function get_supported_countries(): array { | ||
| return [ | ||
| Country_Code::UNITED_STATES, | ||
| Country_Code::CANADA, | ||
| Country_Code::AUSTRALIA, | ||
| Country_Code::NEW_ZEALAND, | ||
| Country_Code::UNITED_KINGDOM, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the payment method capabilities | ||
| * | ||
| * @return string[] | ||
| */ | ||
| public static function get_capabilities(): array { | ||
| return [ | ||
| PaymentMethodCapability::BUY_NOW_PAY_LATER, | ||
| PaymentMethodCapability::REFUNDS, | ||
| PaymentMethodCapability::DOMESTIC_TRANSACTIONS_ONLY, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL for the payment method's icon | ||
| * | ||
| * @param string|null $account_country Optional. The merchant's account country. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function get_icon_url( ?string $account_country = null ): string { | ||
| 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 ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL for the payment method's dark mode icon | ||
| * | ||
| * @param string|null $account_country Optional. The merchant's account country. | ||
| * | ||
| * @return string Returns regular icon URL if no dark mode icon exists | ||
| */ | ||
| public static function get_dark_icon_url( ?string $account_country = null ): string { | ||
| 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 ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL for the payment method's settings icon | ||
| * | ||
| * @param string|null $account_country Optional. The merchant's account country. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function get_settings_icon_url( ?string $account_country = null ): string { | ||
| 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 ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the testing instructions for the payment method | ||
| * | ||
| * @param string $account_country The merchant's account country. | ||
| * @return string HTML string containing testing instructions | ||
| */ | ||
| public static function get_testing_instructions( string $account_country ): string { | ||
| return ''; | ||
| } | ||
|
|
||
| /** | ||
| * Get the currency limits for the payment method | ||
| * | ||
| * @return array<string,array<string,array{min:int,max:int}>> | ||
| */ | ||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes the icons appearing "squished" on checkout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking: Great that you added the height to fix the problem. The logo is not distorted for me, but it does not look aligned with the rest of logos in a GB JN
That said, I don’t think this misalignment is related to your changes. I checked and it appears the same in the latest stable version. There’s likely room for improvement here, but probably as part of a separate task.