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_object(type, card, object, replace_current = false) ⇒ Object
- #add_refund_to_charge(refund, charge) ⇒ Object
- #add_subscription_to_customer(cus, sub) ⇒ Object
- #custom_subscription_params(plan, cus, options = {}) ⇒ 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.
- #verify_trial_end(trial_end) ⇒ Object
Instance Method Details
#add_card_to_object(type, card, object, replace_current = false) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 14 def add_card_to_object(type, card, object, replace_current=false) card[type] = object[:id] if replace_current object[:cards][:data].delete_if {|card| card[:id] == object[:default_card]} object[:default_card] = card[:id] else object[:cards][:count] += 1 end object[:default_card] = card[:id] unless object[:default_card] object[:cards][: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][:count] = (cus[:subscriptions][:count] || 0) + 1 cus[:subscriptions][:data].unshift sub 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/} # 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_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][: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 |
# File 'lib/stripe_mock/request_handlers/helpers/card_helpers.rb', line 5 def get_card(object, card_id, class_name='Customer') card = object[: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 |
#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 |