Module: RockRMS::Client::Refund

Included in:
RockRMS::Client
Defined in:
lib/rock_rms/resources/refund.rb

Constant Summary collapse

PATH =
'FinancialTransactionRefunds'.freeze

Instance Method Summary collapse

Instance Method Details

#create_refund(batch_id:, date:, reason_id:, transaction_id:, transaction_code: nil, amount: nil, gateway_id: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rock_rms/resources/refund.rb', line 6

def create_refund(
  batch_id:,
  date:,
  reason_id:,
  transaction_id:,
  transaction_code: nil,
  amount: nil,
  gateway_id: nil
)

  old_transaction = list_transactions(
    '$expand' => 'TransactionDetails',
    '$filter' => "Id eq #{transaction_id}"
  ).first

  transaction_amount = old_transaction[:details].map{|d| d[:amount]}.reduce(0.0){|sum,x| sum + x }


  refund_amount = amount || transaction_amount

  params = {
    'OriginalTransactionId' => transaction_id,
    'RefundReasonValueId' => reason_id,
    'FinancialTransaction' => {
      'AuthorizedPersonAliasId' => old_transaction[:person_id],
      'BatchId' => batch_id,
      'FinancialGatewayId' => gateway_id,
      'FinancialPaymentDetailId' => old_transaction[:payment_detail_id],
      'TransactionCode'     => transaction_code,
      'TransactionDateTime' => date,
      'TransactionDetails'  => refunded_details(old_transaction[:details], transaction_amount, refund_amount),
      'TransactionTypeValueId' => old_transaction[:transaction_type_id],
      'SourceTypeValueId' => old_transaction[:source_type_id],
      'ScheduledTransactionId' => old_transaction[:recurring_donation_id]
    }
  }
  post(refund_path, params)
end