stripe-ruby-mock 2.5.8 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/rspec_tests.yml +38 -0
  3. data/.gitignore +1 -1
  4. data/.rspec +2 -1
  5. data/CHANGELOG.md +77 -0
  6. data/Gemfile +1 -5
  7. data/README.md +19 -11
  8. data/lib/stripe_mock/api/client.rb +2 -2
  9. data/lib/stripe_mock/api/errors.rb +34 -28
  10. data/lib/stripe_mock/api/instance.rb +1 -1
  11. data/lib/stripe_mock/api/webhooks.rb +68 -24
  12. data/lib/stripe_mock/client.rb +2 -1
  13. data/lib/stripe_mock/data/list.rb +42 -9
  14. data/lib/stripe_mock/data.rb +359 -21
  15. data/lib/stripe_mock/instance.rb +23 -5
  16. data/lib/stripe_mock/request_handlers/account_links.rb +15 -0
  17. data/lib/stripe_mock/request_handlers/accounts.rb +17 -6
  18. data/lib/stripe_mock/request_handlers/balance_transactions.rb +2 -2
  19. data/lib/stripe_mock/request_handlers/charges.rb +31 -5
  20. data/lib/stripe_mock/request_handlers/checkout_session.rb +179 -0
  21. data/lib/stripe_mock/request_handlers/customers.rb +47 -19
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +1 -1
  23. data/lib/stripe_mock/request_handlers/events.rb +30 -3
  24. data/lib/stripe_mock/request_handlers/express_login_links.rb +15 -0
  25. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +6 -0
  26. data/lib/stripe_mock/request_handlers/helpers/search_helpers.rb +67 -0
  27. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +36 -12
  28. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  29. data/lib/stripe_mock/request_handlers/invoices.rb +26 -6
  30. data/lib/stripe_mock/request_handlers/payment_intents.rb +202 -0
  31. data/lib/stripe_mock/request_handlers/payment_methods.rb +124 -0
  32. data/lib/stripe_mock/request_handlers/plans.rb +1 -1
  33. data/lib/stripe_mock/request_handlers/prices.rb +71 -0
  34. data/lib/stripe_mock/request_handlers/products.rb +15 -5
  35. data/lib/stripe_mock/request_handlers/promotion_codes.rb +43 -0
  36. data/lib/stripe_mock/request_handlers/refunds.rb +13 -2
  37. data/lib/stripe_mock/request_handlers/setup_intents.rb +100 -0
  38. data/lib/stripe_mock/request_handlers/sources.rb +12 -6
  39. data/lib/stripe_mock/request_handlers/subscriptions.rb +146 -25
  40. data/lib/stripe_mock/request_handlers/tokens.rb +6 -4
  41. data/lib/stripe_mock/request_handlers/transfers.rb +12 -1
  42. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +124 -9
  43. data/lib/stripe_mock/server.rb +2 -2
  44. data/lib/stripe_mock/test_strategies/base.rb +98 -12
  45. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  46. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  47. data/lib/stripe_mock/version.rb +1 -1
  48. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  49. data/lib/stripe_mock/webhook_fixtures/balance.available.json +27 -15
  50. data/lib/stripe_mock/webhook_fixtures/charge.captured.json +143 -0
  51. data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +63 -16
  52. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +101 -44
  53. data/lib/stripe_mock/webhook_fixtures/charge.refund.updated.json +35 -0
  54. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +145 -50
  55. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +114 -43
  56. data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.json +79 -0
  57. data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.payment_mode.json +53 -0
  58. data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.setup_mode.json +45 -0
  59. data/lib/stripe_mock/webhook_fixtures/customer.created.json +37 -45
  60. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +36 -32
  61. data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +31 -22
  62. data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -25
  63. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +135 -47
  64. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +134 -45
  65. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +135 -56
  66. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +38 -46
  67. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +176 -49
  68. data/lib/stripe_mock/webhook_fixtures/invoice.finalized.json +171 -0
  69. data/lib/stripe_mock/webhook_fixtures/invoice.paid.json +171 -0
  70. data/lib/stripe_mock/webhook_fixtures/invoice.payment_action_required.json +171 -0
  71. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +149 -83
  72. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +149 -90
  73. data/lib/stripe_mock/webhook_fixtures/invoice.upcoming.json +70 -0
  74. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +178 -50
  75. data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +87 -13
  76. data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +88 -14
  77. data/lib/stripe_mock/webhook_fixtures/mandate.updated.json +34 -0
  78. data/lib/stripe_mock/webhook_fixtures/payment_intent.amount_capturable_updated.json +170 -0
  79. data/lib/stripe_mock/webhook_fixtures/payment_intent.canceled.json +73 -0
  80. data/lib/stripe_mock/webhook_fixtures/payment_intent.created.json +86 -0
  81. data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +225 -0
  82. data/lib/stripe_mock/webhook_fixtures/payment_intent.processing.json +162 -0
  83. data/lib/stripe_mock/webhook_fixtures/payment_intent.requires_action.json +191 -0
  84. data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +196 -0
  85. data/lib/stripe_mock/webhook_fixtures/payment_link.created.json +47 -0
  86. data/lib/stripe_mock/webhook_fixtures/payment_link.updated.json +50 -0
  87. data/lib/stripe_mock/webhook_fixtures/payment_method.attached.json +63 -0
  88. data/lib/stripe_mock/webhook_fixtures/payment_method.detached.json +62 -0
  89. data/lib/stripe_mock/webhook_fixtures/payout.created.json +40 -0
  90. data/lib/stripe_mock/webhook_fixtures/payout.paid.json +40 -0
  91. data/lib/stripe_mock/webhook_fixtures/payout.updated.json +46 -0
  92. data/lib/stripe_mock/webhook_fixtures/plan.created.json +30 -13
  93. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +30 -13
  94. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +34 -14
  95. data/lib/stripe_mock/webhook_fixtures/price.created.json +42 -0
  96. data/lib/stripe_mock/webhook_fixtures/price.deleted.json +42 -0
  97. data/lib/stripe_mock/webhook_fixtures/price.updated.json +48 -0
  98. data/lib/stripe_mock/webhook_fixtures/product.created.json +40 -0
  99. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +40 -0
  100. data/lib/stripe_mock/webhook_fixtures/product.updated.json +47 -0
  101. data/lib/stripe_mock/webhook_fixtures/quote.accepted.json +92 -0
  102. data/lib/stripe_mock/webhook_fixtures/quote.canceled.json +92 -0
  103. data/lib/stripe_mock/webhook_fixtures/quote.created.json +92 -0
  104. data/lib/stripe_mock/webhook_fixtures/quote.finalized.json +92 -0
  105. data/lib/stripe_mock/webhook_fixtures/setup_intent.canceled.json +46 -0
  106. data/lib/stripe_mock/webhook_fixtures/setup_intent.created.json +51 -0
  107. data/lib/stripe_mock/webhook_fixtures/setup_intent.setup_failed.json +100 -0
  108. data/lib/stripe_mock/webhook_fixtures/setup_intent.succeeded.json +46 -0
  109. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.canceled.json +119 -0
  110. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.created.json +114 -0
  111. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.released.json +111 -0
  112. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.updated.json +125 -0
  113. data/lib/stripe_mock/webhook_fixtures/tax_rate.created.json +32 -0
  114. data/lib/stripe_mock/webhook_fixtures/tax_rate.updated.json +37 -0
  115. data/lib/stripe_mock.rb +11 -0
  116. data/spec/instance_spec.rb +13 -13
  117. data/spec/integration_examples/completing_checkout_sessions_example.rb +37 -0
  118. data/spec/list_spec.rb +38 -0
  119. data/spec/readme_spec.rb +1 -1
  120. data/spec/server_spec.rb +6 -3
  121. data/spec/shared_stripe_examples/account_examples.rb +10 -2
  122. data/spec/shared_stripe_examples/account_link_examples.rb +16 -0
  123. data/spec/shared_stripe_examples/balance_examples.rb +6 -0
  124. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  125. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  126. data/spec/shared_stripe_examples/bank_token_examples.rb +5 -7
  127. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  128. data/spec/shared_stripe_examples/card_token_examples.rb +17 -21
  129. data/spec/shared_stripe_examples/charge_examples.rb +106 -22
  130. data/spec/shared_stripe_examples/checkout_session_examples.rb +99 -0
  131. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  132. data/spec/shared_stripe_examples/customer_examples.rb +149 -53
  133. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  134. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  135. data/spec/shared_stripe_examples/express_login_link_examples.rb +12 -0
  136. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  137. data/spec/shared_stripe_examples/invoice_examples.rb +148 -40
  138. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  139. data/spec/shared_stripe_examples/payment_intent_examples.rb +283 -0
  140. data/spec/shared_stripe_examples/payment_method_examples.rb +454 -0
  141. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  142. data/spec/shared_stripe_examples/plan_examples.rb +135 -92
  143. data/spec/shared_stripe_examples/price_examples.rb +292 -0
  144. data/spec/shared_stripe_examples/product_examples.rb +215 -0
  145. data/spec/shared_stripe_examples/promotion_code_examples.rb +68 -0
  146. data/spec/shared_stripe_examples/refund_examples.rb +38 -21
  147. data/spec/shared_stripe_examples/setup_intent_examples.rb +85 -0
  148. data/spec/shared_stripe_examples/subscription_examples.rb +706 -324
  149. data/spec/shared_stripe_examples/subscription_items_examples.rb +3 -2
  150. data/spec/shared_stripe_examples/transfer_examples.rb +16 -7
  151. data/spec/shared_stripe_examples/webhook_event_examples.rb +62 -16
  152. data/spec/spec_helper.rb +8 -5
  153. data/spec/stripe_mock_spec.rb +4 -4
  154. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  155. data/spec/support/stripe_examples.rb +11 -1
  156. data/stripe-ruby-mock.gemspec +9 -5
  157. metadata +115 -47
  158. data/.travis.yml +0 -28
  159. data/spec/shared_stripe_examples/product_example.rb +0 -65
@@ -1,49 +1,61 @@
1
1
  require 'spec_helper'
2
+ require 'securerandom'
2
3
 
