houdini/lib/insert/insert_disputes.rb
Bradley M. Kuhn 6772312ea7 Relicense all .rb files under new project license.
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

with some specific files to be licensed under the one of two licenses:
   CC0-1.0
   LGPL-3.0-or-later

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

56 lines
1.7 KiB
Ruby

# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
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
ch = Qx.select("*").from("charges").where("stripe_charge_id=$id", id: stripe_charge_id).ex.first
raise ArgumentError.new("Charge not found") if ch.nil?
result = {}
now = Time.current
result[:payment] = Psql.execute(
Qexpr.new.insert(:payments, [{
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('*')
).first
# Create a dispute record
result[:dispute] = Psql.execute(
Qexpr.new.insert(:disputes, [{
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('*')
).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']])
return result
end
end