Skip to content

Commit

Permalink
Support API 2020-03-02
Browse files Browse the repository at this point in the history
  • Loading branch information
coorasse committed Mar 19, 2020
1 parent 688bd30 commit 08a24ea
Show file tree
Hide file tree
Showing 25 changed files with 50 additions and 57 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ rvm:
- 2.4.6
- 2.5.5
- 2.6.3
before_install:
- rvm 2.1.10 do gem install mime-types -v 2.6.2
- gem install bundler -v '< 2'

before_script:
- "sudo touch /var/log/stripe-mock-server.log"
- "sudo chown travis /var/log/stripe-mock-server.log"
script: "bundle exec rspec && bundle exec rspec -t live"

env:
global:
- IS_TRAVIS=true STRIPE_TEST_SECRET_KEY_A=sk_test_BsztzqQjzd7lqkgo1LjEG5DF00KzH7tWKF STRIPE_TEST_SECRET_KEY_B=sk_test_rKCEu0x8jzg6cKPqoey8kUPQ00usQO3KYE STRIPE_TEST_SECRET_KEY_C=sk_test_qeaB7R6Ywp8sC9pzd1ZIABH700YLC7nhmZ
- STRIPE_TEST_SECRET_KEY_A=sk_test_BsztzqQjzd7lqkgo1LjEG5DF00KzH7tWKF
matrix:
- STRIPE_API_VERSION=2020-03-02

script: "bundle exec rspec && bundle exec rspec -t live"