3
- shared_examples 'Customer Subscriptions' do
4
+ shared_examples 'Customer Subscriptions with plans' do
5
+ let(:gen_card_tk) { stripe_helper.generate_card_token }
4
6
 
5
- def gen_card_tk
6
- stripe_helper.generate_card_token
7
- end
7
+ let(:product) { stripe_helper.create_product }
8
+ let(:plan_attrs) { {id: 'silver', product: product.id, amount: 4999, currency: 'usd'} }
9
+ let(:plan) { stripe_helper.create_plan(plan_attrs) }
10
+
11
+ let(:plan_with_trial_attrs) { {id: 'trial', product: product.id, amount: 999, trial_period_days: 14 } }
12
+ let(:plan_with_trial) { stripe_helper.create_plan(plan_with_trial_attrs) }
13
+
14
+ let(:free_plan) { stripe_helper.create_plan(id: 'free', product: product.id, amount: 0) }
8
15
 
9
16
  context "creating a new subscription" do
10
- it "adds a new subscription to customer with none using items", :live => true do
11
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
12
- amount: 4999, currency: 'usd')
17
+ it "adds a new subscription to customer with none using items", live: true do
18
+ plan
13
19
  customer = Stripe::Customer.create(source: gen_card_tk)
14
20
 
15
21
  expect(customer.subscriptions.data).to be_empty
16
22
  expect(customer.subscriptions.count).to eq(0)
17
23
 
18
- sub = Stripe::Subscription.create({ items: [{ plan: 'silver' }],
19
- customer: customer.id, metadata: { foo: "bar", example: "yes" } })
24
+ subscription = Stripe::Subscription.create({
25
+ customer: customer.id,
26
+ items: [{ plan: 'silver' }],
27
+ metadata: { foo: "bar", example: "yes" }
28
+ })
20
29
 
21
- expect(sub.object).to eq('subscription')
22
- expect(sub.plan.to_hash).to eq(plan.to_hash)
23
- expect(sub.metadata.foo).to eq( "bar" )
24
- expect(sub.metadata.example).to eq( "yes" )
30
+ expect(subscription.object).to eq('subscription')
31
+ expect(subscription.plan.to_hash).to eq(plan.to_hash)
32
+ expect(subscription.metadata.foo).to eq("bar")
33
+ expect(subscription.metadata.example).to eq("yes")
25
34
 
26
35
  customer = Stripe::Customer.retrieve(customer.id)
27
- expect(customer.subscriptions.data).to_not be_empty
28
- expect(customer.subscriptions.count).to eq(1)
29
- expect(customer.subscriptions.data.length).to eq(1)
30
- expect(customer.charges.data.length).to eq(1)
31
- expect(customer.currency).to eq( "usd" )
32
-
33
- expect(customer.subscriptions.data.first.id).to eq(sub.id)
34
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
35
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
36
- expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
37
- expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
36
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
37
+ charges = Stripe::Charge.list(customer: customer.id)
38
+
39
+ expect(subscriptions.data).to_not be_empty
40
+ expect(subscriptions.count).to eq(1)
41
+ expect(subscriptions.data.length).to eq(1)
42
+ expect(charges.data.length).to eq(1)
43
+ expect(customer.currency).to eq("usd")
44
+
45
+ expect(subscriptions.data.first.id).to eq(subscription.id)
46
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
47
+ expect(subscriptions.data.first.customer).to eq(customer.id)
48
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
49
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
38
50
  end
39
51
 
40
- it "adds a new subscription to customer with none", :live => true do
41
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
42
- amount: 4999, currency: 'usd')
52
+ it "adds a new subscription to customer with none", live: true do
53
+ plan
43
54
  customer = Stripe::Customer.create(source: gen_card_tk)
55
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
44
56
 
45
- expect(customer.subscriptions.data).to be_empty
46
- expect(customer.subscriptions.count).to eq(0)
57
+ expect(subscriptions.data).to be_empty
58
+ expect(subscriptions.count).to eq(0)
47
59
 
48
60
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
49
61
 
@@ -53,26 +65,30 @@ shared_examples 'Customer Subscriptions' do
53
65
  expect(sub.metadata.example).to eq( "yes" )
54
66
 
55
67
  customer = Stripe::Customer.retrieve(customer.id)
56
- expect(customer.subscriptions.data).to_not be_empty
57
- expect(customer.subscriptions.count).to eq(1)
58
- expect(customer.subscriptions.data.length).to eq(1)
59
- expect(customer.charges.data.length).to eq(1)
68
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
69
+ charges = Stripe::Charge.list(customer: customer.id)
70
+
71
+ expect(subscriptions.data).to_not be_empty
72
+ expect(subscriptions.count).to eq(1)
73
+ expect(subscriptions.data.length).to eq(1)
74
+ expect(charges.data.length).to eq(1)
60
75
  expect(customer.currency).to eq( "usd" )
61
76
 
62
- expect(customer.subscriptions.data.first.id).to eq(sub.id)
63
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
64
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
65
- expect(customer.subscriptions.data.first.billing).to eq('charge_automatically')
66
- expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
67
- expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
77
+ expect(subscriptions.data.first.id).to eq(sub.id)
78
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
79
+ expect(subscriptions.data.first.customer).to eq(customer.id)
80
+ expect(subscriptions.data.first.billing).to eq('charge_automatically')
81
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
82
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
68
83
  end
69
84
 
70
85
  it 'when customer object provided' do
71
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' }, amount: 4999, currency: 'usd')
86
+ plan
72
87
  customer = Stripe::Customer.create(source: gen_card_tk)
88
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
73
89
 
74
- expect(customer.subscriptions.data).to be_empty
75
- expect(customer.subscriptions.count).to eq(0)
90
+ expect(subscriptions.data).to be_empty
91
+ expect(subscriptions.count).to eq(0)
76
92
 
77
93
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer, metadata: { foo: "bar", example: "yes" } })
78
94
 
@@ -83,121 +99,172 @@ shared_examples 'Customer Subscriptions' do
83
99
  expect(sub.metadata.example).to eq( "yes" )
84
100
 
85
101
  customer = Stripe::Customer.retrieve(customer.id)
86
- expect(customer.subscriptions.data).to_not be_empty
87
- expect(customer.subscriptions.count).to eq(1)
88
- expect(customer.subscriptions.data.length).to eq(1)
89
- expect(customer.charges.data.length).to eq(1)
102
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
103
+ charges = Stripe::Charge.list(customer: customer.id)
104
+
105
+ expect(subscriptions.data).to_not be_empty
106
+ expect(subscriptions.count).to eq(1)
107
+ expect(subscriptions.data.length).to eq(1)
108
+ expect(charges.data.length).to eq(1)
90
109
  expect(customer.currency).to eq( "usd" )
91
110
 
92
- expect(customer.subscriptions.data.first.id).to eq(sub.id)
93
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
111
+ expect(subscriptions.data.first.id).to eq(sub.id)
112
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
94
113
 
95
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
96
- expect(customer.subscriptions.data.first.billing).to eq('charge_automatically')
97
- expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
98
- expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
114
+ expect(subscriptions.data.first.customer).to eq(customer.id)
115
+ expect(subscriptions.data.first.billing).to eq('charge_automatically')
116
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
117
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
118
+ end
119
+
120
+ it "adds a new subscription with payment method provided as default" do
121
+ plan
122
+ customer = Stripe::Customer.create(source: gen_card_tk)
123
+ payment_method = Stripe::PaymentMethod.create(
124
+ type: 'card',
125
+ card: {
126
+ number: 4242_4242_4242_4242,
127
+ exp_month: 9,
128
+ exp_year: (Time.now.year + 5),
129
+ cvc: 999
130
+ }
131
+ )
132
+ sub = Stripe::Subscription.create(
133
+ plan: 'silver',
134
+ customer: customer,
135
+ metadata: { foo: "bar", example: "yes" },
136
+ collection_method: 'send_invoice',
137
+ default_payment_method: payment_method.id,
138
+ )
139
+
140
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
141
+ expect(subscriptions.count).to eq(1)
142
+ expect(subscriptions.data.first.id).to eq(sub.id)
143
+ expect(subscriptions.data.first.collection_method).to eq('send_invoice')
144
+ expect(subscriptions.data.first.default_payment_method).to eq(payment_method.id)
99
145
  end
100
146
 
101
147
  it "adds a new subscription to customer (string/symbol agnostic)" do
102
148
  customer = Stripe::Customer.create(source: gen_card_tk)
103
- expect(customer.subscriptions.count).to eq(0)
149
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
150
+ expect(subscriptions.count).to eq(0)
104
151
 
105
- plan = stripe_helper.create_plan(id: :silver, product: { name: 'Silver Plan' },
106
- amount: 4999, currency: 'usd')
107
- sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
108
- customer = Stripe::Customer.retrieve(customer.id)
109
- expect(sub.plan.to_hash).to eq(plan.to_hash)
110
- expect(customer.subscriptions.count).to eq(1)
152
+ plan
153
+ sub = Stripe::Subscription.create({plan: plan.id, customer: customer.id })
111
154
 
112
- plan = stripe_helper.create_plan(id: 'gold', product: { name: 'Gold Plan' },
113
- amount: 14999, currency: 'usd')
114
- sub = Stripe::Subscription.create({ plan: 'gold', customer: customer.id })
115
155
  customer = Stripe::Customer.retrieve(customer.id)
156
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
116
157
  expect(sub.plan.to_hash).to eq(plan.to_hash)
117
- expect(customer.subscriptions.count).to eq(2)
158
+ expect(subscriptions.count).to eq(1)
118
159
  end
119
160
 
120
161
  it 'creates a charge for the customer', live: true do
121
- stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
122
- amount: 4999)
123
-
124
162
  customer = Stripe::Customer.create(source: gen_card_tk)
125
- Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
163
+ Stripe::Subscription.create({ plan: plan.id, customer: customer.id, metadata: { foo: "bar", example: "yes" } })
126
164
  customer = Stripe::Customer.retrieve(customer.id)
165
+ charges = Stripe::Charge.list(customer: customer.id)
127
166
 
128
- expect(customer.charges.data.length).to eq(1)
129
- expect(customer.charges.data.first.amount).to eq(4999)
167
+ expect(charges.data.length).to eq(1)
168
+ expect(charges.data.first.amount).to eq(4999)
130
169
  end
