class MossGenerator::StripeChargeRow

Parse charge data from single Stripe charge

Attributes

charge[R]
rates[R]

Public Class Methods

new(charge, rates) click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 19
def initialize(charge, rates)
  @charge = charge
  @rates = rates
end

Public Instance Methods

amount_without_vat() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 30
def amount_without_vat
  Money.new(amount_with_vat * percent_without_vat).dollars.to_f
end
amount_without_vat_cents() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 34
def amount_without_vat_cents
  Money.new(amount_with_vat * percent_without_vat).cents
end
country_code() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 24
def country_code
  return fetch_country_code unless fetch_country_code.nil?

  raise NoConsumptionCountryError, "charge: #{charge}"
end
skippable?() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 48
def skippable?
  not_completed? ||
    company? ||
    fetch_country_code.nil? ||
    sold_outside_of_eu? ||
    swedish_charge? ||
    refunded?
end
vat_amount() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 42
def vat_amount
  Money.new(amount_without_vat_cents * vat_rate_calculatable_percent)
       .dollars
       .to_f
end
vat_rate() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 38
def vat_rate
  @vat_rate = special_vat_rate_for_2021_quarter_one
end

Private Instance Methods

amount_with_vat() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 104
def amount_with_vat
  return if skippable?
  return charge['amount'] if charge['currency'].casecmp?('eur')

  exchanged_amount = calculate_amount_from_rate
  return exchanged_amount unless exchanged_amount.nil?

  raise NoExchangeRateForCurrencyOrDateError, "charge: #{charge}"
end
calculate_amount_from_rate() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 114
def calculate_amount_from_rate
  # Have to reverse the rate. The base rate is in EUR, so if we have a rate
  # to SEK, the rate will represent the rate from EUR to SEK, but we need
  # the other way around.
  date = Time.at(charge['created']).to_date.to_s
  currency = charge['currency'].upcase
  rate = rates.dig(date, currency)
  return if rate.nil?

  reversed_rate = 1 / rate
  charge['amount'] * reversed_rate
end
company?() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 70
def company?
  return false if charge.dig('metadata', 'vat_number').nil?

  Valvat::Syntax.validate(charge.dig('metadata', 'vat_number'))
end
fetch_country_code() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 100
def fetch_country_code
  charge.dig('payment_method_details', 'card', 'country')
end
not_completed?() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 84
def not_completed?
  charge['status'] != 'succeeded'
end
percent_without_vat() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 92
def percent_without_vat
  1 / (vat_rate_calculatable_percent + 1)
end
refunded?() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 88
def refunded?
  charge['refunded']
end
sold_outside_of_eu?() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 76
def sold_outside_of_eu?
  ISO3166::Country.new(fetch_country_code).in_eu? ? false : true
end
special_vat_rate_for_2021_quarter_one() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 59
def special_vat_rate_for_2021_quarter_one
  if fetch_country_code.casecmp?('IE')
    changeover_day = Date.parse('2021-03-01').to_time
    return 23 if Time.at(charge['created']) >= changeover_day

    21
  else
    MossGenerator::VatRate.for(country_code)
  end
end
swedish_charge?() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 80
def swedish_charge?
  fetch_country_code.casecmp?('SE')
end
vat_rate_calculatable_percent() click to toggle source
# File lib/moss_generator/stripe_charge_row.rb, line 96
def vat_rate_calculatable_percent
  (vat_rate.to_f / 100)
end