2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'psql'
|
|
|
|
require 'qexpr'
|
|
|
|
require 'calculate/calculate_fees'
|
|
|
|
require 'active_support/core_ext'
|
|
|
|
|
|
|
|
module InsertDisputes
|
|
|
|
# A new dispute takes a charge id and dispute id and creates:
|
|
|
|
# A dispute row with the charge gross amount and the dispute id
|
|
|
|
# A payment row negative gross and net, just like a refund, but with kind "Dispute"
|
|
|
|
def self.create_record(stripe_charge_id, stripe_dispute_id)
|
|
|
|
# Find the existing charge
|
2019-07-30 21:29:24 +00:00
|
|
|
ch = Qx.select('*').from('charges').where('stripe_charge_id=$id', id: stripe_charge_id).ex.first
|
|
|
|
raise ArgumentError, 'Charge not found' if ch.nil?
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
now = Time.current
|
|
|
|
|
|
|
|
result[:payment] = Psql.execute(
|
|
|
|
Qexpr.new.insert(:payments, [{
|
2019-07-30 21:29:24 +00:00
|
|
|
gross_amount: -ch['amount'],
|
|
|
|
fee_total: 0,
|
|
|
|
net_amount: -ch['amount'],
|
|
|
|
kind: 'Dispute',
|
|
|
|
refund_total: 0,
|
|
|
|
nonprofit_id: ch['nonprofit_id'],
|
|
|
|
supporter_id: ch['supporter_id'],
|
|
|
|
donation_id: ch['donation_id'],
|
|
|
|
date: now
|
|
|
|
}]).returning('*')
|
2018-03-25 17:30:42 +00:00
|
|
|
).first
|
|
|
|
|
|
|
|
# Create a dispute record
|
|
|
|
result[:dispute] = Psql.execute(
|
|
|
|
Qexpr.new.insert(:disputes, [{
|
2019-07-30 21:29:24 +00:00
|
|
|
gross_amount: ch['amount'],
|
|
|
|
status: :needs_response,
|
|
|
|
charge_id: ch['id'],
|
|
|
|
reason: :unrecognized,
|
|
|
|
payment_id: result[:payment]['id'],
|
|
|
|
stripe_dispute_id: stripe_dispute_id
|
|
|
|
}]).returning('*')
|
2018-03-25 17:30:42 +00:00
|
|
|
).first
|
|
|
|
|
|
|
|
# Prevent refunds from being able to happen on the payment
|
|
|
|
Qx.update(:payments).set(refund_total: ch['amount']).where(id: ch['payment_id']).ex
|
|
|
|
|
|
|
|
# Insert an activity record
|
|
|
|
InsertActivities.for_disputes([result[:payment]['id']])
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
result
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|