131
170
 
132
171
  it 'contains coupon object', live: true do
133
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
134
- amount: 777)
135
172
  coupon = stripe_helper.create_coupon(id: 'free_coupon', duration: 'repeating', duration_in_months: 3)
136
173
  customer = Stripe::Customer.create(source: gen_card_tk)
137
174
  Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: coupon.id)
138
175
  customer = Stripe::Customer.retrieve(customer.id)
139
176
 
140
- expect(customer.subscriptions.data).to be_a(Array)
141
- expect(customer.subscriptions.data.count).to eq(1)
142
- expect(customer.subscriptions.data.first.discount).not_to be_nil
143
- expect(customer.subscriptions.data.first.discount).to be_a(Stripe::Discount)
144
- expect(customer.subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
177
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
178
+
179
+ expect(subscriptions.data).to be_a(Array)
180
+ expect(subscriptions.data.count).to eq(1)
181
+ expect(subscriptions.data.first.discount).not_to be_nil
182
+ expect(subscriptions.data.first.discount.id).not_to be_nil
183
+ expect(subscriptions.data.first.discount).to be_a(Stripe::Discount)
184
+ expect(subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
145
185
  end
146
186
 
147
187
  it 'when coupon is not exist', live: true do
148
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
149
- amount: 777)
150
188
  customer = Stripe::Customer.create(source: gen_card_tk)
151
189
 
152
- expect { Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none') }.to raise_error {|e|
153
- expect(e).to be_a Stripe::InvalidRequestError
154
- expect(e.http_status).to eq(400)
155
- expect(e.message).to eq('No such coupon: none')
156
- }
190
+ expect {
191
+ Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none')
192
+ }.to raise_error {|e|
193
+ expect(e).to be_a Stripe::InvalidRequestError
194
+ expect(e.http_status).to eq(400)
195
+ expect(e.message).to eq('No such coupon: none')
196
+ }
197
+ end
198
+
199
+ it "allows promotion code" do
200
+ customer = Stripe::Customer.create(source: gen_card_tk)
201
+ coupon = stripe_helper.create_coupon
202
+ promotion_code = Stripe::PromotionCode.create(coupon: coupon)
203
+
204
+ expect {
205
+ Stripe::Subscription.create(plan: plan.id, customer: customer.id, promotion_code: promotion_code.id)
206
+ }.not_to raise_error(Stripe::InvalidRequestError)
207
+ end
208
+
209
+ it "does not permit both coupon and promotion code" do
210
+ customer = Stripe::Customer.create(source: gen_card_tk)
211
+
212
+ expect {
213
+ Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: "test", promotion_code: "test")
214
+ }.to raise_error { |e|
215
+ expect(e).to be_a Stripe::InvalidRequestError
216
+ expect(e.http_status).to eq(400)
217
+ expect(e.message).to eq("You may only specify one of these parameters: coupon, promotion_code")
218
+ }
157
219
  end
158
220
 
159
221
  it "correctly sets quantity, application_fee_percent and tax_percent" do
160
- Stripe::Plan.create(
161
- :amount => 2500,
162
- :interval => 'month',
163
- :product => {
164
- :name => 'Test plan'
165
- },
166
- :currency => 'usd',
167
- :id => 'silver',
168
- :statement_description => "testPlan"
169
- )
170
222
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
171
223
 
172
224
  subscription = Stripe::Subscription.create({
173
- plan: "silver", customer: customer.id, quantity: 2, application_fee_percent: 10, tax_percent: 20})
225
+ plan: plan.id,
226
+ customer: customer.id,
227
+ quantity: 2,
228
+ application_fee_percent: 10,
229
+ tax_percent: 20
230
+ })
174
231
  expect(subscription.quantity).to eq(2)
175
232
  expect(subscription.application_fee_percent).to eq(10)
176
233
  expect(subscription.tax_percent).to eq(20)
177
234
  end
178
235
 
236
+ it "correctly sets pending invoice item interval" do
237
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
238
+
239
+ subscription = Stripe::Subscription.create({
240
+ plan: plan.id,
241
+ customer: customer.id,
242
+ quantity: 2,
243
+ pending_invoice_item_interval: { interval: 'month', interval_count: 1 }
244
+ })
245
+
246
+ expect(subscription.pending_invoice_item_interval.interval).to eq 'month'
247
+ expect(subscription.pending_invoice_item_interval.interval_count).to eq 1
248
+ end
249
+
179
250
  it "correctly sets created when it's not provided as a parameter", live: true do
180
251
  customer = Stripe::Customer.create(source: gen_card_tk)
181
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
182
- amount: 4999, currency: 'usd')
183
- subscription = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
252
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
184
253
 
185
254
  expect(subscription.created).to eq(subscription.current_period_start)
186
255
  end
187
256
 
188
257
  it "correctly sets created when it's provided as a parameter" do
189
258
  customer = Stripe::Customer.create(source: gen_card_tk)
190
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
191
- amount: 4999, currency: 'usd')
192
- subscription = Stripe::Subscription.create({ plan: 'silver', customer: customer.id, created: 1473576318 })
259
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, created: 1473576318 })
193
260
 
194
261
  expect(subscription.created).to eq(1473576318)
195
262
  end
196
263
 
197
264
  it "adds additional subscription to customer with existing subscription" do
198
- silver = stripe_helper.create_plan(id: 'silver')
199
- gold = stripe_helper.create_plan(id: 'gold')
200
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'gold')
265
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
266
+ gold = stripe_helper.create_plan(id: 'gold', product: product.id)
267
+ customer = Stripe::Customer.create(id: 'test_customer_sub', product: product.id, source: gen_card_tk, plan: 'gold')
201
268
 
202
269
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
203
270
 
@@ -218,7 +285,7 @@ shared_examples 'Customer Subscriptions' do
218
285
  end
219
286
 
220
287
  it "subscribes a cardless customer when specifing a card token" do
221
- plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
288
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
222
289
  customer = Stripe::Customer.create(id: 'cardless')
223
290
 
224
291
  sub = Stripe::Subscription.create(plan: 'enterprise', customer: customer.id, source: gen_card_tk)
@@ -246,11 +313,32 @@ shared_examples 'Customer Subscriptions' do
246
313
  expect(customer.subscriptions.count).to eq(0)
247
314
  end
248
315
 
316
+ it "creates a subscription when subscription's payment_behavior is default_incomplete" do
317
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
318
+ customer = Stripe::Customer.create(id: 'cardless')
319
+
320
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, payment_behavior: 'default_incomplete' })
321
+ customer = Stripe::Customer.retrieve('cardless')
322
+
323
+ expect(customer.subscriptions.count).to eq(1)
324
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
325
+ expect(customer.subscriptions.data.first.status).to eq('incomplete')
326
+ end
327
+
328
+ it "allows setting transfer_data" do
329
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
330
+
331
+ sub = Stripe::Subscription.create({ customer: customer.id, plan: plan.id, transfer_data: {destination: "acct_0000000000000000", amount_percent: 50} })
332
+
333
+ expect(sub.transfer_data.destination).to eq("acct_0000000000000000")
334
+ expect(sub.transfer_data.amount_percent).to eq(50)
335
+ end
336
+
249
337
  it "throws an error when subscribing a customer with no card" do
250
- plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
338
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
251
339
  customer = Stripe::Customer.create(id: 'cardless')
252
340
 
253
- expect { Stripe::Subscription.create({ plan: 'enterprise', customer: customer.id }) }.to raise_error {|e|
341
+ expect { Stripe::Subscription.create({ plan: plan.id, customer: customer.id }) }.to raise_error {|e|
254
342
  expect(e).to be_a Stripe::InvalidRequestError
255
343
  expect(e.http_status).to eq(400)
256
344
  expect(e.message).to_not be_nil
@@ -261,12 +349,14 @@ shared_examples 'Customer Subscriptions' do
261
349
  end
262
350
 
263
351
  it "throws an error when subscribing the customer to a second plan in a different currency" do
264
- usd_plan = stripe_helper.create_plan(id: 'enterprise_usd', amount: 499, currency: 'usd')
265
352
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
266
- usd_subscription = Stripe::Subscription.create({ plan: 'enterprise_usd', customer: customer.id })
267
353
 
268
- eur_plan = stripe_helper.create_plan(id: 'enterprise_eur', amount: 499, currency: 'eur')
269
- expect { Stripe::Subscription.create({ plan: 'enterprise_eur', customer: customer.id }) }.to raise_error {|e|
354
+ expect(plan.currency).to eql("usd")
355
+ usd_subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
356
+
357
+ eur_plan = stripe_helper.create_plan(plan_attrs.merge(id: "plan_EURO", currency: 'eur'))
358
+ expect(eur_plan.currency).to eql("eur")
359
+ expect { Stripe::Subscription.create({ plan: eur_plan.id, customer: customer.id }) }.to raise_error {|e|
270
360
  expect(e).to be_a Stripe::InvalidRequestError
271
361
  expect(e.http_status).to eq(400)
272
362
  expect(e.message).to_not be_nil
@@ -274,7 +364,7 @@ shared_examples 'Customer Subscriptions' do
274
364
  end
275
365
 
276
366
  it 'when attempting to create a new subscription with the params trial', live: true do
277
- plan = stripe_helper.create_plan(id: 'trial', amount: 999)
367
+ plan = stripe_helper.create_plan(id: 'trial', product: product.id, amount: 999)
278
368
  customer = Stripe::Customer.create(source: gen_card_tk)
279
369
 
280
370
  expect{ Stripe::Subscription.create(plan: plan.id, customer: customer.id, trial: 10) }.to raise_error {|e|
@@ -286,35 +376,34 @@ shared_examples 'Customer Subscriptions' do
286
376
  end
287
377
 
288
378
  it "subscribes a customer with no card to a plan with a free trial" do
289
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
290
379
  customer = Stripe::Customer.create(id: 'cardless')
291
-
292
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id })
380
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id })
293
381
 
294
382
  expect(sub.object).to eq('subscription')
295
- expect(sub.plan.to_hash).to eq(plan.to_hash)
383
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
296
384
  expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
297
- expect(sub.billing_cycle_anchor).to be_nil
385
+ expect(sub.billing_cycle_anchor).to eq sub.trial_start
298
386
 
299
- customer = Stripe::Customer.retrieve('cardless')
300
- expect(customer.subscriptions.data).to_not be_empty
301
- expect(customer.subscriptions.count).to eq(1)
302
- expect(customer.subscriptions.data.length).to eq(1)
387
+ customer = Stripe::Customer.retrieve(customer.id)
388
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
389
+ charges = Stripe::Charge.list(customer: customer.id)
303
390
 
304
- expect(customer.subscriptions.data.first.id).to eq(sub.id)
305
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
306
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
307
- expect(customer.charges.count).to eq(0)
391
+ expect(subscriptions.data).to_not be_empty
392
+ expect(subscriptions.count).to eq(1)
393
+ expect(subscriptions.data.length).to eq(1)
394
+
395
+ expect(subscriptions.data.first.id).to eq(sub.id)
396
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan_with_trial.to_hash)
397
+ expect(subscriptions.data.first.customer).to eq(customer.id)
398
+ expect(charges.count).to eq(0)
308
399
  end
