diff --git a/app/models/conference.rb b/app/models/conference.rb index 9079bd71..ad95e902 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -20,6 +20,7 @@ class Conference < ActiveRecord::Base has_one :program, dependent: :destroy has_one :venue, dependent: :destroy has_many :ticket_purchases, dependent: :destroy + has_many :payments, dependent: :destroy has_many :supporters, through: :ticket_purchases, source: :user has_many :tickets, dependent: :destroy diff --git a/app/models/payment.rb b/app/models/payment.rb new file mode 100644 index 00000000..ecc4db5d --- /dev/null +++ b/app/models/payment.rb @@ -0,0 +1,27 @@ +class Payment < ActiveRecord::Base + has_many :ticket_purchases + belongs_to :user + belongs_to :conference + + validates :last4, presence: true + validates :authorization_code, presence: true + validates :status, presence: true + validates :amount, presence: true, numericality: { greater_than: 0 } + validates :user_id, presence: true + validates :conference_id, presence: true + + enum status: { + unpaid: 0, + success: 1, + failure: 2 + } + + def self.purchase(gateway_response, user, conference) + create(last4: gateway_response[:source][:last4], + amount: gateway_response[:amount], + status: (gateway_response[:paid] ? 1 : 0), + authorization_code: gateway_response[:id], + user_id: user.id, + conference_id: conference.id) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index d3c847a2..4878ed9a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -38,6 +38,7 @@ class User < ActiveRecord::Base has_many :registrations, dependent: :destroy has_many :events_registrations, through: :registrations has_many :ticket_purchases, dependent: :destroy + has_many :payments, dependent: :destroy has_many :tickets, through: :ticket_purchases, source: :ticket has_many :votes, dependent: :destroy has_many :voted_events, through: :votes, source: :events