Module: StripeMock::RequestHandlers::PaymentIntents

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

Constant Summary collapse

ALLOWED_PARAMS =
[:description, :metadata, :receipt_email, :shipping, :destination, :payment_method, :payment_method_types, :setup_future_usage, :transfer_data, :amount, :currency]
SEARCH_FIELDS =
["amount", "currency", "customer", "status"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

def PaymentIntents.included(klass)
  klass.add_handler 'post /v1/payment_intents',               :new_payment_intent
  klass.add_handler 'get /v1/payment_intents',                :get_payment_intents
  klass.add_handler 'get /v1/payment_intents/((?!search).*)', :get_payment_intent
  klass.add_handler 'get /v1/payment_intents/search',         :search_payment_intents
  klass.add_handler 'post /v1/payment_intents/(.*)/confirm',  :confirm_payment_intent
  klass.add_handler 'post /v1/payment_intents/(.*)/capture',  :capture_payment_intent
  klass.add_handler 'post /v1/payment_intents/(.*)/cancel',   :cancel_payment_intent
  klass.add_handler 'post /v1/payment_intents/(.*)',          :update_payment_intent
end

Instance Method Details

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



100
101
102
103
104
105
106
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 100

def cancel_payment_intent(route, method_url, params, headers)
  route =~ method_url
  payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]

  payment_intent[:status] = 'canceled'
  payment_intent
end

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



82
83
84
85
86
87
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 82

def capture_payment_intent(route, method_url, params, headers)
  route =~ method_url
  payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]

  succeeded_payment_intent(payment_intent)
end

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 89

def confirm_payment_intent(route, method_url, params, headers)
  route =~ method_url
  payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]

  if params[:payment_method]
    payment_intent[:payment_method] = params[:payment_method]
  end

  succeeded_payment_intent(payment_intent)
end

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



65
66
67
68
69
70
71
72
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 65

def get_payment_intent(route, method_url, params, headers)
  route =~ method_url
  payment_intent_id = $1 || params[:payment_intent]
  payment_intent = assert_existence :payment_intent, payment_intent_id, payment_intents[payment_intent_id]

  payment_intent = payment_intent.clone
  payment_intent
end

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



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 52

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

  clone = payment_intents.clone

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

  Data.mock_list_object(clone.values, params)
end

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



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
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 17

def new_payment_intent(route, method_url, params, headers)
  id = new_id('pi')

  ensure_payment_intent_required_params(params)
  status = case params[:amount]
  when 3184 then 'requires_action'
  when 3178 then 'requires_payment_method'
  when 3055 then 'requires_capture'
  else
    'succeeded'
  end
  last_payment_error = params[:amount] == 3178 ? last_payment_error_generator(code: 'card_declined', decline_code: 'insufficient_funds', message: 'Not enough funds.') : nil
  payment_intents[id] = Data.mock_payment_intent(
    params.merge(
      id: id,
      status: status,
      last_payment_error: last_payment_error
    )
  )

  if params[:confirm] && status == 'succeeded'
    payment_intents[id] = succeeded_payment_intent(payment_intents[id])
  end

  payment_intents[id].clone
end

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



75
76
77
78
79
80
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 75

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

  results = search_results(payment_intents.values, params[:query], fields: SEARCH_FIELDS, resource_name: "payment_intents")
  Data.mock_list_object(results, params)
end

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



44
45
46
47
48
49
50
# File 'lib/stripe_mock/request_handlers/payment_intents.rb', line 44

def update_payment_intent(route, method_url, params, headers)
  route =~ method_url
  id = $1

  payment_intent = assert_existence :payment_intent, id, payment_intents[id]
  payment_intents[id] = Util.rmerge(payment_intent, params.select{ |k,v| ALLOWED_PARAMS.include?(k)})
end