Skip to content

Commit f32c2ea

Browse files
committed
Allow to remove discount from customer
1 parent daeee78 commit f32c2ea

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/stripe_mock/request_handlers/customers.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ def update_customer(route, method_url, params, headers)
8787
end
8888

8989
if params[:coupon]
90-
coupon = coupons[ params[:coupon] ]
91-
assert_existence :coupon, params[:coupon], coupon
90+
if params[:coupon] == ''
91+
delete_coupon_from_object(cus)
92+
else
93+
coupon = coupons[params[:coupon]]
94+
assert_existence :coupon, params[:coupon], coupon
9295

93-
add_coupon_to_object(cus, coupon)
96+
add_coupon_to_object(cus, coupon)
97+
end
9498
end
9599

96100
cus

lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb

Lines changed: 5 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,25 @@ def gen_card_tk
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)