forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomers.rb
83 lines (65 loc) · 2.64 KB
/
customers.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
module StripeMock
module RequestHandlers
module Customers
def Customers.included(klass)
klass.add_handler 'post /v1/customers', :new_customer
klass.add_handler 'post /v1/customers/(.*)', :update_customer
klass.add_handler 'get /v1/customers/(.*)', :get_customer
klass.add_handler 'delete /v1/customers/(.*)', :delete_customer
klass.add_handler 'get /v1/customers', :list_customers
end
def new_customer(route, method_url, params, headers)
params[:id] ||= new_id('cus')
cards = []
if params[:card]
cards << get_card_by_token(params.delete(:card))
params[:default_card] = cards.first[:id]
end
customers[ params[:id] ] = Data.mock_customer(cards, params)
if params[:plan]
plan_id = params[:plan].to_s
plan = plans[plan_id]
assert_existance :plan, plan_id, plan
if params[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
end
subscription = Data.mock_subscription({ id: new_id('su') })
subscription.merge!(custom_subscription_params(plan, customers[ params[:id] ], params))
add_subscription_to_customer(customers[ params[:id] ], subscription)
elsif params[:trial_end]
raise Stripe::InvalidRequestError.new('Received unknown parameter: trial_end', nil, 400)
end
customers[ params[:id] ]
end
def update_customer(route, method_url, params, headers)
route =~ method_url
assert_existance :customer, $1, customers[$1]
cus = customers[$1] ||= Data.mock_customer([], :id => $1)
cus.merge!(params)
if params[:card]
new_card = get_card_by_token(params.delete(:card))
add_card_to_customer(new_card, cus)
cus[:default_card] = new_card[:id]
end
cus
end
def delete_customer(route, method_url, params, headers)
route =~ method_url
assert_existance :customer, $1, customers[$1]
customers[$1] = {
id: customers[$1][:id],
deleted: true
}
customers[$1]
end
def get_customer(route, method_url, params, headers)
route =~ method_url
assert_existance :customer, $1, customers[$1]
customers[$1] ||= Data.mock_customer([], :id => $1)
end
def list_customers(route, method_url, params, headers)
customers.values
end
end
end
end