309
400
 
310
401
  it "subscribes a customer with no card to a plan with a free trial with plan as item" do
311
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
312
402
  customer = Stripe::Customer.create(id: 'cardless')
313
-
314
- sub = Stripe::Subscription.create({ items: [ { plan: 'trial' } ], customer: customer.id })
403
+ sub = Stripe::Subscription.create({ items: [ { plan: plan_with_trial.id } ], customer: customer.id })
315
404
 
316
405
  expect(sub.object).to eq('subscription')
317
- expect(sub.items.data[0].plan.to_hash).to eq(plan.to_hash)
406
+ expect(sub.items.data[0].plan.to_hash).to eq(plan_with_trial.to_hash)
318
407
  # no idea how to fix this one
319
408
  # expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
320
409
 
@@ -324,17 +413,17 @@ shared_examples 'Customer Subscriptions' do
324
413
  expect(customer.subscriptions.data.length).to eq(1)
325
414
 
326
415
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
327
- expect(customer.subscriptions.data.first.items.data.first.plan.to_hash).to eq(plan.to_hash)
416
+ expect(customer.subscriptions.data.first.items.data.first.plan.to_hash).to eq(plan_with_trial.to_hash)
328
417
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
329
418
  # No idea on this one
330
419
  # expect(customer.charges.count).to eq(0)
331
420
  end
332
421
 
333
422
  it "subscribes a customer with no card to a free plan" do
334
- plan = stripe_helper.create_plan(id: 'free_tier', amount: 0)
423
+ plan = stripe_helper.create_plan(id: 'free_tier', product: product.id, amount: 0)
335
424
  customer = Stripe::Customer.create(id: 'cardless')
336
425
 
337
- sub = Stripe::Subscription.create({ plan: 'free_tier', customer: customer.id })
426
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
338
427
 
339
428
  expect(sub.object).to eq('subscription')
340
429
  expect(sub.plan.to_hash).to eq(plan.to_hash)
@@ -350,20 +439,21 @@ shared_examples 'Customer Subscriptions' do
350
439
  end
351
440
 
352
441
  it "overrides trial length when trial end is set" do
353
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
354
442
  customer = Stripe::Customer.create(id: 'short_trial')
355
443
  trial_end = Time.now.utc.to_i + 3600
356
444
 
357
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end })
445
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end })
358
446
 
359
447
  expect(sub.object).to eq('subscription')
360
- expect(sub.plan.to_hash).to eq(plan.to_hash)
448
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
361
449
  expect(sub.current_period_end).to eq(trial_end)
362
450
  expect(sub.trial_end).to eq(trial_end)
363
451
  end
364
452
 
365
453
  it "does not override trial end when trial end is not set" do
366
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
454
+ product = stripe_helper.create_product(name: 'Trial')
455
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999,
456
+ trial_period_days: 14, product: product.id)
367
457
  customer = Stripe::Customer.create(id: 'short_trial')
368
458
  trial_end = Time.now.utc.to_i + 3600
369
459
  metadata = {description: 'original description'}
@@ -387,23 +477,21 @@ shared_examples 'Customer Subscriptions' do
387
477
  end
388
478
 
389
479
  it "returns without a trial when trial_end is set to 'now'" do
390
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
391
480
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
392
481
 
393
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: "now" })
482
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: "now" })
394
483
 
395
484
  expect(sub.object).to eq('subscription')
396
- expect(sub.plan.to_hash).to eq(plan.to_hash)
485
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
397
486
  expect(sub.status).to eq('active')
398
487
  expect(sub.trial_start).to be_nil
399
488
  expect(sub.trial_end).to be_nil
400
489
  end
401
490
 
402
491
  it "raises error when trial_end is not an integer or 'now'" do
403
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
404
492
  customer = Stripe::Customer.create(id: 'cus_trial')
405
493
 
406
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: "gazebo" }) }.to raise_error {|e|
494
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: "gazebo" }) }.to raise_error {|e|
407
495
  expect(e).to be_a Stripe::InvalidRequestError
408
496
  expect(e.http_status).to eq(400)
409
497
  expect(e.message).to eq("Invalid timestamp: must be an integer")
@@ -411,11 +499,10 @@ shared_examples 'Customer Subscriptions' do
411
499
  end
412
500
 
413
501
  it "raises error when trial_end is set to a time in the past" do
414
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
415
502
  customer = Stripe::Customer.create(id: 'past_trial')
416
503
  trial_end = Time.now.utc.to_i - 3600
417
504
 
418
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
505
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
419
506
  expect(e).to be_a Stripe::InvalidRequestError
420
507
  expect(e.http_status).to eq(400)
421
508
  expect(e.message).to eq("Invalid timestamp: must be an integer Unix timestamp in the future")
@@ -423,11 +510,10 @@ shared_examples 'Customer Subscriptions' do
423
510
  end
424
511
 
425
512
  it "raises error when trial_end is set to a time more than five years in the future" do
426
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
427
513
  customer = Stripe::Customer.create(id: 'long_trial')
428
514
  trial_end = Time.now.utc.to_i + 31557600*5 + 3600 # 5 years + 1 hour
429
515
 
430
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
516
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
431
517
  expect(e).to be_a Stripe::InvalidRequestError
432
518
  expect(e.http_status).to eq(400)
433
519
  expect(e.message).to eq("Invalid timestamp: can be no more than five years in the future")
@@ -435,11 +521,10 @@ shared_examples 'Customer Subscriptions' do
435
521
  end
436
522
 
437
523
  it 'overrides current period end when billing cycle anchor is set' do
438
- plan = stripe_helper.create_plan(id: 'plan', amount: 999)
439
524
  customer = Stripe::Customer.create(source: gen_card_tk)
440
525
  billing_cycle_anchor = Time.now.utc.to_i + 3600
441
526
 
442
- sub = Stripe::Subscription.create({ plan: 'plan', customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
527
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
443
528
 
444
529
  expect(sub.status).to eq('active')
445
530
  expect(sub.current_period_end).to eq(billing_cycle_anchor)
@@ -447,17 +532,11 @@ shared_examples 'Customer Subscriptions' do
447
532
  end
448
533
 
449
534
  it 'when plan defined inside items', live: true do
450
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
535
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
451
536
 
452
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
537
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
453
538
  customer = Stripe::Customer.create(
454
- source: {
455
- object: 'card',
456
- exp_month: 11,
457
- exp_year: 2019,
458
- number: '4242424242424242',
459
- cvc: '123'
460
- }
539
+ source: stripe_helper.generate_card_token
461
540
  )
462
541
  subscription = Stripe::Subscription.create(
463
542
  customer: customer.id,
@@ -476,9 +555,9 @@ shared_examples 'Customer Subscriptions' do
476
555
  end
477
556
 
478
557
  it 'when plan defined inside items for trials with no card', live: true do
479
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
558
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
480
559
 
481
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
560
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
482
561
  customer = Stripe::Customer.create
483
562
  trial_end = Time.now.utc.to_i + 3600
484
563
 
@@ -497,8 +576,48 @@ shared_examples 'Customer Subscriptions' do
497
576
  expect(subscription.items.data[1].plan.id).to eq plan2.id
498
577
  end
499
578
 
579
+ it 'sets current_period_end based on price week interval', live: true do
580
+ price = stripe_helper.create_price(recurring: {interval: 'week'})
581
+
582
+ subscription = Stripe::Subscription.create(
583
+ customer: Stripe::Customer.create(source: gen_card_tk).id,
584
+ items: [
585
+ {price: price.id, quantity: 1}
586
+ ]
587
+ )
588
+
589
+ expect(subscription.current_period_end).to eq (Time.now + (7 * 60 * 60 * 24)).to_i
590
+ end
591
+
592
+ it 'sets current_period_end based on price month interval', live: true do
593
+ price = stripe_helper.create_price(recurring: {interval: 'month'})
594
+
595
+ subscription = Stripe::Subscription.create(
596
+ customer: Stripe::Customer.create(source: gen_card_tk).id,
597
+ items: [
598
+ {price: price.id, quantity: 1}
599
+ ]
600
+ )
601
+
602
+ expect(subscription.current_period_end).to eq (DateTime.now >> 1).to_time.to_i
603
+ end
604
+
605
+ it 'sets current_period_end based on price year interval', live: true do
606
+ price = stripe_helper.create_price(recurring: {interval: 'year'})
607
+
608
+ subscription = Stripe::Subscription.create(
609
+ customer: Stripe::Customer.create(source: gen_card_tk).id,
610
+ items: [
611
+ {price: price.id, quantity: 1}
612
+ ]
613
+ )
614
+
615
+ expect(subscription.current_period_end).to eq (DateTime.now >> 12).to_time.to_i
616
+ end
617
+
500
618
  it 'add a new subscription to bill via an invoice' do
501
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
619
+ product = stripe_helper.create_product(id: 'invoice_billing')
620
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
502
621
  amount: 4999, currency: 'usd')
503
622
  customer = Stripe::Customer.create(id: 'cardless')
504
623
 
@@ -520,60 +639,120 @@ shared_examples 'Customer Subscriptions' do
520
639
  end
521
640
 
522
641
  let(:subscription_header) {{
523
- :idempotency_key => 'a_idempotency_key'
642
+ :idempotency_key => SecureRandom.hex
524
643
  }}
525
644
 
526
645
  it "adds a new subscription to customer with identical idempotency key" do
527
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
646
+ product = stripe_helper.create_product(name: 'Silver Product')
647
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
528
648
  amount: 4999, currency: 'usd')
529
649
  customer = Stripe::Customer.create(source: gen_card_tk)
530
650
 
531
651
  expect(customer.subscriptions.data).to be_empty
532
652
  expect(customer.subscriptions.count).to eq(0)
533
653
 
654
+ subscription_header = {
655
+ :idempotency_key => "uniq_idempotency_key_#{customer.id}"
656
+ }
657
+
534
658
  sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
535
659
  sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
536
660
  expect(sub1).to eq(sub2)
537
661
  end
538
662
 
539
- it "adds a new subscription to customer with different idempotency key", :live => true do
540
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
663
+ it "adds a new subscription to customer with different idempotency key", live: true do
664
+ product = stripe_helper.create_product(name: 'Silver Product')
665
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
541
666
  amount: 4999, currency: 'usd')
542
667
  customer = Stripe::Customer.create(source: gen_card_tk)
543
668
 
544
669
  expect(customer.subscriptions.data).to be_empty
545
670
  expect(customer.subscriptions.count).to eq(0)
546
671
 
547
- another_subscription_header = {
548
- :idempotency_key => 'another_idempotency_key'
672
+ sub_header_1 = {
673
+ :idempotency_key => SecureRandom.hex
674
+ }
675
+ sub_header_2 = {
676
+ :idempotency_key => SecureRandom.hex
549
677
  }
550
678
 
551
- sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
552
- sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, another_subscription_header)
679
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, sub_header_1)
680
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, sub_header_2)
553
681
  expect(sub1).not_to eq(sub2)
554
682
  end
555
683
 
556
684
  it "accepts a hash of items" do
557
- silver = stripe_helper.create_plan(id: 'silver')
685
+ product = stripe_helper.create_product(name: 'Silver Product')
686
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
558
687
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
559
688
 
560
689
  sub = Stripe::Subscription.create({ items: { '0' => { plan: 'silver' } }, customer: customer.id })
561
- sub.delete(at_period_end: true)
690
+ sub.cancel(at_period_end: true)
562
691
 
563
692
  expect(sub.cancel_at_period_end).to be_truthy
564
693
  expect(sub.save).to be_truthy
565
- expect(sub.cancel_at_period_end).to be_falsey
694
+ expect(sub.cancel_at_period_end).to be_truthy
695
+ end
696
+
697
+ it 'accepts default_tax_rates param' do
698
+ tax_rate = Stripe::TaxRate.create(
699
+ display_name: 'VAT',
700
+ description: 'VAT Germany',
701
+ jurisdiction: 'DE',
702
+ percentage: 19.0,
703
+ inclusive: false,
704
+ )
705
+ product = stripe_helper.create_product(name: 'Silver Product')
706
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
707
+ customer = Stripe::Customer.create(source: gen_card_tk)
708
+ subscription = Stripe::Subscription.create(
709
+ customer: customer.id,
710
+ items: [{ plan: silver.id }],
711
+ default_tax_rates: [ tax_rate ]
712
+ )
713
+
714
+ aggregate_failures do
715
+ expect(subscription.default_tax_rates.length).to eq(1)
716
+ expect(subscription.default_tax_rates.first.id).to eq(tax_rate.id)
717
+ end
566
718
  end
567
719
 
720
+ it 'expands latest_invoice.payment_intent' do
721
+ customer = Stripe::Customer.create(source: gen_card_tk)
722
+ subscription = Stripe::Subscription.create({
723
+ customer: customer.id,
724
+ plan: plan.id,
725
+ expand: ['latest_invoice.payment_intent']
726
+ })
727
+
728
+ expect(subscription.latest_invoice.payment_intent.status).to eq('succeeded')
729
+
730
+ subscription = Stripe::Subscription.create({
731
+ customer: customer.id,
732
+ plan: plan.id,
733
+ expand: ['latest_invoice.payment_intent'],
734
+ payment_behavior: 'default_incomplete'
735
+ })
736
+
737
+ expect(subscription.latest_invoice.payment_intent.status).to eq('requires_payment_method')
738
+
739
+ subscription = Stripe::Subscription.create({
740
+ customer: customer.id,
741
+ plan: plan.id,
742
+ expand: ['latest_invoice.payment_intent'],
743
+ trial_period_days: 14
744
+ })
745
+
746
+ expect(subscription.latest_invoice.payment_intent).to be_nil
747
+ end
568
748
  end
569
749
 
570
750
  context "updating a subscription" do
571
751
  it 'raises invalid request exception when subscription is cancelled' do
572
- stripe_helper.create_plan(id: 'the truth')
573
- customer = Stripe::Customer.create(source: gen_card_tk, plan: 'the truth')
752
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
574
753
 
575
754
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
576
- subscription.delete
755
+ subscription.cancel
577
756
 
578
757
  expect { subscription.save }.to raise_error { |e|
579
758
  expect(e).to be_a(Stripe::InvalidRequestError)
@@ -583,24 +762,22 @@ shared_examples 'Customer Subscriptions' do
583
762
  end
584
763
 
585
764
  it "updates a stripe customer's existing subscription with one plan inside items" do
586
- silver = stripe_helper.create_plan(id: 'silver')
587
765
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
588
766
 
589
- sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
590
- sub.delete(at_period_end: true)
767
+ sub = Stripe::Subscription.create({ items: [ { plan: plan.id } ], customer: customer.id })
768
+ sub.cancel(at_period_end: true)
591
769
 
592
770
  expect(sub.cancel_at_period_end).to be_truthy
593
771
  expect(sub.save).to be_truthy
594
- expect(sub.cancel_at_period_end).to be_falsey
772
+ expect(sub.cancel_at_period_end).to be_truthy
595
773
  end
596
774
 
597
775
  it "updates a stripe customer's existing subscription when plan inside of items" do
598
- silver = stripe_helper.create_plan(id: 'silver')
599
- gold = stripe_helper.create_plan(id: 'gold')
600
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'silver')
776
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id)
601
777
 
778
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
602
779
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
603
- sub.plan = 'gold'
780
+ sub.plan = gold_plan.id
604
781
  sub.quantity = 5
605
782
  sub.metadata.foo = "bar"
606
783
  sub.metadata.example = "yes"
@@ -608,7 +785,7 @@ shared_examples 'Customer Subscriptions' do
608
785
  expect(sub.save).to be_truthy
609
786
 
610
787
  expect(sub.object).to eq('subscription')
611
- expect(sub.plan.to_hash).to eq(gold.to_hash)
788
+ expect(sub.plan.to_hash).to eq(gold_plan.to_hash)
612
789
  expect(sub.quantity).to eq(5)
613
790
  expect(sub.metadata.foo).to eq( "bar" )
614
791
  expect(sub.metadata.example).to eq( "yes" )
@@ -619,16 +796,15 @@ shared_examples 'Customer Subscriptions' do
619
796
  expect(customer.subscriptions.data.length).to eq(1)
620
797
 
621
798
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
622
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold.to_hash)
799
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold_plan.to_hash)
623
800
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
624
801
  end
625
802
 
626
803
  it "updates a stripe customer's existing subscription with single plan when multiple plans inside of items" do
627
- silver_plan = stripe_helper.create_plan(id: 'silver')
628
- gold_plan = stripe_helper.create_plan(id: 'gold')
629
- addon_plan = stripe_helper.create_plan(id: 'addon_plan')
630
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: silver_plan.id)
804
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id)
631
805
 
806
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
807
+ addon_plan = stripe_helper.create_plan(id: 'addon_plan', product: product.id)
632
808
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
633
809
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon_plan.id, quantity: 2 }]
634
810
  expect(sub.save).to be_truthy
@@ -649,12 +825,12 @@ shared_examples 'Customer Subscriptions' do
649
825
  end
650
826
 
651
827
  it "updates a stripe customer's existing subscription with multple plans when multiple plans inside of items" do
652
- silver_plan = stripe_helper.create_plan(id: 'silver')
653
- gold_plan = stripe_helper.create_plan(id: 'gold')
654
- addon1_plan = stripe_helper.create_plan(id: 'addon1')
655
- addon2_plan = stripe_helper.create_plan(id: 'addon2')
656
828
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
657
- sub = Stripe::Subscription.create(customer: customer.id, items: [{ plan: silver_plan.id }, { plan: addon1_plan.id }])
829
+ addon1_plan = stripe_helper.create_plan(id: 'addon1', product: product.id)
830
+ sub = Stripe::Subscription.create(customer: customer.id, items: [{ plan: plan.id }, { plan: addon1_plan.id }])
831
+
832
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
833
+ addon2_plan = stripe_helper.create_plan(id: 'addon2', product: product.id)
658
834
 
659
835
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon2_plan.id, quantity: 2 }]
660
836
  expect(sub.save).to be_truthy
@@ -674,9 +850,87 @@ shared_examples 'Customer Subscriptions' do
674
850
  expect(customer.subscriptions.data.first.items.data[1].plan.to_hash).to eq(addon2_plan.to_hash)
675
851
  end
676
852
 
