forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.rb
133 lines (101 loc) · 4.55 KB
/
subscriptions.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
module StripeMock
module RequestHandlers
module Subscriptions
def Subscriptions.included(klass)
klass.add_handler 'get /v1/customers/(.*)/subscriptions', :retrieve_subscriptions
klass.add_handler 'post /v1/customers/(.*)/subscriptions', :create_subscription
klass.add_handler 'get /v1/customers/(.*)/subscriptions/(.*)', :retrieve_subscription
klass.add_handler 'post /v1/customers/(.*)/subscriptions/(.*)', :update_subscription
klass.add_handler 'delete /v1/customers/(.*)/subscriptions/(.*)', :cancel_subscription
end
def create_subscription(route, method_url, params, headers)
route =~ method_url
customer = customers[$1]
assert_existance :customer, $1, customer
plan = plans[params[:plan]]
assert_existance :plan, params[:plan], plan
if params[:card]
new_card = get_card_by_token(params.delete(:card))
add_card_to_customer(new_card, customer)
customer[:default_card] = new_card[:id]
end
# Ensure customer has card to charge if plan has no trial and is not free
verify_card_present(customer, plan)
subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) })
subscription.merge!(custom_subscription_params(plan, customer, params))
add_subscription_to_customer(customer, subscription)
# oddly, subscription returned from 'create_subscription' does not expand plan
subscription.merge(plan: params[:plan])
end
def retrieve_subscription(route, method_url, params, headers)
route =~ method_url
customer = customers[$1]
assert_existance :customer, $1, customer
subscription = get_customer_subscription(customer, $2)
assert_existance :subscription, $2, subscription
subscription
end
def retrieve_subscriptions(route, method_url, params, headers)
route =~ method_url
customer = customers[$1]
assert_existance :customer, $1, customer
customer[:subscriptions]
end
def update_subscription(route, method_url, params, headers)
route =~ method_url
customer = customers[$1]
assert_existance :customer, $1, customer
subscription = get_customer_subscription(customer, $2)
assert_existance :subscription, $2, subscription
if params[:card]
new_card = get_card_by_token(params.delete(:card))
add_card_to_customer(new_card, customer)
customer[:default_card] = new_card[:id]
end
# expand the plan for addition to the customer object
plan_name = params[:plan] || subscription[:plan][:id]
plan = plans[plan_name]
assert_existance :plan, plan_name, plan
params[:plan] = plan if params[:plan]
verify_card_present(customer, plan)
if subscription[:cancel_at_period_end]
subscription[:cancel_at_period_end] = false
subscription[:canceled_at] = nil
end
subscription.merge!(custom_subscription_params(plan, customer, params))
# delete the old subscription, replace with the new subscription
customer[:subscriptions][:data].reject! { |sub| sub[:id] == subscription[:id] }
customer[:subscriptions][:data] << subscription
# oddly, subscription returned from 'create_subscription' does not expand plan
subscription.merge(plan: plan_name)
end
def cancel_subscription(route, method_url, params, headers)
route =~ method_url
customer = customers[$1]
assert_existance :customer, $1, customer
subscription = get_customer_subscription(customer, $2)
assert_existance :subscription, $2, subscription
cancel_params = { canceled_at: Time.now.utc.to_i }
if params[:at_period_end] == true
cancel_params[:cancel_at_period_end] = true
else
cancel_params[:status] = "canceled"
cancel_params[:cancel_at_period_end] = false
cancel_params[:ended_at] = Time.now.utc.to_i
end
subscription.merge!(cancel_params)
customer[:subscriptions][:data].reject!{|sub|
sub[:id] == subscription[:id]
}
customer[:subscriptions][:data] << subscription
subscription
end
private
def verify_card_present(customer, plan)
if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
end
end
end
end
end