class StripeMock::Instance
Constants
- DUMMY_API_KEY
Attributes
account_balance[RW]
accounts[R]
balance[R]
balance_transactions[R]
bank_tokens[R]
charges[R]
conversion_rate[RW]
country_spec[R]
coupons[R]
customers[R]
debug[RW]
disputes[R]
error_queue[RW]
events[R]
invoice_items[R]
invoices[R]
orders[R]
payment_intents[R]
payment_methods[R]
payouts[R]
plans[R]
products[R]
recipients[R]
refunds[R]
subscriptions[R]
subscriptions_items[R]
tax_rates[R]
transfers[R]
Public Class Methods
add_handler(route, name)
click to toggle source
# File lib/stripe_mock/instance.rb, line 12 def self.add_handler(route, name) @@handlers << { :route => %r{^#{route}$}, :name => name } end
handler_for_method_url(method_url)
click to toggle source
# File lib/stripe_mock/instance.rb, line 19 def self.handler_for_method_url(method_url) @@handlers.find {|h| method_url =~ h[:route] } end
new()
click to toggle source
# File lib/stripe_mock/instance.rb, line 59 def initialize @accounts = {} @balance = Data.mock_balance @balance_transactions = Data.mock_balance_transactions(['txn_05RsQX2eZvKYlo2C0FRTGSSA','txn_15RsQX2eZvKYlo2C0ERTYUIA', 'txn_25RsQX2eZvKYlo2C0ZXCVBNM', 'txn_35RsQX2eZvKYlo2C0QAZXSWE', 'txn_45RsQX2eZvKYlo2C0EDCVFRT', 'txn_55RsQX2eZvKYlo2C0OIKLJUY', 'txn_65RsQX2eZvKYlo2C0ASDFGHJ', 'txn_75RsQX2eZvKYlo2C0EDCXSWQ', 'txn_85RsQX2eZvKYlo2C0UJMCDET', 'txn_95RsQX2eZvKYlo2C0EDFRYUI']) @bank_tokens = {} @card_tokens = {} @customers = {} @charges = {} @payment_intents = {} @coupons = {} @disputes = Data.mock_disputes(['dp_05RsQX2eZvKYlo2C0FRTGSSA','dp_15RsQX2eZvKYlo2C0ERTYUIA', 'dp_25RsQX2eZvKYlo2C0ZXCVBNM', 'dp_35RsQX2eZvKYlo2C0QAZXSWE', 'dp_45RsQX2eZvKYlo2C0EDCVFRT', 'dp_55RsQX2eZvKYlo2C0OIKLJUY', 'dp_65RsQX2eZvKYlo2C0ASDFGHJ', 'dp_75RsQX2eZvKYlo2C0EDCXSWQ', 'dp_85RsQX2eZvKYlo2C0UJMCDET', 'dp_95RsQX2eZvKYlo2C0EDFRYUI']) @events = {} @invoices = {} @invoice_items = {} @orders = {} @payment_methods = {} @plans = {} @products = {} @recipients = {} @refunds = {} @transfers = {} @payouts = {} @subscriptions = {} @subscriptions_items = {} @country_spec = {} @tax_rates = {} @debug = false @error_queue = ErrorQueue.new @id_counter = 0 @balance_transaction_counter = 0 @dispute_counter = 0 @conversion_rate = 1.0 @account_balance = 10000 # This is basically a cache for ParamValidators @base_strategy = TestStrategies::Base.new end
Public Instance Methods
generate_webhook_event(event_data)
click to toggle source
# File lib/stripe_mock/instance.rb, line 130 def generate_webhook_event(event_data) event_data[:id] ||= new_id 'evt' @events[ event_data[:id] ] = symbolize_names(event_data) end
mock_request(method, url, api_key: nil, api_base: nil, params: {}, headers: {})
click to toggle source
# File lib/stripe_mock/instance.rb, line 98 def mock_request(method, url, api_key: nil, api_base: nil, params: {}, headers: {}) return {} if method == :xtest api_key ||= (Stripe.api_key || DUMMY_API_KEY) # Ensure params hash has symbols as keys params = Stripe::Util.symbolize_names(params) method_url = "#{method} #{url}" if handler = Instance.handler_for_method_url(method_url) if @debug == true puts "- - - - " * 8 puts "[StripeMock req]::#{handler[:name]} #{method} #{url}" puts " #{params}" end if mock_error = @error_queue.error_for_handler_name(handler[:name]) @error_queue.dequeue raise mock_error else res = self.send(handler[:name], handler[:route], method_url, params, headers) puts " [res] #{res}" if @debug == true [to_faraday_hash(res), api_key] end else puts "[StripeMock] Warning : Unrecognized endpoint + method : [#{method} #{url}]" puts "[StripeMock] params: #{params}" unless params.empty? [{}, api_key] end end
upsert_stripe_object(object, attributes)
click to toggle source
# File lib/stripe_mock/instance.rb, line 135 def upsert_stripe_object(object, attributes) # Most Stripe entities can be created via the API. However, some entities are created when other Stripe entities are # created - such as when BalanceTransactions are created when Charges are created. This method provides the ability # to create these internal entities. # It also provides the ability to modify existing Stripe entities. id = attributes[:id] if id.nil? || id == "" # Insert new Stripe object case object when :balance_transaction id = new_balance_transaction('txn', attributes) when :dispute id = new_dispute('dp', attributes) else raise UnsupportedRequestError.new "Unsupported stripe object `#{object}`" end else # Update existing Stripe object case object when :balance_transaction btxn = assert_existence :balance_transaction, id, @balance_transactions[id] btxn.merge!(attributes) when :dispute dispute = assert_existence :dispute, id, @disputes[id] dispute.merge!(attributes) else raise UnsupportedRequestError.new "Unsupported stripe object `#{object}`" end end id end
Private Instance Methods
assert_existence(type, id, obj, message=nil)
click to toggle source
# File lib/stripe_mock/instance.rb, line 169 def assert_existence(type, id, obj, message=nil) if obj.nil? msg = message || "No such #{type}: #{id}" raise Stripe::InvalidRequestError.new(msg, type.to_s, http_status: 404) end obj end
calculate_fees(params)
click to toggle source
# File lib/stripe_mock/instance.rb, line 211 def calculate_fees(params) application_fee = params[:application_fee] || 0 params[:fee] = processing_fee(params[:amount]) + application_fee params[:fee_details] = [ { amount: processing_fee(params[:amount]), application: nil, currency: params[:currency] || StripeMock.default_currency, description: "Stripe processing fees", type: "stripe_fee" } ] if application_fee params[:fee_details] << { amount: application_fee, currency: params[:currency] || StripeMock.default_currency, description: "Application fee", type: "application_fee" } end end
new_balance_transaction(prefix, params = {})
click to toggle source
# File lib/stripe_mock/instance.rb, line 182 def new_balance_transaction(prefix, params = {}) # balance transaction ids must be strings id = "#{StripeMock.global_id_prefix}#{prefix}_#{@balance_transaction_counter += 1}" amount = params[:amount] unless amount.nil? # Fee calculation calculate_fees(params) unless params[:fee] params[:net] = amount - params[:fee] params[:amount] = amount * @conversion_rate end @balance_transactions[id] = Data.mock_balance_transaction(params.merge(id: id)) id end
new_dispute(prefix, params = {})
click to toggle source
# File lib/stripe_mock/instance.rb, line 196 def new_dispute(prefix, params = {}) id = "#{StripeMock.global_id_prefix}#{prefix}_#{@dispute_counter += 1}" @disputes[id] = Data.mock_dispute(params.merge(id: id)) id end
new_id(prefix)
click to toggle source
# File lib/stripe_mock/instance.rb, line 177 def new_id(prefix) # Stripe ids must be strings "#{StripeMock.global_id_prefix}#{prefix}_#{@id_counter += 1}" end
processing_fee(amount)
click to toggle source
# File lib/stripe_mock/instance.rb, line 233 def processing_fee(amount) (30 + (amount.abs * 0.029).ceil) * (amount > 0 ? 1 : -1) end
symbolize_names(hash)
click to toggle source
# File lib/stripe_mock/instance.rb, line 202 def symbolize_names(hash) Stripe::Util.symbolize_names(hash) end
to_faraday_hash(hash)
click to toggle source
# File lib/stripe_mock/instance.rb, line 206 def to_faraday_hash(hash) response = Struct.new(:data) response.new(hash) end