Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Use static list of zero-decimal currencies instead of config
Browse files Browse the repository at this point in the history
  • Loading branch information
pmclain committed Aug 11, 2018
1 parent 39ff924 commit abf0b3f
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 87 deletions.
9 changes: 0 additions & 9 deletions src/Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ public function getCurrency()
return $this->getValue(self::KEY_CURRENCY);
}

/**
* @codeCoverageIgnore
* @return string
*/
public function getCurrencyPrecision()
{
return $this->getValue(self::KEY_CURRENCY_PRECISION);
}

/**
* @return bool
*/
Expand Down
35 changes: 34 additions & 1 deletion src/Gateway/Helper/PriceFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,41 @@ public function __construct(
*/
public function formatPrice($price)
{
$price = sprintf('%.' . $this->config->getCurrencyPrecision() . 'F', $price);
$price = sprintf('%.' . ($this->isZeroDecimalCurrency() ? '0' : '2') . 'F', $price);

return str_replace('.', '', $price);
}

/**
* @return bool
*/
private function isZeroDecimalCurrency()
{
return in_array($this->config->getCurrency(), $this->getZeroDecimalCurrencies(), true);
}

/**
* @return array
*/
private function getZeroDecimalCurrencies()
{
return [
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'UGX',
'VND',
'VUV',
'XAF',
'XOF',
'XPF',
];
}
}
39 changes: 0 additions & 39 deletions src/Model/Adminhtml/Source/CurrencyPrecision.php

This file was deleted.

11 changes: 7 additions & 4 deletions src/Test/Unit/Gateway/Helper/PriceFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ protected function setUp()

/**
* @dataProvider formatPriceDataProvider
* @param string $expected
* @param string $price
* @param string $currency
*/
public function testFormatPrice($expected, $price, $precision)
public function testFormatPrice($expected, $price, $currency)
{
$this->configMock->method('getCurrencyPrecision')->willReturn($precision);
$this->configMock->method('getCurrency')->willReturn($currency);

$this->assertEquals($expected, $this->priceFormatter->formatPrice($price));
}
Expand All @@ -39,10 +42,10 @@ public function formatPriceDataProvider()
{
return [
[
'123', '123', '0',
'123', '123', 'JPY',
],
[
'12300', '123', '2',
'12300', '123', 'USD',
]
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Unit/Gateway/Request/CaptureDataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function setUp()
$this->subjectReaderMock = $this->createMock(SubjectReader::class);

$configMock = $this->createMock(Config::class);
$configMock->method('getCurrencyPrecision')->willReturn('2');
$configMock->method('getCurrency')->willReturn('USD');

$priceFormatter = new PriceFormatter($configMock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function setUp()
->getMock();

$this->configMock = $this->createMock(Config::class);
$this->configMock->method('getCurrencyPrecision')->willReturn('2');
$this->configMock->method('getCurrency')->willReturn('USD');

$priceFormatter = new PriceFormatter($this->configMock);

Expand Down Expand Up @@ -160,10 +160,6 @@ public function testBuild()
->method('getOrderIncrementId')
->willReturn('100000001');

$this->configMock->expects($this->once())
->method('getCurrency')
->willReturn('USD');

$this->paymentMock->method('getAdditionalInformation')
->will($this->returnCallback(function ($arg) {
if ($arg === 'is_active_payment_token_enabler') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function setUp()
->getMock();

$this->configMock = $this->createMock(Config::class);
$this->configMock->method('getCurrencyPrecision')->willReturn('2');
$this->configMock->method('getCurrency')->willReturn('USD');

$priceFormatter = new PriceFormatter($this->configMock);

Expand Down Expand Up @@ -185,10 +185,6 @@ public function testBuild()
->method('getOrderIncrementId')
->willReturn('100000001');

$this->configMock->expects($this->once())
->method('getCurrency')
->willReturn('USD');

$this->paymentTokenMock->expects($this->once())
->method('getGatewayToken')
->willReturn('card_token');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function setUp()
->getMockForAbstractClass();

$this->configMock = $this->createMock(Config::class);
$this->configMock->method('getCurrencyPrecision')->willReturn('2');
$this->configMock->method('getCurrency')->willReturn('USD');

$priceFormatter = new PriceFormatter($this->configMock);

Expand Down Expand Up @@ -190,10 +190,6 @@ public function testBuild()
->method('getOrderIncrementId')
->willReturn('100000001');

$this->configMock->expects($this->once())
->method('getCurrency')
->willReturn('USD');

$this->paymentTokenMock->expects($this->once())
->method('getGatewayToken')
->willReturn('card_token');
Expand Down
14 changes: 3 additions & 11 deletions src/Test/Unit/Gateway/Request/PaymentDataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ protected function setUp()

$this->configMock = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->setMethods(['getCurrency', 'getCurrencyPrecision'])
->setMethods(['getCurrency'])
->getMock();

$this->configMock->method('getCurrencyPrecision')->willReturn('2');

$this->paymentMock = $this->getMockBuilder(Payment::class)
->disableOriginalConstructor()
->setMethods([
Expand Down Expand Up @@ -119,6 +117,8 @@ protected function setUp()

$this->orderMock = $this->createMock(OrderAdapterInterface::class);

$this->configMock->method('getCurrency')->willReturn('USD');

$priceFormatter = new PriceFormatter($this->configMock);

$this->builder = $objectManager->getObject(
Expand Down Expand Up @@ -215,10 +215,6 @@ public function testBuildWithToken()
->method('getOrderIncrementId')
->willReturn('000000101');

$this->configMock->expects($this->once())
->method('getCurrency')
->willReturn('USD');

$this->assertEquals(
$expectedResult,
$this->builder->build($buildSubject)
Expand Down Expand Up @@ -263,10 +259,6 @@ public function testBuildWithSavePayment()
->method('getOrderIncrementId')
->willReturn('000000101');

$this->configMock->expects($this->once())
->method('getCurrency')
->willReturn('USD');

$this->paymentMock->expects($this->at(0))
->method('getAdditionalInformation')
->with('cc_token')
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Unit/Gateway/Request/RefundDataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function setUp()
$this->subjectReader = $this->createMock(SubjectReader::class);

$configMock = $this->createMock(Config::class);
$configMock->method('getCurrencyPrecision')->willReturn('2');
$configMock->method('getCurrency')->willReturn('USD');

$priceFormatter = new PriceFormatter($configMock);

Expand Down
5 changes: 0 additions & 5 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@
<label>Accepted Currency</label>
<source_model>Magento\Config\Model\Config\Source\Locale\Currency</source_model>
</field>
<field id="currency_precision" translate="label comment" type="select" sortOrder="95" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Currency Precision</label>
<comment><![CDATA[This MUST match the smallest currency unit supported by your selected currency. More detailed information can be found in the <a href="https://stripe.com/docs/api/php#charge_object-amount">Stripe API documentation</a>.]]></comment>
<source_model>Pmclain\Stripe\Model\Adminhtml\Source\CurrencyPrecision</source_model>
</field>
<field id="cctypes" translate="label" type="multiselect" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Credit Card Types</label>
<source_model>Pmclain\Stripe\Model\Adminhtml\Source\Cctype</source_model>
Expand Down
1 change: 0 additions & 1 deletion src/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<order_status>processing</order_status>
<debug>0</debug>
<currency>USD</currency>
<currency_precision>2</currency_precision>
<test_mode>1</test_mode>
<test_secret_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<test_publishable_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
Expand Down

0 comments on commit abf0b3f

Please sign in to comment.