Skip to content

Commit

Permalink
Using Transaction and its nested classes on InsertRefunds
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarissa Borges committed Jul 9, 2021
1 parent 52bc7fe commit 022cf91
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 110 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ AllCops:
- 'spec/lib/format/currency_spec.rb'
- 'spec/lib/format/url_spec.rb'
- 'spec/lib/format/dedication_spec.rb'
- 'spec/lib/insert/insert_refunds_spec.rb'
- 'spec/lib/insert/insert_recurring_donation_spec.rb'
- 'spec/lib/insert/insert_charge_spec.rb'
- 'spec/lib/insert/insert_disputes_spec.rb'
Expand Down Expand Up @@ -751,3 +750,6 @@ Style/PreferredHashMethods:
Style/LambdaCall:
# jbuilder uses this a lot so we want it.
Enabled: false

RSpec/MultipleMemoizedHelpers:
Max: 15
15 changes: 7 additions & 8 deletions app/models/subtransaction_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class SubtransactionPayment < ApplicationRecord
has_one :supporter, through: :subtransaction
has_one :nonprofit, through: :subtransaction

delegated_type :paymentable,
types: %w[
OfflineTransactionCharge
OfflineTransactionDispute
OfflineTransactionRefund
StripeCharge
StripeRefund
]
delegated_type :paymentable, types: %w[
OfflineTransactionCharge
OfflineTransactionDispute
OfflineTransactionRefund
StripeCharge
StripeRefund
]

delegate :gross_amount, :fee_total, :net_amount, to: :paymentable

Expand Down
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_06_02_220525) do
ActiveRecord::Schema.define(version: 2021_06_21_191202) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -814,6 +814,20 @@
t.index ["payment_id"], name: "index_stripe_charges_on_payment_id"
end

create_table "stripe_disputes", id: :string, force: :cascade do |t|
t.bigint "payment_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["payment_id"], name: "index_stripe_disputes_on_payment_id"
end

create_table "stripe_refunds", id: :string, force: :cascade do |t|
t.bigint "payment_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["payment_id"], name: "index_stripe_refunds_on_payment_id"
end

create_table "stripe_transactions", id: :string, force: :cascade do |t|
t.integer "amount", null: false
t.datetime "created_at", precision: 6, null: false
Expand Down Expand Up @@ -1051,6 +1065,8 @@
add_foreign_key "recurrences", "recurring_donations"
add_foreign_key "recurrences", "supporters"
add_foreign_key "stripe_charges", "payments"
add_foreign_key "stripe_disputes", "payments"
add_foreign_key "stripe_refunds", "payments"
add_foreign_key "subtransaction_payments", "subtransactions"
add_foreign_key "subtransactions", "transactions"
add_foreign_key "ticket_purchases", "event_discounts"
Expand Down
20 changes: 20 additions & 0 deletions lib/insert/insert_refunds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ def self.with_stripe(charge, h)
refund_row = Qx.update(:refunds).set(payment_id: payment_row['id']).ts.where(id: refund_row['id']).returning('*').execute.first
# Update original payment to increment its refund_total for any future refund attempts
Qx.update(:payments).set("refund_total=refund_total + #{h['amount'].to_i}").ts.where(id: original_payment['id']).execute

refund = Refund.find(refund_row['id'])
refund_payment = Payment.find(payment_row['id'])

stripe_transaction_charge = StripeCharge.find_by(payment: original_payment['id'])
stripe_transaction_refund = StripeRefund.new(payment: refund_payment)
transaction_id = stripe_transaction_charge.subtransaction_payment.subtransaction.transaction_id
transaction = Transaction.find(transaction_id)

transaction.amount += gross
refund_subtransaction_payment = transaction.subtransaction.subtransaction_payments.create(paymentable: stripe_transaction_refund)

stripe_transaction_refund.save!
refund_subtransaction_payment.save!
transaction.save!

transaction.publish_updated
transaction.publish_refunded
refund_subtransaction_payment.publish_created

# Send the refund receipts in a delayed job
Houdini.event_publisher.announce(:create_refund, Refund.find(refund_row['id']))
{ 'payment' => payment_row, 'refund' => refund_row }
Expand Down
Loading

0 comments on commit 022cf91

Please sign in to comment.