853
+ it "updates a subscription's cancel_at_period_end" do
854
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
855
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
856
+
857
+ sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
858
+
859
+ expect(sub.cancel_at_period_end).to be_falsey
860
+
861
+ sub.cancel_at_period_end = true
862
+ sub.save
863
+
864
+ expect(sub.save).to be_truthy
865
+ expect(sub.cancel_at_period_end).to be_truthy
866
+ expect(sub.canceled_at).to be_truthy
867
+
868
+ sub.cancel_at_period_end = false
869
+ sub.save
870
+
871
+ expect(sub.save).to be_truthy
872
+ expect(sub.cancel_at_period_end).to be_falsey
873
+ expect(sub.canceled_at).to be_falsey
874
+ end
875
+
876
+ it "updates a subscription's pending invoice item interval" do
877
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
878
+
879
+ subscription = Stripe::Subscription.create({
880
+ plan: plan.id,
881
+ customer: customer.id,
882
+ quantity: 2,
883
+ pending_invoice_item_interval: { interval: 'month', interval_count: 1 }
884
+ })
885
+
886
+ expect(subscription.pending_invoice_item_interval.interval).to eq 'month'
887
+ expect(subscription.pending_invoice_item_interval.interval_count).to eq 1
888
+
889
+ subscription.pending_invoice_item_interval = { interval: 'week', interval_count: 3 }
890
+ subscription.save
891
+
892
+ expect(subscription.pending_invoice_item_interval.interval).to eq 'week'
893
+ expect(subscription.pending_invoice_item_interval.interval_count).to eq 3
894
+ end
895
+
896
+ it "updates a subscription's default payment method" do
897
+ plan
898
+ customer = Stripe::Customer.create(source: gen_card_tk)
899
+ payment_method_card = Stripe::PaymentMethod.create(
900
+ type: 'card',
901
+ card: {
902
+ number: 4242_4242_4242_4242,
903
+ exp_month: 9,
904
+ exp_year: (Time.now.year + 5),
905
+ cvc: 999
906
+ }
907
+ )
908
+ payment_method_sepa = Stripe::PaymentMethod.create(
909
+ type: 'sepa_debit',
910
+ sepa_debit: {iban: 'DE89370400440532013000'},
911
+ )
912
+ subscription = Stripe::Subscription.create(
913
+ plan: 'silver',
914
+ customer: customer,
915
+ metadata: { foo: "bar", example: "yes" },
916
+ default_payment_method: payment_method_card.id,
917
+ )
918
+
919
+ subscriptions = Stripe::Subscription.list(customer: customer)
920
+ expect(subscriptions.data.first.default_payment_method).to eq(payment_method_card.id)
921
+
922
+ Stripe::Subscription.update(
923
+ subscription.id,
924
+ default_payment_method: payment_method_sepa.id,
925
+ collection_method: 'send_invoice',
926
+ )
927
+
928
+ subscriptions = Stripe::Subscription.list(customer: customer)
929
+ expect(subscriptions.data.first.collection_method).to eq('send_invoice')
930
+ expect(subscriptions.data.first.default_payment_method).to eq(payment_method_sepa.id)
931
+ end
932
+
677
933
  it 'when adds coupon', live: true do
678
- plan = stripe_helper.create_plan(id: 'plan_with_coupon2', product: { name: 'One More Test Plan' },
679
- amount: 777)
680
934
  coupon = stripe_helper.create_coupon
681
935
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
682
936
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -690,8 +944,6 @@ shared_examples 'Customer Subscriptions' do
690
944
  end
691
945
 
692
946
  it 'when add not exist coupon' do
693
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
694
- amount: 777)
695
947
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
696
948
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
697
949
 
@@ -706,8 +958,6 @@ shared_examples 'Customer Subscriptions' do
706
958
  end
707
959
 
708
960
  it 'when coupon is removed' do
709
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
710
- amount: 777)
711
961
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
712
962
  coupon = stripe_helper.create_coupon
713
963
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -720,9 +970,25 @@ shared_examples 'Customer Subscriptions' do
720
970
  expect(subscription.discount).to be_nil
721
971
  end
722
972
 
973
+ it "throws an error when promotion code has an amount restriction" do
974
+ coupon = stripe_helper.create_coupon
975
+ promotion_code = Stripe::PromotionCode.create(
976
+ coupon: coupon, restrictions: {minimum_amount: 100, minimum_amount_currency: "USD"}
977
+ )
978
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
979
+ subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
980
+
981
+ subscription.promotion_code = promotion_code.id
982
+
983
+ expect { subscription.save }.to raise_error { |e|
984
+ expect(e).to be_a Stripe::InvalidRequestError
985
+ expect(e.http_status).to eq(400)
986
+ expect(e.message).to_not be_nil
987
+ }
988
+ end
989
+
723
990
  it "throws an error when plan does not exist" do
724
- free = stripe_helper.create_plan(id: 'free', amount: 0)
725
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
991
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
726
992
 
727
993
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
728
994
  sub.plan = 'gazebo'
@@ -736,75 +1002,59 @@ shared_examples 'Customer Subscriptions' do
736
1002
  customer = Stripe::Customer.retrieve('cardless')
737
1003
  expect(customer.subscriptions.count).to eq(1)
738
1004
  expect(customer.subscriptions.data.length).to eq(1)
739
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
1005
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free_plan.to_hash)
740
1006
  end
741
1007
 
742
1008
  it "throws an error when subscription does not exist" do
743
- free = stripe_helper.create_plan(id: 'free', amount: 0)
744
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
745
-
746
- expect { Stripe::Subscription.retrieve("gazebo") }.to raise_error {|e|
1009
+ expect(stripe_helper.list_subscriptions(50).keys).to_not include("sub_NONEXIST")
1010
+ expect { Stripe::Subscription.retrieve("sub_NONEXIST") }.to raise_error {|e|
747
1011
  expect(e).to be_a Stripe::InvalidRequestError
748
1012
  expect(e.http_status).to eq(404)
749
1013
  expect(e.message).to_not be_nil
750
1014
  }
751
-
752
- customer = Stripe::Customer.retrieve('cardless')
753
- expect(customer.subscriptions.count).to eq(1)
754
- expect(customer.subscriptions.data.length).to eq(1)
755
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
756
1015
  end
757
1016
 
758
1017
  [nil, 0].each do |trial_period_days|
759
- it "throws an error when updating a customer with no card, and plan trail_period_days = #{trial_period_days}", live: true do
1018
+ it "raises an error when updating a customer with no card, and plan trial_period_days = #{trial_period_days}", live: true do
760
1019
  begin
761
- free = stripe_helper.create_plan(id: 'free', amount: 0)
762
- paid = stripe_helper.create_plan(id: 'enterprise', amount: 499, trial_period_days: trial_period_days)
763
- customer = Stripe::Customer.create(description: 'cardless', plan: 'free')
1020
+ free_plan
1021
+ paid_plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499, trial_period_days: trial_period_days)
1022
+ customer = Stripe::Customer.create(description: 'cardless', plan: free_plan.id)
764
1023
 
765
1024
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
766
- sub.plan = 'enterprise'
767
-
768
- expect { sub.save }.to raise_error {|e|
769
- expect(e).to be_a Stripe::InvalidRequestError
770
- expect(e.http_status).to eq(400)
771
- expect(e.message).to_not be_nil
772
- }
773
-
774
- customer = Stripe::Customer.retrieve(customer.id)
775
- expect(customer.subscriptions.count).to eq(1)
776
- expect(customer.subscriptions.data.length).to eq(1)
777
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
1025
+ sub.plan = paid_plan.id
1026
+
1027
+ expect {
1028
+ sub.save
1029
+ }.to raise_error(Stripe::InvalidRequestError, "This customer has no attached payment source")
778
1030
  ensure
779
1031
  customer.delete if customer
780
- paid.delete if paid
781
- free.delete if free
1032
+ paid_plan.delete if paid_plan
1033
+ free_plan.delete if free_plan
782
1034
  end
783
1035
  end
784
1036
  end
785
1037
 
786
1038
  it 'updates a subscription if the customer has a free trial', live: true do
787
- stripe_helper.create_plan(id: 'enterprise', amount: 499)
788
1039
  trial_end = Time.now.utc.to_i + 3600
789
- customer = Stripe::Customer.create(plan: 'enterprise',
790
- trial_end: trial_end)
791
- subscription = customer.subscriptions.first
1040
+ customer = Stripe::Customer.create(plan: plan.id, trial_end: trial_end)
1041
+ subscription = customer.subscriptions.first
792
1042
  subscription.quantity = 2
793
1043
  subscription.save
794
1044
  expect(subscription.quantity).to eq(2)
795
1045
  end
796
1046
 
797
1047
  it "updates a customer with no card to a plan with a free trial" do
798
- free = stripe_helper.create_plan(id: 'free', amount: 0)
799
- trial = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
800
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
1048
+ free_plan
1049
+ trial_plan = stripe_helper.create_plan(id: 'trial', product: product.id, amount: 999, trial_period_days: 14)
1050
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
801
1051
 
802
1052
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
803
- sub.plan = 'trial'
1053
+ sub.plan = trial_plan.id
804
1054
  sub.save
805
1055
 
806
1056
  expect(sub.object).to eq('subscription')
807
- expect(sub.plan.to_hash).to eq(trial.to_hash)
1057
+ expect(sub.plan.to_hash).to eq(trial_plan.to_hash)
808
1058
 
809
1059
  customer = Stripe::Customer.retrieve('cardless')
810
1060
  expect(customer.subscriptions.data).to_not be_empty
@@ -812,21 +1062,21 @@ shared_examples 'Customer Subscriptions' do
812
1062
  expect(customer.subscriptions.data.length).to eq(1)
813
1063
 
814
1064
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
815
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial.to_hash)
1065
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial_plan.to_hash)
816
1066
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
817
1067
  end
818
1068
 
819
1069
  it "updates a customer with no card to a free plan" do
820
- free = stripe_helper.create_plan(id: 'free', amount: 0)
821
- gratis = stripe_helper.create_plan(id: 'gratis', amount: 0)
822
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
1070
+ free_plan
1071
+ customer = Stripe::Customer.create(id: 'cardless', product: product.id, plan: free_plan.id)
823
1072
 
824
1073
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
825
- sub.plan = 'gratis'
1074
+ gratis_plan = stripe_helper.create_plan(id: 'gratis', product: product.id, amount: 0)
1075
+ sub.plan = gratis_plan.id
826
1076
  sub.save
827
1077
 
828
1078
  expect(sub.object).to eq('subscription')
829
- expect(sub.plan.to_hash).to eq(gratis.to_hash)
1079
+ expect(sub.plan.to_hash).to eq(gratis_plan.to_hash)
830
1080
 
831
1081
  customer = Stripe::Customer.retrieve('cardless')
832
1082
  expect(customer.subscriptions.data).to_not be_empty
@@ -834,17 +1084,15 @@ shared_examples 'Customer Subscriptions' do
834
1084
  expect(customer.subscriptions.data.length).to eq(1)
835
1085
 
