stripe-ruby-mock 2.5.4 → 2.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/README.md +1 -1
  4. data/lib/stripe_mock.rb +4 -0
  5. data/lib/stripe_mock/api/webhooks.rb +2 -0
  6. data/lib/stripe_mock/data.rb +78 -17
  7. data/lib/stripe_mock/data/list.rb +7 -2
  8. data/lib/stripe_mock/instance.rb +37 -3
  9. data/lib/stripe_mock/request_handlers/accounts.rb +16 -0
  10. data/lib/stripe_mock/request_handlers/charges.rb +7 -8
  11. data/lib/stripe_mock/request_handlers/customers.rb +2 -2
  12. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  13. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +1 -0
  14. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -11
  15. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +20 -2
  16. data/lib/stripe_mock/request_handlers/invoices.rb +1 -1
  17. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  18. data/lib/stripe_mock/request_handlers/refunds.rb +6 -3
  19. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  20. data/lib/stripe_mock/request_handlers/subscriptions.rb +40 -22
  21. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  22. data/lib/stripe_mock/request_handlers/tokens.rb +2 -2
  23. data/lib/stripe_mock/request_handlers/transfers.rb +10 -4
  24. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +3 -0
  25. data/lib/stripe_mock/test_strategies/base.rb +4 -2
  26. data/lib/stripe_mock/version.rb +1 -1
  27. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  28. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  29. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +2 -2
  30. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +2 -2
  31. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +2 -2
  32. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +3 -3
  33. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  34. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  35. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +1 -1
  36. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  37. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  38. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  39. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  40. data/spec/instance_spec.rb +31 -0
  41. data/spec/shared_stripe_examples/account_examples.rb +27 -0
  42. data/spec/shared_stripe_examples/charge_examples.rb +23 -14
  43. data/spec/shared_stripe_examples/customer_examples.rb +11 -1
  44. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  45. data/spec/shared_stripe_examples/invoice_examples.rb +5 -5
  46. data/spec/shared_stripe_examples/plan_examples.rb +19 -4
  47. data/spec/shared_stripe_examples/product_example.rb +65 -0
  48. data/spec/shared_stripe_examples/refund_examples.rb +16 -10
  49. data/spec/shared_stripe_examples/subscription_examples.rb +176 -18
  50. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  51. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  52. data/spec/shared_stripe_examples/transfer_examples.rb +61 -30
  53. data/spec/support/stripe_examples.rb +4 -1
  54. metadata +16 -2
@@ -0,0 +1,88 @@
1
+ {
2
+ "created": 1326853478,
3
+ "livemode": false,
4
+ "id": "evt_00000000000000",
5
+ "type": "charge.dispute.funds_withdrawn",
6
+ "object": "event",
7
+ "request": null,
8
+ "pending_webhooks": 1,
9
+ "api_version": "2017-12-14",
10
+ "data": {
11
+ "object": {
12
+ "id": "dp_00000000000000",
13
+ "object": "dispute",
14
+ "amount": 25000,
15
+ "balance_transaction": "txn_00000000000000",
16
+ "balance_transactions": [
17
+ {
18
+ "id": "txn_1Bl3mMGWCopOTFn18p8iALq8",
19
+ "object": "balance_transaction",
20
+ "amount": -25000,
21
+ "available_on": 1516233600,
22
+ "created": 1516145022,
23
+ "currency": "usd",
24
+ "description": "Chargeback withdrawal for ch_1Bl3mKGWCopOTFn1LKoN557r",
25
+ "exchange_rate": null,
26
+ "fee": 1500,
27
+ "fee_details": [
28
+ {
29
+ "amount": 1500,
30
+ "application": null,
31
+ "currency": "usd",
32
+ "description": "Dispute fee",
33
+ "type": "stripe_fee"
34
+ }
35
+ ],
36
+ "net": -26500,
37
+ "source": "dp_1Bl3mMGWCopOTFn1jpVaJDrU",
38
+ "status": "pending",
39
+ "type": "adjustment"
40
+ }
41
+ ],
42
+ "charge": "ch_00000000000000",
43
+ "created": 1516145022,
44
+ "currency": "usd",
45
+ "evidence": {
46
+ "access_activity_log": null,
47
+ "billing_address": null,
48
+ "cancellation_policy": null,
49
+ "cancellation_policy_disclosure": null,
50
+ "cancellation_rebuttal": null,
51
+ "customer_communication": null,
52
+ "customer_email_address": "amitree.apu@gmail.com",
53
+ "customer_name": "amitree.apu@gmail.com",
54
+ "customer_purchase_ip": "157.131.133.10",
55
+ "customer_signature": null,
56
+ "duplicate_charge_documentation": null,
57
+ "duplicate_charge_explanation": null,
58
+ "duplicate_charge_id": null,
59
+ "product_description": null,
60
+ "receipt": null,
61
+ "refund_policy": null,
62
+ "refund_policy_disclosure": null,
63
+ "refund_refusal_explanation": null,
64
+ "service_date": null,
65
+ "service_documentation": null,
66
+ "shipping_address": null,
67
+ "shipping_carrier": null,
68
+ "shipping_date": null,
69
+ "shipping_documentation": null,
70
+ "shipping_tracking_number": null,
71
+ "uncategorized_file": null,
72
+ "uncategorized_text": null
73
+ },
74
+ "evidence_details": {
75
+ "due_by": 1517529599,
76
+ "has_evidence": false,
77
+ "past_due": false,
78
+ "submission_count": 0
79
+ },
80
+ "is_charge_refundable": false,
81
+ "livemode": false,
82
+ "metadata": {
83
+ },
84
+ "reason": "fraudulent",
85
+ "status": "needs_response"
86
+ }
87
+ }
88
+ }
@@ -16,7 +16,7 @@
16
16
  "created": 1497881783,
