Fix round in stripe amount
This commit is contained in:
parent
4e9965ea70
commit
0e960ea32d
3 changed files with 47 additions and 3 deletions
|
|
@ -252,7 +252,6 @@ if ($event->type == 'payout.created' && getDolGlobalString('STRIPE_AUTO_RECORD_P
|
|||
$dateo = dol_now();
|
||||
$label = $event->data->object->description;
|
||||
$amount = $stripe->convertAmount($event->data->object->amount, $currency_code, 1);
|
||||
$amount_to = $stripe->convertAmount($event->data->object->amount, $currency_code, 1);
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
|
||||
|
||||
|
|
|
|||
|
|
@ -342,14 +342,14 @@ class Stripe extends CommonObject
|
|||
* @param int|float $amount Amount in Stripe format (For example 1234 for 12.34 euros)
|
||||
* @param string $currency_code Currency code (Example 'EUR')
|
||||
* @param int $direction 0=From standard to Stripe amount, 1=From Stripe to standard amount
|
||||
* @return float Standard float amount (For example 12.34)
|
||||
* @return int|float Standard float amount (For example 12.34)
|
||||
*/
|
||||
public function convertAmount($amount, $currency_code, $direction = 0)
|
||||
{
|
||||
$arrayzerounitcurrency = array('BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VND', 'VUV', 'XAF', 'XOF', 'XPF');
|
||||
if (!in_array($currency_code, $arrayzerounitcurrency)) {
|
||||
if (empty($direction)) {
|
||||
$newamount = (int) ($amount * 100);
|
||||
$newamount = (int) round($amount * 100); // If $amount is 79.99, doing 79.99 * 100 returns float 7998.999999999999, and "int" do a truncation into 7998 so we must first use round to get nearest integer value
|
||||
} else {
|
||||
$newamount = (float) ($amount / 100);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ global $conf,$user,$langs,$db;
|
|||
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/core/lib/geturl.lib.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/stripe/lib/stripe.lib.php';
|
||||
require_once dirname(__FILE__).'/../../htdocs/stripe/class/stripe.class.php';
|
||||
require_once dirname(__FILE__).'/CommonClassTest.class.php';
|
||||
|
||||
if (empty($user->id)) {
|
||||
|
|
@ -93,4 +94,48 @@ class StripeTest extends CommonClassTest
|
|||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* testStripeOk
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStripeConvertAmount()
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
$conf = $this->savconf;
|
||||
$user = $this->savuser;
|
||||
$langs = $this->savlangs;
|
||||
$db = $this->savdb;
|
||||
|
||||
$stripe = new Stripe($db);
|
||||
|
||||
$amount = $stripe->convertAmount(79.99, 'EUR', 0);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(7999, $amount);
|
||||
|
||||
$amount = $stripe->convertAmount((float) 79.99, 'EUR', 0);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(7999, $amount);
|
||||
|
||||
$amount = $stripe->convertAmount('79.99', 'EUR', 0);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(7999, $amount);
|
||||
|
||||
$amount = $stripe->convertAmount('79.99', 'JPY', 0);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(79.99, $amount);
|
||||
|
||||
$amount = $stripe->convertAmount((int) 7999, 'EUR', 1);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(79.99, $amount);
|
||||
|
||||
$amount = $stripe->convertAmount((float) 7999, 'EUR', 1);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(79.99, $amount);
|
||||
|
||||
$amount = $stripe->convertAmount(7999, 'JPY', 1);
|
||||
print __METHOD__." amount=".$amount."\n";
|
||||
$this->assertEquals(7999, $amount);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue