@@ -114,7 +114,7 @@ def gen_card_tk
114
114
expect ( customer . subscriptions . first . customer ) . to eq ( customer . id )
115
115
end
116
116
117
- it " creates a customer with a plan (string/symbol agnostic)" do
117
+ it ' creates a customer with a plan (string/symbol agnostic)' do
118
118
stripe_helper . create_plan ( id : 'silver' , product : product . id )
119
119
120
120
Stripe ::Customer . create ( id : 'cust_SLV1' , source : gen_card_tk , :plan => 'silver' )
@@ -135,7 +135,6 @@ def gen_card_tk
135
135
end
136
136
137
137
context "create customer" do
138
-
139
138
it "with a trial when trial_end is set" do
140
139
plan = stripe_helper . create_plan ( id : 'no_trial' , product : product . id , amount : 999 )
141
140
trial_end = Time . now . utc . to_i + 3600
@@ -195,7 +194,7 @@ def gen_card_tk
195
194
expect ( customer . subscriptions . first . trial_end ) . to be_nil
196
195
end
197
196
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
199
198
plan = stripe_helper . create_plan ( id : 'silver' , product : product . id , amount : 999 )
200
199
expect {
201
200
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
205
204
}
206
205
end
207
206
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
209
208
expect {
210
209
Stripe ::Customer . create ( id : 'test_cus_trial_end' , source : gen_card_tk , trial_end : "now" )
211
210
} . to raise_error { |e |
@@ -218,7 +217,7 @@ def gen_card_tk
218
217
219
218
it 'cannot create a customer with a plan that does not exist' do
220
219
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' )
222
221
} . to raise_error { |e |
223
222
expect ( e ) . to be_a ( Stripe ::InvalidRequestError )
224
223
expect ( e . message ) . to eq ( 'No such plan: non-existant' )
@@ -228,18 +227,17 @@ def gen_card_tk
228
227
it 'cannot create a customer with an existing plan, but no card token' do
229
228
plan = stripe_helper . create_plan ( id : 'p' , product : product . id )
230
229
expect {
231
- customer = Stripe ::Customer . create ( id : 'test_cus_no_plan' , :plan => 'p' )
230
+ Stripe ::Customer . create ( id : 'test_cus_no_plan' , :plan => 'p' )
232
231
} . to raise_error { |e |
233
232
expect ( e ) . to be_a ( Stripe ::InvalidRequestError )
234
233
expect ( e . message ) . to eq ( 'You must supply a valid card' )
235
234
}
236
235
end
237
236
238
237
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' )
240
239
241
- customer =
242
- Stripe ::Customer . create ( id : 'test_cus_coupon' , coupon : '10PERCENT' )
240
+ Stripe ::Customer . create ( id : 'test_cus_coupon' , coupon : '10PERCENT' )
243
241
244
242
customer = Stripe ::Customer . retrieve ( 'test_cus_coupon' )
245
243
expect ( customer . discount ) . to_not be_nil
@@ -251,25 +249,46 @@ def gen_card_tk
251
249
describe 'repeating coupon with duration limit' , live : true do
252
250
let! ( :coupon ) { stripe_helper . create_coupon ( id : '10OFF' , amount_off : 1000 , currency : 'usd' , duration : 'repeating' , duration_in_months : 12 ) }
253
251
let! ( :customer ) { Stripe ::Customer . create ( coupon : coupon . id ) }
252
+
254
253
it 'creates the discount with the end date' , live : true do
255
254
discount = Stripe ::Customer . retrieve ( customer . id ) . discount
256
255
expect ( discount ) . to_not be_nil
257
256
expect ( discount . coupon ) . to_not be_nil
258
257
expect ( discount . end ) . to be_within ( 10 ) . of ( DateTime . now >> 12 ) . to_time . to_i
259
258
end
259
+
260
260
after { Stripe ::Coupon . retrieve ( coupon . id ) . delete }
261
261
after { Stripe ::Customer . retrieve ( customer . id ) . delete }
262
262
end
263
263
264
264
it 'cannot create a customer with a coupon that does not exist' do
265
265
expect {
266
- customer = Stripe ::Customer . create ( id : 'test_cus_no_coupon' , coupon : '5OFF' )
266
+ Stripe ::Customer . create ( id : 'test_cus_no_coupon' , coupon : '5OFF' )
267
267
} . to raise_error { |e |
268
268
expect ( e ) . to be_a ( Stripe ::InvalidRequestError )
269
269
expect ( e . message ) . to eq ( 'No such coupon: 5OFF' )
270
270
}
271
271
end
272
272
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
+
273
292
it "stores a created stripe customer in memory" do
274
293
customer = Stripe ::Customer . create ( {
275
294
0 commit comments