-
Notifications
You must be signed in to change notification settings - Fork 73
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
Open
frosso
wants to merge
4
commits into
develop
Choose a base branch
from
refactor/afterpay-clearpay-payment-method-definition
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+275
−178
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
changelog/refactor-afterpay-clearpay-payment-method-definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
| } | ||
|
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 +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; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
259 changes: 259 additions & 0 deletions
259
includes/payment-methods/Configs/Definitions/AfterpayDefinition.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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