Class: StripeMock::Instance

Constant Summary collapse

DUMMY_API_KEY =
(0...32).map { (65 + rand(26)).chr }.join.downcase
@@handlers =

Handlers are ordered by priority

[]

Constants included from RequestHandlers::Products

RequestHandlers::Products::SEARCH_FIELDS

Constants included from RequestHandlers::Prices

RequestHandlers::Prices::SEARCH_FIELDS

Constants included from RequestHandlers::Invoices

RequestHandlers::Invoices::SEARCH_FIELDS

Constants included from RequestHandlers::Customers

RequestHandlers::Customers::SEARCH_FIELDS

Constants included from RequestHandlers::Subscriptions

RequestHandlers::Subscriptions::SEARCH_FIELDS

Constants included from RequestHandlers::Charges

RequestHandlers::Charges::SEARCH_FIELDS

Constants included from RequestHandlers::Accounts

RequestHandlers::Accounts::VALID_START_YEAR

Constants included from RequestHandlers::SetupIntents

RequestHandlers::SetupIntents::ALLOWED_PARAMS

Constants included from RequestHandlers::PaymentMethods

RequestHandlers::PaymentMethods::ALLOWED_PARAMS

Constants included from RequestHandlers::PaymentIntents

RequestHandlers::PaymentIntents::ALLOWED_PARAMS, RequestHandlers::PaymentIntents::SEARCH_FIELDS

Constants included from RequestHandlers::ParamValidators

RequestHandlers::ParamValidators::SUPPORTED_CURRENCIES, RequestHandlers::ParamValidators::SUPPORTED_PLAN_INTERVALS

Constants included from RequestHandlers::Helpers

RequestHandlers::Helpers::QUERYSTRING_PATTERN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestHandlers::Checkout::Session

#get_checkout_session, included, #list_checkout_sessions, #list_line_items, #new_session

Methods included from RequestHandlers::TaxRates

#get_tax_rate, included, #list_tax_rates, #new_tax_rate, #update_tax_rate

Methods included from RequestHandlers::EphemeralKey

#create_ephemeral_key, included

Methods included from RequestHandlers::Payouts

#get_payout, included, #list_payouts, #new_payout

Methods included from RequestHandlers::CountrySpec

included, #retrieve_country_spec

Methods included from RequestHandlers::Tokens

#create_token, #get_token, included

Methods included from RequestHandlers::Transfers

#cancel_transfer, #get_all_transfers, #get_transfer, included, #new_transfer

Methods included from RequestHandlers::Recipients

#get_recipient, included, #new_recipient, #update_recipient

Methods included from RequestHandlers::Refunds

#get_refund, #get_refunds, included, #new_refund, #update_refund

Methods included from RequestHandlers::PromotionCodes

#get_promotion_code, included, #list_promotion_code, #new_promotion_code, #update_promotion_code

Methods included from RequestHandlers::Products

#create_product, #destroy_product, included, #list_products, #retrieve_product, #search_products, #update_product

Methods included from RequestHandlers::Prices

#get_price, included, #list_prices, #new_price, #search_prices, #update_price

Methods included from RequestHandlers::Plans

#delete_plan, #get_plan, included, #list_plans, #new_plan, #update_plan

Methods included from RequestHandlers::Orders

#get_order, included, #list_orders, #new_order, #pay_order, #update_order

Methods included from RequestHandlers::InvoiceItems

#delete_invoice_item, #get_invoice_item, included, #list_invoice_items, #new_invoice_item, #update_invoice_item

Methods included from RequestHandlers::Invoices

#get_invoice, #get_invoice_line_items, included, #list_invoices, #new_invoice, #pay_invoice, #search_invoices, #upcoming_invoice, #update_invoice

Methods included from RequestHandlers::Events

included, #list_events, #retrieve_event

Methods included from RequestHandlers::Disputes

#close_dispute, #get_dispute, included, #list_disputes, #update_dispute

Methods included from RequestHandlers::Coupons

#delete_coupon, #get_coupon, included, #list_coupons, #new_coupon

Methods included from RequestHandlers::Customers

#delete_customer, #delete_customer_discount, #get_customer, included, #list_customers, #new_customer, #search_customers, #update_customer

Methods included from RequestHandlers::SubscriptionItems