17
17
  "plan": {
18
18
  "interval": "month",
19
- "name": "Member's Club",
19
+ "product": "pr_00000000000000",
20
20
  "amount": 100,
21
21
  "currency": "usd",
22
22
  "id": "fkx0AFo_00000000000000",
@@ -34,7 +34,7 @@
34
34
  "created": 1497881788,
35
35
  "plan": {
36
36
  "interval": "month",
37
- "name": "Vistor's Club",
37
+ "product": "pr_00000000000001",
38
38
  "amount": 200,
39
39
  "currency": "eur",
40
40
  "id": "fkx0AFo_00000000000001",
@@ -15,7 +15,7 @@
15
15
  "created": 1497881783,
16
16
  "plan": {
17
17
  "interval": "month",
18
- "name": "Member's Club",
18
+ "product": "pr_00000000000000",
19
19
  "amount": 100,
20
20
  "currency": "usd",
21
21
  "id": "fkx0AFo_00000000000000",
@@ -33,7 +33,7 @@
33
33
  "created": 1497881788,
34
34
  "plan": {
35
35
  "interval": "month",
36
- "name": "Vistor's Club",
36
+ "product": "pr_00000000000001",
37
37
  "amount": 200,
38
38
  "currency": "eur",
39
39
  "id": "fkx0AFo_00000000000001",
@@ -15,7 +15,7 @@
15
15
  "created": 1497881783,
16
16
  "plan": {
17
17
  "interval": "month",
18
- "name": "Member's Club",
18
+ "product": "pr_00000000000000",
19
19
  "amount": 100,
20
20
  "currency": "usd",
21
21
  "id": "fkx0AFo_00000000000000",
@@ -33,7 +33,7 @@
33
33
  "created": 1497881788,
34
34
  "plan": {
35
35
  "interval": "month",
36
- "name": "Vistor's Club",
36
+ "product": "pr_00000000000001",
37
37
  "amount": 200,
38
38
  "currency": "eur",
39
39
  "id": "fkx0AFo_00000000000001",
@@ -15,7 +15,7 @@
15
15
  "created": 1497881783,
16
16
  "plan": {
17
17
  "interval": "month",
18
- "name": "Member's Club",
18
+ "product": "pr_00000000000000",
19
19
  "amount": 100,
20
20
  "currency": "usd",
21
21
  "id": "fkx0AFo_00000000000000",
@@ -33,7 +33,7 @@
33
33
  "created": 1497881788,
34
34
  "plan": {
35
35
  "interval": "month",
36
- "name": "Vistor's Club",
36
+ "product": "pr_00000000000001",
37
37
  "amount": 200,
38
38
  "currency": "eur",
39
39
  "id": "fkx0AFo_00000000000001",
@@ -64,7 +64,7 @@
64
64
  "previous_attributes": {
65
65
  "plan": {
66
66
  "interval": "month",
67
- "name": "Old plan",
67
+ "product": "pr_00000000000002",
68
68
  "amount": 100,
69
69
  "currency": "usd",
70
70
  "id": "OLD_PLAN_ID",
@@ -3,10 +3,11 @@
3
3
  "livemode": false,
4
4
  "id": "evt_00000000000000",
5
5
  "type": "invoice.created",
6
+ "status": "paid",
6
7
  "object": "event",
7
8
  "data": {
8
9
  "object": {
9
- "date": 1380674206,
10
+ "created": 1380674206,
10
11
  "id": "in_00000000000000",
11
12
  "period_start": 1378082075,
12
13
  "period_end": 1380674075,
@@ -30,7 +31,6 @@
30
31
  "quantity": 1,
31
32
  "plan": {
32
33
  "interval": "month",
33
- "name": "Member's Club",
34
34
  "amount": 100,
35
35
  "currency": "usd",
36
36
  "id": "fkx0AFo",
@@ -38,6 +38,7 @@
38
38
  "livemode": false,
39
39
  "interval_count": 1,
40
40
  "trial_period_days": null,
41
+ "product": "pr_00000000000000",
41
42
  "metadata": {}
42
43
  },
43
44
  "description": null,
@@ -64,7 +64,7 @@
64
64
  "quantity": 1,
65
65
  "plan": {
66
66
  "interval": "month",
67
- "name": "Platinum",
67
+ "product": "pr_00000000000000",
68
68
  "created": 1300000000,
69
69
  "amount": 20000,
70
70
  "currency": "usd",
@@ -16,7 +16,7 @@
16
16
  "closed": true,
17
17
  "currency": "eur",
18
18
  "customer": "cus_00000000000000",
19
- "date": 1464084258,
19
+ "created": 1464084258,
20
20
  "description": null,
21
21
  "discount": null,
22
22
  "ending_balance": 0,
@@ -3,10 +3,11 @@
3
3
  "livemode": false,
4
4
  "id": "evt_00000000000000",
5
5
  "type": "invoice.updated",
6
+ "status": "paid",
6
7
  "object": "event",
7
8
  "data": {
8
9
  "object": {
9
- "date": 1380674206,
10
+ "created": 1380674206,
10
11
  "id": "in_00000000000000",
11
12
  "period_start": 1378082075,
12
13
  "period_end": 1380674075,
@@ -30,7 +31,7 @@
30
31
  "quantity": 1,
31
32
  "plan": {
32
33
  "interval": "month",
33
- "name": "Member's Club",
34
+ "product": "pr_00000000000000",
34
35
  "amount": 100,
35
36
  "currency": "usd",
36
37
  "id": "fkx0AFo",
@@ -7,7 +7,7 @@
7
7
  "data": {
8
8
  "object": {
9
9
  "interval": "month",
10
- "name": "Member's Club",
10
+ "product": "pr_00000000000000",
11
11
  "amount": 100,
12
12
  "currency": "usd",
13
13
  "id": "fkx0AFo_00000000000000",
@@ -7,7 +7,7 @@
7
7
  "data": {
8
8
  "object": {
9
9
  "interval": "month",
10
- "name": "Member's Club",
10
+ "product": "pr_00000000000000",
11
11
  "amount": 100,
12
12
  "currency": "usd",
13
13
  "id": "fkx0AFo_00000000000000",
@@ -7,7 +7,7 @@
7
7
  "data": {
8
8
  "object": {
9
9
  "interval": "month",
10
- "name": "Member's Club",
10
+ "product": "pr_00000000000000",
11
11
  "amount": 100,
12
12
  "currency": "usd",
13
13
  "id": "fkx0AFo_00000000000000",
@@ -66,4 +66,35 @@ describe StripeMock::Instance do
66
66
  end
67
67
  expect(customer.currency).to eq("jpy")
68
68
  end
69
+
70
+ context 'when creating sources with metadata' do
71
+ let(:customer) { Stripe::Customer.create(email: 'test@email.com') }
72
+ let(:metadata) { { test_key: 'test_value' } }
73
+
74
+ context 'for credit card' do
75
+ let(:credit_card) do
76
+ customer.sources.create(
77
+ source: stripe_helper.generate_card_token,
78
+ metadata: metadata
79
+ )
80
+ end
81
+
82
+ it('should save metadata') do
83
+ expect(credit_card.metadata.test_key).to eq metadata[:test_key]
84
+ end
85
+ end
86
+
87
+ context 'for bank account' do
88
+ let(:bank_account) do
89
+ customer.sources.create(
90
+ source: stripe_helper.generate_bank_token,
91
+ metadata: metadata
92
+ )
93
+ end
94
+
95
+ it('should save metadata') do
96
+ expect(bank_account.metadata.test_key).to eq metadata[:test_key]
97
+ end
98
+ end
99
+ end
69
100
  end
@@ -57,6 +57,33 @@ shared_examples 'Account API' do
57
57
  account.save
58
58
  }.to raise_error
59
59
  end
60
+
61
+ context 'with tos acceptance date' do
62
+ let(:error_message) { "ToS acceptance date is not valid. Dates are expected to be integers, measured in seconds, not in the future, and after 2009" }
63
+
64
+ it 'raises error when tos date is before 2009' do
65
+ date = Date.new(2008,1,1).strftime("%s").to_i
66
+
67
+ account = Stripe::Account.retrieve
68
+ account.tos_acceptance.date = date
69
+
70
+ expect {
71
+ account.save
72
+ }.to raise_error Stripe::InvalidRequestError, error_message
73
+ end
74
+
75
+ it 'raises error when tos date is in the future' do
76
+ year = Time.now.year + 5
77
+ date = Date.new(year,1,1).strftime("%s").to_i
78
+
79
+ account = Stripe::Account.retrieve
80
+ account.tos_acceptance.date = date
81
+
82
+ expect {
83
+ account.save
84
+ }.to raise_error Stripe::InvalidRequestError, error_message
85
+ end
86
+ end
60
87
  end
61
88
 
62
89
  it 'deauthorizes the stripe account', live: false do
@@ -163,15 +163,19 @@ shared_examples 'Charge API' do
163
163
  end
164
164
 
165
165
  it "creates a balance transaction" do
166
+ amount = 300
167
+ fee = 10
166
168
  charge = Stripe::Charge.create({
167
- amount: 300,
169
+ amount: amount,
168
170
  currency: 'USD',
169
- source: stripe_helper.generate_card_token
171
+ source: stripe_helper.generate_card_token,
172
+ application_fee: fee,
170
173
  })
171
174
  bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
172
- expect(bal_trans.amount).to eq(charge.amount)
173
- expect(bal_trans.fee).to eq(39)
175
+ expect(bal_trans.amount).to eq(amount)
176
+ expect(bal_trans.fee).to eq(39 + fee)
174
177
  expect(bal_trans.source).to eq(charge.id)
178
+ expect(bal_trans.net).to eq(amount - bal_trans.fee)
175
179
  end
176
180
 
177
181
  context 'when conversion rate is set' do
@@ -472,29 +476,34 @@ shared_examples 'Charge API' do
472
476
 
473
477
  describe "idempotency" do
474
478
  let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
475
- let(:idempotent_charge_params) {{
479
+ let(:charge_params) {{
476
480
  amount: 777,
477
481
  currency: 'USD',
478
482
  customer: customer.id,
479
- capture: true,
483
+ capture: true
484
+ }}
485
+ let(:charge_headers) {{
480
486
  idempotency_key: 'onceisenough'
481
487
  }}
482
488
 
483
489
  it "returns the original charge if the same idempotency_key is passed in" do
484
- charge1 = Stripe::Charge.create(idempotent_charge_params)
485
- charge2 = Stripe::Charge.create(idempotent_charge_params)
490
+ charge1 = Stripe::Charge.create(charge_params, charge_headers)
491
+ charge2 = Stripe::Charge.create(charge_params, charge_headers)
486
492
 
487
493
  expect(charge1).to eq(charge2)
488
494
  end
489
495
 
490
- it "returns different charges if different idempotency_keys are used for each charge" do
491
- idempotent_charge_params2 = idempotent_charge_params.clone
492
- idempotent_charge_params2[:idempotency_key] = 'thisoneisdifferent'
496
+ context 'different key' do
497
+ let(:different_charge_headers) {{
498
+ idempotency_key: 'thisoneisdifferent'
499
+ }}
493
500
 
494
- charge1 = Stripe::Charge.create(idempotent_charge_params)
495
- charge2 = Stripe::Charge.create(idempotent_charge_params2)
501
+ it "returns different charges if different idempotency_keys are used for each charge" do
502
+ charge1 = Stripe::Charge.create(charge_params, charge_headers)
503
+ charge2 = Stripe::Charge.create(charge_params, different_charge_headers)
496
504
 
497
- expect(charge1).not_to eq(charge2)
505
+ expect(charge1).not_to eq(charge2)
506
+ end
498
507
  end
499
508
  end
500
509
 
@@ -78,6 +78,15 @@ shared_examples 'Customer API' do
78
78
  expect(customer.sources.data.first.exp_year).to eq 2024
79
79
  end
80
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
+
81
90
  it 'creates a customer with a plan' do
82
91
  plan = stripe_helper.create_plan(id: 'silver')
83
92
  customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'silver')
@@ -226,7 +235,7 @@ shared_examples 'Customer API' do
226
235
  discount = Stripe::Customer.retrieve(customer.id).discount
227
236
  expect(discount).to_not be_nil
228
237
  expect(discount.coupon).to_not be_nil
229
- expect(discount.end).to be_within(1).of (Time.now + 365 * 24 * 3600).to_i
238
+ expect(discount.end).to be_within(1).of (Time.now.to_datetime >> 12).to_time.to_i
230
239
  end
231
240
  after { Stripe::Coupon.retrieve(coupon.id).delete }
232
241
  after { Stripe::Customer.retrieve(customer.id).delete }
@@ -267,6 +276,7 @@ shared_examples 'Customer API' do
267
276
 
268
277
  expect(customer.id).to eq(original.id)
269
278
  expect(customer.email).to eq(original.email)
279
+ expect(customer.name).to eq(nil)
270
280
  expect(customer.default_source).to eq(original.default_source)
271
281
  expect(customer.default_source).not_to be_a(Stripe::Card)
272
282
  expect(customer.subscriptions.count).to eq(0)