epopia-stripe-ruby-mock 2.5.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env +2 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.travis.yml +28 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +413 -0
- data/Rakefile +14 -0
- data/bin/stripe-mock-server +19 -0
- data/lib/stripe_mock.rb +95 -0
- data/lib/stripe_mock/api/account_balance.rb +14 -0
- data/lib/stripe_mock/api/bank_tokens.rb +13 -0
- data/lib/stripe_mock/api/card_tokens.rb +13 -0
- data/lib/stripe_mock/api/client.rb +41 -0
- data/lib/stripe_mock/api/conversion_rate.rb +14 -0
- data/lib/stripe_mock/api/debug.rb +11 -0
- data/lib/stripe_mock/api/errors.rb +65 -0
- data/lib/stripe_mock/api/global_id_prefix.rb +22 -0
- data/lib/stripe_mock/api/instance.rb +38 -0
- data/lib/stripe_mock/api/live.rb +15 -0
- data/lib/stripe_mock/api/server.rb +39 -0
- data/lib/stripe_mock/api/test_helpers.rb +24 -0
- data/lib/stripe_mock/api/webhooks.rb +88 -0
- data/lib/stripe_mock/client.rb +127 -0
- data/lib/stripe_mock/data.rb +1193 -0
- data/lib/stripe_mock/data/list.rb +73 -0
- data/lib/stripe_mock/error_queue.rb +27 -0
- data/lib/stripe_mock/errors/closed_client_connection_error.rb +9 -0
- data/lib/stripe_mock/errors/server_timeout_error.rb +12 -0
- data/lib/stripe_mock/errors/stripe_mock_error.rb +15 -0
- data/lib/stripe_mock/errors/uninitialized_instance_error.rb +9 -0
- data/lib/stripe_mock/errors/unstarted_state_error.rb +9 -0
- data/lib/stripe_mock/errors/unsupported_request_error.rb +4 -0
- data/lib/stripe_mock/instance.rb +237 -0
- data/lib/stripe_mock/request_handlers/accounts.rb +86 -0
- data/lib/stripe_mock/request_handlers/balance.rb +17 -0
- data/lib/stripe_mock/request_handlers/balance_transactions.rb +37 -0
- data/lib/stripe_mock/request_handlers/cards.rb +35 -0
- data/lib/stripe_mock/request_handlers/charges.rb +177 -0
- data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
- data/lib/stripe_mock/request_handlers/coupons.rb +35 -0
- data/lib/stripe_mock/request_handlers/customers.rb +137 -0
- data/lib/stripe_mock/request_handlers/disputes.rb +35 -0
- data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
- data/lib/stripe_mock/request_handlers/events.rb +21 -0
- data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
- data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +14 -0
- data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +127 -0
- data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
- data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +17 -0
- data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
- data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +119 -0
- data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +44 -0
- data/lib/stripe_mock/request_handlers/invoice_items.rb +45 -0
- data/lib/stripe_mock/request_handlers/invoices.rb +177 -0
- data/lib/stripe_mock/request_handlers/orders.rb +80 -0
- data/lib/stripe_mock/request_handlers/payment_intents.rb +203 -0
- data/lib/stripe_mock/request_handlers/payment_methods.rb +112 -0
- data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
- data/lib/stripe_mock/request_handlers/plans.rb +42 -0
- data/lib/stripe_mock/request_handlers/products.rb +43 -0
- data/lib/stripe_mock/request_handlers/recipients.rb +60 -0
- data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
- data/lib/stripe_mock/request_handlers/sources.rb +55 -0
- data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
- data/lib/stripe_mock/request_handlers/subscriptions.rb +296 -0
- data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
- data/lib/stripe_mock/request_handlers/tokens.rb +75 -0
- data/lib/stripe_mock/request_handlers/transfers.rb +65 -0
- data/lib/stripe_mock/request_handlers/validators/param_validators.rb +32 -0
- data/lib/stripe_mock/server.rb +93 -0
- data/lib/stripe_mock/test_strategies/base.rb +81 -0
- data/lib/stripe_mock/test_strategies/live.rb +40 -0
- data/lib/stripe_mock/test_strategies/mock.rb +27 -0
- data/lib/stripe_mock/util.rb +44 -0
- data/lib/stripe_mock/version.rb +4 -0
- data/lib/stripe_mock/webhook_fixtures/account.application.deauthorized.json +12 -0
- data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
- data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
- data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
- data/lib/stripe_mock/webhook_fixtures/account.updated.json +26 -0
- data/lib/stripe_mock/webhook_fixtures/balance.available.json +25 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.closed.json +22 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +22 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.updated.json +25 -0
- data/lib/stripe_mock/webhook_fixtures/charge.failed.json +56 -0
- data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +69 -0
- data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +55 -0
- data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
- data/lib/stripe_mock/webhook_fixtures/coupon.created.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/coupon.deleted.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/customer.created.json +54 -0
- data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +42 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.created.json +28 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.deleted.json +28 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.updated.json +43 -0
- data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +32 -0
- data/lib/stripe_mock/webhook_fixtures/customer.source.deleted.json +32 -0
- data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +66 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +65 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +65 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +78 -0
- data/lib/stripe_mock/webhook_fixtures/customer.updated.json +57 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.created.json +71 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +105 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +112 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +74 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.deleted.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +24 -0
- data/lib/stripe_mock/webhook_fixtures/plan.created.json +20 -0
- data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +20 -0
- data/lib/stripe_mock/webhook_fixtures/plan.updated.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.created.json +89 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.failed.json +89 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.paid.json +89 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.updated.json +92 -0
- data/lib/trollop.rb +782 -0
- data/spec/_dummy/webhooks/dummy.event.json +6 -0
- data/spec/api/instance_spec.rb +30 -0
- data/spec/fixtures/create_refund.yml +126 -0
- data/spec/fixtures/stripe_webhooks/account.updated.json +7 -0
- data/spec/fixtures/stripe_webhooks/custom.account.updated.json +5 -0
- data/spec/instance_spec.rb +100 -0
- data/spec/integration_examples/charge_token_examples.rb +51 -0
- data/spec/integration_examples/customer_card_examples.rb +42 -0
- data/spec/integration_examples/prepare_error_examples.rb +38 -0
- data/spec/list_spec.rb +140 -0
- data/spec/readme_spec.rb +75 -0
- data/spec/server_spec.rb +139 -0
- data/spec/shared_stripe_examples/account_examples.rb +96 -0
- data/spec/shared_stripe_examples/balance_examples.rb +11 -0
- data/spec/shared_stripe_examples/balance_transaction_examples.rb +63 -0
- data/spec/shared_stripe_examples/bank_examples.rb +229 -0
- data/spec/shared_stripe_examples/bank_token_examples.rb +59 -0
- data/spec/shared_stripe_examples/card_examples.rb +307 -0
- data/spec/shared_stripe_examples/card_token_examples.rb +185 -0
- data/spec/shared_stripe_examples/charge_examples.rb +510 -0
- data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
- data/spec/shared_stripe_examples/coupon_examples.rb +85 -0
- data/spec/shared_stripe_examples/customer_examples.rb +453 -0
- data/spec/shared_stripe_examples/dispute_examples.rb +98 -0
- data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
- data/spec/shared_stripe_examples/error_mock_examples.rb +162 -0
- data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
- data/spec/shared_stripe_examples/extra_features_examples.rb +36 -0
- data/spec/shared_stripe_examples/invoice_examples.rb +524 -0
- data/spec/shared_stripe_examples/invoice_item_examples.rb +69 -0
- data/spec/shared_stripe_examples/payment_intent_examples.rb +131 -0
- data/spec/shared_stripe_examples/payment_method_examples.rb +175 -0
- data/spec/shared_stripe_examples/payout_examples.rb +68 -0
- data/spec/shared_stripe_examples/plan_examples.rb +194 -0
- data/spec/shared_stripe_examples/product_example.rb +65 -0
- data/spec/shared_stripe_examples/recipient_examples.rb +118 -0
- data/spec/shared_stripe_examples/refund_examples.rb +472 -0
- data/spec/shared_stripe_examples/subscription_examples.rb +1148 -0
- data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
- data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
- data/spec/shared_stripe_examples/transfer_examples.rb +130 -0
- data/spec/shared_stripe_examples/validation_examples.rb +19 -0
- data/spec/shared_stripe_examples/webhook_event_examples.rb +261 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/stripe_mock_spec.rb +123 -0
- data/spec/support/stripe_examples.rb +42 -0
- data/spec/util_spec.rb +121 -0
- data/stripe-ruby-mock.gemspec +27 -0
- metadata +344 -0
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
|
6
|
+
begin
|
7
|
+
gem 'rubygems-tasks', '~> 0.2'
|
8
|
+
require 'rubygems/tasks'
|
9
|
+
|
10
|
+
Gem::Tasks.new
|
11
|
+
rescue LoadError => e
|
12
|
+
warn e.message
|
13
|
+
warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
|
4
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'trollop'
|
7
|
+
|
8
|
+
opts = Trollop::options do
|
9
|
+
opt :port, "Listening port", :type => :int, :default => 4999
|
10
|
+
opt :host, "Host to listen on", :type => :string, :default => '0.0.0.0'
|
11
|
+
opt :server, "Server to use", :type => :string, :default => 'thin'
|
12
|
+
opt :debug, "Request and response output", :default => true
|
13
|
+
opt :pid_path, "Location to put server pid file", :type => :string, :default => './stripe-mock-server.pid'
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'stripe_mock'
|
17
|
+
require 'stripe_mock/server'
|
18
|
+
|
19
|
+
StripeMock::Server.start_new(opts)
|
data/lib/stripe_mock.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'multi_json'
|
3
|
+
require 'dante'
|
4
|
+
require 'time'
|
5
|
+
|
6
|
+
require 'stripe'
|
7
|
+
|
8
|
+
require 'stripe_mock/version'
|
9
|
+
require 'stripe_mock/util'
|
10
|
+
require 'stripe_mock/error_queue'
|
11
|
+
|
12
|
+
require 'stripe_mock/data'
|
13
|
+
require 'stripe_mock/data/list'
|
14
|
+
|
15
|
+
require 'stripe_mock/errors/stripe_mock_error'
|
16
|
+
require 'stripe_mock/errors/unsupported_request_error'
|
17
|
+
require 'stripe_mock/errors/uninitialized_instance_error'
|
18
|
+
require 'stripe_mock/errors/unstarted_state_error'
|
19
|
+
require 'stripe_mock/errors/server_timeout_error'
|
20
|
+
require 'stripe_mock/errors/closed_client_connection_error'
|
21
|
+
|
22
|
+
require 'stripe_mock/client'
|
23
|
+
require 'stripe_mock/server'
|
24
|
+
|
25
|
+
require 'stripe_mock/api/instance'
|
26
|
+
require 'stripe_mock/api/client'
|
27
|
+
require 'stripe_mock/api/server'
|
28
|
+
|
29
|
+
require 'stripe_mock/api/bank_tokens'
|
30
|
+
require 'stripe_mock/api/account_balance'
|
31
|
+
require 'stripe_mock/api/conversion_rate'
|
32
|
+
require 'stripe_mock/api/card_tokens'
|
33
|
+
require 'stripe_mock/api/debug'
|
34
|
+
require 'stripe_mock/api/errors'
|
35
|
+
require 'stripe_mock/api/global_id_prefix'
|
36
|
+
require 'stripe_mock/api/live'
|
37
|
+
require 'stripe_mock/api/test_helpers'
|
38
|
+
require 'stripe_mock/api/webhooks'
|
39
|
+
|
40
|
+
require 'stripe_mock/request_handlers/helpers/bank_account_helpers.rb'
|
41
|
+
require 'stripe_mock/request_handlers/helpers/external_account_helpers.rb'
|
42
|
+
require 'stripe_mock/request_handlers/helpers/card_helpers.rb'
|
43
|
+
require 'stripe_mock/request_handlers/helpers/charge_helpers.rb'
|
44
|
+
require 'stripe_mock/request_handlers/helpers/coupon_helpers.rb'
|
45
|
+
require 'stripe_mock/request_handlers/helpers/subscription_helpers.rb'
|
46
|
+
require 'stripe_mock/request_handlers/helpers/token_helpers.rb'
|
47
|
+
|
48
|
+
require 'stripe_mock/request_handlers/validators/param_validators.rb'
|
49
|
+
|
50
|
+
require 'stripe_mock/request_handlers/accounts.rb'
|
51
|
+
require 'stripe_mock/request_handlers/external_accounts.rb'
|
52
|
+
require 'stripe_mock/request_handlers/balance.rb'
|
53
|
+
require 'stripe_mock/request_handlers/balance_transactions.rb'
|
54
|
+
require 'stripe_mock/request_handlers/charges.rb'
|
55
|
+
require 'stripe_mock/request_handlers/cards.rb'
|
56
|
+
require 'stripe_mock/request_handlers/sources.rb'
|
57
|
+
require 'stripe_mock/request_handlers/customers.rb'
|
58
|
+
require 'stripe_mock/request_handlers/coupons.rb'
|
59
|
+
require 'stripe_mock/request_handlers/disputes.rb'
|
60
|
+
require 'stripe_mock/request_handlers/events.rb'
|
61
|
+
require 'stripe_mock/request_handlers/invoices.rb'
|
62
|
+
require 'stripe_mock/request_handlers/invoice_items.rb'
|
63
|
+
require 'stripe_mock/request_handlers/orders.rb'
|
64
|
+
require 'stripe_mock/request_handlers/plans.rb'
|
65
|
+
require 'stripe_mock/request_handlers/recipients.rb'
|
66
|
+
require 'stripe_mock/request_handlers/refunds.rb'
|
67
|
+
require 'stripe_mock/request_handlers/transfers.rb'
|
68
|
+
require 'stripe_mock/request_handlers/payment_intents.rb'
|
69
|
+
require 'stripe_mock/request_handlers/payment_methods.rb'
|
70
|
+
require 'stripe_mock/request_handlers/payouts.rb'
|
71
|
+
require 'stripe_mock/request_handlers/subscriptions.rb'
|
72
|
+
require 'stripe_mock/request_handlers/subscription_items.rb'
|
73
|
+
require 'stripe_mock/request_handlers/tokens.rb'
|
74
|
+
require 'stripe_mock/request_handlers/country_spec.rb'
|
75
|
+
require 'stripe_mock/request_handlers/ephemeral_key.rb'
|
76
|
+
require 'stripe_mock/request_handlers/products.rb'
|
77
|
+
require 'stripe_mock/request_handlers/tax_rates.rb'
|
78
|
+
require 'stripe_mock/instance'
|
79
|
+
|
80
|
+
require 'stripe_mock/test_strategies/base.rb'
|
81
|
+
require 'stripe_mock/test_strategies/mock.rb'
|
82
|
+
require 'stripe_mock/test_strategies/live.rb'
|
83
|
+
|
84
|
+
module StripeMock
|
85
|
+
|
86
|
+
@default_currency = 'usd'
|
87
|
+
lib_dir = File.expand_path(File.dirname(__FILE__), '../..')
|
88
|
+
@webhook_fixture_path = './spec/fixtures/stripe_webhooks/'
|
89
|
+
@webhook_fixture_fallback_path = File.join(lib_dir, 'stripe_mock/webhook_fixtures')
|
90
|
+
|
91
|
+
class << self
|
92
|
+
attr_accessor :default_currency
|
93
|
+
attr_accessor :webhook_fixture_path
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.generate_bank_token(bank_params = {})
|
4
|
+
case @state
|
5
|
+
when 'local'
|
6
|
+
instance.generate_bank_token(bank_params)
|
7
|
+
when 'remote'
|
8
|
+
client.generate_bank_token(bank_params)
|
9
|
+
else
|
10
|
+
raise UnstartedStateError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.generate_card_token(card_params = {})
|
4
|
+
case @state
|
5
|
+
when 'local'
|
6
|
+
instance.generate_card_token(card_params)
|
7
|
+
when 'remote'
|
8
|
+
client.generate_card_token(card_params)
|
9
|
+
else
|
10
|
+
raise UnstartedStateError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.client
|
4
|
+
@client
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.start_client(port=4999)
|
8
|
+
return false if @state == 'live'
|
9
|
+
return @client unless @client.nil?
|
10
|
+
|
11
|
+
Stripe::StripeClient.send(:define_method, :execute_request) { |*args| StripeMock.redirect_to_mock_server(*args) }
|
12
|
+
@client = StripeMock::Client.new(port)
|
13
|
+
@state = 'remote'
|
14
|
+
@client
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.stop_client(opts={})
|
18
|
+
return false unless @state == 'remote'
|
19
|
+
@state = 'ready'
|
20
|
+
|
21
|
+
restore_stripe_execute_request_method
|
22
|
+
@client.clear_server_data if opts[:clear_server_data] == true
|
23
|
+
@client.cleanup
|
24
|
+
@client = nil
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def self.redirect_to_mock_server(method, url, api_key: nil, api_base: nil, params: {}, headers: {})
|
31
|
+
handler = Instance.handler_for_method_url("#{method} #{url}")
|
32
|
+
|
33
|
+
if mock_error = client.error_queue.error_for_handler_name(handler[:name])
|
34
|
+
client.error_queue.dequeue
|
35
|
+
raise mock_error
|
36
|
+
end
|
37
|
+
|
38
|
+
Stripe::Util.symbolize_names client.mock_request(method, url, api_key: api_key, params: params, headers: headers)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.prepare_error(stripe_error, *handler_names)
|
4
|
+
handler_names.push(:all) if handler_names.count == 0
|
5
|
+
|
6
|
+
if @state == 'local'
|
7
|
+
instance
|
8
|
+
elsif @state == 'remote'
|
9
|
+
client
|
10
|
+
else
|
11
|
+
raise UnstartedStateError
|
12
|
+
end.error_queue.queue stripe_error, handler_names
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.prepare_card_error(code, *handler_names)
|
16
|
+
handler_names.push(:new_charge) if handler_names.count == 0
|
17
|
+
|
18
|
+
args = CardErrors.argument_map[code]
|
19
|
+
raise StripeMockError.new("Unrecognized stripe card error code: #{code}") if args.nil?
|
20
|
+
self.prepare_error Stripe::CardError.new(*args), *handler_names
|
21
|
+
end
|
22
|
+
|
23
|
+
module CardErrors
|
24
|
+
|
25
|
+
def self.argument_map
|
26
|
+
@__map ||= {
|
27
|
+
incorrect_number: add_json_body(["The card number is incorrect", 'number', 'incorrect_number', http_status: 402]),
|
28
|
+
invalid_number: add_json_body(["The card number is not a valid credit card number", 'number', 'invalid_number', http_status: 402]),
|
29
|
+
invalid_expiry_month: add_json_body(["The card's expiration month is invalid", 'exp_month', 'invalid_expiry_month', http_status: 402]),
|
30
|
+
invalid_expiry_year: add_json_body(["The card's expiration year is invalid", 'exp_year', 'invalid_expiry_year', http_status: 402]),
|
31
|
+
invalid_cvc: add_json_body(["The card's security code is invalid", 'cvc', 'invalid_cvc', http_status: 402]),
|
32
|
+
expired_card: add_json_body(["The card has expired", 'exp_month', 'expired_card', http_status: 402]),
|
33
|
+
incorrect_cvc: add_json_body(["The card's security code is incorrect", 'cvc', 'incorrect_cvc', http_status: 402]),
|
34
|
+
card_declined: add_json_body(["The card was declined", nil, 'card_declined', http_status: 402]),
|
35
|
+
missing: add_json_body(["There is no card on a customer that is being charged.", nil, 'missing', http_status: 402]),
|
36
|
+
processing_error: add_json_body(["An error occurred while processing the card", nil, 'processing_error', http_status: 402]),
|
37
|
+
card_error: add_json_body(['The card number is not a valid credit card number.', 'number', 'invalid_number', http_status: 402]),
|
38
|
+
incorrect_zip: add_json_body(['The zip code you supplied failed validation.', 'address_zip', 'incorrect_zip', http_status: 402])
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.get_decline_code(code)
|
43
|
+
decline_code_map = {
|
44
|
+
card_declined: 'do_not_honor',
|
45
|
+
missing: nil
|
46
|
+
}
|
47
|
+
decline_code_map.default = code.to_s
|
48
|
+
|
49
|
+
code_key = code.to_sym
|
50
|
+
decline_code_map[code_key]
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.add_json_body(error_values)
|
54
|
+
error_keys = [:message, :param, :code]
|
55
|
+
|
56
|
+
json_hash = Hash[error_keys.zip error_values]
|
57
|
+
json_hash[:type] = 'card_error'
|
58
|
+
json_hash[:decline_code] = get_decline_code(json_hash[:code])
|
59
|
+
|
60
|
+
error_values.last.merge!(json_body: { error: json_hash }, http_body: { error: json_hash })
|
61
|
+
|
62
|
+
error_values
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.global_id_prefix
|
4
|
+
if StripeMock.client
|
5
|
+
StripeMock.client.server_global_id_prefix
|
6
|
+
else
|
7
|
+
case @global_id_prefix
|
8
|
+
when false then ""
|
9
|
+
when nil then "test_"
|
10
|
+
else @global_id_prefix
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.global_id_prefix=(value)
|
16
|
+
if StripeMock.client
|
17
|
+
StripeMock.client.set_server_global_id_prefix(value)
|
18
|
+
else
|
19
|
+
@global_id_prefix = value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
@state = 'ready'
|
4
|
+
@instance = nil
|
5
|
+
@original_execute_request_method = Stripe::StripeClient.instance_method(:execute_request)
|
6
|
+
|
7
|
+
def self.start
|
8
|
+
return false if @state == 'live'
|
9
|
+
@instance = instance = Instance.new
|
10
|
+
Stripe::StripeClient.send(:define_method, :execute_request) { |*args| instance.mock_request(*args) }
|
11
|
+
@state = 'local'
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.stop
|
15
|
+
return unless @state == 'local'
|
16
|
+
restore_stripe_execute_request_method
|
17
|
+
@instance = nil
|
18
|
+
@state = 'ready'
|
19
|
+
end
|
20
|
+
|
21
|
+
# Yield the given block between StripeMock.start and StripeMock.stop
|
22
|
+
def self.mock(&block)
|
23
|
+
begin
|
24
|
+
self.start
|
25
|
+
yield
|
26
|
+
ensure
|
27
|
+
self.stop
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.restore_stripe_execute_request_method
|
32
|
+
Stripe::StripeClient.send(:define_method, :execute_request, @original_execute_request_method)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.instance; @instance; end
|
36
|
+
def self.state; @state; end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.toggle_live(toggle)
|
4
|
+
if @state != 'ready' && @state != 'live'
|
5
|
+
raise "You cannot toggle StripeMock live when it has already started."
|
6
|
+
end
|
7
|
+
if toggle
|
8
|
+
@state = 'live'
|
9
|
+
StripeMock.set_default_test_helper_strategy(:live)
|
10
|
+
else
|
11
|
+
@state = 'ready'
|
12
|
+
StripeMock.set_default_test_helper_strategy(:mock)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module StripeMock
|
2
|
+
@default_server_pid_path = './stripe-mock-server.pid'
|
3
|
+
@default_server_log_path = './stripe-mock-server.log'
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_writer :default_server_pid_path, :default_server_log_path
|
7
|
+
|
8
|
+
["pid", "log"].each do |config_type|
|
9
|
+
define_method("default_server_#{config_type}_path") do
|
10
|
+
instance_variable_get("@default_server_#{config_type}_path") || "./stripe-mock-server.#{config_type}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def spawn_server(opts={})
|
15
|
+
pid_path = opts[:pid_path] || default_server_pid_path
|
16
|
+
log_path = opts[:log_path] || default_server_log_path
|
17
|
+
|
18
|
+
Dante::Runner.new('stripe-mock-server').execute(
|
19
|
+
:daemonize => true, :pid_path => pid_path, :log_path => log_path
|
20
|
+
){
|
21
|
+
StripeMock::Server.start_new(opts)
|
22
|
+
}
|
23
|
+
at_exit {
|
24
|
+
begin
|
25
|
+
e = $! # last exception
|
26
|
+
kill_server(pid_path)
|
27
|
+
ensure
|
28
|
+
raise e if $! != e
|
29
|
+
end
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def kill_server(pid_path=nil)
|
34
|
+
puts "Killing server at #{pid_path}"
|
35
|
+
path = pid_path || default_server_pid_path
|
36
|
+
Dante::Runner.new('stripe-mock-server').execute(:kill => true, :pid_path => path)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module StripeMock
|
2
|
+
|
3
|
+
def self.create_test_helper(strategy=nil)
|
4
|
+
if strategy
|
5
|
+
get_test_helper_strategy(strategy).new
|
6
|
+
elsif @__test_strat
|
7
|
+
@__test_strat.new
|
8
|
+
else
|
9
|
+
TestStrategies::Mock.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.set_default_test_helper_strategy(strategy)
|
14
|
+
@__test_strat = get_test_helper_strategy(strategy)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.get_test_helper_strategy(strategy)
|
18
|
+
case strategy.to_sym
|
19
|
+
when :mock then TestStrategies::Mock
|
20
|
+
when :live then TestStrategies::Live
|
21
|
+
else raise "Invalid test helper strategy: #{strategy.inspect}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|