Commit 59a6f5d0 authored by bojanz's avatar bojanz

Issue #2928972 by bojanz: Add PaymentGatewayBase::toMinorUnits()

parent 70ab01e5
......@@ -9,6 +9,7 @@ use Drupal\commerce_payment\Exception\HardDeclineException;
use Drupal\commerce_payment\Exception\InvalidRequestException;
use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_price\Calculator;
use Drupal\commerce_price\Price;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\NestedArray;
......@@ -360,6 +361,30 @@ abstract class PaymentGatewayBase extends PluginBase implements PaymentGatewayIn
return $operations;
}
/**
* Converts the given amount to its minor units.
*
* For example, 9.99 USD becomes 999.
*
* @param \Drupal\commerce_price\Price $amount
* The amount.
*
* @return int
* The amount in minor units, as an integer.
*/
protected function toMinorUnits(Price $amount) {
$currency_storage = $this->entityTypeManager->getStorage('commerce_currency');
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
$currency = $currency_storage->load($amount->getCurrencyCode());
$fraction_digits = $currency->getFractionDigits();
$number = $amount->getNumber();
if ($fraction_digits > 0) {
$number = Calculator::multiply($number, pow(10, $fraction_digits));
}
return round($number, 0);
}
/**
* Gets the remote customer ID for the given user.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment