2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
describe InsertRefunds, pending: true do
|
2018-03-25 17:30:42 +00:00
|
|
|
# before(:all) do
|
2019-07-30 21:29:24 +00:00
|
|
|
# stripe_acct = VCR.use_cassette('InsertRefunds/stripe_acct'){Stripe::Account.create(managed: true, country: 'US', email: 'uzr@example.com').id}
|
|
|
|
# @supp = Psql.execute(Qexpr.new.insert('supporters', [{name: 'nnname'}])).first
|
|
|
|
# @payment = Psql.execute(Qexpr.new.insert('payments', [{supporter_id: @supp['id'], gross_amount: 100, fee_total: 33, net_amount: 67, refund_total: 0}]).returning('*')).first
|
|
|
|
# @nonprofit = Psql.execute(Qexpr.new.insert('nonprofits', [{name: 'xxxx', stripe_account_id: stripe_acct}]).returning('*')).first
|
|
|
|
# stripe_token = VCR.use_cassette('InsertRefunds/stripe_token'){Stripe::Token.create(card: StripeTestHelpers::Card_immediate).id}
|
|
|
|
# stripe_charge = VCR.use_cassette('InsertRefunds/stripe_charge'){Stripe::Charge.create(amount: 10000, currency: 'usd', source: stripe_token, description: 'charge 1', destination: stripe_acct)}
|
|
|
|
# @charge = Qx.insert_into(:charges).values({
|
|
|
|
# amount: 100,
|
|
|
|
# stripe_charge_id: stripe_charge.id,
|
|
|
|
# nonprofit_id: @nonprofit['id'],
|
|
|
|
# payment_id: @payment['id'],
|
|
|
|
# supporter_id: @supp['id']
|
|
|
|
# }).returning('*').timestamps.execute.first
|
|
|
|
# @refund_amount = 100
|
|
|
|
# @fees = CalculateFees.for_single_amount(100)
|
|
|
|
# end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
describe '.with_stripe' do
|
|
|
|
context 'when invalid' do
|
|
|
|
it 'raises an error with an invalid charge' do
|
2018-03-25 17:30:42 +00:00
|
|
|
bad_ch = @charge.merge('stripe_charge_id' => 'xxx')
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { InsertRefunds.with_stripe(bad_ch, 'amount' => 1) }.to raise_error(ParamValidation::ValidationError)
|
|
|
|
raise
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
it 'sets a failure message an error with an invalid amount' do
|
2018-03-25 17:30:42 +00:00
|
|
|
bad_ch = @charge.merge('amount' => 0)
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { InsertRefunds.with_stripe(bad_ch, 'amount' => 0) }.to raise_error(ParamValidation::ValidationError)
|
|
|
|
raise
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'returns err if refund amount is greater than payment gross minus payment refund total' do
|
2019-07-30 21:29:24 +00:00
|
|
|
new_payment = Qx.insert_into(:payments).values(gross_amount: 1000, fee_total: 0, net_amount: 1000, refund_total: 500).ts.returning('*').execute.first
|
2018-03-25 17:30:42 +00:00
|
|
|
new_charge = @charge.merge('payment_id' => new_payment['id'])
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { InsertRefunds.with_stripe(new_charge, 'amount' => 600) }.to raise_error(RuntimeError)
|
|
|
|
raise
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
context 'when valid' do
|
2018-03-25 17:30:42 +00:00
|
|
|
# before(:each) do
|
|
|
|
# @result = VCR.use_cassette 'InsertRefunds/result' do
|
|
|
|
# InsertRefunds.with_stripe(@charge, {'amount' => 100})
|
2019-07-30 21:29:24 +00:00
|
|
|
# end
|
2018-03-25 17:30:42 +00:00
|
|
|
# @new_payment = Psql.execute("SELECT * FROM payments WHERE id=#{@payment['id']}").first
|
|
|
|
# end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
it 'sets the stripe refund id' do
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(@result['refund']['stripe_refund_id']).to match(/^re_/)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'creates a negative payment for the refund with the gross amount' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(@result['payment']['gross_amount']).to eq(-@refund_amount)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a negative payment for the refund with the net amount' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(@result['payment']['net_amount']).to eq(-@refund_amount + @fees)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the payment_id on the refund' do
|
|
|
|
expect(@result['refund']['payment_id']).to eq(@result['payment']['id'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments the payment refund total by the gross amount' do
|
|
|
|
expect(@new_payment['refund_total']).to eq(@refund_amount)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the payment supporter id' do
|
|
|
|
expect(@result['payment']['supporter_id']).to eq(@supp['id'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the payment fee_total as negative fees of the original payment' do
|
|
|
|
expect(@result['payment']['fee_total']).to eq(CalculateFees.for_single_amount(@refund_amount))
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|