Skip to content

Commit 5e42f81

Browse files
Merge pull request stripe-ruby-mock#668 from mnin/feature/adjust_coupon_behaviour
Allow to remove discount from customer
2 parents 4e5652d + 0e9e271 commit 5e42f81

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

lib/stripe_mock/request_handlers/customers.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def new_customer(route, method_url, params, headers)
2929
params[:default_source] = sources.first[:id]
3030
end
3131

32-
customers[ params[:id] ] = Data.mock_customer(sources, params)
32+
customers[params[:id]] = Data.mock_customer(sources, params)
3333

3434
if params[:plan]
3535
plan_id = params[:plan].to_s
@@ -48,13 +48,12 @@ def new_customer(route, method_url, params, headers)
4848
end
4949

5050
if params[:coupon]
51-
coupon = coupons[ params[:coupon] ]
51+
coupon = coupons[params[:coupon]]
5252
assert_existence :coupon, params[:coupon], coupon
53-
5453
add_coupon_to_object(customers[params[:id]], coupon)
5554
end
5655

57-
customers[ params[:id] ]
56+
customers[params[:id]]
5857
end
5958

6059
def update_customer(route, method_url, params, headers)
@@ -87,10 +86,14 @@ def update_customer(route, method_url, params, headers)
8786
end
8887

8988
if params[:coupon]
90-
coupon = coupons[ params[:coupon] ]
91-
assert_existence :coupon, params[:coupon], coupon
89+
if params[:coupon] == ''
90+
delete_coupon_from_object(cus)
91+
else
92+
coupon = coupons[params[:coupon]]
93+
assert_existence :coupon, params[:coupon], coupon
9294

93-
add_coupon_to_object(cus, coupon)
95+
add_coupon_to_object(cus, coupon)
96+
end
9497
end
9598

9699
cus

lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def add_coupon_to_object(object, coupon)
1212
object[:discount] = Stripe::Discount.construct_from(discount_attrs)
1313
object
1414
end
15+
16+
def delete_coupon_from_object(object)
17+
object[:discount] = nil
18+
object
19+
end
1520
end
1621
end
1722
end

spec/shared_stripe_examples/customer_examples.rb

+29-10
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def gen_card_tk
114114
expect(customer.subscriptions.first.customer).to eq(customer.id)
115115
end
116116

117-
it "creates a customer with a plan (string/symbol agnostic)" do
117+
it 'creates a customer with a plan (string/symbol agnostic)' do
118118
stripe_helper.create_plan(id: 'silver', product: product.id)
119119

120120
Stripe::Customer.create(id: 'cust_SLV1', source: gen_card_tk, :plan => 'silver')
@@ -135,7 +135,6 @@ def gen_card_tk
135135
end
136136

137137
context "create customer" do
138-
139138
it "with a trial when trial_end is set" do
140139
plan = stripe_helper.create_plan(id: 'no_trial', product: product.id, amount: 999)
141140
trial_end = Time.now.utc.to_i + 3600
@@ -195,7 +194,7 @@ def gen_card_tk
195194
expect(customer.subscriptions.first.trial_end).to be_nil
196195
end
197196

198-
it "returns an error if trial_end is set to a past time" do
197+
it 'returns an error if trial_end is set to a past time' do
199198
plan = stripe_helper.create_plan(id: 'silver', product: product.id, amount: 999)
200199
expect {
201200
Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: Time.now.utc.to_i - 3600)
@@ -205,7 +204,7 @@ def gen_card_tk
205204
}
206205
end
207206

208-
it "returns an error if trial_end is set without a plan" do
207+
it 'returns an error if trial_end is set without a plan' do
209208
expect {
210209
Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, trial_end: "now")
211210
}.to raise_error {|e|
@@ -218,7 +217,7 @@ def gen_card_tk
218217

219218
it 'cannot create a customer with a plan that does not exist' do
220219
expect {
221-
customer = Stripe::Customer.create(id: 'test_cus_no_plan', source: gen_card_tk, :plan => 'non-existant')
220+
Stripe::Customer.create(id: 'test_cus_no_plan', source: gen_card_tk, :plan => 'non-existant')
222221
}.to raise_error {|e|
223222
expect(e).to be_a(Stripe::InvalidRequestError)
224223
expect(e.message).to eq('No such plan: non-existant')
@@ -228,18 +227,17 @@ def gen_card_tk
228227
it 'cannot create a customer with an existing plan, but no card token' do
229228
plan = stripe_helper.create_plan(id: 'p', product: product.id)
230229
expect {
231-
customer = Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
230+
Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
232231
}.to raise_error {|e|
233232
expect(e).to be_a(Stripe::InvalidRequestError)
234233
expect(e.message).to eq('You must supply a valid card')
235234
}
236235
end
237236

238237
it 'creates a customer with a coupon discount' do
239-
coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
238+
coupon = Stripe::Coupon.create(id: '10PERCENT', duration: 'once')
240239

241-
customer =
242-
Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
240+
Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
243241

244242
customer = Stripe::Customer.retrieve('test_cus_coupon')
245243
expect(customer.discount).to_not be_nil
@@ -251,25 +249,46 @@ def gen_card_tk
251249
describe 'repeating coupon with duration limit', live: true do
252250
let!(:coupon) { stripe_helper.create_coupon(id: '10OFF', amount_off: 1000, currency: 'usd', duration: 'repeating', duration_in_months: 12) }
253251
let!(:customer) { Stripe::Customer.create(coupon: coupon.id) }
252+
254253
it 'creates the discount with the end date', live: true do
255254
discount = Stripe::Customer.retrieve(customer.id).discount
256255
expect(discount).to_not be_nil
257256
expect(discount.coupon).to_not be_nil
258257
expect(discount.end).to be_within(10).of (DateTime.now >> 12).to_time.to_i
259258
end
259+
260260
after { Stripe::Coupon.retrieve(coupon.id).delete }
261261
after { Stripe::Customer.retrieve(customer.id).delete }
262262
end
263263

264264
it 'cannot create a customer with a coupon that does not exist' do
265265
expect{
266-
customer = Stripe::Customer.create(id: 'test_cus_no_coupon', coupon: '5OFF')
266+
Stripe::Customer.create(id: 'test_cus_no_coupon', coupon: '5OFF')
267267
}.to raise_error {|e|
268268
expect(e).to be_a(Stripe::InvalidRequestError)
269269
expect(e.message).to eq('No such coupon: 5OFF')
270270
}
271271
end
272272

273+
context 'with coupon on customer' do
274+
before do
275+
Stripe::Coupon.create(id: '10PERCENT', duration: 'once')
276+
Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
277+
end
278+
279+
it 'remove the coupon from customer' do
280+
customer = Stripe::Customer.retrieve('test_cus_coupon')
281+
expect(customer.discount).to_not be_nil
282+
expect(customer.discount.coupon).to_not be_nil
283+
expect(customer.discount.customer).to eq customer.id
284+
expect(customer.discount.start).to be_within(1).of Time.now.to_i
285+
286+
Stripe::Customer.update('test_cus_coupon', coupon: '')
287+
customer = Stripe::Customer.retrieve('test_cus_coupon')
288+
expect(customer.discount).to be_nil
289+
end
290+
end
291+
273292
it "stores a created stripe customer in memory" do
274293
customer = Stripe::Customer.create({
275294

0 commit comments

Comments
 (0)