epopia-stripe-ruby-mock 2.5.8

Sign up to get free protection for your applications and to get access to all the features.
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,69 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Invoice Item API' do
4
+
5
+ context "creating a new invoice item" do
6
+ it "creates a stripe invoice item" do
7
+ invoice_item = Stripe::InvoiceItem.create({
8
+ amount: 1099,
9
+ customer: 1234,
10
+ currency: 'USD',
11
+ description: "invoice item desc"
12
+ }, 'abcde')
13
+
14
+ expect(invoice_item.id).to match(/^test_ii/)
15
+ expect(invoice_item.amount).to eq(1099)
16
+ expect(invoice_item.description).to eq('invoice item desc')
17
+ end
18
+
19
+ it "stores a created stripe invoice item in memory" do
20
+ invoice_item = Stripe::InvoiceItem.create
21
+ data = test_data_source(:invoice_items)
22
+ expect(data[invoice_item.id]).to_not be_nil
23
+ expect(data[invoice_item.id][:id]).to eq(invoice_item.id)
24
+ end
25
+ end
26
+
27
+ context "retrieving an invoice item" do
28
+ it "retrieves a stripe invoice item" do
29
+ original = Stripe::InvoiceItem.create
30
+ invoice_item = Stripe::InvoiceItem.retrieve(original.id)
31
+ expect(invoice_item.id).to eq(original.id)
32
+ end
33
+ end
34
+
35
+ context "retrieving a list of invoice items" do
36
+ before do
37
+ Stripe::InvoiceItem.create({ amount: 1075 })
38
+ Stripe::InvoiceItem.create({ amount: 1540 })
39
+ end
40
+
41
+ it "retrieves all invoice items" do
42
+ all = Stripe::InvoiceItem.all
43
+ expect(all.count).to eq(2)
44
+ expect(all.map &:amount).to include(1075, 1540)
45
+ end
46
+ end
47
+
48
+ it "updates a stripe invoice_item" do
49
+ original = Stripe::InvoiceItem.create(id: 'test_invoice_item_update')
50
+ amount = original.amount
51
+
52
+ original.description = 'new desc'
53
+ original.save
54
+
55
+ expect(original.amount).to eq(amount)
56
+ expect(original.description).to eq('new desc')
57
+
58
+ invoice_item = Stripe::InvoiceItem.retrieve("test_invoice_item_update")
59
+ expect(invoice_item.amount).to eq(original.amount)
60
+ expect(invoice_item.description).to eq('new desc')
61
+ end
62
+
63
+ it "deletes a invoice_item" do
64
+ invoice_item = Stripe::InvoiceItem.create(id: 'test_invoice_item_sub')
65
+ invoice_item = invoice_item.delete
66
+ expect(invoice_item.deleted).to eq true
67
+ end
68
+
69
+ end
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'PaymentIntent API' do
4
+
5
+ it "creates a succeeded stripe payment_intent" do
6
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
7
+
8
+ expect(payment_intent.id).to match(/^test_pi/)
9
+ expect(payment_intent.amount).to eq(100)
10
+ expect(payment_intent.currency).to eq('usd')
11
+ expect(payment_intent.metadata.to_hash).to eq({})
12
+ expect(payment_intent.status).to eq('succeeded')
13
+ end
14
+
15
+ it "creates a requires_action stripe payment_intent when amount matches 3184" do
16
+ payment_intent = Stripe::PaymentIntent.create(amount: 3184, currency: "usd")
17
+
18
+ expect(payment_intent.id).to match(/^test_pi/)
19
+ expect(payment_intent.amount).to eq(3184)
20
+ expect(payment_intent.currency).to eq('usd')
21
+ expect(payment_intent.metadata.to_hash).to eq({})
22
+ expect(payment_intent.status).to eq('requires_action')
23
+ end
24
+
25
+ it "creates a requires_payment_method stripe payment_intent when amount matches 3178" do
26
+ payment_intent = Stripe::PaymentIntent.create(amount: 3178, currency: "usd")
27
+
28
+ expect(payment_intent.id).to match(/^test_pi/)
29
+ expect(payment_intent.amount).to eq(3178)
30
+ expect(payment_intent.currency).to eq('usd')
31
+ expect(payment_intent.metadata.to_hash).to eq({})
32
+ expect(payment_intent.status).to eq('requires_payment_method')
33
+ expect(payment_intent.last_payment_error.code).to eq('card_declined')
34
+ expect(payment_intent.last_payment_error.decline_code).to eq('insufficient_funds')
35
+ expect(payment_intent.last_payment_error.message).to eq('Not enough funds.')
36
+ end
37
+
38
+ it "creates a requires_capture stripe payment_intent when amount matches 3169" do
39
+ payment_intent = Stripe::PaymentIntent.create(amount: 3169, currency: "usd")
40
+
41
+ expect(payment_intent.id).to match(/^test_pi/)
42
+ expect(payment_intent.amount).to eq(3169)
43
+ expect(payment_intent.currency).to eq('usd')
44
+ expect(payment_intent.metadata.to_hash).to eq({})
45
+ expect(payment_intent.status).to eq('requires_capture')
46
+ end
47
+
48
+ describe "listing payment_intent" do
49
+ before do
50
+ 3.times do
51
+ Stripe::PaymentIntent.create(amount: 100, currency: "usd")
52
+ end
53
+ end
54
+
55
+ it "without params retrieves all stripe payment_intent" do
56
+ expect(Stripe::PaymentIntent.all.count).to eq(3)
57
+ end
58
+
59
+ it "accepts a limit param" do
60
+ expect(Stripe::PaymentIntent.all(limit: 2).count).to eq(2)
61
+ end
62
+ end
63
+
64
+ it "retrieves a stripe payment_intent" do
65
+ original = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
66
+ payment_intent = Stripe::PaymentIntent.retrieve(original.id)
67
+
68
+ expect(payment_intent.id).to eq(original.id)
69
+ expect(payment_intent.amount).to eq(original.amount)
70
+ expect(payment_intent.currency).to eq(original.currency)
71
+ expect(payment_intent.metadata.to_hash).to eq(original.metadata.to_hash)
72
+ end
73
+
74
+ it "cannot retrieve a payment_intent that doesn't exist" do
75
+ expect { Stripe::PaymentIntent.retrieve('nope') }.to raise_error {|e|
76
+ expect(e).to be_a Stripe::InvalidRequestError
77
+ expect(e.param).to eq('payment_intent')
78
+ expect(e.http_status).to eq(404)
79
+ }
80
+ end
81
+
82
+ it "confirms a stripe payment_intent" do
83
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
84
+ confirmed_payment_intent = payment_intent.confirm()
85
+ expect(confirmed_payment_intent.status).to eq("succeeded")
86
+ end
87
+
88
+ it "captures a stripe payment_intent" do
89
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
90
+ confirmed_payment_intent = payment_intent.capture()
91
+ expect(confirmed_payment_intent.status).to eq("succeeded")
92
+ expect(confirmed_payment_intent.charges).not_to be_nil
93
+ end
94
+
95
+ it "cancels a stripe payment_intent" do
96
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
97
+ confirmed_payment_intent = payment_intent.cancel()
98
+ expect(confirmed_payment_intent.status).to eq("canceled")
99
+ end
100
+
101
+ it "updates a stripe payment_intent" do
102
+ original = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
103
+ payment_intent = Stripe::PaymentIntent.retrieve(original.id)
104
+
105
+ payment_intent.amount = 200
106
+ payment_intent.save
107
+
108
+ updated = Stripe::PaymentIntent.retrieve(original.id)
109
+
110
+ expect(updated.amount).to eq(200)
111
+ end
112
+
113
+ it 'when amount is not integer', live: true do
114
+ expect { Stripe::PaymentIntent.create(amount: 400.2,
115
+ currency: 'usd') }.to raise_error { |e|
116
+ expect(e).to be_a Stripe::InvalidRequestError
117
+ expect(e.param).to eq('amount')
118
+ expect(e.http_status).to eq(400)
119
+ }
120
+ end
121
+
122
+ it 'when amount is negative', live: true do
123
+ expect { Stripe::PaymentIntent.create(amount: -400,
124
+ currency: 'usd') }.to raise_error { |e|
125
+ expect(e).to be_a Stripe::InvalidRequestError
126
+ expect(e.param).to eq('amount')
127
+ expect(e.message).to match(/^Invalid.*integer/)
128
+ expect(e.http_status).to eq(400)
129
+ }
130
+ end
131
+ end
@@ -0,0 +1,175 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'PaymentMethod API' do
4
+ let(:billing_details) do
5
+ {
6
+ address: {
7
+ city: 'North New Portland',
8
+ country: 'US',
9
+ line1: '2631 Bloomfield Way',
10
+ line2: 'Apartment 5B',
11
+ postal_code: '05555',
12
+ state: 'ME'
13
+ },
14
+ email: 'john@example.com',
15
+ name: 'John Doe',
16
+ phone: '555-555-5555'
17
+ }
18
+ end
19
+ let(:card_details) do
20
+ {
21
+ number: 4242_4242_4242_4242,
22
+ exp_month: 9,
23
+ exp_year: (Time.now.year + 5),
24
+ cvc: 999
25
+ }
26
+ end
27
+
28
+ # post /v1/payment_methods
29
+ describe 'Create a PaymentMethod', live: true do
30
+ let(:payment_method) do
31
+ Stripe::PaymentMethod.create(
32
+ type: type,
33
+ billing_details: billing_details,
34
+ card: card_details,
35
+ metadata: {
36
+ order_id: '123456789'
37
+ }
38
+ )
39
+ end
40
+ let(:type) { 'card' }
41
+
42
+ it 'creates a payment method with a valid id', live: false do
43
+ expect(payment_method.id).to match(/^test_pm/)
44
+ end
45
+
46
+ it 'creates a payment method with a billing address' do
47
+ expect(payment_method.billing_details.address.city).to eq('North New Portland')
48
+ expect(payment_method.billing_details.address.country).to eq('US')
49
+ expect(payment_method.billing_details.address.line1).to eq('2631 Bloomfield Way')
50
+ expect(payment_method.billing_details.address.line2).to eq('Apartment 5B')
51
+ expect(payment_method.billing_details.address.postal_code).to eq('05555')
52
+ expect(payment_method.billing_details.address.state).to eq('ME')
53
+ expect(payment_method.billing_details.email).to eq('john@example.com')
54
+ expect(payment_method.billing_details.name).to eq('John Doe')
55
+ expect(payment_method.billing_details.phone).to eq('555-555-5555')
56
+ end
57
+
58
+ it 'creates a payment method with metadata' do
59
+ expect(payment_method.metadata.order_id).to eq('123456789')
60
+ end
61
+
62
+ context 'when type is invalid' do
63
+ let(:type) { 'bank_account' }
64
+
65
+ it 'raises invalid requestion exception' do
66
+ expect { payment_method }.to raise_error(Stripe::InvalidRequestError)
67
+ end
68
+ end
69
+ end
70
+
71
+ # get /v1/payment_methods/:id
72
+ describe 'Retrieve a PaymentMethod', live: true do
73
+ it 'retrieves a given payment method' do
74
+ customer = Stripe::Customer.create
75
+ original = Stripe::PaymentMethod.create(type: 'card', card: card_details)
76
+ Stripe::PaymentMethod.attach(original.id, customer: customer.id)
77
+
78
+ payment_method = Stripe::PaymentMethod.retrieve(original.id)
79
+
80
+ expect(payment_method.id).to eq(original.id)
81
+ expect(payment_method.type).to eq(original.type)
82
+ expect(payment_method.customer).to eq(customer.id)
83
+ end
84
+ end
85
+
86
+ # get /v1/payment_methods
87
+ describe "List a Customer's PaymentMethods", live: true do
88
+ let(:customer) { Stripe::Customer.create }
89
+ let(:customer2) { Stripe::Customer.create }
90
+ before do
91
+ 3.times do
92
+ payment_method = Stripe::PaymentMethod.create(type: 'card', card: card_details)
93
+ Stripe::PaymentMethod.attach(payment_method.id, customer: customer.id)
94
+ end
95
+ end
96
+
97
+ it 'lists all payment methods' do
98
+ expect(Stripe::PaymentMethod.list(customer: customer.id, type: 'card').count).to eq(3)
99
+ end
100
+
101
+ context 'when passing a limit' do
102
+ it 'only lists the limited number of payment methods' do
103
+ expect(Stripe::PaymentMethod.list(customer: customer.id, type: 'card', limit: 2).count).to eq(2)
104
+ end
105
+ end
106
+
107
+ context 'when listing the payment methods of another customer' do
108
+ it 'does not list any payment methods' do
109
+ expect(Stripe::PaymentMethod.list(customer: customer2.id, type: 'card').count).to eq(0)
110
+ end
111
+ end
112
+ end
113
+
114
+ # post /v1/payment_methods/:id/attach
115
+ describe 'Attach a PaymentMethod to a Customer', live: true do
116
+ let(:customer) { Stripe::Customer.create }
117
+ let(:payment_method) { Stripe::PaymentMethod.create(type: 'card', card: card_details) }
118
+
119
+ it 'attaches a payment method to a customer' do
120
+ expect { Stripe::PaymentMethod.attach(payment_method.id, customer: customer.id) }
121
+ .to change { Stripe::PaymentMethod.retrieve(payment_method.id).customer }
122
+ .from(nil).to(customer.id)
123
+ end
124
+
125
+ context "when the customer doesn't exist" do
126
+ it 'raises invalid requestion exception' do
127
+ expect { Stripe::PaymentMethod.attach(payment_method.id, customer: 'cus_invalid') }
128
+ .to raise_error(Stripe::InvalidRequestError)
129
+ end
130
+ end
131
+ end
132
+
133
+ # post /v1/payment_methods/:id/detach
134
+ describe 'Detach a PaymentMethod from a Customer', live: true do
135
+ let(:customer) { Stripe::Customer.create }
136
+ let(:payment_method) do
137
+ payment_method = Stripe::PaymentMethod.create(type: 'card', card: card_details)
138
+ Stripe::PaymentMethod.attach(payment_method.id, customer: customer.id)
139
+ end
140
+
141
+ it 'detaches a PaymentMethod from a customer' do
142
+ expect { Stripe::PaymentMethod.detach(payment_method.id) }
143
+ .to change { Stripe::PaymentMethod.retrieve(payment_method.id).customer }
144
+ .from(customer.id).to(nil)
145
+ end
146
+ end
147
+
148
+ # post /v1/payment_methods/:id
149
+ describe 'Update a PaymentMethod', live: true do
150
+ let(:customer) { Stripe::Customer.create }
151
+ let(:payment_method) do
152
+ Stripe::PaymentMethod.create(type: 'card', card: card_details)
153
+ end
154
+
155
+ it 'updates the card for the payment method' do
156
+ Stripe::PaymentMethod.attach(payment_method.id, customer: customer.id)
157
+
158
+ original_card_exp_month = payment_method.card.exp_month
159
+ new_card_exp_month = 12
160
+
161
+ expect do
162
+ Stripe::PaymentMethod.update(payment_method.id, card: { exp_month: new_card_exp_month })
163
+ end.to change { Stripe::PaymentMethod.retrieve(payment_method.id).card.exp_month }
164
+ .from(original_card_exp_month).to(new_card_exp_month)
165
+ end
166
+
167
+ context 'without a customer' do
168
+ it 'raises invalid requestion exception' do
169
+ expect do
170
+ Stripe::PaymentMethod.update(payment_method.id, card: { exp_month: 12 })
171
+ end.to raise_error(Stripe::InvalidRequestError)
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Payout API' do
4
+
5
+ it "creates a stripe payout" do
6
+ payout = Stripe::Payout.create(amount: "100", currency: "usd")
7
+
8
+ expect(payout.id).to match(/^test_po/)
9
+ expect(payout.amount).to eq('100')
10
+ expect(payout.currency).to eq('usd')
11
+ expect(payout.metadata.to_hash).to eq({})
12
+ end
13
+
14
+ describe "listing payouts" do
15
+ before do
16
+ 3.times do
17
+ Stripe::Payout.create(amount: "100", currency: "usd")
18
+ end
19
+ end
20
+
21
+ it "without params retrieves all tripe payouts" do
22
+ expect(Stripe::Payout.all.count).to eq(3)
23
+ end
24
+
25
+ it "accepts a limit param" do
26
+ expect(Stripe::Payout.all(limit: 2).count).to eq(2)
27
+ end
28
+ end
29
+
30
+ it "retrieves a stripe payout" do
31
+ original = Stripe::Payout.create(amount: "100", currency: "usd")
32
+ payout = Stripe::Payout.retrieve(original.id)
33
+
34
+ expect(payout.id).to eq(original.id)
35
+ expect(payout.amount).to eq(original.amount)
36
+ expect(payout.currency).to eq(original.currency)
37
+ expect(payout.metadata.to_hash).to eq(original.metadata.to_hash)
38
+ end
39
+
40
+ it "cannot retrieve a payout that doesn't exist" do
41
+ expect { Stripe::Payout.retrieve('nope') }.to raise_error {|e|
42
+ expect(e).to be_a Stripe::InvalidRequestError
43
+ expect(e.param).to eq('payout')
44
+ expect(e.http_status).to eq(404)
45
+ }
46
+ end
47
+
48
+ it 'when amount is not integer', live: true do
49
+ expect { Stripe::Payout.create(amount: '400.2',
50
+ currency: 'usd',
51
+ description: 'Payout for test@example.com') }.to raise_error { |e|
52
+ expect(e).to be_a Stripe::InvalidRequestError
53
+ expect(e.param).to eq('amount')
54
+ expect(e.http_status).to eq(400)
55
+ }
56
+ end
57
+
58
+ it 'when amount is negative', live: true do
59
+ expect { Stripe::Payout.create(amount: '-400',
60
+ currency: 'usd',
61
+ description: 'Payout for test@example.com') }.to raise_error { |e|
62
+ expect(e).to be_a Stripe::InvalidRequestError
63
+ expect(e.param).to eq('amount')
64
+ expect(e.message).to match(/^Invalid.*integer/)
65
+ expect(e.http_status).to eq(400)
66
+ }
67
+ end
68
+ end
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Plan API' do
4
+
5
+ it "creates a stripe plan" do
6
+ plan = Stripe::Plan.create(
7
+ :id => 'pid_1',
8
+ :name => 'The Mock Plan',
9
+ :amount => 9900,
10
+ :currency => 'USD',
11
+ :interval => 1,
12
+ :product => {
13
+ :name => 'A product'
14
+ },
15
+ :metadata => {
16
+ :description => "desc text",
17
+ :info => "info text"
18
+ },
19
+ :trial_period_days => 30
20
+ )
21
+
22
+ expect(plan.id).to eq('pid_1')
23
+ expect(plan.name).to eq('The Mock Plan')
24
+ expect(plan.amount).to eq(9900)
25
+
26
+ expect(plan.currency).to eq('USD')
27
+ expect(plan.interval).to eq(1)
28
+
29
+ expect(plan.metadata.description).to eq('desc text')
30
+ expect(plan.metadata.info).to eq('info text')
31
+
32
+ expect(plan.trial_period_days).to eq(30)
33
+ end
34
+
35
+
36
+ it "creates a stripe plan without specifying ID" do
37
+ plan = Stripe::Plan.create(
38
+ :name => 'The Mock Plan',
39
+ :amount => 9900,
40
+ :currency => 'USD',
41
+ :interval => 1,
42
+ :product => {
43
+ :name => 'A product'
44
+ }
45
+ )
46
+
47
+ expect(plan.id).to match(/^test_plan/)
48
+ end
49
+
50
+ it "stores a created stripe plan in memory" do
51
+ plan = Stripe::Plan.create(
52
+ :id => 'pid_2',
53
+ :name => 'The Memory Plan',
54
+ :amount => 1100,
55
+ :currency => 'USD',
56
+ :interval => 1,
57
+ :product => {
58
+ :name => 'A product'
59
+ }
60
+ )
61
+ plan2 = Stripe::Plan.create(
62
+ :id => 'pid_3',
63
+ :name => 'The Bonk Plan',
64
+ :amount => 7777,
65
+ :currency => 'USD',
66
+ :interval => 1,
67
+ :product => {
68
+ :name => 'A product'
69
+ }
70
+ )
71
+ data = test_data_source(:plans)
72
+ expect(data[plan.id]).to_not be_nil
73
+ expect(data[plan.id][:amount]).to eq(1100)
74
+
75
+ expect(data[plan2.id]).to_not be_nil
76
+ expect(data[plan2.id][:amount]).to eq(7777)
77
+ end
78
+
79
+
80
+ it "retrieves a stripe plan" do
81
+ original = stripe_helper.create_plan(amount: 1331)
82
+ plan = Stripe::Plan.retrieve(original.id)
83
+
84
+ expect(plan.id).to eq(original.id)
85
+ expect(plan.amount).to eq(original.amount)
86
+ end
87
+
88
+
89
+ it "updates a stripe plan" do
90
+ stripe_helper.create_plan(id: 'super_member', amount: 111)
91
+
92
+ plan = Stripe::Plan.retrieve('super_member')
93
+ expect(plan.amount).to eq(111)
94
+
95
+ plan.amount = 789
96
+ plan.save
97
+ plan = Stripe::Plan.retrieve('super_member')
98
+ expect(plan.amount).to eq(789)
99
+ end
100
+
101
+
102
+ it "cannot retrieve a stripe plan that doesn't exist" do
103
+ expect { Stripe::Plan.retrieve('nope') }.to raise_error {|e|
104
+ expect(e).to be_a Stripe::InvalidRequestError
105
+ expect(e.param).to eq('plan')
106
+ expect(e.http_status).to eq(404)
107
+ }
108
+ end
109
+
110
+ it "deletes a stripe plan" do
111
+ stripe_helper.create_plan(id: 'super_member', amount: 111)
112
+
113
+ plan = Stripe::Plan.retrieve('super_member')
114
+ expect(plan).to_not be_nil
115
+
116
+ plan.delete
117
+
118
+ expect { Stripe::Plan.retrieve('super_member') }.to raise_error {|e|
119
+ expect(e).to be_a Stripe::InvalidRequestError
120
+ expect(e.param).to eq('plan')
121
+ expect(e.http_status).to eq(404)
122
+ }
123
+ end
124
+
125
+ it "retrieves all plans" do
126
+ stripe_helper.create_plan(id: 'Plan One', amount: 54321)
127
+ stripe_helper.create_plan(id: 'Plan Two', amount: 98765)
128
+
129
+ all = Stripe::Plan.all
130
+ expect(all.count).to eq(2)
131
+ expect(all.map &:id).to include('Plan One', 'Plan Two')
132
+ expect(all.map &:amount).to include(54321, 98765)
133
+ end
134
+
135
+ it 'retrieves plans with limit' do
136
+ 101.times do | i|
137
+ stripe_helper.create_plan(id: "Plan #{i}", amount: 11)
138
+ end
139
+ all = Stripe::Plan.all(limit: 100)
140
+
141
+ expect(all.count).to eq(100)
142
+ end
143
+
144
+ it 'validates the amount' do
145
+ expect {
146
+ Stripe::Plan.create(
147
+ :id => 'pid_1',
148
+ :name => 'The Mock Plan',
149
+ :amount => 99.99,
150
+ :currency => 'USD',
151
+ :interval => 'month',
152
+ :product => {
153
+ :name => 'A product'
154
+ }
155
+ )
156
+ }.to raise_error(Stripe::InvalidRequestError, "Invalid integer: 99.99")
157
+ end
158
+
159
+ describe "Validation", :live => true do
160
+ let(:params) { stripe_helper.create_plan_params }
161
+ let(:subject) { Stripe::Plan.create(params) }
162
+
163
+ describe "Required Parameters" do
164
+ after do
165
+ params.delete(@name)
166
+ message =
167
+ if @name == :amount
168
+ "Plans require an `#{@name}` parameter to be set."
169
+ else
170
+ "Missing required param: #{@name}."
171
+ end
172
+ expect { subject }.to raise_error(Stripe::InvalidRequestError, message)
173
+ end
174
+
175
+ it("requires a product") { @name = :product }
176
+ it("requires an amount") { @name = :amount }
177
+ it("requires a currency") { @name = :currency }
178
+ it("requires an interval") { @name = :interval }
179
+ end
180
+
181
+ describe "Uniqueness" do
182
+
183
+ it "validates for uniqueness" do
184
+ stripe_helper.delete_plan(params[:id])
185
+
186
+ Stripe::Plan.create(params)
187
+ expect {
188
+ Stripe::Plan.create(params)
189
+ }.to raise_error(Stripe::InvalidRequestError, "Plan already exists.")
190
+ end
191
+ end
192
+ end
193
+
194
+ end