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

111 lines
4.8 KiB
Ruby
Raw Normal View History

module JamRuby
class RecurlyTransactionWebHook < ActiveRecord::Base
belongs_to :user, class_name: 'JamRuby::User'
validates :recurly_transaction_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'
sale = Sale.find_by_recurly_invoice_id(transaction.invoice_id)
if sale && sale.is_jam_track_sale?
if sale.sale_line_items.length == 1
if sale.recurly_total_in_cents == transaction.amount_in_cents
jam_track = sale.sale_line_items[0].product
jam_track_right = jam_track.right_for_user(transaction.user) if jam_track
if jam_track_right
jam_track_right.destroy
AdminMailer.alerts({
subject:"NOTICE: #{transaction.user.email} has had JamTrack: #{jam_track.name} revoked",
body: "A void event came from Recurly for sale with Recurly invoice ID #{sale.recurly_invoice_id}. We deleted their right to the track in our own database as a result."
}).deliver
else
AdminMailer.alerts({
subject:"NOTICE: #{transaction.user.email} got a refund, but unable to find JamTrackRight to delete",
body: "This should just mean the user already has no rights to the JamTrackRight when the refund came in. Not a big deal, but sort of weird..."
}).deliver
end
else
AdminMailer.alerts({
subject:"ACTION REQUIRED: #{transaction.user.email} got a refund it was not for total value of a JamTrack sale",
body: "We received a refund notice for an amount that was not the same as the original sale. So, no action was taken in the database. sale total: #{sale.recurly_total_in_cents}, refund amount: #{transaction.amount_in_cents}"
}).deliver
end
else
AdminMailer.alerts({
subject: "ACTION REQUIRED: #{transaction.user.email} has refund on invoice with multiple JamTracks",
body: "You will have to manually revoke any JamTrackRights in our database for the appropriate JamTracks"
}).deliver
end
end
end
transaction
end
end
end