forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refunds.rb
91 lines (77 loc) · 3.06 KB
/
refunds.rb
1
2
3
4
5
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module StripeMock
module RequestHandlers
module Refunds
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
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
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
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
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
private
def ensure_refund_required_params(params)
if non_integer_charge_amount?(params)
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
elsif non_positive_charge_amount?(params)
raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
elsif params[:charge].nil?
raise Stripe::InvalidRequestError.new('Must provide the identifier of the charge to refund.', nil)
end
end
def allowed_refund_params(params)
[:metadata]
end
end
end
end