Module: StripeMock::RequestHandlers::Refunds

Included in:
Instance
Defined in:
lib/stripe_mock/request_handlers/refunds.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
# File 'lib/stripe_mock/request_handlers/refunds.rb', line 5

def Refunds.included(klass)
  klass.add_handler 'post /v1/refunds',               :new_refund
  klass.add_handler 'get /v1/refunds',                :get_refunds
  klass.add_handler 'get /v1/refunds/(.*)',           :get_refund
  klass.add_handler 'post /v1/refunds/(.*)',          :update_refund
end

Instance Method Details

#get_refund(route, method_url, params, headers) ⇒ Object



68
69
70
71
72
# File 'lib/stripe_mock/request_handlers/refunds.rb', line 68

def get_refund(route, method_url, params, headers)
  route =~ method_url
  refund_id = $1 || params[:refund]
  assert_existence :refund, refund_id, refunds[refund_id]
end

#get_refunds(route, method_url, params, headers) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/stripe_mock/request_handlers/refunds.rb', line 59

def get_refunds(route, method_url, params, headers)
  params[:offset] ||= 0
  params[:limit] ||= 10

  clone = refunds.clone

  Data.mock_list_object(clone.values, params)
end

#new_refund(route, method_url, params, headers) ⇒ Object



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/stripe_mock/request_handlers/refunds.rb', line 12

def new_refund(route, method_url, params, headers)
  if headers && headers[:idempotency_key]
    params[:idempotency_key] = headers[:idempotency_key]
    if refunds.any?
      original_refund = refunds.values.find { |c| c[:idempotency_key] == headers[:idempotency_key]}
      return refunds[original_refund[:id]] if original_refund
    end
  end

  charge = assert_existence :charge, params[:charge], charges[params[:charge]]
  params[:amount] ||= charge[:amount]
  id = new_id('re')
  bal_trans_params = {
    amount: params[:amount] * -1,
    source: id,
    type: 'refund'
  }
  balance_transaction_id = new_balance_transaction('txn', bal_trans_params)
  refund = Data.mock_refund params.merge(
    :balance_transaction => balance_transaction_id,
    :id => id,
    :charge => charge[:id],
  )
  add_refund_to_charge(refund, charge)
  refunds[id] = refund

  if params[:expand] == ['balance_transaction']
    refunds[id][:balance_transaction] =
      balance_transactions[balance_transaction_id]
  end
  refund
end

#update_refund(route, method_url, params, headers) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/stripe_mock/request_handlers/refunds.rb', line 45

def update_refund(route, method_url, params, headers)
  route =~ method_url
  id = $1

  refund = assert_existence :refund, id, refunds[id]
  allowed = allowed_refund_params(params)
  disallowed = params.keys - allowed
  if disallowed.count > 0
    raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', http_status: 400)
  end

  refunds[id] = Util.rmerge(refund, params)
end