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,18 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Country Spec API' do
4
+ context 'retrieve country', live: true do
5
+ it 'retrieves a stripe country spec' do
6
+ country = Stripe::CountrySpec.retrieve('US')
7
+
8
+ expect(country).to be_a Stripe::CountrySpec
9
+ expect(country.id).to match /US/
10
+ end
11
+
12
+ it "cannot retrieve a stripe country that doesn't exist" do
13
+ expect { Stripe::CountrySpec.retrieve('nope') }
14
+ .to raise_error(Stripe::InvalidRequestError, /(nope is not currently supported by Stripe)|(Country 'nope' is unknown)/)
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Coupon API' do
4
+ context 'create coupon' do
5
+ let(:coupon) { stripe_helper.create_coupon }
6
+
7
+ it 'creates a stripe coupon', live: true do
8
+ expect(coupon.id).to eq('10BUCKS')
9
+ expect(coupon.amount_off).to eq(1000)
10
+
11
+ expect(coupon.currency).to eq('usd')
12
+ expect(coupon.max_redemptions).to eq(100)
13
+ expect(coupon.metadata.to_hash).to eq( { :created_by => 'admin_acct_1' } )
14
+ expect(coupon.duration).to eq('once')
15
+ end
16
+ it 'stores a created stripe coupon in memory' do
17
+ coupon
18
+
19
+ data = test_data_source(:coupons)
20
+
21
+ expect(data[coupon.id]).to_not be_nil
22
+ expect(data[coupon.id][:amount_off]).to eq(1000)
23
+ end
24
+ it 'fails when a coupon is created without a duration' do
25
+ expect { Stripe::Coupon.create(id: '10PERCENT') }.to raise_error {|e|
26
+ expect(e).to be_a(Stripe::InvalidRequestError)
27
+ expect(e.message).to match /duration/
28
+ }
29
+ end
30
+ it 'fails when a coupon is created without a currency when amount_off is specified' do
31
+ expect { Stripe::Coupon.create(id: '10OFF', duration: 'once', amount_off: 1000) }.to raise_error {|e|
32
+ expect(e).to be_a(Stripe::InvalidRequestError)
33
+ expect(e.message).to match /You must pass currency when passing amount_off/
34
+ }
35
+ end
36
+ end
37
+
38
+ context 'retrieve coupon', live: true do
39
+ let(:coupon1) { stripe_helper.create_coupon }
40
+ let(:coupon2) { stripe_helper.create_coupon(id: '11BUCKS', amount_off: 3000) }
41
+
42
+ it 'retrieves a stripe coupon' do
43
+ coupon1
44
+
45
+ coupon = Stripe::Coupon.retrieve(coupon1.id)
46
+
47
+ expect(coupon.id).to eq(coupon1.id)
48
+ expect(coupon.amount_off).to eq(coupon1.amount_off)
49
+ end
50
+ it 'retrieves all coupons' do
51
+ stripe_helper.delete_all_coupons
52
+
53
+ coupon1
54
+ coupon2
55
+
56
+ all = Stripe::Coupon.all
57
+
58
+ expect(all.count).to eq(2)
59
+ expect(all.map &:id).to include('10BUCKS', '11BUCKS')
60
+ expect(all.map &:amount_off).to include(1000, 3000)
61
+ end
62
+ it "cannot retrieve a stripe coupon that doesn't exist" do
63
+ expect { Stripe::Coupon.retrieve('nope') }.to raise_error {|e|
64
+ expect(e).to be_a Stripe::InvalidRequestError
65
+ expect(e.param).to eq('coupon')
66
+ expect(e.http_status).to eq(404)
67
+ }
68
+ end
69
+ end
70
+
71
+ context 'Delete coupon', live: true do
72
+ it 'deletes a stripe coupon' do
73
+ original = stripe_helper.create_coupon
74
+ coupon = Stripe::Coupon.retrieve(original.id)
75
+
76
+ coupon.delete
77
+
78
+ expect { Stripe::Coupon.retrieve(coupon.id) }.to raise_error {|e|
79
+ expect(e).to be_a Stripe::InvalidRequestError
80
+ expect(e.param).to eq('coupon')
81
+ expect(e.http_status).to eq(404)
82
+ }
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,453 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Customer API' do
4
+
5
+ def gen_card_tk
6
+ stripe_helper.generate_card_token
7
+ end
8
+
9
+ it "creates a stripe customer with a default card" do
10
+ customer = Stripe::Customer.create({
11
+ email: 'johnny@appleseed.com',
12
+ source: gen_card_tk,
13
+ description: "a description"
14
+ })
15
+ expect(customer.id).to match(/^test_cus/)
16
+ expect(customer.email).to eq('johnny@appleseed.com')
17
+ expect(customer.description).to eq('a description')
18
+
19
+ expect(customer.sources.count).to eq(1)
20
+ expect(customer.sources.data.length).to eq(1)
21
+ expect(customer.default_source).to_not be_nil
22
+ expect(customer.default_source).to eq customer.sources.data.first.id
23
+
24
+ expect { customer.source }.to raise_error
25
+ end
26
+
27
+ it "creates a stripe customer with multiple cards and updates the default card" do
28
+ card_a = gen_card_tk
29
+ card_b = gen_card_tk
30
+ customer = Stripe::Customer.create({
31
+ email: 'johnny.multiple@appleseed.com',
32
+ source: card_a,
33
+ description: "a description"
34
+ })
35
+
36
+ original_card = customer.sources.data.first.id
37
+
38
+ customer.sources.create(source: card_b)
39
+ retrieved_customer = Stripe::Customer.retrieve(customer.id)
40
+
41
+ expect(retrieved_customer.sources.data.length).to eq(2)
42
+ retrieved_customer.default_source = retrieved_customer.sources.data.last.id
43
+ retrieved_customer.save
44
+ expect(Stripe::Customer.retrieve(customer.id).default_source).to eq(retrieved_customer.sources.data.last.id)
45
+ expect(Stripe::Customer.retrieve(customer.id).default_source).to_not eq(original_card)
46
+ end
47
+
48
+ it "creates a stripe customer without a card" do
49
+ customer = Stripe::Customer.create({
50
+ email: 'cardless@appleseed.com',
51
+ description: "no card"
52
+ })
53
+ expect(customer.id).to match(/^test_cus/)
54
+ expect(customer.email).to eq('cardless@appleseed.com')
55
+ expect(customer.description).to eq('no card')
56
+
57
+ expect(customer.sources.count).to eq(0)
58
+ expect(customer.sources.data.length).to eq(0)
59
+ expect(customer.default_source).to be_nil
60
+ end
61
+
62
+ it 'creates a stripe customer with a dictionary of card values', live: true do
63
+ customer = Stripe::Customer.create(source: {
64
+ object: 'card',
65
+ number: '4242424242424242',
66
+ exp_month: 12,
67
+ exp_year: 2024,
68
+ cvc: 123
69
+ },
70
+ email: 'blah@blah.com')
71
+
72
+ expect(customer).to be_a Stripe::Customer
73
+ expect(customer.id).to match(/cus_/)
74
+ expect(customer.email).to eq 'blah@blah.com'
75
+ expect(customer.sources.data.first.object).to eq 'card'
76
+ expect(customer.sources.data.first.last4).to eq '4242'
77
+ expect(customer.sources.data.first.exp_month).to eq 12
78
+ expect(customer.sources.data.first.exp_year).to eq 2024
79
+ end
80
+
81
+ it 'creates a customer with name' do
82
+ customer = Stripe::Customer.create(
83
+ source: gen_card_tk,
84
+ name: 'John Appleseed'
85
+ )
86
+ expect(customer.id).to match(/^test_cus/)
87
+ expect(customer.name).to eq('John Appleseed')
88
+ end
89
+
90
+ it 'creates a customer with a plan' do
91
+ plan = stripe_helper.create_plan(id: 'silver')
92
+ customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'silver')
93
+
94
+ customer = Stripe::Customer.retrieve('test_cus_plan')
95
+ expect(customer.subscriptions.count).to eq(1)
96
+ expect(customer.subscriptions.data.length).to eq(1)
97
+
98
+ expect(customer.subscriptions).to_not be_nil
99
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
100
+ expect(customer.subscriptions.first.customer).to eq(customer.id)
101
+ end
102
+
103
+ it "creates a customer with a plan (string/symbol agnostic)" do
104
+ plan = stripe_helper.create_plan(id: 'string_id')
105
+ customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => :string_id)
106
+
107
+ customer = Stripe::Customer.retrieve('test_cus_plan')
108
+ expect(customer.subscriptions.first.plan.id).to eq('string_id')
109
+
110
+ plan = stripe_helper.create_plan(:id => :sym_id)
111
+ customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'sym_id')
112
+
113
+ customer = Stripe::Customer.retrieve('test_cus_plan')
114
+ expect(customer.subscriptions.first.plan.id).to eq('sym_id')
115
+ end
116
+
117
+ context "create customer" do
118
+
119
+ it "with a trial when trial_end is set" do
120
+ plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
121
+ trial_end = Time.now.utc.to_i + 3600
122
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'no_trial', trial_end: trial_end)
123
+
124
+ customer = Stripe::Customer.retrieve('test_cus_trial_end')
125
+ expect(customer.subscriptions.count).to eq(1)
126
+ expect(customer.subscriptions.data.length).to eq(1)
127
+
128
+ expect(customer.subscriptions).to_not be_nil
129
+ expect(customer.subscriptions.first.plan.id).to eq('no_trial')
130
+ expect(customer.subscriptions.first.status).to eq('trialing')
131
+ expect(customer.subscriptions.first.current_period_end).to eq(trial_end)
132
+ expect(customer.subscriptions.first.trial_end).to eq(trial_end)
133
+ end
134
+
135
+ it 'overrides trial period length when trial_end is set' do
136
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999, trial_period_days: 14)
137
+ trial_end = Time.now.utc.to_i + 3600
138
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: trial_end)
139
+
140
+ customer = Stripe::Customer.retrieve('test_cus_trial_end')
141
+ expect(customer.subscriptions.count).to eq(1)
142
+ expect(customer.subscriptions.data.length).to eq(1)
143
+
144
+ expect(customer.subscriptions).to_not be_nil
145
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
146
+ expect(customer.subscriptions.first.current_period_end).to eq(trial_end)
147
+ expect(customer.subscriptions.first.trial_end).to eq(trial_end)
148
+ end
149
+
150
+ it 'creates a customer when trial_end is set and no source', live: true do
151
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999)
152
+ trial_end = Time.now.utc.to_i + 3600
153
+ customer = Stripe::Customer.create(plan: 'silver', trial_end: trial_end)
154
+ expect(customer.subscriptions.count).to eq(1)
155
+ expect(customer.subscriptions.data.length).to eq(1)
156
+
157
+ expect(customer.subscriptions).to_not be_nil
158
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
159
+ expect(customer.subscriptions.first.current_period_end).to eq(trial_end)
160
+ expect(customer.subscriptions.first.trial_end).to eq(trial_end)
161
+ end
162
+
163
+ it "returns no trial when trial_end is set to 'now'" do
164
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999, trial_period_days: 14)
165
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: "now")
166
+
167
+ customer = Stripe::Customer.retrieve('test_cus_trial_end')
168
+ expect(customer.subscriptions.count).to eq(1)
169
+ expect(customer.subscriptions.data.length).to eq(1)
170
+
171
+ expect(customer.subscriptions).to_not be_nil
172
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
173
+ expect(customer.subscriptions.first.status).to eq('active')
174
+ expect(customer.subscriptions.first.trial_start).to be_nil
175
+ expect(customer.subscriptions.first.trial_end).to be_nil
176
+ end
177
+
178
+ it "returns an error if trial_end is set to a past time" do
179
+ plan = stripe_helper.create_plan(id: 'silver', amount: 999)
180
+ expect {
181
+ Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: Time.now.utc.to_i - 3600)
182
+ }.to raise_error {|e|
183
+ expect(e).to be_a(Stripe::InvalidRequestError)
184
+ expect(e.message).to eq('Invalid timestamp: must be an integer Unix timestamp in the future')
185
+ }
186
+ end
187
+
188
+ it "returns an error if trial_end is set without a plan" do
189
+ expect {
190
+ Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, trial_end: "now")
191
+ }.to raise_error {|e|
192
+ expect(e).to be_a(Stripe::InvalidRequestError)
193
+ expect(e.message).to eq('Received unknown parameter: trial_end')
194
+ }
195
+ end
196
+
197
+ end
198
+
199
+ it 'cannot create a customer with a plan that does not exist' do
200
+ expect {
201
+ customer = Stripe::Customer.create(id: 'test_cus_no_plan', source: gen_card_tk, :plan => 'non-existant')
202
+ }.to raise_error {|e|
203
+ expect(e).to be_a(Stripe::InvalidRequestError)
204
+ expect(e.message).to eq('No such plan: non-existant')
205
+ }
206
+ end
207
+
208
+ it 'cannot create a customer with an existing plan, but no card token' do
209
+ plan = stripe_helper.create_plan(id: 'p')
210
+ expect {
211
+ customer = Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
212
+ }.to raise_error {|e|
213
+ expect(e).to be_a(Stripe::InvalidRequestError)
214
+ expect(e.message).to eq('You must supply a valid card')
215
+ }
216
+ end
217
+
218
+ it 'creates a customer with a coupon discount' do
219
+ coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
220
+
221
+ customer =
222
+ Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
223
+
224
+ customer = Stripe::Customer.retrieve('test_cus_coupon')
225
+ expect(customer.discount).to_not be_nil
226
+ expect(customer.discount.coupon).to_not be_nil
227
+ expect(customer.discount.customer).to eq customer.id
228
+ expect(customer.discount.start).to be_within(1).of Time.now.to_i
229
+ end
230
+
231
+ it 'creates a customer with default invoice settings', live: true do
232
+ Stripe::Customer.create(id: 'test_cus_invoice_settings')
233
+ customer = Stripe::Customer.retrieve('test_cus_invoice_settings')
234
+
235
+ expect(customer.invoice_prefix).to_not be_nil
236
+ expect(customer.invoice_settings).to_not be_nil
237
+ expect(customer.invoice_settings.custom_fields).to be_nil
238
+ expect(customer.invoice_settings.default_payment_method).to be_nil
239
+ expect(customer.invoice_settings.footer).to be_nil
240
+ end
241
+
242
+ describe 'repeating coupon with duration limit', live: true do
243
+ let!(:coupon) { stripe_helper.create_coupon(id: '10OFF', amount_off: 1000, currency: 'usd', duration: 'repeating', duration_in_months: 12) }
244
+ let!(:customer) { Stripe::Customer.create(coupon: coupon.id) }
245
+ it 'creates the discount with the end date', live: true do
246
+ discount = Stripe::Customer.retrieve(customer.id).discount
247
+ expect(discount).to_not be_nil
248
+ expect(discount.coupon).to_not be_nil
249
+ expect(discount.end).to be_within(1).of (Time.now.to_datetime >> 12).to_time.to_i
250
+ end
251
+ after { Stripe::Coupon.retrieve(coupon.id).delete }
252
+ after { Stripe::Customer.retrieve(customer.id).delete }
253
+ end
254
+
255
+ it 'cannot create a customer with a coupon that does not exist' do
256
+ expect{
257
+ customer = Stripe::Customer.create(id: 'test_cus_no_coupon', coupon: '5OFF')
258
+ }.to raise_error {|e|
259
+ expect(e).to be_a(Stripe::InvalidRequestError)
260
+ expect(e.message).to eq('No such coupon: 5OFF')
261
+ }
262
+ end
263
+
264
+ it "stores a created stripe customer in memory" do
265
+ customer = Stripe::Customer.create({
266
+ email: 'johnny@appleseed.com',
267
+ source: gen_card_tk
268
+ })
269
+ customer2 = Stripe::Customer.create({
270
+ email: 'bob@bobbers.com',
271
+ source: gen_card_tk
272
+ })
273
+ data = test_data_source(:customers)
274
+ expect(data[customer.id]).to_not be_nil
275
+ expect(data[customer.id][:email]).to eq('johnny@appleseed.com')
276
+
277
+ expect(data[customer2.id]).to_not be_nil
278
+ expect(data[customer2.id][:email]).to eq('bob@bobbers.com')
279
+ end
280
+
281
+ it "retrieves a stripe customer" do
282
+ original = Stripe::Customer.create({
283
+ email: 'johnny@appleseed.com',
284
+ source: gen_card_tk
285
+ })
286
+ customer = Stripe::Customer.retrieve(original.id)
287
+
288
+ expect(customer.id).to eq(original.id)
289
+ expect(customer.email).to eq(original.email)
290
+ expect(customer.name).to eq(nil)
291
+ expect(customer.default_source).to eq(original.default_source)
292
+ expect(customer.default_source).not_to be_a(Stripe::Card)
293
+ expect(customer.subscriptions.count).to eq(0)
294
+ expect(customer.subscriptions.data).to be_empty
295
+ end
296
+
297
+ it "can expand default_source" do
298
+ original = Stripe::Customer.create({
299
+ email: 'johnny@appleseed.com',
300
+ source: gen_card_tk
301
+ })
302
+ customer = Stripe::Customer.retrieve(
303
+ id: original.id,
304
+ expand: ['default_source']
305
+ )
306
+ expect(customer.default_source).to be_a(Stripe::Card)
307
+ end
308
+
309
+ it "cannot retrieve a customer that doesn't exist" do
310
+ expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
311
+ expect(e).to be_a Stripe::InvalidRequestError
312
+ expect(e.param).to eq('customer')
313
+ expect(e.http_status).to eq(404)
314
+ }
315
+ end
316
+
317
+ it "retrieves all customers" do
318
+ Stripe::Customer.create({ email: 'one@one.com' })
319
+ Stripe::Customer.create({ email: 'two@two.com' })
320
+
321
+ all = Stripe::Customer.all
322
+ expect(all.count).to eq(2)
323
+ expect(all.map &:email).to include('one@one.com', 'two@two.com')
324
+ end
325
+
326
+ it "updates a stripe customer" do
327
+ original = Stripe::Customer.create(id: 'test_customer_update')
328
+ email = original.email
329
+
330
+ coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
331
+ original.description = 'new desc'
332
+ original.coupon = coupon.id
333
+ original.save
334
+
335
+ expect(original.email).to eq(email)
336
+ expect(original.description).to eq('new desc')
337
+ expect(original.discount.coupon).to be_a Stripe::Coupon
338
+
339
+ customer = Stripe::Customer.retrieve("test_customer_update")
340
+ expect(customer.email).to eq(original.email)
341
+ expect(customer.description).to eq('new desc')
342
+ expect(customer.discount.coupon).to be_a Stripe::Coupon
343
+ end
344
+
345
+ it "retrieves the customer's default source after it was updated" do
346
+ customer = Stripe::Customer.create()
347
+ customer.source = gen_card_tk
348
+ customer.save
349
+ card = customer.sources.retrieve(customer.default_source)
350
+
351
+ expect(customer.sources).to be_a(Stripe::ListObject)
352
+ expect(card).to be_a(Stripe::Card)
353
+ end
354
+
355
+ it "updates a stripe customer's card from a token" do
356
+ original = Stripe::Customer.create( source: gen_card_tk)
357
+ card = original.sources.data.first
358
+ expect(original.default_source).to eq(card.id)
359
+ expect(original.sources.data.count).to eq(1)
360
+
361
+ original.source = gen_card_tk
362
+ original.save
363
+
364
+ new_card = original.sources.data.last
365
+ expect(original.sources.data.count).to eq(1)
366
+ expect(original.default_source).to_not eq(card.id)
367
+
368
+ expect(new_card.id).to_not eq(card.id)
369
+ end
370
+
371
+ it "updates a stripe customer's card from a hash" do
372
+ original = Stripe::Customer.create( source: gen_card_tk)
373
+ card = original.sources.data.first
374
+ expect(original.default_source).to eq(card.id)
375
+ expect(original.sources.data.count).to eq(1)
376
+
377
+ original.source = {
378
+ object: 'card',
379
+ number: '4012888888881881',
380
+ exp_year: 2018,
381
+ exp_month: 12,
382
+ cvc: 666
383
+ }
384
+
385
+ original.save
386
+
387
+ new_card = original.sources.data.last
388
+ expect(original.sources.data.count).to eq(1)
389
+ expect(original.default_source).to_not eq(card.id)
390
+ end
391
+
392
+ it "still has sources after save when sources unchanged" do
393
+ original = Stripe::Customer.create(source: gen_card_tk)
394
+ card = original.sources.data.first
395
+ card_id = card.id
396
+ expect(original.sources.total_count).to eq(1)
397
+
398
+ original.save
399
+
400
+ expect(original.sources.data.first.id).to eq(card_id)
401
+ expect(original.sources.total_count).to eq(1)
402
+ end
403
+
404
+ it "still has subscriptions after save when subscriptions unchanged" do
405
+ plan = stripe_helper.create_plan(id: 'silver')
406
+ original = Stripe::Customer.create(source: gen_card_tk, plan: 'silver')
407
+ subscription = original.subscriptions.data.first
408
+ subscription_id = subscription.id
409
+ expect(original.subscriptions.total_count).to eq(1)
410
+
411
+ original.save
412
+
413
+ expect(original.subscriptions.data.first.id).to eq(subscription_id)
414
+ expect(original.subscriptions.total_count).to eq(1)
415
+ end
416
+
417
+ it "should add a customer to a subscription" do
418
+ plan = stripe_helper.create_plan(id: 'silver')
419
+ customer = Stripe::Customer.create(source: gen_card_tk)
420
+ customer.subscriptions.create(plan: plan.id)
421
+
422
+ expect(Stripe::Customer.retrieve(customer.id).subscriptions.total_count).to eq(1)
423
+ end
424
+
425
+ it "deletes a customer" do
426
+ customer = Stripe::Customer.create(id: 'test_customer_sub')
427
+ customer = customer.delete
428
+ expect(customer.deleted).to eq(true)
429
+ end
430
+
431
+ it 'works with the update_subscription method' do
432
+ stripe_helper.create_plan(id: 'silver')
433
+ cus = Stripe::Customer.create(source: gen_card_tk)
434
+ expect {
435
+ cus.update_subscription(plan: 'silver')
436
+ }.not_to raise_error
437
+ end
438
+
439
+ it "deletes a stripe customer discount" do
440
+ original = Stripe::Customer.create(id: 'test_customer_update')
441
+
442
+ coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
443
+ original.coupon = coupon.id
444
+ original.save
445
+
446
+ expect(original.discount.coupon).to be_a Stripe::Coupon
447
+
448
+ original.delete_discount
449
+
450
+ customer = Stripe::Customer.retrieve("test_customer_update")
451
+ expect(customer.discount).to be nil
452
+ end
453
+ end