Module: StripeMock::RequestHandlers::Helpers
- Included in:
- Instance
- Defined in:
- 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/subscription_helpers.rb
Instance Method Summary collapse
- #add_card_to(type, type_id, params, objects) ⇒ Object
- #add_card_to_object(type, card, object, replace_current = false) ⇒ Object
- #add_refund_to_charge(refund, charge) ⇒ Object
- #add_subscription_to_customer(cus, sub) ⇒ Object
- #card_from_params(attrs_or_token) ⇒ Object
- #custom_subscription_params(plan, cus, options = {}) ⇒ Object
- #delete_card_from(type, type_id, card_id, objects) ⇒ Object
- #delete_subscription_from_customer(cus, subscription) ⇒ Object
- #generate_bank_token(bank_params) ⇒ Object
- #generate_card_token(card_params) ⇒ Object
- #get_bank_by_token(token) ⇒ Object
- #get_card(object, card_id, class_name = 'Customer') ⇒ Object
- #get_card_by_token(token) ⇒ Object
- #get_customer_subscription(customer, sub_id) ⇒ Object
-
#get_ending_time(start_time, plan, intervals = 1) ⇒ Object
‘intervals` is set to 1 when calculating current_period_end from current_period_start & plan `intervals` is set to 2 when calculating Stripe::Invoice.upcoming end from current_period_start & plan.
- #retrieve_object_cards(type, type_id, objects) ⇒ Object
- #validate_card(card) ⇒ Object
- #verify_trial_end(trial_end) ⇒ Object
Instance Method Details
#add_card_to(type, type_id, params, objects) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 61 def add_card_to(type, type_id, params, objects) resource = assert_existence type, type_id, objects[type_id] card = card_from_params(params[:card] || params[:source]) add_card_to_object(type, card, resource) end |
#add_card_to_object(type, card, object, replace_current = false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 15 def add_card_to_object(type, card, object, replace_current=false) card[type] = object[:id] cards_or_sources = object[:cards] || object[:sources] is_customer = object.has_key?(:sources) if replace_current cards_or_sources[:data].delete_if {|card| card[:id] == object[:default_card]} object[:default_card] = card[:id] unless is_customer object[:default_source] = card[:id] if is_customer else cards_or_sources[:total_count] += 1 end object[:default_card] = card[:id] if !is_customer && object[:default_card].nil? object[:default_source] = card[:id] if is_customer && object[:default_source].nil? cards_or_sources[:data] << card card end |
#add_refund_to_charge(refund, charge) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/stripe_mock/request_handlers/helpers/charge_helpers.rb', line 5 def add_refund_to_charge(refund, charge) refunds = charge[:refunds] refunds[:data] << refund refunds[:total_count] = refunds[:data].count charge[:amount_refunded] = refunds[:data].reduce(0) {|sum, r| sum + r[:amount].to_i } charge[:refunded] = true end |
#add_subscription_to_customer(cus, sub) ⇒ Object
28 29 30 31 |
# File 'lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb', line 28 def add_subscription_to_customer(cus, sub) cus[:subscriptions][:total_count] = (cus[:subscriptions][:total_count] || 0) + 1 cus[:subscriptions][:data].unshift sub end |
#card_from_params(attrs_or_token) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 75 def card_from_params(attrs_or_token) if attrs_or_token.is_a? Hash attrs_or_token = generate_card_token(attrs_or_token) end card = get_card_by_token(attrs_or_token) validate_card(card) end |
#custom_subscription_params(plan, cus, options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb', line 9 def custom_subscription_params(plan, cus, = {}) verify_trial_end([:trial_end]) if [:trial_end] start_time = [:current_period_start] || Time.now.utc.to_i params = { plan: plan, customer: cus[:id], current_period_start: start_time } params.merge! .select {|k,v| k =~ /application_fee_percent|quantity|metadata|tax_percent/} # TODO: Implement coupon logic if (plan[:trial_period_days].nil? && [:trial_end].nil?) || [:trial_end] == "now" end_time = get_ending_time(start_time, plan) params.merge!({status: 'active', current_period_end: end_time, trial_start: nil, trial_end: nil}) else end_time = [:trial_end] || (Time.now.utc.to_i + plan[:trial_period_days]*86400) params.merge!({status: 'trialing', current_period_end: end_time, trial_start: start_time, trial_end: end_time}) end params end |
#delete_card_from(type, type_id, card_id, objects) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 43 def delete_card_from(type, type_id, card_id, objects) resource = assert_existence type, type_id, objects[type_id] assert_existence :card, card_id, get_card(resource, card_id) card = { id: card_id, deleted: true } cards_or_sources = resource[:cards] || resource[:sources] cards_or_sources[:data].reject!{|cc| cc[:id] == card[:id] } is_customer = resource.has_key?(:sources) new_default = cards_or_sources[:data].count > 0 ? cards_or_sources[:data].first[:id] : nil resource[:default_card] = new_default unless is_customer resource[:default_source] = new_default if is_customer card end |
#delete_subscription_from_customer(cus, subscription) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb', line 33 def delete_subscription_from_customer(cus, subscription) cus[:subscriptions][:data].reject!{|sub| sub[:id] == subscription[:id] } cus[:subscriptions][:total_count] -=1 end |
#generate_bank_token(bank_params) ⇒ Object
5 6 7 8 9 |
# File 'lib/stripe_mock/request_handlers/helpers/token_helpers.rb', line 5 def generate_bank_token(bank_params) token = new_id 'btok' @bank_tokens[token] = Data.mock_bank_account bank_params token end |
#generate_card_token(card_params) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/stripe_mock/request_handlers/helpers/token_helpers.rb', line 11 def generate_card_token(card_params) token = new_id 'tok' card_params[:id] = new_id 'cc' @card_tokens[token] = Data.mock_card symbolize_names(card_params) token end |
#get_bank_by_token(token) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/stripe_mock/request_handlers/helpers/token_helpers.rb', line 18 def get_bank_by_token(token) if token.nil? || @bank_tokens[token].nil? Data.mock_bank_account else @bank_tokens.delete(token) end end |
#get_card(object, card_id, class_name = 'Customer') ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 5 def get_card(object, card_id, class_name='Customer') cards = object[:cards] || object[:sources] card = cards[:data].find{|cc| cc[:id] == card_id } if card.nil? msg = "#{class_name} #{object[:id]} does not have card #{card_id}" raise Stripe::InvalidRequestError.new(msg, 'card', 404) end card end |
#get_card_by_token(token) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/stripe_mock/request_handlers/helpers/token_helpers.rb', line 26 def get_card_by_token(token) if token.nil? || @card_tokens[token].nil? # TODO: Make this strict msg = "Invalid token id: #{token}" raise Stripe::InvalidRequestError.new(msg, 'tok', 404) else @card_tokens.delete(token) end end |
#get_customer_subscription(customer, sub_id) ⇒ Object
5 6 7 |
# File 'lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb', line 5 def get_customer_subscription(customer, sub_id) customer[:subscriptions][:data].find{|sub| sub[:id] == sub_id } end |
#get_ending_time(start_time, plan, intervals = 1) ⇒ Object
‘intervals` is set to 1 when calculating current_period_end from current_period_start & plan `intervals` is set to 2 when calculating Stripe::Invoice.upcoming end from current_period_start & plan
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb', line 42 def get_ending_time(start_time, plan, intervals = 1) case plan[:interval] when "week" start_time + (604800 * (plan[:interval_count] || 1) * intervals) when "month" (Time.at(start_time).to_datetime >> ((plan[:interval_count] || 1) * intervals)).to_time.to_i when "year" (Time.at(start_time).to_datetime >> (12 * intervals)).to_time.to_i # max period is 1 year else start_time end end |
#retrieve_object_cards(type, type_id, objects) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 36 def retrieve_object_cards(type, type_id, objects) resource = assert_existence type, type_id, objects[type_id] cards = resource[:cards] || resource[:sources] Data.mock_list_object(cards[:data]) end |
#validate_card(card) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 68 def validate_card(card) [:exp_month, :exp_year].each do |field| card[field] = card[field].to_i end card end |
#verify_trial_end(trial_end) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb', line 55 def verify_trial_end(trial_end) if trial_end != "now" if !trial_end.is_a? Integer raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer', nil, 400) elsif trial_end < Time.now.utc.to_i raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer Unix timestamp in the future', nil, 400) elsif trial_end > Time.now.utc.to_i + 31557600*5 # five years raise Stripe::InvalidRequestError.new('Invalid timestamp: can be no more than five years in the future', nil, 400) end end end |