epopia-stripe-ruby-mock 2.5.8

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 (171) hide show
  1. checksums.yaml +7 -0
  2. data/.env +2 -0
  3. data/.gitignore +8 -0
  4. data/.rspec +1 -0
  5. data/.travis.yml +28 -0
  6. data/Gemfile +12 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +413 -0
  9. data/Rakefile +14 -0
  10. data/bin/stripe-mock-server +19 -0
  11. data/lib/stripe_mock.rb +95 -0
  12. data/lib/stripe_mock/api/account_balance.rb +14 -0
  13. data/lib/stripe_mock/api/bank_tokens.rb +13 -0
  14. data/lib/stripe_mock/api/card_tokens.rb +13 -0
  15. data/lib/stripe_mock/api/client.rb +41 -0
  16. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  17. data/lib/stripe_mock/api/debug.rb +11 -0
  18. data/lib/stripe_mock/api/errors.rb +65 -0
  19. data/lib/stripe_mock/api/global_id_prefix.rb +22 -0
  20. data/lib/stripe_mock/api/instance.rb +38 -0
  21. data/lib/stripe_mock/api/live.rb +15 -0
  22. data/lib/stripe_mock/api/server.rb +39 -0
  23. data/lib/stripe_mock/api/test_helpers.rb +24 -0
  24. data/lib/stripe_mock/api/webhooks.rb +88 -0
  25. data/lib/stripe_mock/client.rb +127 -0
  26. data/lib/stripe_mock/data.rb +1193 -0
  27. data/lib/stripe_mock/data/list.rb +73 -0
  28. data/lib/stripe_mock/error_queue.rb +27 -0
  29. data/lib/stripe_mock/errors/closed_client_connection_error.rb +9 -0
  30. data/lib/stripe_mock/errors/server_timeout_error.rb +12 -0
  31. data/lib/stripe_mock/errors/stripe_mock_error.rb +15 -0
  32. data/lib/stripe_mock/errors/uninitialized_instance_error.rb +9 -0
  33. data/lib/stripe_mock/errors/unstarted_state_error.rb +9 -0
  34. data/lib/stripe_mock/errors/unsupported_request_error.rb +4 -0
  35. data/lib/stripe_mock/instance.rb +237 -0
  36. data/lib/stripe_mock/request_handlers/accounts.rb +86 -0
  37. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  38. data/lib/stripe_mock/request_handlers/balance_transactions.rb +37 -0
  39. data/lib/stripe_mock/request_handlers/cards.rb +35 -0
  40. data/lib/stripe_mock/request_handlers/charges.rb +177 -0
  41. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  42. data/lib/stripe_mock/request_handlers/coupons.rb +35 -0
  43. data/lib/stripe_mock/request_handlers/customers.rb +137 -0
  44. data/lib/stripe_mock/request_handlers/disputes.rb +35 -0
  45. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  46. data/lib/stripe_mock/request_handlers/events.rb +21 -0
  47. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  48. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +14 -0
  49. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +127 -0
  50. data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
  51. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +17 -0
  52. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  53. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +119 -0
  54. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +44 -0
  55. data/lib/stripe_mock/request_handlers/invoice_items.rb +45 -0
  56. data/lib/stripe_mock/request_handlers/invoices.rb +177 -0
  57. data/lib/stripe_mock/request_handlers/orders.rb +80 -0
  58. data/lib/stripe_mock/request_handlers/payment_intents.rb +203 -0
  59. data/lib/stripe_mock/request_handlers/payment_methods.rb +112 -0
  60. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  61. data/lib/stripe_mock/request_handlers/plans.rb +42 -0
  62. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  63. data/lib/stripe_mock/request_handlers/recipients.rb +60 -0
  64. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  65. data/lib/stripe_mock/request_handlers/sources.rb +55 -0
  66. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  67. data/lib/stripe_mock/request_handlers/subscriptions.rb +296 -0
  68. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  69. data/lib/stripe_mock/request_handlers/tokens.rb +75 -0
  70. data/lib/stripe_mock/request_handlers/transfers.rb +65 -0
  71. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +32 -0
  72. data/lib/stripe_mock/server.rb +93 -0
  73. data/lib/stripe_mock/test_strategies/base.rb +81 -0
  74. data/lib/stripe_mock/test_strategies/live.rb +40 -0
  75. data/lib/stripe_mock/test_strategies/mock.rb +27 -0
  76. data/lib/stripe_mock/util.rb +44 -0
  77. data/lib/stripe_mock/version.rb +4 -0
  78. data/lib/stripe_mock/webhook_fixtures/account.application.deauthorized.json +12 -0
  79. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  80. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  81. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  82. data/lib/stripe_mock/webhook_fixtures/account.updated.json +26 -0
  83. data/lib/stripe_mock/webhook_fixtures/balance.available.json +25 -0
  84. data/lib/stripe_mock/webhook_fixtures/charge.dispute.closed.json +22 -0
  85. data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +22 -0
  86. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  87. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  88. data/lib/stripe_mock/webhook_fixtures/charge.dispute.updated.json +25 -0
  89. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +56 -0
  90. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +69 -0
  91. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +55 -0
  92. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  93. data/lib/stripe_mock/webhook_fixtures/coupon.created.json +23 -0
  94. data/lib/stripe_mock/webhook_fixtures/coupon.deleted.json +23 -0
  95. data/lib/stripe_mock/webhook_fixtures/customer.created.json +54 -0
  96. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +42 -0
  97. data/lib/stripe_mock/webhook_fixtures/customer.discount.created.json +28 -0
  98. data/lib/stripe_mock/webhook_fixtures/customer.discount.deleted.json +28 -0
  99. data/lib/stripe_mock/webhook_fixtures/customer.discount.updated.json +43 -0
  100. data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +32 -0
  101. data/lib/stripe_mock/webhook_fixtures/customer.source.deleted.json +32 -0
  102. data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -0
  103. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +66 -0
  104. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +65 -0
  105. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +65 -0
  106. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +78 -0
  107. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +57 -0
  108. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +71 -0
  109. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +105 -0
  110. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +112 -0
  111. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +74 -0
  112. data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +21 -0
  113. data/lib/stripe_mock/webhook_fixtures/invoiceitem.deleted.json +21 -0
  114. data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +24 -0
  115. data/lib/stripe_mock/webhook_fixtures/plan.created.json +20 -0
  116. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +20 -0
  117. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +23 -0
  118. data/lib/stripe_mock/webhook_fixtures/transfer.created.json +89 -0
  119. data/lib/stripe_mock/webhook_fixtures/transfer.failed.json +89 -0
  120. data/lib/stripe_mock/webhook_fixtures/transfer.paid.json +89 -0
  121. data/lib/stripe_mock/webhook_fixtures/transfer.updated.json +92 -0
  122. data/lib/trollop.rb +782 -0
  123. data/spec/_dummy/webhooks/dummy.event.json +6 -0
  124. data/spec/api/instance_spec.rb +30 -0
  125. data/spec/fixtures/create_refund.yml +126 -0
  126. data/spec/fixtures/stripe_webhooks/account.updated.json +7 -0
  127. data/spec/fixtures/stripe_webhooks/custom.account.updated.json +5 -0
  128. data/spec/instance_spec.rb +100 -0
  129. data/spec/integration_examples/charge_token_examples.rb +51 -0
  130. data/spec/integration_examples/customer_card_examples.rb +42 -0
  131. data/spec/integration_examples/prepare_error_examples.rb +38 -0
  132. data/spec/list_spec.rb +140 -0
  133. data/spec/readme_spec.rb +75 -0
  134. data/spec/server_spec.rb +139 -0
  135. data/spec/shared_stripe_examples/account_examples.rb +96 -0
  136. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  137. data/spec/shared_stripe_examples/balance_transaction_examples.rb +63 -0
  138. data/spec/shared_stripe_examples/bank_examples.rb +229 -0
  139. data/spec/shared_stripe_examples/bank_token_examples.rb +59 -0
  140. data/spec/shared_stripe_examples/card_examples.rb +307 -0
  141. data/spec/shared_stripe_examples/card_token_examples.rb +185 -0
  142. data/spec/shared_stripe_examples/charge_examples.rb +510 -0
  143. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  144. data/spec/shared_stripe_examples/coupon_examples.rb +85 -0
  145. data/spec/shared_stripe_examples/customer_examples.rb +453 -0
  146. data/spec/shared_stripe_examples/dispute_examples.rb +98 -0
  147. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  148. data/spec/shared_stripe_examples/error_mock_examples.rb +162 -0
  149. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  150. data/spec/shared_stripe_examples/extra_features_examples.rb +36 -0
  151. data/spec/shared_stripe_examples/invoice_examples.rb +524 -0
  152. data/spec/shared_stripe_examples/invoice_item_examples.rb +69 -0
  153. data/spec/shared_stripe_examples/payment_intent_examples.rb +131 -0
  154. data/spec/shared_stripe_examples/payment_method_examples.rb +175 -0
  155. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  156. data/spec/shared_stripe_examples/plan_examples.rb +194 -0
  157. data/spec/shared_stripe_examples/product_example.rb +65 -0
  158. data/spec/shared_stripe_examples/recipient_examples.rb +118 -0
  159. data/spec/shared_stripe_examples/refund_examples.rb +472 -0
  160. data/spec/shared_stripe_examples/subscription_examples.rb +1148 -0
  161. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  162. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  163. data/spec/shared_stripe_examples/transfer_examples.rb +130 -0
  164. data/spec/shared_stripe_examples/validation_examples.rb +19 -0
  165. data/spec/shared_stripe_examples/webhook_event_examples.rb +261 -0
  166. data/spec/spec_helper.rb +58 -0
  167. data/spec/stripe_mock_spec.rb +123 -0
  168. data/spec/support/stripe_examples.rb +42 -0
  169. data/spec/util_spec.rb +121 -0
  170. data/stripe-ruby-mock.gemspec +27 -0
  171. metadata +344 -0