#create_subscription_items, included, #retrieve_subscription_items, #update_subscription_item

Methods included from RequestHandlers::Subscriptions

#cancel_subscription, #create_customer_subscription, #create_subscription, included, #retrieve_customer_subscription, #retrieve_customer_subscriptions, #retrieve_subscription, #retrieve_subscriptions, #search_subscriptions, #update_subscription

Methods included from RequestHandlers::Sources

#create_source, #delete_source, included, #retrieve_source, #retrieve_sources, #update_source, #verify_source

Methods included from RequestHandlers::Cards

#create_recipient_card, #delete_recipient_card, included, #retrieve_recipient_card, #retrieve_recipient_cards

Methods included from RequestHandlers::Charges

#capture_charge, #get_charge, #get_charges, included, #new_charge, #refund_charge, #search_charges, #update_charge

Methods included from RequestHandlers::BalanceTransactions

#get_balance_transaction, included, #list_balance_transactions

Methods included from RequestHandlers::Balance

#get_balance, included

Methods included from RequestHandlers::Accounts

#deauthorize, #delete_account, #get_account, included, #list_accounts, #new_account, #update_account

Methods included from RequestHandlers::ExpressLoginLinks

included, #new_account_login_link

Methods included from RequestHandlers::AccountLinks

included, #new_account_link

Methods included from RequestHandlers::ExternalAccounts

#create_external_account, #delete_external_account, included, #retrieve_external_account, #retrieve_external_accounts, #update_external_account, #verify_external_account

Methods included from RequestHandlers::SetupIntents

#cancel_setup_intent, #capture_setup_intent, #confirm_setup_intent, #get_setup_intent, #get_setup_intents, included, #new_setup_intent, #update_setup_intent

Methods included from RequestHandlers::PaymentMethods

#attach_payment_method, #detach_payment_method, #get_payment_method, #get_payment_methods, included, #new_payment_method, #update_payment_method

Methods included from RequestHandlers::PaymentIntents

#cancel_payment_intent, #capture_payment_intent, #confirm_payment_intent, #get_payment_intent, #get_payment_intents, included, #new_payment_intent, #search_payment_intents, #update_payment_intent

Methods included from RequestHandlers::ParamValidators

#already_exists_message, #invalid_currency_message, #invalid_integer_message, #invalid_plan_interval_message, #missing_param_message, #missing_plan_amount_message, #not_found_message, #require_param, #validate_create_plan_params, #validate_create_price_params, #validate_create_product_params, #validate_list_prices_params

Methods included from RequestHandlers::Helpers

#add_card_to, #add_card_to_object, #add_coupon_to_object, #add_external_account_to, #add_external_account_to_object, #add_refund_to_charge, #add_source_to, #add_source_to_object, #add_subscription_to_customer, #bank_from_params, #card_from_params, #custom_subscription_params, #delete_card_from, #delete_coupon_from_object, #delete_subscription_from_customer, #exact_match?, #field_value, #filter_by_timestamp, #generate_bank_token, #generate_card_token, #get_bank_by_token, #get_card, #get_card_by_token, #get_card_or_bank_by_token, #get_customer_subscription, #get_ending_time, #resolve_subscription_changes, #retrieve_object_cards, #search_results, #total_items_amount, #validate_card, #verify_bank_account, #verify_trial_end

Constructor Details

#initializeInstance

Returns a new instance of Instance.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stripe_mock/instance.rb', line 67

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 = { Stripe.api_key => {} }
  @charges = {}
  @payment_intents = {}
  @payment_methods = {}
  @setup_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 = {}
  @prices = {}
  @products = {}
  @promotion_codes = {}
  @recipients = {}
  @refunds = {}
  @transfers = {}
  @payouts = {}
  @subscriptions = {}
  @subscriptions_items = {}
  @country_spec = {}
  @tax_rates = {}
  @checkout_sessions = {}
  @checkout_session_line_items = {}

  @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

Instance Attribute Details

#account_balanceObject

Returns the value of attribute account_balance.



65
66
67
# File 'lib/stripe_mock/instance.rb', line 65

def 
  @account_balance
end

#accountsObject (readonly)

Returns the value of attribute accounts.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def accounts
  @accounts
end

#balanceObject (readonly)

