osem-fcy/app/models/payment.rb

44 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Payment < ApplicationRecord
2016-07-27 19:27:39 +05:30
has_many :ticket_purchases
belongs_to :user
belongs_to :conference
attr_accessor :stripe_customer_email
attr_accessor :stripe_customer_token
2016-07-27 19:27:39 +05:30
validates :status, presence: true
validates :user_id, presence: true
validates :conference_id, presence: true
2024-12-02 13:15:26 +01:00
enum :status, {
2018-11-13 18:01:49 -08:00
unpaid: 0,
2016-07-27 19:27:39 +05:30
success: 1,
failure: 2
}
def amount_to_pay
Ticket.total_price(conference, user, paid: false).cents
end
def purchase
2018-11-13 18:01:49 -08:00
gateway_response = Stripe::Charge.create source: stripe_customer_token,
receipt_email: stripe_customer_email,
2018-11-13 18:01:49 -08:00
description: "ticket purchases(#{user.username})",
amount: amount_to_pay,
currency: conference.tickets.first.price_currency
2016-08-09 02:29:44 +05:30
self.amount = gateway_response[:amount]
self.last4 = gateway_response[:source][:last4]
self.authorization_code = gateway_response[:id]
self.status = 'success'
true
rescue Stripe::StripeError => error
errors.add(:base, error.message)
self.status = 'failure'
false
2016-07-27 19:27:39 +05:30
end
end