2019-09-14 12:56:28 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
module DiscourseSubscriptions
|
|
|
|
class PaymentSerializer < ApplicationSerializer
|
2022-12-29 12:35:06 +00:00
|
|
|
attributes :payment_intent_id,
|
|
|
|
:receipt_email,
|
|
|
|
:url,
|
|
|
|
:created_at_age,
|
|
|
|
:amount,
|
|
|
|
:amount_currency,
|
|
|
|
:username,
|
|
|
|
:user_id
|
2019-09-14 14:20:16 +10:00
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
def created_at_age
|
|
|
|
Time.now - object.created_at
|
|
|
|
end
|
2019-09-14 15:55:25 +10:00
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
def amount_currency
|
|
|
|
ActiveSupport::NumberHelper.number_to_currency(
|
|
|
|
object.amount / 100,
|
|
|
|
precision: 2,
|
2022-12-29 12:35:06 +00:00
|
|
|
unit: currency_unit,
|
2020-07-06 15:19:45 -05:00
|
|
|
)
|
|
|
|
end
|
2019-09-14 15:55:25 +10:00
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
def username
|
|
|
|
user&.username
|
|
|
|
end
|
2019-09-14 16:34:51 +10:00
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
private
|
2019-09-14 15:55:25 +10:00
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
def user
|
|
|
|
begin
|
|
|
|
User.find(object.user_id)
|
2022-12-29 12:35:06 +00:00
|
|
|
rescue StandardError
|
2020-07-06 15:19:45 -05:00
|
|
|
nil
|
|
|
|
end
|
2020-04-23 16:06:02 +05:30
|
|
|
end
|
2019-09-14 16:34:51 +10:00
|
|
|
|
2020-07-06 15:19:45 -05:00
|
|
|
def currency_unit
|
|
|
|
case object.currency
|
|
|
|
when "eur"
|
|
|
|
"€"
|
|
|
|
when "gbp"
|
|
|
|
"£"
|
|
|
|
when "inr"
|
|
|
|
"₹"
|
|
|
|
when "brl"
|
|
|
|
"R$"
|
2021-04-06 16:59:27 +02:00
|
|
|
when "dkk"
|
|
|
|
"KR"
|
2021-04-13 18:20:00 +08:00
|
|
|
when "sgd"
|
|
|
|
"S$"
|
2020-07-06 15:19:45 -05:00
|
|
|
else
|
|
|
|
"$"
|
|
|
|
end
|
2019-09-14 15:55:25 +10:00
|
|
|
end
|
|
|
|
end
|
2019-09-14 12:56:28 +10:00
|
|
|
end
|