836
1086
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
837
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis.to_hash)
1087
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis_plan.to_hash)
838
1088
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
839
1089
  end
840
1090
 
841
1091
  it "sets a card when updating a customer's subscription" do
842
- free = stripe_helper.create_plan(id: 'free', amount: 0)
843
- paid = stripe_helper.create_plan(id: 'paid', amount: 499)
844
- customer = Stripe::Customer.create(id: 'test_customer_sub', plan: 'free')
1092
+ customer = Stripe::Customer.create(id: 'test_customer_sub', plan: free_plan.id)
845
1093
 
846
1094
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
847
- sub.plan = 'paid'
1095
+ sub.plan = plan.id
848
1096
  sub.source = gen_card_tk
849
1097
  sub.save
850
1098
 
@@ -857,11 +1105,8 @@ shared_examples 'Customer Subscriptions' do
857
1105
  end
858
1106
 
859
1107
  it "overrides trial length when trial end is set" do
860
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
861
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
862
-
1108
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id)
863
1109
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
864
-
865
1110
  trial_end = Time.now.utc.to_i + 3600
866
1111
  sub.trial_end = trial_end
867
1112
  sub.save
@@ -872,8 +1117,7 @@ shared_examples 'Customer Subscriptions' do
872
1117
  end
873
1118
 
874
1119
  it "returns without a trial when trial_end is set to 'now'" do
875
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
876
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
1120
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id, default_source: 'tok_visa')
877
1121
 
878
1122
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
879
1123
 
@@ -881,18 +1125,15 @@ shared_examples 'Customer Subscriptions' do
881
1125
  sub.save
882
1126
 
883
1127
  expect(sub.object).to eq('subscription')
884
- expect(sub.plan.to_hash).to eq(plan.to_hash)
1128
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
885
1129
  expect(sub.status).to eq('active')
886
1130
  expect(sub.trial_start).to be_nil
887
1131
  expect(sub.trial_end).to be_nil
888
1132
  end
889
1133
 
890
1134
  it "changes an active subscription to a trial when trial_end is set" do
891
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
892
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', source: gen_card_tk)
893
-
1135
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
894
1136
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
895
-
896
1137
  trial_end = Time.now.utc.to_i + 3600
897
1138
  sub.trial_end = trial_end
898
1139
  sub.save
@@ -906,8 +1147,8 @@ shared_examples 'Customer Subscriptions' do
906
1147
 
907
1148
 
908
1149
  it "raises error when trial_end is not an integer or 'now'" do
909
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
910
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', source: gen_card_tk)
1150
+ expect(plan.trial_period_days).to be_nil
1151
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
911
1152
 
912
1153
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
913
1154
  sub.trial_end = "gazebo"
@@ -918,16 +1159,28 @@ shared_examples 'Customer Subscriptions' do
918
1159
  expect(e.message).to eq("Invalid timestamp: must be an integer")
919
1160
  }
920
1161
  end
1162
+
1163
+ it "converts billing_cycle_anchor=now to a timestamp" do
1164
+ customer = Stripe::Customer.create(id: 'test_billing_anchor', plan: plan.id, source: gen_card_tk)
1165
+
1166
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1167
+ sub.billing_cycle_anchor = 'now'
1168
+ sub.save
1169
+
1170
+ expect(sub.billing_cycle_anchor).to be_a(Integer)
1171
+ end
1172
+
1173
+
921
1174
  end
922
1175
 
923
1176
  context "cancelling a subscription" do
1177
+ let(:customer) { Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id) }
924
1178
 
925
- it "cancels a stripe customer's subscription", :live => true do
926
- truth = stripe_helper.create_plan(id: 'the truth')
927
- customer = Stripe::Customer.create(source: gen_card_tk, plan: "the truth")
1179
+ it "cancels a stripe customer's subscription", live: true do
1180
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
928
1181
 
929
1182
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
930
- result = sub.delete
1183
+ result = sub.cancel
931
1184
 
932
1185
  expect(result.status).to eq('canceled')
933
1186
  expect(result.cancel_at_period_end).to eq false
@@ -939,53 +1192,11 @@ shared_examples 'Customer Subscriptions' do
939
1192
  expect(customer.subscriptions.count).to eq(0)
940
1193
  expect(customer.subscriptions.data.length).to eq(0)
941
1194
  end
942
-
943
- it "cancels a stripe customer's subscription at period end" do
944
- truth = stripe_helper.create_plan(id: 'the_truth')
945
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
946
-
947
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
948
- result = sub.delete(at_period_end: true)
949
-
950
- expect(result.status).to eq('active')
951
- expect(result.cancel_at_period_end).to eq(true)
952
- expect(result.id).to eq(sub.id)
953
-
954
- customer = Stripe::Customer.retrieve('test_customer_sub')
955
- expect(customer.subscriptions.data).to_not be_empty
956
- expect(customer.subscriptions.count).to eq(1)
957
- expect(customer.subscriptions.data.length).to eq(1)
958
-
959
- expect(customer.subscriptions.data.first.status).to eq('active')
960
- expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
961
- expect(customer.subscriptions.data.first.ended_at).to be_nil
962
- expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
963
- end
964
-
965
- it "resumes an at period end cancelled subscription" do
966
- truth = stripe_helper.create_plan(id: 'the_truth')
967
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
968
-
969
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
970
- result = sub.delete(at_period_end: true)
971
-
972
- sub.plan = 'the_truth'
973
- sub.save
974
-
975
- customer = Stripe::Customer.retrieve('test_customer_sub')
976
- expect(customer.subscriptions.data).to_not be_empty
977
- expect(customer.subscriptions.count).to eq(1)
978
- expect(customer.subscriptions.data.length).to eq(1)
979
-
980
- expect(customer.subscriptions.data.first.status).to eq('active')
981
- expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
982
- expect(customer.subscriptions.data.first.ended_at).to be_nil
983
- expect(customer.subscriptions.data.first.canceled_at).to be_nil
984
- end
985
1195
  end
986
1196
 
987
1197
  it "supports 'cancelling' by updating cancel_at_period_end" do
988
- truth = stripe_helper.create_plan(id: 'the_truth')
1198
+ product = stripe_helper.create_product(name: 'Truth Product')
1199
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
989
1200
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
990
1201
 
991
1202
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1007,7 +1218,8 @@ shared_examples 'Customer Subscriptions' do
1007
1218
  end
1008
1219
 
1009
1220
  it "resumes a subscription cancelled by updating cancel_at_period_end" do
1010
- truth = stripe_helper.create_plan(id: 'the_truth')
1221
+ product = stripe_helper.create_product(name: 'Truth Product')
1222
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
1011
1223
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
1012
1224
 
1013
1225
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1031,11 +1243,11 @@ shared_examples 'Customer Subscriptions' do
1031
1243
  end
1032
1244
 
1033
1245
  it "doesn't change status of subscription when cancelling at period end" do
1034
- trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
1246
+ trial = stripe_helper.create_plan(id: 'trial', product: product.id, trial_period_days: 14)
1035
1247
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
1036
1248
 
1037
1249
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1038
- result = sub.delete(at_period_end: true)
1250
+ result = sub.cancel(at_period_end: true)
1039
1251
 
1040
1252
  expect(result.status).to eq('trialing')
1041
1253
 
@@ -1044,11 +1256,11 @@ shared_examples 'Customer Subscriptions' do
1044
1256
  expect(customer.subscriptions.data.first.status).to eq('trialing')
1045
1257
  end
1046
1258
 
1047
- it "doesn't require a card when trial_end is present", :live => true do
1259
+ it "doesn't require a card when trial_end is present", live: true do
1048
1260
  plan = stripe_helper.create_plan(
1049
1261
  :amount => 2000,
1262
+ :product => product.id,
1050
1263
  :interval => 'month',
1051
- :name => 'Amazing Gold Plan',
1052
1264
  :currency => 'usd',
1053
1265
  :id => 'gold'
1054
1266
  )
@@ -1063,7 +1275,7 @@ shared_examples 'Customer Subscriptions' do
1063
1275
  let(:subscription) { Stripe::Subscription.retrieve(customer.subscriptions.data.first.id) }
1064
1276
 
1065
1277
  before do
1066
- stripe_helper.create_plan(id: 'free', amount: 0)
1278
+ free_plan
1067
1279
  Stripe::Subscription.create({ plan: 'free', customer: customer.id })
1068
1280
  end
1069
1281
 
@@ -1076,7 +1288,7 @@ shared_examples 'Customer Subscriptions' do
1076
1288
  expect(subscription.items.object).to eq('list')
1077
1289
  expect(subscription.items.data.class).to eq(Array)
1078
1290
  expect(subscription.items.data.count).to eq(1)
1079
- expect(subscription.items.data.first.id).to eq('test_txn_default')
1291
+ expect(subscription.items.data.first.id).to include('test_si_')
1080
1292
  expect(subscription.items.data.first.created).to eq(1504716183)
1081
1293
  expect(subscription.items.data.first.object).to eq('subscription_item')
1082
1294
  expect(subscription.items.data.first.plan.amount).to eq(0)
@@ -1084,17 +1296,21 @@ shared_examples 'Customer Subscriptions' do
1084
1296
  expect(subscription.items.data.first.plan.currency).to eq('usd')
1085
1297
  expect(subscription.items.data.first.quantity).to eq(2)
1086
1298
  end
1299
+
1300
+ it "has a start_date attribute" do
1301
+ expect(subscription).to respond_to(:start_date)
1302
+ end
1087
1303
  end
1088
1304
 
1089
1305
  context "retrieve multiple subscriptions" do
1090
1306
 
1091
1307
  it "retrieves a list of multiple subscriptions" do
1092
- free = stripe_helper.create_plan(id: 'free', amount: 0)
1093
- paid = stripe_helper.create_plan(id: 'paid', amount: 499)
1094
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "free")
1308
+ free_plan
1309
+ paid = stripe_helper.create_plan(id: 'paid', product: product.id, amount: 499)
1310
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: free_plan.id)
1095
1311
  Stripe::Subscription.create({ plan: 'paid', customer: customer.id })
1096
1312
 
1097
- subs = Stripe::Subscription.all({ customer: customer.id })
1313
+ subs = Stripe::Subscription.list({ customer: customer.id })
1098
1314
 
