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,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
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Recipient API' do
4
+
5
+ it "creates a stripe recipient with a default bank and card", skip: 'Stripe has deprecated Recipients' do
6
+ recipient = Stripe::Recipient.create({
7
+ type: "corporation",
8
+ name: "MyCo",
9
+ email: "owner@myco.com",
10
+ bank_account: 'void_bank_token',
11
+ card: stripe_helper.generate_card_token
12
+ })
13
+ expect(recipient.id).to match /^test_rp/
14
+ expect(recipient.type).to eq('corporation')
15
+ expect(recipient.name).to eq('MyCo')
16
+ expect(recipient.email).to eq('owner@myco.com')
17
+ expect(recipient.default_card).to_not be_nil
18
+
19
+ expect(recipient.active_account).to_not be_nil
20
+ expect(recipient.active_account.bank_name).to_not be_nil
21
+ expect(recipient.active_account.last4).to_not be_nil
22
+
23
+ expect(recipient.cards.count).to eq(1)
24
+ expect(recipient.cards.data.length).to eq(1)
25
+ expect(recipient.default_card).to_not be_nil
26
+ expect(recipient.default_card).to eq recipient.cards.data.first.id
27
+
28
+ expect { recipient.card }.to raise_error
29
+ end
30
+
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
39
+ recipient = Stripe::Recipient.create({
40
+ type: "corporation",
41
+ name: "MyCo",
42
+ email: "cardless@appleseed.com"
43
+ })
44
+ expect(recipient.id).to match(/^test_rp/)
45
+ expect(recipient.type).to eq('corporation')
46
+ expect(recipient.name).to eq('MyCo')
47
+ expect(recipient.email).to eq('cardless@appleseed.com')
48
+
49
+ expect(recipient.cards.count).to eq(0)
50
+ expect(recipient.cards.data.length).to eq(0)
51
+ expect(recipient.default_card).to be_nil
52
+ end
53
+
54
+ it "stores a created stripe recipient in memory", skip: 'Stripe has deprecated Recipients' do
55
+ recipient = Stripe::Recipient.create({
56
+ type: "individual",
57
+ name: "Customer One",
58
+ bank_account: 'bank_account_token_1',
59
+ card: stripe_helper.generate_card_token
60
+ })
61
+ recipient2 = Stripe::Recipient.create({
62
+ type: "individual",
63
+ name: "Customer Two",
64
+ bank_account: 'bank_account_token_1',
65
+ card: stripe_helper.generate_card_token
66
+ })
67
+ data = test_data_source(:recipients)
68
+ expect(data[recipient.id]).to_not be_nil
69
+ expect(data[recipient.id][:type]).to eq("individual")
70
+ expect(data[recipient.id][:name]).to eq("Customer One")
71
+ expect(data[recipient.id][:default_card]).to_not be_nil
72
+
73
+ expect(data[recipient2.id]).to_not be_nil
74
+ expect(data[recipient2.id][:type]).to eq("individual")
75
+ expect(data[recipient2.id][:name]).to eq("Customer Two")
76
+ expect(data[recipient2.id][:default_card]).to_not be_nil
77
+ end
78
+
79
+ it "retrieves a stripe recipient", skip: 'Stripe has deprecated Recipients' do
80
+ original = Stripe::Recipient.create({
81
+ type: "individual",
82
+ name: "Bob",
83
+ email: "bob@example.com",
84
+ card: stripe_helper.generate_card_token
85
+ })
86
+ recipient = Stripe::Recipient.retrieve(original.id)
87
+
88
+ expect(recipient.id).to eq(original.id)
89
+ expect(recipient.type).to eq(original.type)
90
+ expect(recipient.name).to eq(original.name)
91
+ expect(recipient.email).to eq(original.email)
92
+ expect(recipient.default_card).to_not be_nil
93
+ end
94
+
95
+ it "cannot retrieve a recipient that doesn't exist", skip: 'Stripe has deprecated Recipients' do
96
+ expect { Stripe::Recipient.retrieve('nope') }.to raise_error {|e|
97
+ expect(e).to be_a Stripe::InvalidRequestError
98
+ expect(e.param).to eq('recipient')
99
+ expect(e.http_status).to eq(404)
100
+ }
101
+ end
102
+
103
+ describe "Errors", :live => true, skip: 'Stripe has deprecated Recipients' do
104
+ it "throws an error when the customer does not have the retrieving card id" do
105
+ recipient = Stripe::Recipient.create(:name => "Bob Bobber", :type => "individual")
106
+ card_id = "card_123"
107
+ expect { recipient.cards.retrieve(card_id) }.to raise_error {|e|
108
+ expect(e).to be_a Stripe::InvalidRequestError
109
+ expect(e.message).to include "Recipient", recipient.id, "does not have", card_id
110
+ expect(e.param).to eq 'card'
111
+ expect(e.http_status).to eq 404
112
+ }
113
+ end
114
+
115
+ it "throws an error when the name does not have both first and last"
116
+ end
117
+
118
+ end
@@ -0,0 +1,472 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Refund API' do
4
+
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
+ )
13
+
14
+ Stripe::Refund.create(
15
+ charge: charge.id
16
+ )
17
+
18
+ charge = Stripe::Charge.retrieve(charge.id)
19
+
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)
225
+
226
+ refund.metadata[:order_id] = 6735
227
+ refund.save
228
+
229
+ updated = Stripe::Refund.retrieve(original.id)
230
+
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
368
+ end
369
+
370
+
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)
453
+
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
457
+
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
461
+
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
465
+
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
471
+ end
472
+ end