forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoices.rb
96 lines (78 loc) · 3.4 KB
/
invoices.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
92
93
94
95
96
module StripeMock
module RequestHandlers
module Invoices
def Invoices.included(klass)
klass.add_handler 'post /v1/invoices', :new_invoice
klass.add_handler 'get /v1/invoices/upcoming', :upcoming_invoice
klass.add_handler 'get /v1/invoices/(.*)/lines', :get_invoice_line_items
klass.add_handler 'get /v1/invoices/(.*)', :get_invoice
klass.add_handler 'get /v1/invoices', :list_invoices
klass.add_handler 'post /v1/invoices/(.*)/pay', :pay_invoice
klass.add_handler 'post /v1/invoices/(.*)', :update_invoice
end
def new_invoice(route, method_url, params, headers)
id = new_id('in')
invoice_item = Data.mock_line_item()
invoices[id] = Data.mock_invoice([invoice_item], params.merge(:id => id))
end
def update_invoice(route, method_url, params, headers)
route =~ method_url
assert_existence :invoice, $1, invoices[$1]
invoices[$1].merge!(params)
end
def list_invoices(route, method_url, params, headers)
params[:offset] ||= 0
params[:limit] ||= 10
result = invoices.clone
if params[:customer]
result.delete_if { |k,v| v[:customer] != params[:customer] }
end
Data.mock_list_object(result.values, params)
end
def get_invoice(route, method_url, params, headers)
route =~ method_url
assert_existence :invoice, $1, invoices[$1]
end
def get_invoice_line_items(route, method_url, params, headers)
route =~ method_url
assert_existence :invoice, $1, invoices[$1]
invoices[$1][:lines]
end
def pay_invoice(route, method_url, params, headers)
route =~ method_url
assert_existence :invoice, $1, invoices[$1]
invoices[$1].merge!(:paid => true, :attempted => true, :charge => 'ch_1fD6uiR9FAA2zc')
end
def upcoming_invoice(route, method_url, params, headers)
route =~ method_url
raise Stripe::InvalidRequestError.new('Missing required param: customer', nil, 400) if params[:customer].nil?
customer = customers[params[:customer]]
assert_existence :customer, params[:customer], customer
raise Stripe::InvalidRequestError.new("No upcoming invoices for customer: #{customer[:id]}", nil, 404) if customer[:subscriptions][:data].length == 0
most_recent = customer[:subscriptions][:data].min_by { |sub| sub[:current_period_end] }
invoice_item = get_mock_subscription_line_item(most_recent)
id = new_id('in')
invoices[id] = Data.mock_invoice([invoice_item],
id: id,
customer: customer[:id],
subscription: most_recent[:id],
period_start: most_recent[:current_period_start],
period_end: most_recent[:current_period_end],
next_payment_attempt: most_recent[:current_period_end] + 3600 )
end
private
def get_mock_subscription_line_item(subscription)
Data.mock_line_item(
id: subscription[:id],
type: "subscription",
plan: subscription[:plan],
amount: subscription[:plan][:amount],
quantity: 1,
period: {
start: subscription[:current_period_end],
end: get_ending_time(subscription[:current_period_start], subscription[:plan], 2)
})
end
end
end
end