discourse-subscriptions/app/serializers/payment_serializer.rb

50 lines
794 B
Ruby
Raw Normal View History

2019-09-13 22:56:28 -04:00
# frozen_string_literal: true
class PaymentSerializer < ApplicationSerializer
2019-09-14 02:34:51 -04:00
attributes :payment_intent_id,
:receipt_email,
:url,
:created_at_age,
:amount,
:amount_currency,
:username
2019-09-14 02:53:07 -04:00
: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(
2019-09-14 02:34:51 -04:00
object.amount / 100,
2019-09-14 01:55:25 -04:00
precision: 2,
unit: currency_unit
)
end
2019-09-14 02:34:51 -04:00
def username
user&.username
end
2019-09-14 01:55:25 -04:00
private
2019-09-14 02:34:51 -04:00
def user
begin
User.find(object.user_id)
rescue
nil
end
end
2019-09-14 01:55:25 -04:00
def currency_unit
case object.currency
when "eur"
""
when "gbp"
"£"
else
"$"
end
end
2019-09-13 22:56:28 -04:00
end