stripe-ruby-mock 2.3.1 → 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 (106) hide show
  1. checksums.yaml +4 -4
  2. data/.env +2 -0
  3. data/.travis.yml +8 -4
  4. data/README.md +12 -5
  5. data/lib/stripe_mock/api/account_balance.rb +14 -0
  6. data/lib/stripe_mock/api/client.rb +4 -4
  7. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  8. data/lib/stripe_mock/api/errors.rb +25 -13
  9. data/lib/stripe_mock/api/instance.rb +16 -6
  10. data/lib/stripe_mock/api/webhooks.rb +8 -1
  11. data/lib/stripe_mock/client.rb +18 -2
  12. data/lib/stripe_mock/data/list.rb +14 -2
  13. data/lib/stripe_mock/data.rb +398 -58
  14. data/lib/stripe_mock/instance.rb +105 -9
  15. data/lib/stripe_mock/request_handlers/accounts.rb +41 -2
  16. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  17. data/lib/stripe_mock/request_handlers/balance_transactions.rb +18 -2
  18. data/lib/stripe_mock/request_handlers/charges.rb +44 -33
  19. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  20. data/lib/stripe_mock/request_handlers/coupons.rb +5 -4
  21. data/lib/stripe_mock/request_handlers/customers.rb +29 -11
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  23. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  24. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +1 -1
  25. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +9 -7
  26. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -6
  27. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  28. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +59 -16
  29. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -2
  30. data/lib/stripe_mock/request_handlers/invoices.rb +93 -14
  31. data/lib/stripe_mock/request_handlers/orders.rb +5 -5
  32. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  33. data/lib/stripe_mock/request_handlers/plans.rb +1 -0
  34. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  35. data/lib/stripe_mock/request_handlers/recipients.rb +12 -0
  36. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  37. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  38. data/lib/stripe_mock/request_handlers/subscriptions.rb +101 -39
  39. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  40. data/lib/stripe_mock/request_handlers/tokens.rb +9 -3
  41. data/lib/stripe_mock/request_handlers/transfers.rb +11 -5
  42. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +15 -1
  43. data/lib/stripe_mock/server.rb +14 -1
  44. data/lib/stripe_mock/test_strategies/base.rb +10 -5
  45. data/lib/stripe_mock/test_strategies/live.rb +5 -0
  46. data/lib/stripe_mock/test_strategies/mock.rb +8 -0
  47. data/lib/stripe_mock/util.rb +8 -2
  48. data/lib/stripe_mock/version.rb +1 -1
  49. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  50. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  51. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  52. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  53. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  54. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  55. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  56. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +40 -10
  57. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +39 -10
  58. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +39 -10
  59. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +40 -11
  60. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  61. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  62. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +92 -85
  63. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  64. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  65. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  66. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  67. data/lib/stripe_mock.rb +15 -0
  68. data/spec/api/instance_spec.rb +30 -0
  69. data/spec/instance_spec.rb +54 -4
  70. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  71. data/spec/list_spec.rb +27 -10
  72. data/spec/readme_spec.rb +2 -0
  73. data/spec/server_spec.rb +7 -3
  74. data/spec/shared_stripe_examples/account_examples.rb +46 -0
  75. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  76. data/spec/shared_stripe_examples/balance_transaction_examples.rb +28 -0
  77. data/spec/shared_stripe_examples/bank_examples.rb +28 -1
  78. data/spec/shared_stripe_examples/card_examples.rb +23 -5
  79. data/spec/shared_stripe_examples/card_token_examples.rb +1 -0
  80. data/spec/shared_stripe_examples/charge_examples.rb +131 -26
  81. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  82. data/spec/shared_stripe_examples/coupon_examples.rb +8 -2
  83. data/spec/shared_stripe_examples/customer_examples.rb +90 -0
  84. data/spec/shared_stripe_examples/dispute_examples.rb +19 -8
  85. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  86. data/spec/shared_stripe_examples/error_mock_examples.rb +15 -5
  87. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  88. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  89. data/spec/shared_stripe_examples/invoice_examples.rb +314 -51
  90. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  91. data/spec/shared_stripe_examples/plan_examples.rb +47 -4
  92. data/spec/shared_stripe_examples/product_example.rb +65 -0
  93. data/spec/shared_stripe_examples/recipient_examples.rb +13 -7
  94. data/spec/shared_stripe_examples/refund_examples.rb +453 -84
  95. data/spec/shared_stripe_examples/subscription_examples.rb +477 -32
  96. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  97. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  98. data/spec/shared_stripe_examples/transfer_examples.rb +72 -23
  99. data/spec/shared_stripe_examples/webhook_event_examples.rb +74 -5
  100. data/spec/spec_helper.rb +7 -6
  101. data/spec/stripe_mock_spec.rb +16 -3
  102. data/spec/support/stripe_examples.rb +8 -1
  103. data/spec/util_spec.rb +35 -1
  104. data/stripe-ruby-mock.gemspec +1 -1
  105. metadata +44 -8
  106. data/ChangeLog.rdoc +0 -4
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Product API' do
4
+ it 'creates a product' do
5
+ product = Stripe::Product.create(
6
+ name: 'my awesome product',
7
+ type: 'service'
8
+ )
9
+
10
+ expect(product.name).to eq 'my awesome product'
11
+ expect(product.type).to eq 'service'
12
+ end
13
+
14
+ it 'retrieves a product' do
15
+ Stripe::Product.create(
16
+ id: 'test_prod_1',
17
+ name: 'my awesome product',
18
+ type: 'service'
19
+ )
20
+
21
+ product = Stripe::Product.retrieve('test_prod_1')
22
+
23
+ expect(product.name).to eq 'my awesome product'
24
+ expect(product.type).to eq 'service'
25
+ end
26
+
27
+ it 'updates a product' do
28
+ Stripe::Product.create(
29
+ id: 'test_prod_1',
30
+ name: 'my awesome product',
31
+ type: 'service'
32
+ )
33
+
34
+ Stripe::Product.update('test_prod_1', name: 'my lame product')
35
+
36
+ product = Stripe::Product.retrieve('test_prod_1')
37
+
38
+ expect(product.name).to eq 'my lame product'
39
+ end
40
+
41
+ it 'lists all products' do
42
+ 2.times do |n|
43
+ Stripe::Product.create(
44
+ name: "product #{n}",
45
+ type: 'service'
46
+ )
47
+ end
48
+
49
+ products = Stripe::Product.list
50
+
51
+ expect(products.map(&:name)).to match_array ['product 0', 'product 1']
52
+ end
53
+
54
+ it 'destroys a product', live: true do
55
+ Stripe::Product.create(
56
+ id: 'test_prod_1',
57
+ name: 'my awesome product',
58
+ type: 'service'
59
+ )
60
+
61
+ Stripe::Product.delete('test_prod_1')
62
+
63
+ expect { Stripe::Product.retrieve('test_prod_1') }. to raise_error(Stripe::InvalidRequestError)
64
+ end
65
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Recipient API' do
4
4
 
