Module: StripeMock::RequestHandlers::Customers

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

Constant Summary collapse

SEARCH_FIELDS =
["email", "name", "phone"].freeze

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/([^/]*)',             :update_customer
  klass.add_handler 'get /v1/customers/((?!search)[^/]*)',    :get_customer
  klass.add_handler 'delete /v1/customers/([^/]*)',           :delete_customer
  klass.add_handler 'get /v1/customers',                      :list_customers
  klass.add_handler 'get /v1/customers/search',               :search_customers
  klass.add_handler 'delete /v1/customers/([^/]*)/discount',  :delete_customer_discount
end

Instance Method Details

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/stripe_mock/request_handlers/customers.rb', line 113

def delete_customer(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  route =~ method_url
  assert_existence :customer, $1, customers[][$1]

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

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



154
155
156
157
158
159
160
161
162
# File 'lib/stripe_mock/request_handlers/customers.rb', line 154

def delete_customer_discount(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  route =~ method_url
  cus = assert_existence :customer, $1, customers[][$1]

  cus[:discount] = nil

  cus
end

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/stripe_mock/request_handlers/customers.rb', line 124

def get_customer(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  route =~ method_url
  customer = assert_existence :customer, $1, customers[][$1]

  customer = customer.clone
  if params[:expand] == ['default_source'] && customer[:sources][:data]
    customer[:default_source] = customer[:sources][:data].detect do |source|
      source[:id] == customer[:default_source]
    end
  end

  customer
end

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



139
140
141
142
# File 'lib/stripe_mock/request_handlers/customers.rb', line 139

def list_customers(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  Data.mock_list_object(customers[]&.values, params)
end

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stripe_mock/request_handlers/customers.rb', line 15

def new_customer(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  params[:id] ||= new_id('cus')
  sources = []

  if params[:source]
    new_card =
      if params[:source].is_a?(Hash)
        unless params[:source][:object] && params[:source][:number] && params[:source][:exp_month] && params[:source][:exp_year]
          raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, http_status: 400)
        end
        card_from_params(params[:source])
      else
        get_card_or_bank_by_token(params.delete(:source))
      end
    sources << new_card
    params[:default_source] = sources.first[:id]
  end

  customers[] ||= {}
  customers[][params[:id]] = Data.mock_customer(sources, params)

  if params[:plan]
    plan_id = params[:plan].to_s
    plan = assert_existence :plan, plan_id, plans[plan_id]

    if params[:default_source].nil? && params[:trial_end].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
      raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, http_status: 400)
    end

    subscription = Data.mock_subscription({ id: new_id('su') })
    subscription = resolve_subscription_changes(subscription, [plan], customers[][params[:id]], params)
    add_subscription_to_customer(customers[][params[:id]], subscription)
    subscriptions[subscription[:id]] = subscription
  elsif params[:trial_end]
    raise Stripe::InvalidRequestError.new('Received unknown parameter: trial_end', nil, http_status: 400)
  end

  if params[:coupon]
    coupon = coupons[params[:coupon]]
    assert_existence :coupon, params[:coupon], coupon
    add_coupon_to_object(customers[][params[:id]], coupon)
  end

  customers[][params[:id]]
end

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



145
146
147
148
149
150
151
152
# File 'lib/stripe_mock/request_handlers/customers.rb', line 145

def search_customers(route, method_url, params, headers)
  require_param(:query) unless params[:query]

   = headers && headers[:stripe_account] || Stripe.api_key
  all_customers = customers[]&.values
  results = search_results(all_customers, params[:query], fields: SEARCH_FIELDS, resource_name: "customers")
  Data.mock_list_object(results, params)
end

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



62
63
64
65
66
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
111
# File 'lib/stripe_mock/request_handlers/customers.rb', line 62

def update_customer(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  route =~ method_url
  cus = assert_existence :customer, $1, customers[][$1]

  # get existing and pending metadata
   = cus.delete(:metadata) || {}
   = params.delete(:metadata) || {}

  # Delete those params if their value is nil. Workaround of the problematic way Stripe serialize objects
  params.delete(:sources) if params[:sources] && params[:sources][:data].nil?
  params.delete(:subscriptions) if params[:subscriptions] && params[:subscriptions][:data].nil?
  # Delete those params if their values aren't valid. Workaround of the problematic way Stripe serialize objects
  if params[:sources] && !params[:sources][:data].nil?
    params.delete(:sources) unless params[:sources][:data].any?{ |v| !!v[:type]}
  end
  if params[:subscriptions] && !params[:subscriptions][:data].nil?
    params.delete(:subscriptions) unless params[:subscriptions][:data].any?{ |v| !!v[:type]}
  end
  cus.merge!(params)
  cus[:metadata] = {**, **}

  if params[:source]
    if params[:source].is_a?(String)
      new_card = get_card_or_bank_by_token(params.delete(:source))
    elsif params[:source].is_a?(Stripe::Token)
      new_card = get_card_or_bank_by_token(params[:source][:id])
    elsif params[:source].is_a?(Hash)
      unless params[:source][:object] && params[:source][:number] && params[:source][:exp_month] && params[:source][:exp_year]
        raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, http_status: 400)
      end
      new_card = card_from_params(params.delete(:source))
    end
    add_card_to_object(:customer, new_card, cus, true)
    cus[:default_source] = new_card[:id]
  end

  if params[:coupon]
    if params[:coupon] == ''
      delete_coupon_from_object(cus)
    else
      coupon = coupons[params[:coupon]]
      assert_existence :coupon, params[:coupon], coupon

      add_coupon_to_object(cus, coupon)
    end
  end

  cus
end