Module: StripeMock::RequestHandlers::PaymentMethods

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

Constant Summary collapse

ALLOWED_PARAMS =
[:customer, :type]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

def PaymentMethods.included(klass)
  klass.add_handler 'post /v1/payment_methods',             :new_payment_method
  klass.add_handler 'get /v1/payment_methods/(.*)',         :get_payment_method
  klass.add_handler 'get /v1/payment_methods',              :get_payment_methods
  klass.add_handler 'post /v1/payment_methods/(.*)/attach', :attach_payment_method
  klass.add_handler 'post /v1/payment_methods/(.*)/detach', :detach_payment_method
  klass.add_handler 'post /v1/payment_methods/(.*)',        :update_payment_method
end

Instance Method Details

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

post /v1/payment_methods/:id/attach



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stripe_mock/request_handlers/payment_methods.rb', line 56

def attach_payment_method(route, method_url, params, headers)
   = headers && headers[:stripe_account] || Stripe.api_key
  allowed_params = [:customer]

  id = method_url.match(route)[1]

  assert_existence :customer, params[:customer], customers[][params[:customer]]

  payment_method = assert_existence :payment_method, id, payment_methods[id]
  payment_methods[id] = Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k) })
  payment_methods[id].clone
end

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

post /v1/payment_methods/:id/detach



70
71
72
73
74
75
76
77
# File 'lib/stripe_mock/request_handlers/payment_methods.rb', line 70

def detach_payment_method(route, method_url, params, headers)
  id = method_url.match(route)[1]

  payment_method = assert_existence :payment_method, id, payment_methods[id]
  payment_method[:customer] = nil

  payment_method.clone
end

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

params: :customer=>“test_cus_3”

get /v1/payment_methods/:id



34
35
36
37
38
39
# File 'lib/stripe_mock/request_handlers/payment_methods.rb', line 34

def get_payment_method(route, method_url, params, headers)
  id = method_url.match(route)[1] || params[:payment_method]
  payment_method = assert_existence :payment_method, id, payment_methods[id]

  payment_method.clone
end

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

get /v1/payment_methods



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stripe_mock/request_handlers/payment_methods.rb', line 42

def get_payment_methods(route, method_url, params, headers)
  params[:offset] ||= 0
  params[:limit] ||= 10

  clone = payment_methods.clone

  if params[:customer]
    clone.delete_if { |_k, v| v[:customer] != params[:customer] }
  end

  Data.mock_list_object(clone.values, params)
end

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

post /v1/payment_methods



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stripe_mock/request_handlers/payment_methods.rb', line 16

def new_payment_method(route, method_url, params, headers)
  id = new_id('pm')

  ensure_payment_method_required_params(params)

  payment_methods[id] = Data.mock_payment_method(
    params.merge(
      id: id
    )
  )

  payment_methods[id].clone
end

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

post /v1/payment_methods/:id



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/stripe_mock/request_handlers/payment_methods.rb', line 80

def update_payment_method(route, method_url, params, headers)
  allowed_params = [:billing_details, :card, :metadata]
  disallowed_params = params.keys - allowed_params
  unless disallowed_params.empty?
    raise Stripe::InvalidRequestError.new("Received unknown parameter: #{disallowed_params.first}", disallowed_params.first)
  end

  id = method_url.match(route)[1]

  payment_method = assert_existence :payment_method, id, payment_methods[id]

  if payment_method[:customer].nil?
    raise Stripe::InvalidRequestError.new(
      'You must save this PaymentMethod to a customer before you can update it.',
      nil,
      http_status: 400
    )
  end

  payment_methods[id] =
    Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k)} )

  payment_methods[id].clone
end