notifications:
webhooks:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ version `3.0.0` has [breaking changes](https://github.com/stripe-ruby-mock/strip

### Specifications

**STRIPE API TARGET VERSION:** 2019-08-20 (master) - we try, but some features are not implemented yet.
**STRIPE API TARGET VERSION:** 2020-03-02 (master) - we try, but some features are not implemented yet.

Older API version branches:

Expand Down
4 changes: 2 additions & 2 deletions lib/stripe_mock/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def self.mock_customer(sources, params)
livemode: false,
delinquent: false,
discount: nil,
account_balance: 0,
balance: 0,
currency: currency,
invoice_settings: {
default_payment_method: nil,
Expand Down Expand Up @@ -310,7 +310,7 @@ def self.mock_coupon(params={})
def self.mock_subscription(params={})
StripeMock::Util.rmerge({
created: 1478204116,
billing: 'charge_automatically',
collection_method: 'charge_automatically',
current_period_start: 1308595038,
current_period_end: 1308681468,
status: 'trialing',
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/request_handlers/charges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def ensure_required_params(params)
elsif non_integer_charge_amount?(params)
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
elsif non_positive_charge_amount?(params)
raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
raise Stripe::InvalidRequestError.new('This value must be greater than or equal to 1.', 'amount', http_status: 400)
elsif params[:source].nil? && params[:customer].nil?
raise Stripe::InvalidRequestError.new('Must provide source or customer.', http_status: nil)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def custom_subscription_params(plans, cus, options = {})
start_time = options[:current_period_start] || now
params = { customer: cus[:id], current_period_start: start_time, created: created_time }
params.merge!({ :plan => (plans.size == 1 ? plans.first : nil) })
keys_to_merge = /application_fee_percent|quantity|metadata|tax_percent|billing|days_until_due|default_tax_rates|pending_invoice_item_interval/
keys_to_merge = /application_fee_percent|quantity|metadata|tax_percent|collection_method|days_until_due|default_tax_rates|pending_invoice_item_interval/
params.merge! options.select {|k,v| k =~ keys_to_merge}

if options[:cancel_at_period_end] == true
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/request_handlers/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def upcoming_invoice(route, method_url, params, headers)
customer: customer[:id],
discount: customer[:discount],
created: invoice_date,
starting_balance: customer[:account_balance],
starting_balance: customer[:balance],
subscription: preview_subscription[:id],
period_start: prorating ? invoice_date : preview_subscription[:current_period_start],
period_end: prorating ? invoice_date : preview_subscription[:current_period_end],
Expand Down
4 changes: 2 additions & 2 deletions lib/stripe_mock/request_handlers/payment_intents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def ensure_payment_intent_required_params(params)
elsif params[:currency].nil?
require_param(:currency)
elsif non_integer_charge_amount?(params)
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
raise Stripe::InvalidRequestError.new("This value must be greater than or equal to 1.", 'amount', http_status: 400)
elsif non_positive_charge_amount?(params)
raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
raise Stripe::InvalidRequestError.new('This value must be greater than or equal to 1.', 'amount', http_status: 400)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/request_handlers/payouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def new_payout(route, method_url, params, headers)
id = new_id('po')

unless params[:amount].is_a?(Integer) || (params[:amount].is_a?(String) && /^\d+$/.match(params[:amount]))
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
raise Stripe::InvalidRequestError.new("This value must be greater than or equal to 1.", 'amount', http_status: 400)
end

payouts[id] = Data.mock_payout(params.merge :id => id)
Expand Down
7 changes: 3 additions & 4 deletions lib/stripe_mock/request_handlers/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def create_subscription(route, method_url, params, headers)
if headers && headers[:idempotency_key]
if subscriptions.any?
original_subscription = subscriptions.values.find { |c| c[:idempotency_key] == headers[:idempotency_key]}
puts original_subscription
return subscriptions[original_subscription[:id]] if original_subscription
end
end
Expand All @@ -102,7 +101,7 @@ def create_subscription(route, method_url, params, headers)
customer[:default_source] = new_card[:id]
end

allowed_params = %w(customer application_fee_percent coupon items metadata plan quantity source tax_percent trial_end trial_period_days current_period_start created prorate billing_cycle_anchor billing days_until_due idempotency_key enable_incomplete_payments cancel_at_period_end default_tax_rates payment_behavior pending_invoice_item_interval)
allowed_params = %w(customer application_fee_percent coupon items metadata plan quantity source tax_percent trial_end trial_period_days current_period_start created prorate billing_cycle_anchor collection_method days_until_due idempotency_key enable_incomplete_payments cancel_at_period_end default_tax_rates payment_behavior pending_invoice_item_interval)
unknown_params = params.keys - allowed_params.map(&:to_sym)
if unknown_params.length > 0
raise Stripe::InvalidRequestError.new("Received unknown parameter: #{unknown_params.join}", unknown_params.first.to_s, http_status: 400)
Expand Down Expand Up @@ -292,9 +291,9 @@ def verify_card_present(customer, plan, subscription, params={})
return if trial
end

return if params[:billing] == 'send_invoice'
return if params[:collection_method] == 'send_invoice'

raise Stripe::InvalidRequestError.new('This customer has no attached payment source', nil, http_status: 400)
raise Stripe::InvalidRequestError.new('This customer has no attached payment source or default payment method.', nil, http_status: 400)
end

def verify_active_status(subscription)
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/request_handlers/transfers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def new_transfer(route, method_url, params, headers)
end

unless params[:amount].is_a?(Integer) || (params[:amount].is_a?(String) && /^\d+$/.match(params[:amount]))
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
raise Stripe::InvalidRequestError.new("This value must be greater than or equal to 1.", 'amount', http_status: 400)
end

transfers[id] = Data.mock_transfer(params.merge :id => id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module RequestHandlers
module ParamValidators

def already_exists_message(obj_class)
"#{obj_class.to_s.split("::").last} already exists."
/#{obj_class.to_s.split("::").last.downcase} already exists./i
end

def not_found_message(obj_class, obj_id)
Expand Down Expand Up @@ -49,7 +49,7 @@ def missing_plan_amount_message
SUPPORTED_PLAN_INTERVALS = ["month", "year", "week", "day"]

def invalid_plan_interval_message
"Invalid interval: must be one of day, month, week, or year"
"Invalid interval: must be one of month, year, week, or day"
end

SUPPORTED_CURRENCIES = [
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/webhook_fixtures/customer.created.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"preferred_locales": [],
"subscription": null,
"discount": null,
"account_balance": 0,
"balance": 0,
"sources": {
"object": "list",
"count": 1,
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/webhook_fixtures/customer.deleted.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"created": 1326853478,
"data": {
"object": {
"account_balance": 0,
"balance": 0,
"active_card": {
"address_city": null,
"address_country": null,
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/webhook_fixtures/customer.updated.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"preferred_locales": [],
"subscription": null,
"discount": null,
"account_balance": 0,
"balance": 0,
"sources": {
"object": "list",
"count": 1,
Expand Down
2 changes: 1 addition & 1 deletion spec/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
255.times { data << Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
list = StripeMock::Data::List.new(data, starting_after: "test_ch_unknown")

expect { list.to_h }.to raise_error
expect { list.to_h }.to raise_error(RuntimeError)
end
end
end
2 changes: 1 addition & 1 deletion spec/shared_stripe_examples/account_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
account.tos_acceptance.date = nil
expect {
account.save
}.to raise_error
}.to raise_error(Stripe::InvalidRequestError)
end

context 'with tos acceptance date' do
Expand Down
2 changes: 1 addition & 1 deletion spec/shared_stripe_examples/charge_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
currency: 'usd',
source: stripe_helper.generate_card_token
)
}.to raise_error(Stripe::InvalidRequestError, /invalid positive integer/i)
}.to raise_error(Stripe::InvalidRequestError, /This value must be greater than or equal to 1./i)
end

it "requires a valid integer amount", :live => true do
Expand Down
3 changes: 2 additions & 1 deletion spec/shared_stripe_examples/customer_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def gen_card_tk
expect(customer.default_source).to_not be_nil
expect(customer.default_source).to eq customer.sources.data.first.id

expect { customer.source }.to raise_error
# TODO: is it correct?
expect { customer.source }.to raise_error(NoMethodError)
end

it "creates a stripe customer with a default payment method" do
Expand Down
4 changes: 2 additions & 2 deletions spec/shared_stripe_examples/invoice_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
remaining_amount = new_monthly_plan.amount * new_quantity * (subscription.current_period_end - proration_date.to_i) / (subscription.current_period_end - subscription.current_period_start)
prorated_amount_due = new_monthly_plan.amount * new_quantity - unused_amount + remaining_amount
credit_balance = 1000
customer.account_balance = -credit_balance
customer.balance = -credit_balance
customer.save
query = { customer: customer.id, subscription: subscription.id, subscription_plan: new_monthly_plan.id, subscription_proration_date: proration_date.to_i, subscription_quantity: new_quantity }
query[:subscription_trial_end] = (DateTime.now >> 1).to_time.to_i if with_trial
Expand Down Expand Up @@ -312,7 +312,7 @@
prorated_amount_due = new_yearly_plan.amount * new_quantity - unused_amount
credit_balance = 1000
amount_due = prorated_amount_due - credit_balance
customer.account_balance = -credit_balance
customer.balance = -credit_balance
customer.save
query = { customer: customer.id, subscription: subscription.id, subscription_plan: new_yearly_plan.id, subscription_proration_date: proration_date.to_i, subscription_quantity: new_quantity }
query[:subscription_trial_end] = (DateTime.now >> 1).to_time.to_i if with_trial
Expand Down
2 changes: 1 addition & 1 deletion spec/shared_stripe_examples/payment_intent_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
currency: 'usd') }.to raise_error { |e|
expect(e).to be_a Stripe::InvalidRequestError
expect(e.param).to eq('amount')
expect(e.message).to match(/^Invalid.*integer/)
expect(e.message).to match(/^This value must be greater than or equal to 1./)
expect(e.http_status).to eq(400)
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/shared_stripe_examples/payout_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
description: 'Payout for test@example.com') }.to raise_error { |e|
expect(e).to be_a Stripe::InvalidRequestError
expect(e.param).to eq('amount')
expect(e.message).to match(/^Invalid.*integer/)
expect(e.message).to match(/^This value must be greater than or equal to 1./)
expect(e.http_status).to eq(400)
}
end
Expand Down
12 changes: 6 additions & 6 deletions spec/shared_stripe_examples/subscription_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
expect(subscriptions.data.first.id).to eq(sub.id)
expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
expect(subscriptions.data.first.customer).to eq(customer.id)
expect(subscriptions.data.first.billing).to eq('charge_automatically')
expect(subscriptions.data.first.collection_method).to eq('charge_automatically')
expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
expect(subscriptions.data.first.metadata.example).to eq( "yes" )
end
Expand All @@ -94,7 +94,7 @@

expect(sub.object).to eq('subscription')
expect(sub.plan.to_hash).to eq(plan.to_hash)
expect(sub.billing).to eq('charge_automatically')
expect(sub.collection_method).to eq('charge_automatically')
expect(sub.metadata.foo).to eq( "bar" )
expect(sub.metadata.example).to eq( "yes" )

Expand All @@ -112,7 +112,7 @@
expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)

expect(subscriptions.data.first.customer).to eq(customer.id)
expect(subscriptions.data.first.billing).to eq('charge_automatically')
expect(subscriptions.data.first.collection_method).to eq('charge_automatically')
expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
expect(subscriptions.data.first.metadata.example).to eq( "yes" )
end
Expand Down Expand Up @@ -518,13 +518,13 @@
plan: 'silver',
customer: customer.id,
metadata: { foo: 'bar', example: 'yes' },
billing: 'send_invoice',
collection_method: 'send_invoice',
days_until_due: 30,
})

expect(sub.object).to eq('subscription')
expect(sub.plan.to_hash).to eq(plan.to_hash)
expect(sub.billing).to eq 'send_invoice'
expect(sub.collection_method).to eq 'send_invoice'
expect(sub.days_until_due).to eq 30
end

Expand Down Expand Up @@ -833,7 +833,7 @@

expect {
sub.save
}.to raise_error(Stripe::InvalidRequestError, "This customer has no attached payment source")
}.to raise_error(Stripe::InvalidRequestError, "This customer has no attached payment source or default payment method.")
ensure
customer.delete if customer
paid_plan.delete if paid_plan
Expand Down
6 changes: 3 additions & 3 deletions spec/shared_stripe_examples/transfer_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
end

it "when amount is not integer", live: true do
dest = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", requested_capabilities: ['card_payments', 'platform_payments'])
dest = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", requested_capabilities: ['card_payments', 'transfers'])
expect { Stripe::Transfer.create(amount: '400.2',
currency: 'usd',
destination: dest.id,
Expand All @@ -116,14 +116,14 @@
end

it "when amount is negative", live: true do
dest = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", requested_capabilities: ['card_payments', 'platform_payments'])
dest = Stripe::Account.create(type: "custom", email: "#{SecureRandom.uuid}@example.com", requested_capabilities: ['card_payments', 'transfers'])
expect { Stripe::Transfer.create(amount: '-400',
currency: 'usd',
destination: dest.id,
description: 'Transfer for test@example.com') }.to raise_error { |e|
expect(e).to be_a Stripe::InvalidRequestError
expect(e.param).to eq('amount')
expect(e.message).to match(/^Invalid.*integer/)
expect(e.message).to match(/^This value must be greater than or equal to 1./)
expect(e.http_status).to eq(400)
}
end
Expand Down
8 changes: 4 additions & 4 deletions spec/shared_stripe_examples/webhook_event_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@

it "takes a hash and deep merges into the data object" do
event = StripeMock.mock_webhook_event('customer.created', {
:account_balance => 12345
:balance => 12345
})
payload = StripeMock.mock_webhook_event('customer.created', {
:account_balance => 12345
:balance => 12345
})
expect(event.data.object.account_balance).to eq(12345)
expect(payload[:data][:object][:account_balance]).to eq(12345)
expect(event.data.object.balance).to eq(12345)
expect(payload[:data][:object][:balance]).to eq(12345)
end

it "takes a hash and deep merges arrays in the data object" do
Expand Down
16 changes: 4 additions & 12 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,16 @@
c.filter_run_excluding :mock_server => true, :oauth => true
end

if ENV['IS_TRAVIS']
puts "Travis ruby version: #{RUBY_VERSION}"
api_key = case RUBY_VERSION
when '2.4.6' then ENV['STRIPE_TEST_SECRET_KEY_A']
when '2.5.5' then ENV['STRIPE_TEST_SECRET_KEY_B']
when '2.6.3' then ENV['STRIPE_TEST_SECRET_KEY_C']
end
else
api_key = ENV['STRIPE_TEST_SECRET_KEY']
if api_key.nil? || api_key == ''
raise "Please set your STRIPE_TEST_SECRET_KEY environment variable."
end
api_key = ENV['STRIPE_TEST_SECRET_KEY']
if api_key.nil? || api_key == ''
raise "Please set your STRIPE_TEST_SECRET_KEY environment variable."
end

c.before(:each) do
allow(StripeMock).to receive(:start).and_return(nil)
allow(StripeMock).to receive(:stop).and_return(nil)
Stripe.api_key = api_key
Stripe.api_version = ENV['STRIPE_API_VERSION']
end
c.after(:each) { sleep 0.01 }
else
Expand Down

0 comments on commit 08a24ea

Please sign in to comment.