5
- it "creates a stripe recipient with a default bank and card" do
5
+ it "creates a stripe recipient with a default bank and card", skip: 'Stripe has deprecated Recipients' do
6
6
  recipient = Stripe::Recipient.create({
7
7
  type: "corporation",
8
8
  name: "MyCo",
@@ -28,7 +28,14 @@ shared_examples 'Recipient API' do
28
28
  expect { recipient.card }.to raise_error
29
29
  end
30
30
 
31
- it "creates a stripe recipient without a card" do
31
+ it "raises a error if params are invalid", skip: 'Stripe has deprecated Recipients' do
32
+ expect { Stripe::Recipient.create(name: "foo") }.to raise_error
33
+ expect { Stripe::Recipient.create(type: "individual") }.to raise_error
34
+ expect { Stripe::Recipient.create(name: "foo", type: "bar") }.to raise_error
35
+ expect { Stripe::Recipient.create(name: "foo", type: "individual") }.not_to raise_error
36
+ end
37
+
38
+ it "creates a stripe recipient without a card", skip: 'Stripe has deprecated Recipients' do
32
39
  recipient = Stripe::Recipient.create({
33
40
  type: "corporation",
34
41
  name: "MyCo",
@@ -44,7 +51,7 @@ shared_examples 'Recipient API' do
44
51
  expect(recipient.default_card).to be_nil
45
52
  end
46
53
 
47
- it "stores a created stripe recipient in memory" do
54
+ it "stores a created stripe recipient in memory", skip: 'Stripe has deprecated Recipients' do
48
55
  recipient = Stripe::Recipient.create({
49
56
  type: "individual",
50
57
  name: "Customer One",
@@ -69,7 +76,7 @@ shared_examples 'Recipient API' do
69
76
  expect(data[recipient2.id][:default_card]).to_not be_nil
70
77
  end
71
78
 
72
- it "retrieves a stripe recipient" do
79
+ it "retrieves a stripe recipient", skip: 'Stripe has deprecated Recipients' do
73
80
  original = Stripe::Recipient.create({
74
81
  type: "individual",
75
82
  name: "Bob",
@@ -85,7 +92,7 @@ shared_examples 'Recipient API' do
85
92
  expect(recipient.default_card).to_not be_nil
86
93
  end
87
94
 
88
- it "cannot retrieve a recipient that doesn't exist" do
95
+ it "cannot retrieve a recipient that doesn't exist", skip: 'Stripe has deprecated Recipients' do
89
96
  expect { Stripe::Recipient.retrieve('nope') }.to raise_error {|e|
90
97
  expect(e).to be_a Stripe::InvalidRequestError
91
98
  expect(e.param).to eq('recipient')
@@ -93,7 +100,7 @@ shared_examples 'Recipient API' do
93
100
  }
94
101
  end
95
102
 
96
- describe "Errors", :live => true do
103
+ describe "Errors", :live => true, skip: 'Stripe has deprecated Recipients' do
97
104
  it "throws an error when the customer does not have the retrieving card id" do
98
105
  recipient = Stripe::Recipient.create(:name => "Bob Bobber", :type => "individual")
99
106
  card_id = "card_123"
@@ -109,4 +116,3 @@ shared_examples 'Recipient API' do
109
116
  end
110
117
 
111
118
  end
112
-
@@ -2,102 +2,471 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Refund API' do
4
4
 
5
- it "refunds a stripe charge item" do
6
- charge = Stripe::Charge.create(
7
- amount: 999,
8
- currency: 'USD',
9
- card: stripe_helper.generate_card_token,
10
- description: 'card charge'
11
- )
12
-
13
- charge = charge.refund(amount: 999)
14
-
15
- expect(charge.refunded).to eq(true)
16
- expect(charge.refunds.data.first.amount).to eq(999)
17
- expect(charge.amount_refunded).to eq(999)
18
- end
5
+ describe 'standard API' do
6
+ it "refunds a stripe charge item" do
7
+ charge = Stripe::Charge.create(
8
+ amount: 999,
9
+ currency: 'USD',
10
+ source: stripe_helper.generate_card_token,
11
+ description: 'card charge'
12
+ )
19
13
 
20
- it "creates a stripe refund with the charge ID", :live => true do
21
- charge = Stripe::Charge.create(
22
- amount: 999,
23
- currency: 'USD',
24
- card: stripe_helper.generate_card_token,
25
- description: 'card charge'
26
- )
27
- refund = charge.refund
28
-
29
- expect(charge.id).to match(/^(test_)?ch/)
30
- expect(refund.id).to eq(charge.id)
31
- end
14
+ Stripe::Refund.create(
15
+ charge: charge.id
16
+ )
32
17
 
33
- it "creates a stripe refund with a refund ID" do
34
- charge = Stripe::Charge.create(
35
- amount: 999,
36
- currency: 'USD',
37
- card: stripe_helper.generate_card_token,
38
- description: 'card charge'
39
- )
40
- refund = charge.refund
41
-
42
- expect(refund.refunds.data.count).to eq 1
43
- expect(refund.refunds.data.first.id).to match(/^test_re/)
44
- end
18
+ charge = Stripe::Charge.retrieve(charge.id)
45
19
 
46
- it "creates a stripe refund with a status" do
47
- charge = Stripe::Charge.create(
48
- amount: 999,
49
- currency: 'USD',
50
- card: stripe_helper.generate_card_token,
51
- description: 'card charge'
52
- )
53
- refund = charge.refund
54
-
55
- expect(refund.refunds.data.count).to eq 1
56
- expect(refund.refunds.data.first.status).to eq("succeeded")
57
- end
58
-
59
- it "creates a stripe refund with a different balance transaction than the charge" do
60
- charge = Stripe::Charge.create(
61
- amount: 999,
62
- currency: 'USD',
63
- card: stripe_helper.generate_card_token,
64
- description: 'card charge'
65
- )
66
- refund = charge.refund
67
-
68
- expect(charge.balance_transaction).not_to eq(refund.refunds.data.first.balance_transaction)
69
- end
20
+ expect(charge.refunded).to eq(true)
21
+ expect(charge.refunds.data.first.amount).to eq(999)
22
+ expect(charge.amount_refunded).to eq(999)
23
+ end
24
+
25
+ it "creates a stripe refund with a status" do
26
+ charge = Stripe::Charge.create(
27
+ amount: 999,
28
+ currency: 'USD',
29
+ source: stripe_helper.generate_card_token,
30
+ description: 'card charge'
31
+ )
32
+
33
+ Stripe::Refund.create(
34
+ charge: charge.id
35
+ )
36
+
37
+ charge = Stripe::Charge.retrieve(charge.id)
38
+
39
+ expect(charge.refunds.data.count).to eq 1
40
+ expect(charge.refunds.data.first.status).to eq("succeeded")
41
+ end
42
+
43
+ it "creates a stripe refund with a different balance transaction than the charge" do
44
+ charge = Stripe::Charge.create(
45
+ amount: 999,
46
+ currency: 'USD',
47
+ source: stripe_helper.generate_card_token,
48
+ description: 'card charge'
49
+ )
50
+
51
+ Stripe::Refund.create(
52
+ charge: charge.id
53
+ )
54
+
55
+ charge = Stripe::Charge.retrieve(charge.id)
56
+
57
+ expect(charge.balance_transaction).not_to eq(charge.refunds.data.first.balance_transaction)
58
+ end
59
+
60
+ it "creates a refund off a charge", :live => true do
61
+ original = Stripe::Charge.create(
62
+ amount: 555,
63
+ currency: 'USD',
64
+ source: stripe_helper.generate_card_token
65
+ )
66
+
67
+ charge = Stripe::Charge.retrieve(original.id)
68
+
69
+ refund = Stripe::Refund.create(
70
+ charge: charge.id,
71
+ amount: 555
72
+ )
73
+
74
+ expect(refund.amount).to eq 555
75
+ expect(refund.charge).to eq charge.id
76
+ end
77
+
78
+ it "handles multiple refunds", :live => true do
79
+ original = Stripe::Charge.create(
80
+ amount: 1100,
81
+ currency: 'USD',
82
+ source: stripe_helper.generate_card_token
83
+ )
84
+
85
+ charge = Stripe::Charge.retrieve(original.id)
86
+
87
+ refund_1 = Stripe::Refund.create(
88
+ charge: charge.id,
89
+ amount: 300
90
+ )
91
+ expect(refund_1.amount).to eq 300
92
+ expect(refund_1.charge).to eq charge.id
93
+
94
+ refund_2 = Stripe::Refund.create(
95
+ charge: charge.id,
96
+ amount: 400
97
+ )
98
+ expect(refund_2.amount).to eq 400
99
+ expect(refund_2.charge).to eq charge.id
100
+
101
+ expect(charge.refunds.count).to eq 0
102
+ expect(charge.refunds.total_count).to eq 0
103
+ expect(charge.amount_refunded).to eq 0
104
+
105
+ charge = Stripe::Charge.retrieve(original.id)
106
+ expect(charge.refunds.count).to eq 2
107
+ expect(charge.refunds.total_count).to eq 2
108
+ expect(charge.amount_refunded).to eq 700
109
+ end
110
+
111
+ it 'returns Stripe::Refund object', live: true do
112
+ charge = Stripe::Charge.create(
113
+ amount: 999,
114
+ currency: 'USD',
115
+ source: stripe_helper.generate_card_token,
116
+ description: 'card charge'
117
+ )
118
+ refund = Stripe::Refund.create(
119
+ charge: charge.id,
120
+ amount: 500
121
+ )
122
+
123
+ expect(refund).to be_a(Stripe::Refund)
124
+ expect(refund.amount).to eq(500)
125
+ end
126
+
127
+ it 'refunds entire charge if amount is not set', live: true do
128
+ charge = Stripe::Charge.create(
129
+ amount: 999,
130
+ currency: 'USD',
131
+ source: stripe_helper.generate_card_token,
132
+ description: 'card charge'
133
+ )
134
+ refund = Stripe::Refund.create(charge: charge.id)
135
+
136
+ expect(refund.amount).to eq(charge.amount)
137
+ end
138
+
139
+ it "stores a created stripe refund in memory" do
140
+ charge_1 = Stripe::Charge.create({
141
+ amount: 333,
142
+ currency: 'USD',
143
+ source: stripe_helper.generate_card_token
144
+ })
145
+ refund_1 = Stripe::Refund.create(
146
+ charge: charge_1.id,
147
+ )
148
+ charge_2 = Stripe::Charge.create({
149
+ amount: 777,
150
+ currency: 'USD',
151
+ source: stripe_helper.generate_card_token
152
+ })
153
+ refund_2 = Stripe::Refund.create(
154
+ charge: charge_2.id,
155
+ )
156
+
157
+ data = test_data_source(:refunds)
158
+ expect(data[refund_1.id]).to_not be_nil
159
+ expect(data[refund_1.id][:amount]).to eq(333)
160
+
161
+ expect(data[refund_2.id]).to_not be_nil
162
+ expect(data[refund_2.id][:amount]).to eq(777)
163
+ end
164
+
165
+ it "creates a balance transaction" do
166
+ charge = Stripe::Charge.create({
167
+ amount: 300,
168
+ currency: 'USD',
169
+ source: stripe_helper.generate_card_token
170
+ })
171
+ refund = Stripe::Refund.create(
172
+ charge: charge.id,
173
+ )
174
+ bal_trans = Stripe::BalanceTransaction.retrieve(refund.balance_transaction)
175
+ expect(bal_trans.amount).to eq(charge.amount * -1)
176
+ expect(bal_trans.fee).to eq(-39)
177
+ expect(bal_trans.source).to eq(refund.id)
178
+ end
179
+
180
+ it "can expand balance transaction" do
181
+ charge = Stripe::Charge.create({
182
+ amount: 300,
183
+ currency: 'USD',
184
+ source: stripe_helper.generate_card_token
185
+ })
186
+ refund = Stripe::Refund.create(
187
+ charge: charge.id,
188
+ expand: ['balance_transaction']
189
+ )
190
+ expect(refund.balance_transaction).to be_a(Stripe::BalanceTransaction)
191
+ end
192
+
193
+ it "retrieves a stripe refund" do
194
+ charge = Stripe::Charge.create({
195
+ amount: 777,
196
+ currency: 'USD',
197
+ source: stripe_helper.generate_card_token
198
+ })
199
+ original = Stripe::Refund.create(
200
+ charge: charge.id
201
+ )
202
+ refund = Stripe::Refund.retrieve(original.id)
203
+
204
+ expect(refund.id).to eq(original.id)
205
+ expect(refund.amount).to eq(original.amount)
206
+ end
207
+
208
+ it "cannot retrieve a refund that doesn't exist" do
209
+ expect { Stripe::Refund.retrieve('nope') }.to raise_error {|e|
210
+ expect(e).to be_a Stripe::InvalidRequestError
211
+ expect(e.param).to eq('refund')
212
+ expect(e.http_status).to eq(404)
213
+ }
214
+ end
215
+
216
+ it "updates a stripe charge" do
217
+ charge = Stripe::Charge.create({
218
+ amount: 777,
219
+ currency: 'USD',
220
+ source: stripe_helper.generate_card_token,
221
+ description: 'Original description',
222
+ })
223
+ original = Stripe::Refund.create(charge: charge.id)
224
+ refund = Stripe::Refund.retrieve(original.id)
70
225
 
71
- it "creates a refund off a charge", :live => true do
72
- original = Stripe::Charge.create(amount: 555, currency: 'USD', card: stripe_helper.generate_card_token)
226
+ refund.metadata[:order_id] = 6735
227
+ refund.save
73
228
 
74
- charge = Stripe::Charge.retrieve(original.id)
229
+ updated = Stripe::Refund.retrieve(original.id)
75
230
 
76
- refund = charge.refunds.create(amount: 555)
77
- expect(refund.amount).to eq 555
78
- expect(refund.charge).to eq charge.id
231
+ expect(updated.metadata.to_hash).to eq(refund.metadata.to_hash)
232
+ end
233
+
234
+ it "disallows most parameters on updating a stripe charge" do
235
+ charge = Stripe::Charge.create({
236
+ amount: 777,
237
+ currency: 'USD',
238
+ source: stripe_helper.generate_card_token,
239
+ description: 'Original description',
240
+ })
241
+ original = Stripe::Refund.create(charge: charge.id)
242
+
243
+ refund = Stripe::Refund.retrieve(original.id)
244
+ refund.reason = "customer changed is mind"
245
+ refund.amount = 777
246
+
247
+ expect { refund.save }.to raise_error(Stripe::InvalidRequestError) do |error|
248
+ expect(error.message).to match(/Received unknown parameters/)
249
+ expect(error.message).to match(/reason/)
250
+ expect(error.message).to match(/amount/)
251
+ end
252
+ end
253
+
254
+ context "retrieving a list of charges" do
255
+ before do
256
+ customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
257
+ customer2 = Stripe::Customer.create(email: 'johnny2@appleseed.com')
258
+ charge = Stripe::Charge.create(amount: 15, currency: 'usd', customer: customer.id)
259
+ @refund = Stripe::Refund.create(charge: charge.id)
260
+ charge2 = Stripe::Charge.create(amount: 27, currency: 'usd', customer: customer2.id)
261
+ @refund2 = Stripe::Refund.create(charge: charge2.id)
262
+ end
263
+
264
+ it "stores all charges in memory" do
265
+ expect(Stripe::Refund.all.data.map(&:id)).to eq([@refund2.id, @refund.id])
266
+ end
267
+
268
+ it "defaults count to 10 charges" do
269
+ 11.times do
270
+ charge = Stripe::Charge.create(
271
+ amount: 1,
272
+ currency: 'usd',
273
+ source: stripe_helper.generate_card_token
274
+ )
275
+ Stripe::Refund.create(charge: charge.id)
276
+ end
277
+
278
+ expect(Stripe::Refund.all.data.count).to eq(10)
279
+ end
280
+
281
+ it "is marked as having more when more objects exist" do
282
+ 11.times do
283
+ charge = Stripe::Charge.create(
284
+ amount: 1,
285
+ currency: 'usd',
286
+ source: stripe_helper.generate_card_token
287
+ )
288
+ Stripe::Refund.create(charge: charge.id)
289
+ end
290
+
291
+ expect(Stripe::Refund.all.has_more).to eq(true)
292
+ end
293
+
294
+ context "when passing limit" do
295
+ it "gets that many charges" do
296
+ expect(Stripe::Refund.all(limit: 1).count).to eq(1)
297
+ end
298
+ end
299
+ end
300
+
301
+ it 'when use starting_after param', live: true do
302
+ customer = Stripe::Customer.create(
303
+ description: 'Customer for test@example.com',
304
+ source: {
305
+ object: 'card',
306
+ number: '4242424242424242',
307
+ exp_month: 12,
308
+ exp_year: 2024,
309
+ cvc: 123
310
+ }
311
+ )
312
+ 12.times do
313
+ charge = Stripe::Charge.create(
314
+ customer: customer.id,
315
+ amount: 100,
316
+ currency: 'usd'
317
+ )
318
+ Stripe::Refund.create(charge: charge.id)
319
+ end
320
+
321
+ all = Stripe::Refund.all
322
+ default_limit = 10
323
+ half = Stripe::Refund.all(starting_after: all.data.at(1).id)
324
+
325
+ expect(half).to be_a(Stripe::ListObject)
326
+ expect(half.data.count).to eq(default_limit)
327
+ expect(half.data.first.id).to eq(all.data.at(2).id)
328
+ end
329
+
330
+ describe "idempotency" do
331
+ let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
332
+ let(:charge) do
333
+ Stripe::Charge.create(
334
+ customer: customer.id,
335
+ amount: 777,
336
+ currency: 'USD',
337
+ capture: true
338
+ )
339
+ end
340
+ let(:refund_params) {{
341
+ charge: charge.id
342
+ }}
343
+
344
+ let(:refund_headers) {{
345
+ idempotency_key: 'onceisenough'
346
+ }}
347
+
348
+ it "returns the original refund if the same idempotency_key is passed in" do
349
+ refund1 = Stripe::Refund.create(refund_params, refund_headers)
350
+ refund2 = Stripe::Refund.create(refund_params, refund_headers)
351
+
352
+ expect(refund1).to eq(refund2)
353
+ end
354
+
355
+ context 'different key' do
356
+ let(:different_refund_headers) {{
357
+ idempotency_key: 'thisoneisdifferent'
358
+ }}
359
+
360
+ it "returns different charges if different idempotency_keys are used for each charge" do
361
+ refund1 = Stripe::Refund.create(refund_params, refund_headers)
362
+ refund2 = Stripe::Refund.create(refund_params, different_refund_headers)
363
+
364
+ expect(refund1).not_to eq(refund2)
365
+ end
366
+ end
367
+ end
79
368
  end
80
369
 
81
- it "handles multiple refunds", :live => true do
82
- original = Stripe::Charge.create(amount: 1100, currency: 'USD', card: stripe_helper.generate_card_token)
83
370
 
84
- charge = Stripe::Charge.retrieve(original.id)
371
+ describe 'charge refund API' do
372
+
373
+ it "refunds a stripe charge item" do
374
+ charge = Stripe::Charge.create(
375
+ amount: 999,
376
+ currency: 'USD',
377
+ source: stripe_helper.generate_card_token,
378
+ description: 'card charge'
379
+ )
380
+
381
+ charge = charge.refund(amount: 999)
382
+
383
+ expect(charge.refunded).to eq(true)
384
+ expect(charge.refunds.data.first.amount).to eq(999)
385
+ expect(charge.amount_refunded).to eq(999)
386
+ end
387
+
388
+ it "creates a stripe refund with the charge ID", :live => true do
389
+ charge = Stripe::Charge.create(
390
+ amount: 999,
391
+ currency: 'USD',
392
+ source: stripe_helper.generate_card_token,
393
+ description: 'card charge'
394
+ )
395
+ refund = charge.refund
396
+
397
+ expect(charge.id).to match(/^(test_)?ch/)
398
+ expect(refund.id).to eq(charge.id)
399
+ end
400
+
401
+ it "creates a stripe refund with a refund ID" do
402
+ charge = Stripe::Charge.create(
403
+ amount: 999,
404
+ currency: 'USD',
405
+ source: stripe_helper.generate_card_token,
406
+ description: 'card charge'
407
+ )
408
+ refund = charge.refund
409
+
410
+ expect(refund.refunds.data.count).to eq 1
411
+ expect(refund.refunds.data.first.id).to match(/^test_re/)
412
+ end
413
+
414
+ it "creates a stripe refund with a status" do
415
+ charge = Stripe::Charge.create(
416
+ amount: 999,
417
+ currency: 'USD',
418
+ source: stripe_helper.generate_card_token,
419
+ description: 'card charge'
420
+ )
421
+ refund = charge.refund
422
+
423
+ expect(refund.refunds.data.count).to eq 1
424
+ expect(refund.refunds.data.first.status).to eq("succeeded")
425
+ end
426
+
427
+ it "creates a stripe refund with a different balance transaction than the charge" do
428
+ charge = Stripe::Charge.create(
429
+ amount: 999,
430
+ currency: 'USD',
431
+ source: stripe_helper.generate_card_token,
432
+ description: 'card charge'
433
+ )
434
+ refund = charge.refund
435
+
436
+ expect(charge.balance_transaction).not_to eq(refund.refunds.data.first.balance_transaction)
437
+ end
438
+
439
+ it "creates a refund off a charge", :live => true do
440
+ original = Stripe::Charge.create(amount: 555, currency: 'USD', source: stripe_helper.generate_card_token)
441
+
442
+ charge = Stripe::Charge.retrieve(original.id)
443
+
444
+ refund = charge.refunds.create(amount: 555)
445
+ expect(refund.amount).to eq 555
446
+ expect(refund.charge).to eq charge.id
447
+ end
448
+
449
+ it "handles multiple refunds", :live => true do
450
+ original = Stripe::Charge.create(amount: 1100, currency: 'USD', source: stripe_helper.generate_card_token)
451
+
452
+ charge = Stripe::Charge.retrieve(original.id)
85
453
 
86
- refund_1 = charge.refunds.create(amount: 300)
87
- expect(refund_1.amount).to eq 300
88
- expect(refund_1.charge).to eq charge.id
454
+ refund_1 = charge.refunds.create(amount: 300)
455
+ expect(refund_1.amount).to eq 300
456
+ expect(refund_1.charge).to eq charge.id
89
457
 
90
- refund_2 = charge.refunds.create(amount: 400)
91
- expect(refund_2.amount).to eq 400
92
- expect(refund_2.charge).to eq charge.id
458
+ refund_2 = charge.refunds.create(amount: 400)
459
+ expect(refund_2.amount).to eq 400
460
+ expect(refund_2.charge).to eq charge.id
93
461
 
94
- expect(charge.refunds.count).to eq 0
95
- expect(charge.refunds.total_count).to eq 0
96
- expect(charge.amount_refunded).to eq 0
462
+ expect(charge.refunds.count).to eq 0
463
+ expect(charge.refunds.total_count).to eq 0
464
+ expect(charge.amount_refunded).to eq 0
97
465
 
98
- charge = Stripe::Charge.retrieve(original.id)
99
- expect(charge.refunds.count).to eq 2
100
- expect(charge.refunds.total_count).to eq 2
101
- expect(charge.amount_refunded).to eq 700
466
+ charge = Stripe::Charge.retrieve(original.id)
467
+ expect(charge.refunds.count).to eq 2
468
+ expect(charge.refunds.total_count).to eq 2
469
+ expect(charge.amount_refunded).to eq 700
470
+ end
102
471
  end
103
472
  end