Module: StripeMock
- Defined in:
- lib/stripe_mock.rb,
lib/stripe_mock/data.rb,
lib/stripe_mock/util.rb,
lib/stripe_mock/client.rb,
lib/stripe_mock/server.rb,
lib/stripe_mock/version.rb,
lib/stripe_mock/api/live.rb,
lib/stripe_mock/instance.rb,
lib/stripe_mock/api/debug.rb,
lib/stripe_mock/data/list.rb,
lib/stripe_mock/api/client.rb,
lib/stripe_mock/api/errors.rb,
lib/stripe_mock/api/server.rb,
lib/stripe_mock/error_queue.rb,
lib/stripe_mock/api/instance.rb,
lib/stripe_mock/api/webhooks.rb,
lib/stripe_mock/api/bank_tokens.rb,
lib/stripe_mock/api/card_tokens.rb,
lib/stripe_mock/api/test_helpers.rb,
lib/stripe_mock/api/account_balance.rb,
lib/stripe_mock/api/conversion_rate.rb,
lib/stripe_mock/api/global_id_prefix.rb,
lib/stripe_mock/test_strategies/base.rb,
lib/stripe_mock/test_strategies/live.rb,
lib/stripe_mock/test_strategies/mock.rb,
lib/stripe_mock/request_handlers/cards.rb,
lib/stripe_mock/request_handlers/plans.rb,
lib/stripe_mock/request_handlers/events.rb,
lib/stripe_mock/request_handlers/orders.rb,
lib/stripe_mock/request_handlers/prices.rb,
lib/stripe_mock/request_handlers/tokens.rb,
lib/stripe_mock/errors/stripe_mock_error.rb,
lib/stripe_mock/request_handlers/balance.rb,
lib/stripe_mock/request_handlers/charges.rb,
lib/stripe_mock/request_handlers/coupons.rb,
lib/stripe_mock/request_handlers/payouts.rb,
lib/stripe_mock/request_handlers/refunds.rb,
lib/stripe_mock/request_handlers/sources.rb,
lib/stripe_mock/request_handlers/accounts.rb,
lib/stripe_mock/request_handlers/disputes.rb,
lib/stripe_mock/request_handlers/invoices.rb,
lib/stripe_mock/request_handlers/products.rb,
lib/stripe_mock/request_handlers/customers.rb,
lib/stripe_mock/request_handlers/tax_rates.rb,
lib/stripe_mock/request_handlers/transfers.rb,
lib/stripe_mock/errors/server_timeout_error.rb,
lib/stripe_mock/request_handlers/recipients.rb,
lib/stripe_mock/errors/unstarted_state_error.rb,
lib/stripe_mock/request_handlers/country_spec.rb,
lib/stripe_mock/request_handlers/account_links.rb,
lib/stripe_mock/request_handlers/ephemeral_key.rb,
lib/stripe_mock/request_handlers/invoice_items.rb,
lib/stripe_mock/request_handlers/setup_intents.rb,
lib/stripe_mock/request_handlers/subscriptions.rb,
lib/stripe_mock/errors/unsupported_request_error.rb,
lib/stripe_mock/request_handlers/payment_intents.rb,
lib/stripe_mock/request_handlers/payment_methods.rb,
lib/stripe_mock/request_handlers/promotion_codes.rb,
lib/stripe_mock/request_handlers/checkout_session.rb,
lib/stripe_mock/request_handlers/external_accounts.rb,
lib/stripe_mock/errors/uninitialized_instance_error.rb,
lib/stripe_mock/request_handlers/subscription_items.rb,
lib/stripe_mock/request_handlers/express_login_links.rb,
lib/stripe_mock/errors/closed_client_connection_error.rb,
lib/stripe_mock/request_handlers/balance_transactions.rb,
lib/stripe_mock/request_handlers/helpers/card_helpers.rb,
lib/stripe_mock/request_handlers/helpers/token_helpers.rb,
lib/stripe_mock/request_handlers/helpers/charge_helpers.rb,
lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb,
lib/stripe_mock/request_handlers/helpers/search_helpers.rb,
lib/stripe_mock/request_handlers/validators/param_validators.rb,
lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb,
lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb,
lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb
Defined Under Namespace
Modules: CardErrors, Data, RequestHandlers, TestStrategies, Util, Webhooks
Classes: Client, ClosedClientConnectionError, ErrorQueue, Instance, Server, ServerTimeoutError, StripeMockError, UninitializedInstanceError, UnstartedStateError, UnsupportedRequestError
Constant Summary
collapse
- VERSION =
"4.0.0"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.checkout_base ⇒ Object
Returns the value of attribute checkout_base.
101
102
103
|
# File 'lib/stripe_mock.rb', line 101
def checkout_base
@checkout_base
end
|
.default_currency ⇒ Object
Returns the value of attribute default_currency.
100
101
102
|
# File 'lib/stripe_mock.rb', line 100
def default_currency
@default_currency
end
|
.default_server_log_path=(value) ⇒ Object
Sets the attribute default_server_log_path
6
7
8
|
# File 'lib/stripe_mock/api/server.rb', line 6
def default_server_log_path=(value)
@default_server_log_path = value
end
|
.default_server_pid_path=(value) ⇒ Object
Sets the attribute default_server_pid_path
6
7
8
|
# File 'lib/stripe_mock/api/server.rb', line 6
def default_server_pid_path=(value)
@default_server_pid_path = value
end
|
.webhook_fixture_path ⇒ Object
Returns the value of attribute webhook_fixture_path.
102
103
104
|
# File 'lib/stripe_mock.rb', line 102
def webhook_fixture_path
@webhook_fixture_path
end
|
Class Method Details
.client ⇒ Object
3
4
5
|
# File 'lib/stripe_mock/api/client.rb', line 3
def self.client
@client
end
|
.create_test_helper(strategy = nil) ⇒ Object
3
4
5
6
7
8
9
10
11
|
# File 'lib/stripe_mock/api/test_helpers.rb', line 3
def self.create_test_helper(strategy=nil)
if strategy
get_test_helper_strategy(strategy).new
elsif @__test_strat
@__test_strat.new
else
TestStrategies::Mock.new
end
end
|
.generate_bank_token(bank_params = {}) ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/stripe_mock/api/bank_tokens.rb', line 3
def self.generate_bank_token(bank_params = {})
case @state
when 'local'
instance.generate_bank_token(bank_params)
when 'remote'
client.generate_bank_token(bank_params)
else
raise UnstartedStateError
end
end
|
.generate_card_token(card_params = {}) ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/stripe_mock/api/card_tokens.rb', line 3
def self.generate_card_token(card_params = {})
case @state
when 'local'
instance.generate_card_token(card_params)
when 'remote'
client.generate_card_token(card_params)
else
raise UnstartedStateError
end
end
|
.get_test_helper_strategy(strategy) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/stripe_mock/api/test_helpers.rb', line 17
def self.get_test_helper_strategy(strategy)
case strategy.to_sym
when :mock then TestStrategies::Mock
when :live then TestStrategies::Live
else raise "Invalid test helper strategy: #{strategy.inspect}"
end
end
|
.global_id_prefix ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
|
# File 'lib/stripe_mock/api/global_id_prefix.rb', line 3
def self.global_id_prefix
if StripeMock.client
StripeMock.client.server_global_id_prefix
else
case @global_id_prefix
when false then ""
when nil then "test_"
else @global_id_prefix
end
end
end
|
.global_id_prefix=(value) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/stripe_mock/api/global_id_prefix.rb', line 15
def self.global_id_prefix=(value)
if StripeMock.client
StripeMock.client.set_server_global_id_prefix(value)
else
@global_id_prefix = value
end
end
|
.instance ⇒ Object
35
|
# File 'lib/stripe_mock/api/instance.rb', line 35
def self.instance; @instance; end
|
.kill_server(pid_path = nil) ⇒ Object
33
34
35
36
37
|
# File 'lib/stripe_mock/api/server.rb', line 33
def kill_server(pid_path=nil)
puts "Killing server at #{pid_path}"
path = pid_path || default_server_pid_path
Dante::Runner.new('stripe-mock-server').execute(:kill => true, :pid_path => path)
end
|
.mock(&block) ⇒ Object
Yield the given block between StripeMock.start and StripeMock.stop
22
23
24
25
26
27
28
29
|
# File 'lib/stripe_mock/api/instance.rb', line 22
def self.mock(&block)
begin
self.start
yield
ensure
self.stop
end
end
|
.mock_webhook_event(type, params = {}) ⇒ Object
33
34
35
|
# File 'lib/stripe_mock/api/webhooks.rb', line 33
def self.mock_webhook_event(type, params={})
Stripe::Event.construct_from(mock_webhook_payload(type, params))
end
|
.mock_webhook_payload(type, params = {}) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/stripe_mock/api/webhooks.rb', line 3
def self.mock_webhook_payload(type, params = {})
fixture_file = File.join(@webhook_fixture_path, "#{type}.json")
unless File.exist?(fixture_file)
unless Webhooks.event_list.include?(type)
raise UnsupportedRequestError.new "Unsupported webhook event `#{type}` (Searched in #{@webhook_fixture_path})"
end
fixture_file = File.join(@webhook_fixture_fallback_path, "#{type}.json")
end
json = MultiJson.load File.read(fixture_file)
json = Stripe::Util.symbolize_names(json)
params = Stripe::Util.symbolize_names(params)
json[:account] = params.delete(:account) if params.key?(:account)
json[:data][:object] = Util.rmerge(json[:data][:object], params)
json.delete(:id)
json[:created] = params[:created] || Time.now.to_i
if @state == 'local'
event_data = instance.generate_webhook_event(json)
elsif @state == 'remote'
event_data = client.generate_webhook_event(json)
else
raise UnstartedStateError
end
event_data
end
|
.prepare_card_error(code, *handler_names) ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/stripe_mock/api/errors.rb', line 14
def self.prepare_card_error(code, *handler_names)
handler_names.push(:new_charge) if handler_names.count == 0
error = CardErrors.build_error_for(code)
if error.nil?
raise StripeMockError, "Unrecognized stripe card error code: #{code}"
end
prepare_error error, *handler_names
end
|
.prepare_error(stripe_error, *handler_names) ⇒ Object
2
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/stripe_mock/api/errors.rb', line 2
def self.prepare_error(stripe_error, *handler_names)
handler_names.push(:all) if handler_names.count == 0
if @state == 'local'
instance
elsif @state == 'remote'
client
else
raise UnstartedStateError
end.error_queue.queue stripe_error, handler_names
end
|
.restore_stripe_execute_request_method ⇒ Object
31
32
33
|
# File 'lib/stripe_mock/api/instance.rb', line 31
def self.restore_stripe_execute_request_method
Stripe::StripeClient.send(:define_method, :execute_request, @original_execute_request_method)
end
|
.set_account_balance(value) ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/stripe_mock/api/account_balance.rb', line 3
def self.set_account_balance(value)
case @state
when 'local'
instance.account_balance = value
when 'remote'
client.set_account_balance(value)
else
raise UnstartedStateError
end
end
|
.set_conversion_rate(value) ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/stripe_mock/api/conversion_rate.rb', line 3
def self.set_conversion_rate(value)
case @state
when 'local'
instance.conversion_rate = value
when 'remote'
client.set_conversion_rate(value)
else
raise UnstartedStateError
end
end
|
.set_default_test_helper_strategy(strategy) ⇒ Object
13
14
15
|
# File 'lib/stripe_mock/api/test_helpers.rb', line 13
def self.set_default_test_helper_strategy(strategy)
@__test_strat = get_test_helper_strategy(strategy)
end
|
.spawn_server(opts = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/stripe_mock/api/server.rb', line 14
def spawn_server(opts={})
pid_path = opts[:pid_path] || default_server_pid_path
log_path = opts[:log_path] || default_server_log_path
Dante::Runner.new('stripe-mock-server').execute(
:daemonize => true, :pid_path => pid_path, :log_path => log_path
){
StripeMock::Server.start_new(opts)
}
at_exit {
begin
e = $! kill_server(pid_path)
ensure
raise e if $! != e
end
}
end
|
.start ⇒ Object
7
8
9
10
11
12
|
# File 'lib/stripe_mock/api/instance.rb', line 7
def self.start
return false if @state == 'live'
@instance = instance = Instance.new
Stripe::StripeClient.send(:define_method, :execute_request) { |*args, **keyword_args| instance.mock_request(*args, **keyword_args) }
@state = 'local'
end
|
.start_client(port = 4999) ⇒ Object
7
8
9
10
11
12
13
14
15
|
# File 'lib/stripe_mock/api/client.rb', line 7
def self.start_client(port=4999)
return false if @state == 'live'
return @client unless @client.nil?
Stripe::StripeClient.send(:define_method, :execute_request) { |*args, **keyword_args| StripeMock.redirect_to_mock_server(*args, **keyword_args) }
@client = StripeMock::Client.new(port)
@state = 'remote'
@client
end
|
.state ⇒ Object
36
|
# File 'lib/stripe_mock/api/instance.rb', line 36
def self.state; @state; end
|
.stop ⇒ Object
14
15
16
17
18
19
|
# File 'lib/stripe_mock/api/instance.rb', line 14
def self.stop
return unless @state == 'local'
restore_stripe_execute_request_method
@instance = nil
@state = 'ready'
end
|
.stop_client(opts = {}) ⇒ Object
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/stripe_mock/api/client.rb', line 17
def self.stop_client(opts={})
return false unless @state == 'remote'
@state = 'ready'
restore_stripe_execute_request_method
@client.clear_server_data if opts[:clear_server_data] == true
@client.cleanup
@client = nil
true
end
|
.toggle_debug(toggle) ⇒ Object
3
4
5
6
7
8
9
|
# File 'lib/stripe_mock/api/debug.rb', line 3
def self.toggle_debug(toggle)
if @state == 'local'
@instance.debug = toggle
elsif @state == 'remote'
@client.set_server_debug(toggle)
end
end
|
.toggle_live(toggle) ⇒ Object