class SKVReport::StripeChargeRow

Parse charge data from single Stripe charge Sales amount will not contain any cents, so we round All amounts should be handled as without vat

Attributes

buyer_vat_number[R]
charge[R]
exchange_rates[R]

Public Class Methods

new(charge, exchange_rates) click to toggle source

charge: a stripe charge exchange_rates: a SKVReport::ExchangeRates object

# File lib/skv_report/stripe_charge_row.rb, line 21
def initialize(charge, exchange_rates)
  @charge = charge
  @buyer_vat_number = vat_number
  @exchange_rates = exchange_rates
end

Public Instance Methods

sales_amount() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 27
def sales_amount
  Money.new(amount).dollars.to_f.round
end
skippable?() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 31
def skippable?
  buyer_vat_number.nil? ||
    swedish_buyer? ||
    !valid_vat_number? ||
    !eu_member? ||
    not_completed? ||
    refunded?
end

Private Instance Methods

amount() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 62
def amount
  currency = charge['currency']
  return charge['amount'] if currency.casecmp?('sek')

  date = Time.at(charge['created']).to_date.to_s
  convert_amount_to_sek(exchange_rates.rate_for(currency, date))
end
convert_amount_to_sek(exchange_rate) click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 70
def convert_amount_to_sek(exchange_rate)
  (charge['amount'] * (1 / exchange_rate)).round
end
eu_member?() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 54
def eu_member?
  ISO3166::Country.new(buyer_vat_number[0...2])&.in_eu?
end
not_completed?() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 74
def not_completed?
  charge['status'] != 'succeeded'
end
refunded?() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 78
def refunded?
  charge['refunded']
end
swedish_buyer?() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 50
def swedish_buyer?
  buyer_vat_number[0...2].casecmp?('SE')
end
valid_vat_number?() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 58
def valid_vat_number?
  Valvat::Syntax.validate(buyer_vat_number)
end
vat_number() click to toggle source
# File lib/skv_report/stripe_charge_row.rb, line 42
def vat_number
  tax_object = charge.dig('customer', 'tax_ids', 'data').find do |tax_data|
    tax_data['type'].casecmp?('eu_vat')
  end

  tax_object&.fetch('value', '')
end