Ejegg has uploaded a new change for review. https://gerrit.wikimedia.org/r/271703
Change subject: Allow a different fallback currency for each country ...................................................................... Allow a different fallback currency for each country Only available for gateways with a single valid currency for each supported country. Bug: T124327 Change-Id: If28fb97eb0a75b3aaeebeaf69b6ff373654229b7 --- M DonationInterface.php M gateway_common/DonationData.php M tests/GatewayPageTest.php 3 files changed, 37 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface refs/changes/03/271703/1 diff --git a/DonationInterface.php b/DonationInterface.php index 4311309..f323b5a 100644 --- a/DonationInterface.php +++ b/DonationInterface.php @@ -188,6 +188,13 @@ $wgDonationInterfaceFallbackCurrency = false; /** + * For a gateway that has exactly one valid currency per supported country, + * you can instead set this variable to true to make gateway forms use an + * appropriate fallback currency for the selected country. + */ +$wgDonationInterfaceFallbackCurrencyByCountry = false; + +/** * When this is true and an unsupported currency has been converted to the * fallback (see above), we show an interstitial page notifying the user * of the conversion before sending the donation to the gateway. diff --git a/gateway_common/DonationData.php b/gateway_common/DonationData.php index 002ae08..1b71918 100644 --- a/gateway_common/DonationData.php +++ b/gateway_common/DonationData.php @@ -1028,7 +1028,21 @@ * @throws DomainException */ protected function fallbackToDefaultCurrency() { - $defaultCurrency = $this->gateway->getGlobal( 'FallbackCurrency' ); + $adapterClass = $this->gateway->getGatewayAdapterClass(); + if ( $this->gateway->getGlobal( 'FallbackCurrencyByCountry' ) ) { + $supportedCurrencies = $this->gateway->getCurrencies( $this->normalized ); + $numSupported = count( $supportedCurrencies ); + if ( $numSupported !== 1 ) { + $country = isset( $this->normalized['country'] ) ? + $this->normalized['country'] : + 'Unknown country'; + throw new DomainException( __FUNCTION__ . + " FallbackCurrencyByCountry is true for $adapterClass, but $numSupported currencies are supported for country $country" ); + } + $defaultCurrency = $supportedCurrencies[0]; + } else { + $defaultCurrency = $this->gateway->getGlobal( 'FallbackCurrency' ); + } if ( !$defaultCurrency ) { return; } @@ -1036,7 +1050,6 @@ // intermediate currency if converting between two others. $oldCurrency = $this->normalized['currency_code']; if ( $oldCurrency === $defaultCurrency ) { - $adapterClass = $this->gateway->getGatewayAdapterClass(); throw new DomainException( __FUNCTION__ . " Unsupported currency $defaultCurrency set as fallback for $adapterClass." ); } $oldAmount = $this->normalized['amount']; diff --git a/tests/GatewayPageTest.php b/tests/GatewayPageTest.php index d98808f..ab58d43 100644 --- a/tests/GatewayPageTest.php +++ b/tests/GatewayPageTest.php @@ -125,4 +125,19 @@ $this->assertEquals( 200, $this->adapter->getData_Unstaged_Escaped( 'amount' ) ); $this->assertEquals( 'BBD', $this->adapter->getData_Unstaged_Escaped( 'currency_code' ) ); } + + public function testFallbackByCountry() { + // With 'FallbackCurrencyByCountry', we need to return a single supported currency + TestingGenericAdapter::$acceptedCurrencies = array( 'USD' ); + TestingGenericAdapter::$fakeGlobals = array( + 'FallbackCurrency' => false, + 'FallbackCurrencyByCountry' => true, + ); + $this->setUpAdapter(); + + $this->page->validateForm(); + + $this->assertEquals( 100, $this->adapter->getData_Unstaged_Escaped( 'amount' ) ); + $this->assertEquals( 'USD', $this->adapter->getData_Unstaged_Escaped( 'currency_code' ) ); + } } -- To view, visit https://gerrit.wikimedia.org/r/271703 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If28fb97eb0a75b3aaeebeaf69b6ff373654229b7 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/DonationInterface Gerrit-Branch: master Gerrit-Owner: Ejegg <eeggles...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits