Skip to content

Commit

Permalink
* Add support for refunds
Browse files Browse the repository at this point in the history
* Add payment_detail_id and transaction_type_id to Transaction response
  • Loading branch information
taylorbrooks committed Feb 6, 2018
1 parent 00100dc commit 5633ade
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/rock_rms/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Client
include RockRMS::Client::Person
include RockRMS::Client::PhoneNumber
include RockRMS::Client::RecurringDonation
include RockRMS::Client::Refund
include RockRMS::Client::Transaction
include RockRMS::Client::TransactionDetail

Expand Down
48 changes: 48 additions & 0 deletions lib/rock_rms/resources/refund.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module RockRMS
class Client
module Refund
PATH = 'FinancialTransactionRefunds'.freeze

def create_refund(
batch_id:,
date:,
reason_id:,
transaction_id:
)
old_transaction = list_transactions(
'$expand' => 'TransactionDetails',
'$filter' => "Id eq #{transaction_id}"
).first

params = {
'OriginalTransactionId' => transaction_id,
'RefundReasonValueId' => reason_id,
'FinancialTransaction' => {
'AuthorizedPersonAliasId' => old_transaction[:person_id],
'BatchId' => batch_id,
'FinancialPaymentDetailId' => old_transaction[:payment_detail_id],
'TransactionDateTime' => date,
'TransactionDetails' => translate_negative(old_transaction[:details]),
'TransactionTypeValueId' => old_transaction[:transaction_type_id]
}
}
post(refund_path, params)
end

private

def translate_negative(details)
details.map do |dt|
{
'Amount' => -dt[:amount],
'AccountId' => dt[:fund_id]
}
end
end

def refund_path(id = nil)
id ? "#{PATH}/#{id}" : PATH
end
end
end
end
4 changes: 3 additions & 1 deletion lib/rock_rms/response/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class Transaction < Base
recurring_donation_id: 'ScheduledTransactionId',
summary: 'Summary',
transaction_code: 'TransactionCode',
details: 'TransactionDetails'
details: 'TransactionDetails',
payment_detail_id: 'FinancialPaymentDetailId',
transaction_type_id: 'TransactionTypeValueId'
}.freeze


Expand Down
67 changes: 67 additions & 0 deletions spec/rock_rms/resources/refund_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper'

RSpec.describe RockRMS::Client::Refund, type: :model do
include_context 'resource specs'

describe '#create_refund' do
context 'arguments' do
it 'require `authorized_person_id`' do
expect { client.create_refund }
.to raise_error(ArgumentError, /transaction_id/)
end

it 'require `batch_id`' do
expect { client.create_refund }
.to raise_error(ArgumentError, /batch_id/)
end

it 'require `date`' do
expect { client.create_refund }
.to raise_error(ArgumentError, /date/)
end

it 'require `funds`' do
expect { client.create_refund }
.to raise_error(ArgumentError, /reason_id/)
end

end

subject(:resource) do
client.create_refund(
transaction_id: 1422,
batch_id: 1,
date: '2018-02-01',
reason_id: 1
)
end

it 'returns integer' do
expect(resource).to be_a(Integer)
end

it 'passes options' do
expect(client).to receive(:post)
.with(
'FinancialTransactionRefunds',
'OriginalTransactionId' => 1422,
'RefundReasonValueId' => 1,
'FinancialTransaction' => {
'AuthorizedPersonAliasId' => 120,
'BatchId' => 1,
'FinancialPaymentDetailId' => 156,
'TransactionDateTime' => '2018-02-01',
'TransactionDetails' => [
{
'Amount' => -100.0,
'AccountId' => 23
}
],
'TransactionTypeValueId' => 53
}
)
.and_call_original
resource
end
end
end
2 changes: 2 additions & 0 deletions spec/rock_rms/response/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
expect(r[:recurring_donation_id]).to eq(p['ScheduledTransactionId'])
expect(r[:summary]).to eq(p['Summary'])
expect(r[:transaction_code]).to eq(p['TransactionCode'])
expect(r[:payment_detail_id]).to eq(p['FinancialPaymentDetailId'])
expect(r[:transaction_type_id]).to eq(p['TransactionTypeValueId'])
expect(r[:details]).to eq(
RockRMS::Response::TransactionDetail.format(p['TransactionDetails'])
)
Expand Down
1 change: 1 addition & 0 deletions spec/support/fixtures/create_refund.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345
3 changes: 2 additions & 1 deletion spec/support/rock_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class RockMock < Sinatra::Base
{
create_group_member: 'GroupMembers',
create_transaction: 'FinancialTransactions',
create_batch: 'FinancialBatches'
create_batch: 'FinancialBatches',
create_refund: 'FinancialTransactionRefunds'
}.each do |json, end_point|
post "/api/#{end_point}" do
json_response 201, "#{json}.json"
Expand Down

0 comments on commit 5633ade

Please sign in to comment.