jam-cloud/ruby/lib/jam_ruby/models/recurly_transaction_web_hoo...

78 lines
2.8 KiB
Ruby

module JamRuby
class RecurlyTransactionWebHook < ActiveRecord::Base
belongs_to :user, class_name: 'JamRuby::User'
validates :recurly_transaction_id, presence: true
validates :subscription_id, presence: true
validates :action, presence: true
validates :status, presence: true
validates :amount_in_cents, numericality: {only_integer: true}
validates :user, presence: true
SUCCESSFUL_PAYMENT = 'payment'
FAILED_PAYMENT = 'failed_payment'
REFUND = 'refund'
VOID = 'void'
def self.is_transaction_web_hook?(document)
return false if document.root.nil?
case document.root.name
when 'successful_payment_notification'
true
when 'successful_refund_notification'
true
when 'failed_payment_notification'
true
when 'void_payment_notification'
true
else
false
end
end
# see spec for examples of XML
def self.create_from_xml(document)
transaction = RecurlyTransactionWebHook.new
case document.root.name
when 'successful_payment_notification'
transaction.transaction_type = SUCCESSFUL_PAYMENT
when 'successful_refund_notification'
transaction.transaction_type = REFUND
when 'failed_payment_notification'
transaction.transaction_type = FAILED_PAYMENT
when 'void_payment_notification'
transaction.transaction_type = VOID
else
raise 'unknown document type ' + document.root.name
end
transaction.recurly_transaction_id = document.at_css('transaction id').content
transaction.user_id = document.at_css('account account_code').content
transaction.subscription_id = document.at_css('subscription_id').content
transaction.invoice_id = document.at_css('invoice_id').content
transaction.invoice_number_prefix = document.at_css('invoice_number_prefix').content
transaction.invoice_number = document.at_css('invoice_number').content
transaction.action = document.at_css('action').content
transaction.status = document.at_css('status').content
transaction.transaction_at = Time.parse(document.at_css('date').content)
transaction.amount_in_cents = document.at_css('amount_in_cents').content
transaction.reference = document.at_css('reference').content
transaction.message = document.at_css('message').content
transaction.save!
# now that we have the transaction saved, we also need to delete the jam_track_right if this is a refund, or voided
if transaction.transaction_type == 'refund' || transaction.transaction_type == 'void'
right = JamTrackRight.find_by_recurly_subscription_uuid(transaction.subscription_id)
right.destroy if right
end
transaction
end
end
end