Returns the value of attribute balance.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def balance
  @balance
end

#balance_transactionsObject (readonly)

Returns the value of attribute balance_transactions.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def balance_transactions
  @balance_transactions
end

#bank_tokensObject (readonly)

Returns the value of attribute bank_tokens.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def bank_tokens
  @bank_tokens
end

#chargesObject (readonly)

Returns the value of attribute charges.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def charges
  @charges
end

#checkout_session_line_itemsObject (readonly)

Returns the value of attribute checkout_session_line_items.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def checkout_session_line_items
  @checkout_session_line_items
end

#checkout_sessionsObject (readonly)

Returns the value of attribute checkout_sessions.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def checkout_sessions
  @checkout_sessions
end

#conversion_rateObject

Returns the value of attribute conversion_rate.



65
66
67
# File 'lib/stripe_mock/instance.rb', line 65

def conversion_rate
  @conversion_rate
end

#country_specObject (readonly)

Returns the value of attribute country_spec.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def country_spec
  @country_spec
end

#couponsObject (readonly)

Returns the value of attribute coupons.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def coupons
  @coupons
end

#customersObject (readonly)

Returns the value of attribute customers.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def customers
  @customers
end

#debugObject

Returns the value of attribute debug.



65
66
67
# File 'lib/stripe_mock/instance.rb', line 65

def debug
  @debug
end

#disputesObject (readonly)

Returns the value of attribute disputes.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def disputes
  @disputes
end

#error_queueObject

Returns the value of attribute error_queue.



65
66
67
# File 'lib/stripe_mock/instance.rb', line 65

def error_queue
  @error_queue
end

#eventsObject (readonly)

Returns the value of attribute events.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def events
  @events
end

#invoice_itemsObject (readonly)

Returns the value of attribute invoice_items.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def invoice_items
  @invoice_items
end

#invoicesObject (readonly)

Returns the value of attribute invoices.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def invoices
  @invoices
end

#ordersObject (readonly)

Returns the value of attribute orders.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def orders
  @orders
end

#payment_intentsObject (readonly)

Returns the value of attribute payment_intents.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def payment_intents
  @payment_intents
end

#payment_methodsObject (readonly)

Returns the value of attribute payment_methods.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def payment_methods
  @payment_methods
end

#payoutsObject (readonly)

Returns the value of attribute payouts.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def payouts
  @payouts
end

#plansObject (readonly)

Returns the value of attribute plans.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def plans
  @plans
end

#pricesObject (readonly)

Returns the value of attribute prices.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def prices
  @prices
end

#productsObject (readonly)

Returns the value of attribute products.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def products
  @products
end

#promotion_codesObject (readonly)

Returns the value of attribute promotion_codes.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def promotion_codes
  @promotion_codes
end

#recipientsObject (readonly)

Returns the value of attribute recipients.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def recipients
  @recipients
end

#refundsObject (readonly)

Returns the value of attribute refunds.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def refunds
  @refunds
end

#setup_intentsObject (readonly)

Returns the value of attribute setup_intents.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def setup_intents
  @setup_intents
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def subscriptions
  @subscriptions
end

#subscriptions_itemsObject (readonly)

Returns the value of attribute subscriptions_items.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def subscriptions_items
  @subscriptions_items
end

#tax_ratesObject (readonly)

Returns the value of attribute tax_rates.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def tax_rates
  @tax_rates
end

#transfersObject (readonly)

Returns the value of attribute transfers.



59
60
61
# File 'lib/stripe_mock/instance.rb', line 59

def transfers
  @transfers
end

Class Method Details

.add_handler(route, name) ⇒ Object



12
13
14
15
16
17
# 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) ⇒ Object



19
20
21
# File 'lib/stripe_mock/instance.rb', line 19

def self.handler_for_method_url(method_url)
  @@handlers.find {|h| method_url =~ h[:route] }
end

Instance Method Details

#generate_webhook_event(event_data) ⇒ Object



144
145
146
147
# File 'lib/stripe_mock/instance.rb', line 144

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, usage: [], params: {}, headers: {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/stripe_mock/instance.rb', line 112

def mock_request(method, url, api_key: nil, api_base: nil, usage: [], 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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/stripe_mock/instance.rb', line 149

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