Module: SolidusStripe::MoneyToStripeAmountConverter

Extended by:
ActiveSupport::Concern, MoneyToStripeAmountConverter
Included in:
Gateway, MoneyToStripeAmountConverter, RefundsSynchronizer, Webhook::ChargeSubscriber, Webhook::PaymentIntentSubscriber
Defined in:
lib/solidus_stripe/money_to_stripe_amount_converter.rb

Overview

Helpers to interoperate amounts between Solidus and Stripe.

Solidus will provide a "fractional" amount, that is specific for each currency following the configuration defined in the Money gem.

Stripe uses the "smallest currency unit", (e.g., 100 cents to charge $1.00 or 100 to charge ¥100, a zero-decimal currency).

We need to ensure the fractional amount is considering the same number of decimals.

Constant Summary collapse

ZERO_DECIMAL_CURRENCIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[
  BIF
  CLP
  DJF
  GNF
  JPY
  KMF
  KRW
  MGA
  PYG
  RWF
  UGX
  VND
  VUV
  XAF
  XOF
  XPF
].freeze
THREE_DECIMAL_CURRENCIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[
  BHD
  JOD
  KWD
  OMR
  TND
].freeze
DIVISIBLE_BY_100 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Special currencies that are represented in cents but should be divisible by 100, thus making them integer only.

%w[
  HUF
  TWD
  UGX
].freeze

Instance Method Summary collapse

Instance Method Details

#solidus_decimal_to_subunit(amount, currency) ⇒ Integer

Transforms a decimal amount into a subunit amount

Parameters:

  • amount (BigDecimal)
  • currency (String)

Returns:

  • (Integer)


82
83
84
# File 'lib/solidus_stripe/money_to_stripe_amount_converter.rb', line 82

def solidus_decimal_to_subunit(amount, currency)
  Money.from_amount(amount, currency).fractional
end

#solidus_subunit_to_decimal(amount, currency) ⇒ BigDecimal

Transforms a subunit amount into a decimal amount

Parameters:

  • amount (Integer)
  • currency (String)

Returns:

  • (BigDecimal)


91
92
93
# File 'lib/solidus_stripe/money_to_stripe_amount_converter.rb', line 91

def solidus_subunit_to_decimal(amount, currency)
  Money.new(amount, currency).to_d
end

#to_solidus_amount(fractional, currency) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/solidus_stripe/money_to_stripe_amount_converter.rb', line 67

def to_solidus_amount(fractional, currency)
  solidus_subunit_to_unit, stripe_subunit_to_unit = subunit_to_unit(currency)

  if stripe_subunit_to_unit == solidus_subunit_to_unit
    fractional
  else
    (fractional / stripe_subunit_to_unit.to_d) * solidus_subunit_to_unit.to_d
  end
end

#to_stripe_amount(fractional, currency) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/solidus_stripe/money_to_stripe_amount_converter.rb', line 57

def to_stripe_amount(fractional, currency)
  solidus_subunit_to_unit, stripe_subunit_to_unit = subunit_to_unit(currency)

  if stripe_subunit_to_unit == solidus_subunit_to_unit
    fractional
  else
    (fractional / solidus_subunit_to_unit.to_d) * stripe_subunit_to_unit.to_d
  end
end