1099
1315
  expect(subs.object).to eq("list")
1100
1316
  expect(subs.count).to eq(2)
@@ -1105,25 +1321,67 @@ shared_examples 'Customer Subscriptions' do
1105
1321
  Stripe::Customer.create(id: 'no_subs')
1106
1322
  customer = Stripe::Customer.retrieve('no_subs')
1107
1323
 
1108
- list = Stripe::Subscription.all({ customer: customer.id })
1324
+ list = Stripe::Subscription.list({ customer: customer.id })
1109
1325
 
1110
1326
  expect(list.object).to eq("list")
1111
1327
  expect(list.count).to eq(0)
1112
1328
  expect(list.data.length).to eq(0)
1113
1329
  end
1330
+
1331
+ it "does not include canceled subscriptions by default" do
1332
+ customer = Stripe::Customer.create(source: gen_card_tk)
1333
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
1334
+ subscription.cancel
1335
+
1336
+ list = Stripe::Subscription.list({customer: customer.id})
1337
+
1338
+ expect(list.object).to eq("list")
1339
+ expect(list.data).to be_empty
1340
+ expect(list.data.length).to eq(0)
1341
+ end
1342
+
1343
+ it "filters out subscriptions based on their current_period", live: true do
1344
+ price = stripe_helper.create_price(recurring: { interval: 'month' })
1345
+ price2 = stripe_helper.create_price(recurring: { interval: 'year' })
1346
+
1347
+ subscription1 = Stripe::Subscription.create(
1348
+ customer: Stripe::Customer.create(source: gen_card_tk).id,
1349
+ items: [{ price: price.id, quantity: 1 }]
1350
+ )
1351
+ subscription2 = Stripe::Subscription.create(
1352
+ customer: Stripe::Customer.create(source: gen_card_tk).id,
1353
+ items: [{ price: price2.id, quantity: 1 }]
1354
+ )
1355
+
1356
+ list = Stripe::Subscription.list({ current_period_end: { gt: subscription1.current_period_end }})
1357
+ expect(list.data).to contain_exactly(subscription2)
1358
+
1359
+ list = Stripe::Subscription.list({ current_period_end: { gte: subscription1.current_period_end }})
1360
+ expect(list.data).to contain_exactly(subscription1, subscription2)
1361
+
1362
+ list = Stripe::Subscription.list({ current_period_end: { lt: subscription1.current_period_end }})
1363
+ expect(list.data).to be_empty
1364
+
1365
+ list = Stripe::Subscription.list({ current_period_end: { lte: subscription1.current_period_end }})
1366
+ expect(list.data).to contain_exactly(subscription1)
1367
+
1368
+ list = Stripe::Subscription.list({ current_period_start: subscription1.current_period_start })
1369
+ expect(list.data).to contain_exactly(subscription1, subscription2)
1370
+
1371
+ list = Stripe::Subscription.list({ current_period_end: subscription2.current_period_end })
1372
+ expect(list.data).to contain_exactly(subscription2)
1373
+ end
1114
1374
  end
1115
1375
 
1116
1376
  describe "metadata" do
1117
1377
 
1118
- it "creates a stripe customer and subscribes them to a plan with meta data", :live => true do
1378
+ it "creates a stripe customer and subscribes them to a plan with meta data", live: true do
1119
1379
 
1120
1380
  stripe_helper.
1121
1381
  create_plan(
1122
1382
  :amount => 500,
1123
1383
  :interval => 'month',
1124
- :product => {
1125
- :name => 'Sample Plan'
1126
- },
1384
+ :product => product.id,
1127
1385
  :currency => 'usd',
1128
1386
  :id => 'Sample5'
1129
1387
  )
@@ -1143,6 +1401,130 @@ shared_examples 'Customer Subscriptions' do
1143
1401
  expect(customer.subscriptions.first.plan.id).to eq('Sample5')
1144
1402
  expect(customer.subscriptions.first.metadata['foo']).to eq('bar')
1145
1403
  end
1404
+
1405
+ it "saves subscription item metadata" do
1406
+ stripe_helper.
1407
+ create_plan(
1408
+ :amount => 500,
1409
+ :interval => 'month',
1410
+ :product => product.id,
1411
+ :currency => 'usd',
1412
+ :id => 'Sample5'
1413
+ )
1414
+ customer = Stripe::Customer.create({
1415
+ email: 'johnny@appleseed.com',
1416
+ source: gen_card_tk
1417
+ })
1418
+
1419
+ subscription = Stripe::Subscription.create(
1420
+ customer: customer.id,
1421
+ items: [{plan: "Sample5", metadata: {foo: 'bar'}}],
1422
+ )
1423
+ expect(subscription.items.data[0].metadata.to_h).to eq(foo: 'bar')
1424
+ end
1425
+ end
1426
+
1427
+ context "search" do
1428
+ # the Search API requires about a minute between writes and reads, so add sleeps accordingly when running live
1429
+ it "searches subscriptions for exact matches", :aggregate_failures do
1430
+ response = Stripe::Subscription.search({query: 'status:"active"'}, stripe_version: '2020-08-27')
1431
+ expect(response.data.size).to eq(0)
1432
+
1433
+ stripe_helper.create_plan(
1434
+ amount: 500,
1435
+ interval: 'month',
1436
+ product: product.id,
1437
+ currency: 'usd',
1438
+ id: 'Sample5'
1439
+ )
1440
+ customer = Stripe::Customer.create(email: 'johnny@appleseed.com', source: gen_card_tk)
1441
+ one = Stripe::Subscription.create(customer: customer.id, items: [{plan: "Sample5"}], metadata: {key: 'uno'})
1442
+ two = Stripe::Subscription.create(customer: customer.id, items: [{plan: "Sample5"}], metadata: {key: 'dos'})
1443
+ Stripe::Subscription.cancel(two.id)
1444
+
1445
+ response = Stripe::Subscription.search({query: 'status:"active"'}, stripe_version: '2020-08-27')
1446
+ expect(response.data.map(&:id)).to match_array([one.id])
1447
+
1448
+ response = Stripe::Subscription.search({query: 'metadata["key"]:"dos"'}, stripe_version: '2020-08-27')
1449
+ expect(response.data.map(&:id)).to match_array([two.id])
1450
+ end
1451
+
1452
+ it "respects limit", :aggregate_failures do
1453
+ stripe_helper.create_plan(
1454
+ amount: 500,
1455
+ interval: 'month',
1456
+ product: product.id,
1457
+ currency: 'usd',
1458
+ id: 'Sample5'
1459
+ )
1460
+ customer = Stripe::Customer.create(email: 'johnny@appleseed.com', source: gen_card_tk)
1461
+ 11.times do
1462
+ Stripe::Subscription.create(customer: customer.id, items: [{plan: "Sample5"}])
1463
+ end
1464
+
1465
+ response = Stripe::Subscription.search({query: 'status:"active"'}, stripe_version: '2020-08-27')
1466
+ expect(response.data.size).to eq(10)
1467
+ response = Stripe::Subscription.search({query: 'status:"active"', limit: 1}, stripe_version: '2020-08-27')
1468
+ expect(response.data.size).to eq(1)
1469
+ end
1470
+
1471
+ it "reports search errors", :aggregate_failures do
1472
+ expect {
1473
+ Stripe::Subscription.search({limit: 1}, stripe_version: '2020-08-27')
1474
+ }.to raise_error(Stripe::InvalidRequestError, /Missing required param: query./)
1475
+
1476
+ expect {
1477
+ Stripe::Subscription.search({query: 'asdf'}, stripe_version: '2020-08-27')
1478
+ }.to raise_error(Stripe::InvalidRequestError, /We were unable to parse your search query./)
1479
+
1480
+ expect {
1481
+ Stripe::Subscription.search({query: 'foo:"bar"'}, stripe_version: '2020-08-27')
1482
+ }.to raise_error(Stripe::InvalidRequestError, /Field `foo` is an unsupported search field for resource `subscriptions`./)
1483
+ end
1146
1484
  end
1485
+ end
1486
+
1487
+ shared_examples 'Customer Subscriptions with prices' do
1488
+ let(:gen_card_tk) { stripe_helper.generate_card_token }
1147
1489
 
1490
+ let(:product) { stripe_helper.create_product }
1491
+ let(:price) { stripe_helper.create_price(product: product.id, amount: 4999, currency: 'usd') }
1492
+
1493
+ context "creating a new subscription" do
1494
+ it "adds a new subscription to customer with none using items", live: true do
1495
+ customer = Stripe::Customer.create(source: gen_card_tk)
1496
+
1497
+ expect(customer.subscriptions.data).to be_empty
1498
+ expect(customer.subscriptions.count).to eq(0)
1499
+
1500
+ subscription = Stripe::Subscription.create({
1501
+ customer: customer.id,
1502
+ items: [{ price: price.id }],
1503
+ metadata: { foo: "bar", example: "yes" }
1504
+ })
1505
+
1506
+ expect(subscription.object).to eq('subscription')
1507
+ expect(subscription.plan.to_hash).to eq(price.to_hash)
1508
+ expect(subscription.items.first.price.to_hash).to eq(price.to_hash)
1509
+ expect(subscription.metadata.foo).to eq("bar")
1510
+ expect(subscription.metadata.example).to eq("yes")
1511
+
1512
+ customer = Stripe::Customer.retrieve(customer.id)
1513
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
1514
+ charges = Stripe::Charge.list(customer: customer.id)
1515
+
1516
+ expect(subscriptions.data).to_not be_empty
1517
+ expect(subscriptions.count).to eq(1)
1518
+ expect(subscriptions.data.length).to eq(1)
1519
+ expect(charges.data.length).to eq(1)
1520
+ expect(customer.currency).to eq("usd")
1521
+
1522
+ expect(subscriptions.data.first.id).to eq(subscription.id)
1523
+ expect(subscriptions.data.first.plan.to_hash).to eq(price.to_hash)
1524
+ expect(subscriptions.data.first.items.first.price.to_hash).to eq(price.to_hash)
1525
+ expect(subscriptions.data.first.customer).to eq(customer.id)
1526
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
1527
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
1528
+ end
1529
+ end
1148
1530
  end