@@ -0,0 +1,510 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Charge API' do
4
+
5
+ it "requires a valid card token", :live => true do
6
+ expect {
7
+ charge = Stripe::Charge.create(
8
+ amount: 99,
9
+ currency: 'usd',
10
+ source: 'bogus_card_token'
11
+ )
12
+ }.to raise_error(Stripe::InvalidRequestError, /token/i)
13
+ end
14
+
15
+ it "requires a valid customer or source", :live => true do
16
+ expect {
17
+ charge = Stripe::Charge.create(
18
+ amount: 99,
19
+ currency: 'usd',
20
+ )
21
+ }.to raise_error(Stripe::InvalidRequestError, /Must provide source or customer/i)
22
+ end
23
+
24
+ it "requires presence of amount", :live => true do
25
+ expect {
26
+ charge = Stripe::Charge.create(
27
+ currency: 'usd',
28
+ source: stripe_helper.generate_card_token
29
+ )
30
+ }.to raise_error(Stripe::InvalidRequestError, /missing required param: amount/i)
31
+ end
32
+
33
+ it "requires presence of currency", :live => true do
34
+ expect {
35
+ charge = Stripe::Charge.create(
36
+ amount: 99,
37
+ source: stripe_helper.generate_card_token
38
+ )
39
+ }.to raise_error(Stripe::InvalidRequestError, /missing required param: currency/i)
40
+ end
41
+
42
+ it "requires a valid positive amount", :live => true do
43
+ expect {
44
+ charge = Stripe::Charge.create(
45
+ amount: -99,
46
+ currency: 'usd',
47
+ source: stripe_helper.generate_card_token
48
+ )
49
+ }.to raise_error(Stripe::InvalidRequestError, /invalid positive integer/i)
50
+ end
51
+
52
+ it "requires a valid integer amount", :live => true do
53
+ expect {
54
+ charge = Stripe::Charge.create(
55
+ amount: 99.0,
56
+ currency: 'usd',
57
+ source: stripe_helper.generate_card_token
58
+ )
59
+ }.to raise_error(Stripe::InvalidRequestError, /invalid integer/i)
60
+ end
61
+
62
+ it "creates a stripe charge item with a card token" do
63
+ charge = Stripe::Charge.create(
64
+ amount: 999,
65
+ currency: 'USD',
66
+ source: stripe_helper.generate_card_token,
67
+ description: 'card charge'
68
+ )
69
+
70
+ expect(charge.id).to match(/^test_ch/)
71
+ expect(charge.amount).to eq(999)
72
+ expect(charge.description).to eq('card charge')
73
+ expect(charge.captured).to eq(true)
74
+ expect(charge.status).to eq('succeeded')
75
+ end
76
+
77
+ it "creates a stripe charge item with a bank token" do
78
+ charge = Stripe::Charge.create(
79
+ amount: 999,
80
+ currency: 'USD',
81
+ source: stripe_helper.generate_bank_token,
82
+ description: 'bank charge'
83
+ )
84
+
85
+ expect(charge.id).to match(/^test_ch/)
86
+ expect(charge.amount).to eq(999)
87
+ expect(charge.description).to eq('bank charge')
88
+ expect(charge.captured).to eq(true)
89
+ expect(charge.status).to eq('succeeded')
90
+ end
91
+
92
+ it 'creates a stripe charge item with a customer', :live => true do
93
+ customer = Stripe::Customer.create({
94
+ email: 'johnny@appleseed.com',
95
+ source: stripe_helper.generate_card_token(number: '4012888888881881', address_city: 'LA'),
96
+ description: "a description"
97
+ })
98
+
99
+ expect(customer.sources.data.length).to eq(1)
100
+ expect(customer.sources.data[0].id).not_to be_nil
101
+ expect(customer.sources.data[0].last4).to eq('1881')
102
+
103
+ charge = Stripe::Charge.create(
104
+ amount: 999,
105
+ currency: 'USD',
106
+ customer: customer.id,
107
+ description: 'a charge with a specific customer'
108
+ )
109
+
110
+ expect(charge.id).to match(/^(test_)?ch/)
111
+ expect(charge.amount).to eq(999)
112
+ expect(charge.description).to eq('a charge with a specific customer')
113
+ expect(charge.captured).to eq(true)
114
+ expect(charge.source.last4).to eq('1881')
115
+ expect(charge.source.address_city).to eq('LA')
116
+ end
117
+
118
+ it "creates a stripe charge item with a customer and card id" do
119
+ customer = Stripe::Customer.create({
120
+ email: 'johnny@appleseed.com',
121
+ source: stripe_helper.generate_card_token(number: '4012888888881881'),
122
+ description: "a description"
123
+ })
124
+
125
+ expect(customer.sources.data.length).to eq(1)
126
+ expect(customer.sources.data[0].id).not_to be_nil
127
+ expect(customer.sources.data[0].last4).to eq('1881')
128
+
129
+ card = customer.sources.data[0]
130
+ charge = Stripe::Charge.create(
131
+ amount: 999,
132
+ currency: 'USD',
133
+ customer: customer.id,
134
+ source: card.id,
135
+ description: 'a charge with a specific card'
136
+ )
137
+
138
+ expect(charge.id).to match(/^test_ch/)
139
+ expect(charge.amount).to eq(999)
140
+ expect(charge.description).to eq('a charge with a specific card')
141
+ expect(charge.captured).to eq(true)
142
+ expect(charge.source.last4).to eq('1881')
143
+ end
144
+
145
+
146
+ it "stores a created stripe charge in memory" do
147
+ charge = Stripe::Charge.create({
148
+ amount: 333,
149
+ currency: 'USD',
150
+ source: stripe_helper.generate_card_token
151
+ })
152
+ charge2 = Stripe::Charge.create({
153
+ amount: 777,
154
+ currency: 'USD',
155
+ source: stripe_helper.generate_card_token
156
+ })
157
+ data = test_data_source(:charges)
158
+ expect(data[charge.id]).to_not be_nil
159
+ expect(data[charge.id][:amount]).to eq(333)
160
+
161
+ expect(data[charge2.id]).to_not be_nil
162
+ expect(data[charge2.id][:amount]).to eq(777)
163
+ end
164
+
165
+ it "creates a balance transaction" do
166
+ amount = 300
167
+ fee = 10
168
+ charge = Stripe::Charge.create({
169
+ amount: amount,
170
+ currency: 'USD',
171
+ source: stripe_helper.generate_card_token,
172
+ application_fee: fee,
173
+ })
174
+ bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
175
+ expect(bal_trans.amount).to eq(amount)
176
+ expect(bal_trans.fee).to eq(39 + fee)
177
+ expect(bal_trans.source).to eq(charge.id)
178
+ expect(bal_trans.net).to eq(amount - bal_trans.fee)
179
+ end
180
+
181
+ context 'when conversion rate is set' do
182
+ it "balance transaction stores amount converted from charge currency to USD" do
183
+ StripeMock.set_conversion_rate(1.2)
184
+
185
+ charge = Stripe::Charge.create({
186
+ amount: 300,
187
+ currency: 'CAD',
188
+ source: stripe_helper.generate_card_token
189
+ })
190
+ bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
191
+ expect(bal_trans.amount).to eq(charge.amount * 1.2)
192
+ expect(bal_trans.fee).to eq(39)
193
+ expect(bal_trans.currency).to eq('usd')
194
+ end
195
+ end
196
+
197
+ it "can expand balance transaction when creating a charge" do
198
+ charge = Stripe::Charge.create({
199
+ amount: 300,
200
+ currency: 'USD',
201
+ source: stripe_helper.generate_card_token,
202
+ expand: ['balance_transaction']
203
+ })
204
+ expect(charge.balance_transaction).to be_a(Stripe::BalanceTransaction)
205
+ end
206
+
207
+ it "retrieves a stripe charge" do
208
+ original = Stripe::Charge.create({
209
+ amount: 777,
210
+ currency: 'USD',
211
+ source: stripe_helper.generate_card_token
212
+ })
213
+ charge = Stripe::Charge.retrieve(original.id)
214
+
215
+ expect(charge.id).to eq(original.id)
216
+ expect(charge.amount).to eq(original.amount)
217
+ end
218
+
219
+ it "can expand balance transaction when retrieving a charge" do
220
+ original = Stripe::Charge.create({
221
+ amount: 300,
222
+ currency: 'USD',
223
+ source: stripe_helper.generate_card_token
224
+ })
225
+ charge = Stripe::Charge.retrieve(
226
+ id: original.id,
227
+ expand: ['balance_transaction']
228
+ )
229
+
230
+ expect(charge.balance_transaction).to be_a(Stripe::BalanceTransaction)
231
+ end
232
+
233
+ it "cannot retrieve a charge that doesn't exist" do
234
+ expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
235
+ expect(e).to be_a Stripe::InvalidRequestError
236
+ expect(e.param).to eq('charge')
237
+ expect(e.http_status).to eq(404)
238
+ }
239
+ end
240
+
241
+ it "updates a stripe charge" do
242
+ original = Stripe::Charge.create({
243
+ amount: 777,
244
+ currency: 'USD',
245
+ source: stripe_helper.generate_card_token,
246
+ description: 'Original description',
247
+ })
248
+ charge = Stripe::Charge.retrieve(original.id)
249
+
250
+ charge.description = "Updated description"
251
+ charge.metadata[:receipt_id] = 1234
252
+ charge.receipt_email = "newemail@email.com"
253
+ charge.fraud_details = {"user_report" => "safe"}
254
+ charge.save
255
+
256
+ updated = Stripe::Charge.retrieve(original.id)
257
+
258
+ expect(updated.description).to eq(charge.description)
259
+ expect(updated.metadata.to_hash).to eq(charge.metadata.to_hash)
260
+ expect(updated.receipt_email).to eq(charge.receipt_email)
261
+ expect(updated.fraud_details.to_hash).to eq(charge.fraud_details.to_hash)
262
+ end
263
+
264
+ it "updates a stripe charge with no changes" do
265
+ original = Stripe::Charge.create({
266
+ amount: 777,
267
+ currency: 'USD',
268
+ source: stripe_helper.generate_card_token,
269
+ description: 'Original description',
270
+ destination: {
271
+ account: "acct_SOMEBOGUSID",
272
+ amount: 150
273
+ }
274
+ })
275
+
276
+ expect {
277
+ updated = original.save
278
+ }.not_to raise_error
279
+ end
280
+
281
+ it "marks a charge as safe" do
282
+ original = Stripe::Charge.create({
283
+ amount: 777,
284
+ currency: 'USD',
285
+ source: stripe_helper.generate_card_token
286
+ })
287
+ charge = Stripe::Charge.retrieve(original.id)
288
+
289
+ charge.mark_as_safe
290
+
291
+ updated = Stripe::Charge.retrieve(original.id)
292
+ expect(updated.fraud_details[:user_report]).to eq "safe"
293
+ end
294
+
295
+ it "does not lose data when updating a charge" do
296
+ original = Stripe::Charge.create({
297
+ amount: 777,
298
+ currency: 'USD',
299
+ source: stripe_helper.generate_card_token,
300
+ metadata: {:foo => "bar"}
301
+ })
302
+ original.metadata[:receipt_id] = 1234
303
+ original.save
304
+
305
+ updated = Stripe::Charge.retrieve(original.id)
306
+
307
+ expect(updated.metadata[:foo]).to eq "bar"
308
+ expect(updated.metadata[:receipt_id]).to eq 1234
309
+ end
310
+
311
+ it "disallows most parameters on updating a stripe charge" do
312
+ original = Stripe::Charge.create({
313
+ amount: 777,
314
+ currency: 'USD',
315
+ source: stripe_helper.generate_card_token,
316
+ description: 'Original description',
317
+ })
318
+
319
+ charge = Stripe::Charge.retrieve(original.id)
320
+ charge.currency = "CAD"
321
+ charge.amount = 777
322
+ charge.source = {any: "source"}
323
+
324
+ expect { charge.save }.to raise_error(Stripe::InvalidRequestError) do |error|
325
+ expect(error.message).to match(/Received unknown parameters/)
326
+ expect(error.message).to match(/currency/)
327
+ expect(error.message).to match(/amount/)
328
+ expect(error.message).to match(/source/)
329
+ end
330
+ end
331
+
332
+
333
+ it "creates a unique balance transaction" do
334
+ charge1 = Stripe::Charge.create(
335
+ amount: 999,
336
+ currency: 'USD',
337
+ source: stripe_helper.generate_card_token,
338
+ description: 'card charge'
339
+ )
340
+
341
+ charge2 = Stripe::Charge.create(
342
+ amount: 999,
343
+ currency: 'USD',
344
+ source: stripe_helper.generate_card_token,
345
+ description: 'card charge'
346
+ )
347
+
348
+ expect(charge1.balance_transaction).not_to eq(charge2.balance_transaction)
349
+ end
350
+
351
+ context "retrieving a list of charges" do
352
+ before do
353
+ @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
354
+ @customer2 = Stripe::Customer.create(email: 'johnny2@appleseed.com')
355
+ @charge = Stripe::Charge.create(amount: 1, currency: 'usd', customer: @customer.id)
356
+ @charge2 = Stripe::Charge.create(amount: 1, currency: 'usd', customer: @customer2.id)
357
+ end
358
+
359
+ it "stores charges for a customer in memory" do
360
+ expect(@customer.charges.data.map(&:id)).to eq([@charge.id])
361
+ end
362
+
363
+ it "stores all charges in memory" do
364
+ expect(Stripe::Charge.all.data.map(&:id).reverse).to eq([@charge.id, @charge2.id])
365
+ end
366
+
367
+ it "defaults count to 10 charges" do
368
+ 11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
369
+
370
+ expect(Stripe::Charge.all.data.count).to eq(10)
371
+ end
372
+
373
+ it "is marked as having more when more objects exist" do
374
+ 11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
375
+
376
+ expect(Stripe::Charge.all.has_more).to eq(true)
377
+ end
378
+
379
+ context "when passing limit" do
380
+ it "gets that many charges" do
381
+ expect(Stripe::Charge.all(limit: 1).count).to eq(1)
382
+ end
383
+ end
384
+ end
385
+
386
+ it 'when use starting_after param', live: true do
387
+ cus = Stripe::Customer.create(
388
+ description: 'Customer for test@example.com',
389
+ source: {
390
+ object: 'card',
391
+ number: '4242424242424242',
392
+ exp_month: 12,
393
+ exp_year: 2024,
394
+ cvc: 123
395
+ }
396
+ )
397
+ 12.times do
398
+ Stripe::Charge.create(customer: cus.id, amount: 100, currency: "usd")
399
+ end
400
+
401
+ all = Stripe::Charge.all
402
+ default_limit = 10
403
+ half = Stripe::Charge.all(starting_after: all.data.at(1).id)
404
+
405
+ expect(half).to be_a(Stripe::ListObject)
406
+ expect(half.data.count).to eq(default_limit)
407
+ expect(half.data.first.id).to eq(all.data.at(2).id)
408
+ end
409
+
410
+
411
+ describe 'captured status value' do
412
+ it "reports captured by default" do
413
+ charge = Stripe::Charge.create({
414
+ amount: 777,
415
+ currency: 'USD',
416
+ source: stripe_helper.generate_card_token
417
+ })
418
+
419
+ expect(charge.captured).to eq(true)
420
+ end
421
+
422
+ it "reports captured if capture requested" do
423
+ charge = Stripe::Charge.create({
424
+ amount: 777,
425
+ currency: 'USD',
426
+ source: stripe_helper.generate_card_token,
427
+ capture: true
428
+ })
429
+
430
+ expect(charge.captured).to eq(true)
431
+ end
432
+
433
+ it "reports not captured if capture: false requested" do
434
+ charge = Stripe::Charge.create({
435
+ amount: 777,
436
+ currency: 'USD',
437
+ source: stripe_helper.generate_card_token,
438
+ capture: false
439
+ })
440
+
441
+ expect(charge.captured).to eq(false)
442
+ end
443
+ end
444
+
445
+ describe "two-step charge (auth, then capture)" do
446
+ it "changes captured status upon #capture" do
447
+ charge = Stripe::Charge.create({
448
+ amount: 777,
449
+ currency: 'USD',
450
+ source: stripe_helper.generate_card_token,
451
+ capture: false
452
+ })
453
+
454
+ returned_charge = charge.capture
455
+ expect(charge.captured).to eq(true)
456
+ expect(returned_charge.id).to eq(charge.id)
457
+ expect(returned_charge.captured).to eq(true)
458
+ end
459
+
460
+ it "captures with specified amount" do
461
+ charge = Stripe::Charge.create({
462
+ amount: 777,
463
+ currency: 'USD',
464
+ source: stripe_helper.generate_card_token,
465
+ capture: false
466
+ })
467
+
468
+ returned_charge = charge.capture({ amount: 677, application_fee: 123 })
469
+ expect(charge.captured).to eq(true)
470
+ expect(returned_charge.amount_refunded).to eq(100)
471
+ expect(returned_charge.application_fee).to eq(123)
472
+ expect(returned_charge.id).to eq(charge.id)
473
+ expect(returned_charge.captured).to eq(true)
474
+ end
475
+ end
476
+
477
+ describe "idempotency" do
478
+ let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
479
+ let(:charge_params) {{
480
+ amount: 777,
481
+ currency: 'USD',
482
+ customer: customer.id,
483
+ capture: true
484
+ }}
485
+ let(:charge_headers) {{
486
+ idempotency_key: 'onceisenough'
487
+ }}
488
+
489
+ it "returns the original charge if the same idempotency_key is passed in" do
490
+ charge1 = Stripe::Charge.create(charge_params, charge_headers)
491
+ charge2 = Stripe::Charge.create(charge_params, charge_headers)
492
+
493
+ expect(charge1).to eq(charge2)
494
+ end
495
+
496
+ context 'different key' do
497
+ let(:different_charge_headers) {{
498
+ idempotency_key: 'thisoneisdifferent'
499
+ }}
500
+
501
+ it "returns different charges if different idempotency_keys are used for each charge" do
502
+ charge1 = Stripe::Charge.create(charge_params, charge_headers)
503
+ charge2 = Stripe::Charge.create(charge_params, different_charge_headers)
504
+
505
+ expect(charge1).not_to eq(charge2)
506
+ end
507
+ end
508
+ end
509
+
510
+ end