discourse-subscriptions/app/serializers/discourse_subscriptions/payment_serializer.rb

60 lines
1.0 KiB
Ruby
Raw Permalink Normal View History

2019-09-13 22:56:28 -04:00
# frozen_string_literal: true
module DiscourseSubscriptions
class PaymentSerializer < ApplicationSerializer
attributes :payment_intent_id,
:receipt_email,
:url,
:created_at_age,
:amount,
:amount_currency,
:username,
:user_id
2019-09-14 00:20:16 -04:00
def created_at_age
Time.now - object.created_at
end
2019-09-14 01:55:25 -04:00
def amount_currency
ActiveSupport::NumberHelper.number_to_currency(
object.amount / 100,
precision: 2,
unit: currency_unit
)
end
2019-09-14 01:55:25 -04:00
def username
user&.username
end
2019-09-14 02:34:51 -04:00
private
2019-09-14 01:55:25 -04:00
def user
begin
User.find(object.user_id)
rescue
nil
end
end
2019-09-14 02:34:51 -04:00
def currency_unit
case object.currency
when "eur"
""
when "gbp"
"£"
when "inr"
""
when "brl"
"R$"
2021-04-06 10:59:27 -04:00
when "dkk"
"KR"
when "sgd"
"S$"
else
"$"
end
2019-09-14 01:55:25 -04:00
end
end
2019-09-13 22:56:28 -04:00
end