2015-04-03 20:34:12 +00:00
|
|
|
module JamRuby
|
2015-04-23 21:20:21 +00:00
|
|
|
class RecurlyTransactionWebHook < ActiveRecord::Base
|
|
|
|
|
|
|
|
|
|
attr_accessible :admin_description, :jam_track_id, as: :admin
|
2015-04-03 20:34:12 +00:00
|
|
|
|
|
|
|
|
belongs_to :user, class_name: 'JamRuby::User'
|
2015-04-12 18:45:26 +00:00
|
|
|
belongs_to :sale_line_item, class_name: 'JamRuby::SaleLineItem', foreign_key: 'subscription_id', primary_key: 'recurly_subscription_uuid', inverse_of: :recurly_transactions
|
|
|
|
|
belongs_to :sale, class_name: 'JamRuby::Sale', foreign_key: 'invoice_id', primary_key: 'recurly_invoice_id', inverse_of: :recurly_transactions
|
2015-04-03 20:34:12 +00:00
|
|
|
|
2015-04-23 21:20:21 +00:00
|
|
|
# when we know what JamTrack this refund is related to, we set this value
|
|
|
|
|
belongs_to :jam_track, class_name: 'JamRuby::JamTrack'
|
|
|
|
|
|
2015-04-03 20:34:12 +00:00
|
|
|
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
|
|
|
|
|
|
2015-04-12 18:45:26 +00:00
|
|
|
|
2015-04-03 20:34:12 +00:00
|
|
|
SUCCESSFUL_PAYMENT = 'payment'
|
|
|
|
|
FAILED_PAYMENT = 'failed_payment'
|
|
|
|
|
REFUND = 'refund'
|
|
|
|
|
VOID = 'void'
|
|
|
|
|
|
2015-04-23 21:20:21 +00:00
|
|
|
|
|
|
|
|
HOOK_TYPES = [SUCCESSFUL_PAYMENT, FAILED_PAYMENT, REFUND, VOID]
|
|
|
|
|
|
2015-04-12 18:45:26 +00:00
|
|
|
def is_credit_type?
|
|
|
|
|
transaction_type == REFUND || transaction_type == VOID
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_voided?
|
|
|
|
|
transaction_type == VOID
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_refund?
|
|
|
|
|
transaction_type == REFUND
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-03 20:34:12 +00:00
|
|
|
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
|
|
|
|
|
|
2015-04-23 21:20:21 +00:00
|
|
|
def admin_url
|
|
|
|
|
APP_CONFIG.admin_root_url + "/admin/recurly_hooks/" + id
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-03 20:34:12 +00:00
|
|
|
# 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'
|
2015-04-10 20:19:08 +00:00
|
|
|
sale = Sale.find_by_recurly_invoice_id(transaction.invoice_id)
|
|
|
|
|
|
2015-11-29 19:58:10 +00:00
|
|
|
if sale
|
|
|
|
|
AdminMailer.recurly_alerts(transaction.user, {
|
|
|
|
|
subject: "ACTION REQUIRED: #{transaction.user.email} has refund on invoice",
|
|
|
|
|
body: "You will have to manually revoke any JamTrackRights in our database for the appropriate JamTracks"
|
2016-07-17 15:16:27 +00:00
|
|
|
}).deliver_now
|
2015-11-29 19:58:10 +00:00
|
|
|
|
2015-04-23 21:20:21 +00:00
|
|
|
else
|
|
|
|
|
AdminMailer.recurly_alerts(transaction.user, {
|
|
|
|
|
subject: "ACTION REQUIRED: #{transaction.user.email} has refund with no correlator to sales",
|
|
|
|
|
body: "You will have to manually revoke any JamTrackRights in our database for the appropriate JamTracks"
|
2016-07-17 15:16:27 +00:00
|
|
|
}).deliver_now
|
2015-04-10 20:19:08 +00:00
|
|
|
end
|
|
|
|
|
|
2015-04-23 21:20:21 +00:00
|
|
|
|
2015-04-03 20:34:12 +00:00
|
|
|
end
|
|
|
|
|
transaction
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|