Skip to content
Snippets Groups Projects

Issue #3208109: Decimal places from Commerce config is not reliable

1 file
+ 24
0
Compare changes
  • Side-by-side
  • Inline
@@ -9,6 +9,8 @@ use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayBase;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsAuthorizationsInterface;
use Drupal\commerce_price\Calculator;
use Drupal\commerce_price\Price;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -20,6 +22,12 @@ use Stripe\Stripe;
*/
abstract class StripeGatewayBase extends OnsitePaymentGatewayBase implements SupportsAuthorizationsInterface {
/**
* List of zero decimal currencies to convert amount correctly for Stripe API.
* https://stripe.com/docs/currencies#zero-decimal
*/
const ZERO_DECIMAL_CURRENCIES = ['BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'];
/**
* {@inheritdoc}
*/
@@ -173,4 +181,20 @@ abstract class StripeGatewayBase extends OnsitePaymentGatewayBase implements Sup
throw new HardDeclineException('The method is not implemented yet.');
}
/**
* Convert amount to minor units of specific currency according to Stripe.
*
* Stripe currencies could be different to Drupal currencies, so use constant lists from:
* https://stripe.com/docs/currencies#zero-decimal
*/
public function toMinorUnits(Price $amount) {
$fraction_digits = in_array($amount->getCurrencyCode(), self::ZERO_DECIMAL_CURRENCIES) ? 0 : 2;
$number = $amount->getNumber();
if ($fraction_digits > 0) {
$number = Calculator::multiply($number, pow(10, $fraction_digits));
}
return round($number, 0);
}
}
Loading