Module: StripeMock::RequestHandlers::Customers

Included in:
Instance
Defined in:
lib/stripe_mock/request_handlers/customers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/stripe_mock/request_handlers/customers.rb', line 5

def Customers.included(klass)
  klass.add_handler 'post /v1/customers',                     :new_customer
  klass.add_handler 'post /v1/customers/(.*)/subscription',   :update_subscription
  klass.add_handler 'delete /v1/customers/(.*)/subscription', :cancel_subscription
  klass.add_handler 'post /v1/customers/(.*)',                :update_customer
  klass.add_handler 'get /v1/customers/(.*)',                 :get_customer
  klass.add_handler 'delete /v1/customers/(.*)',              :delete_customer
  klass.add_handler 'get /v1/customers',                      :list_customers
end

Instance Method Details

#cancel_subscription(route, method_url, params, headers) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/stripe_mock/request_handlers/customers.rb', line 43

def cancel_subscription(route, method_url, params, headers)
  route =~ method_url

  customer = customers[$1]
  assert_existance :customer, $1, customer

  sub = customer[:subscription]
  assert_existance nil, nil, sub, "No active subscription for customer: #{$1}"

  customer[:subscription] = nil

  plan = plans[ sub[:plan][:id] ]
  assert_existance :plan, params[:plan], plan

  Data.mock_delete_subscription(id: sub[:id])
end

#delete_customer(route, method_url, params, headers) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/stripe_mock/request_handlers/customers.rb', line 83

def delete_customer(route, method_url, params, headers)
  route =~ method_url
  assert_existance :customer, $1, customers[$1]

  customers[$1] = {
    id: customers[$1][:id],
    deleted: true
  }

  customers[$1]
end

#get_customer(route, method_url, params, headers) ⇒ Object



95
96
97
98
99
# File 'lib/stripe_mock/request_handlers/customers.rb', line 95

def get_customer(route, method_url, params, headers)
  route =~ method_url
  assert_existance :customer, $1, customers[$1]
  customers[$1] ||= Data.mock_customer([], :id => $1)
end

#list_customers(route, method_url, params, headers) ⇒ Object



101
102
103
# File 'lib/stripe_mock/request_handlers/customers.rb', line 101

def list_customers(route, method_url, params, headers)
  customers.values
end

#new_customer(route, method_url, params, headers) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/stripe_mock/request_handlers/customers.rb', line 15

def new_customer(route, method_url, params, headers)
  params[:id] ||= new_id('cus')
  cards = []
  if params[:card]
    cards << get_card_by_token(params.delete(:card))
    params[:default_card] = cards.first[:id]
  end
  customers[ params[:id] ] = Data.mock_customer(cards, params)
end

#update_customer(route, method_url, params, headers) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stripe_mock/request_handlers/customers.rb', line 60

def update_customer(route, method_url, params, headers)
  route =~ method_url
  assert_existance :customer, $1, customers[$1]

  cus = customers[$1] ||= Data.mock_customer([], :id => $1)
  cus.merge!(params)

  if params[:card]
    new_card = get_card_by_token(params.delete(:card))

    if cus[:cards][:count] == 0
      cus[:cards][:count] += 1
    else
      cus[:cards][:data].delete_if {|card| card[:id] == cus[:default_card]}
    end

    cus[:cards][:data] << new_card
    cus[:default_card] = new_card[:id]
  end

  cus
end

#update_subscription(route, method_url, params, headers) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stripe_mock/request_handlers/customers.rb', line 25

def update_subscription(route, method_url, params, headers)
  route =~ method_url

  customer = customers[$1]
  assert_existance :customer, $1, customer

  plan = plans[ params[:plan] ]
  assert_existance :plan, params[:plan], plan

  # Ensure customer has card to charge if plan has no trial and is not free
  if customer[:default_card].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
    raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
  end

  sub = Data.mock_subscription id: new_id('su'), plan: plan, customer: $1
  customer[:subscription] = sub
end