From 196369d950e4421fd9e397f3281e35a61fddb2fd Mon Sep 17 00:00:00 2001 From: Makoto Mizukami Date: Wed, 31 Jul 2024 13:18:55 +0900 Subject: [PATCH 1/4] Examine currency before recognizing as an available payment method --- class-wc-settings-page-komoju.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/class-wc-settings-page-komoju.php b/class-wc-settings-page-komoju.php index 75bf211..a515b8d 100644 --- a/class-wc-settings-page-komoju.php +++ b/class-wc-settings-page-komoju.php @@ -365,10 +365,12 @@ private function fetch_all_payment_methods() try { $all_payment_methods = $api->paymentMethods(); $methods_by_slug = []; + $wc_currency = get_woocommerce_currency(); foreach ($all_payment_methods as $payment_method) { - $slug = $payment_method['type_slug']; - if (isset($methods_by_slug[$slug])) { + $slug = $payment_method['type_slug']; + $pm_currency = $payment_method['currency']; + if (isset($methods_by_slug[$slug]) && $pm_currency !== $wc_currency) { continue; } $methods_by_slug[$slug] = $payment_method; From a5a44460753c3f2e14297bafb2fe814565336993 Mon Sep 17 00:00:00 2001 From: Makoto Mizukami Date: Fri, 2 Aug 2024 13:19:09 +0900 Subject: [PATCH 2/4] Elaborate on the rationale behind the conditional branching --- class-wc-settings-page-komoju.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/class-wc-settings-page-komoju.php b/class-wc-settings-page-komoju.php index a515b8d..0c88099 100644 --- a/class-wc-settings-page-komoju.php +++ b/class-wc-settings-page-komoju.php @@ -370,9 +370,17 @@ private function fetch_all_payment_methods() foreach ($all_payment_methods as $payment_method) { $slug = $payment_method['type_slug']; $pm_currency = $payment_method['currency']; + + // If the payment method satisfies both of these two conditions, reject it: + // 1. There is an alternative payment method with the same slug (possibly with a more preferable currency) + // 2. The currency for the payment method defers from the default currency for the WooCommerce configuration + // That means, we accept payment methods if and only if + // 1. there is no alternative payment method with the same slug, OR + // 2. an alternative payment method with the same slug exists, but the currency for the payment method matches to the currency for the WooCommerce configuration and thus preferred if (isset($methods_by_slug[$slug]) && $pm_currency !== $wc_currency) { continue; } + $methods_by_slug[$slug] = $payment_method; } From de569d22c2b21d82c92e2e475ad6db8ea8a318b0 Mon Sep 17 00:00:00 2001 From: Makoto Mizukami Date: Fri, 2 Aug 2024 13:25:41 +0900 Subject: [PATCH 3/4] Refactor; split the conditional branching into two --- class-wc-settings-page-komoju.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/class-wc-settings-page-komoju.php b/class-wc-settings-page-komoju.php index 0c88099..93ef418 100644 --- a/class-wc-settings-page-komoju.php +++ b/class-wc-settings-page-komoju.php @@ -371,14 +371,15 @@ private function fetch_all_payment_methods() $slug = $payment_method['type_slug']; $pm_currency = $payment_method['currency']; - // If the payment method satisfies both of these two conditions, reject it: - // 1. There is an alternative payment method with the same slug (possibly with a more preferable currency) - // 2. The currency for the payment method defers from the default currency for the WooCommerce configuration - // That means, we accept payment methods if and only if - // 1. there is no alternative payment method with the same slug, OR - // 2. an alternative payment method with the same slug exists, but the currency for the payment method matches to the currency for the WooCommerce configuration and thus preferred - if (isset($methods_by_slug[$slug]) && $pm_currency !== $wc_currency) { - continue; + // If $slug is not set, register + if (!isset($methods_by_slug[$slug])) { + $methods_by_slug[$slug] = $payment_method; + } else { + // If $slug is already registered and + // the payment currency matches the WooCommerce currency then override it + if ($pm_currency === $wc_currency) { + $methods_by_slug[$slug] = $payment_method; + } } $methods_by_slug[$slug] = $payment_method; From 95fc946190a37ed985fe02f210da22874d7df673 Mon Sep 17 00:00:00 2001 From: Makoto Mizukami Date: Fri, 2 Aug 2024 13:56:09 +0900 Subject: [PATCH 4/4] Fix tests; sign in more slowly --- tests/cypress/support/commands.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index b826252..0f588b2 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -49,6 +49,7 @@ Cypress.Commands.add('installWordpress', () => { Cypress.Commands.add('signinToWordpress', () => { cy.visit('/wp-admin'); + cy.wait(1000); cy.get('body').then(($body) => { if (!$body.find('#loginform').length) { @@ -60,6 +61,7 @@ Cypress.Commands.add('signinToWordpress', () => { cy.get('#user_pass').should('be.visible').clear().type('deg1kaX7reme!'); cy.get('#wp-submit').should('be.visible').click(); }); + cy.wait(1000); }); Cypress.Commands.add('installWooCommerce', () => {