Module: StripeMock::RequestHandlers::Orders

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

def Orders.included(klass)
  klass.add_handler 'post /v1/orders',                     :new_order
  klass.add_handler 'post /v1/orders/(.*)/pay',            :pay_order
  klass.add_handler 'post /v1/orders/(.*)',                :update_order
  klass.add_handler 'get /v1/orders/(.*)',                 :get_order
  klass.add_handler 'get /v1/orders',                      :list_orders
end

Instance Method Details

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



54
55
56
57
# File 'lib/stripe_mock/request_handlers/orders.rb', line 54

def get_order(route, method_url, params, headers)
  route =~ method_url
  assert_existence :order, $1, orders[$1]
end

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



74
75
76
# File 'lib/stripe_mock/request_handlers/orders.rb', line 74

def list_orders(route, method_url, params, headers)
  Data.mock_list_object(orders.values, params)
end

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stripe_mock/request_handlers/orders.rb', line 13

def new_order(route, method_url, params, headers)
  params[:id] ||= new_id('or')
  order_items = []

  unless params[:currency].to_s.size == 3
    raise Stripe::InvalidRequestError.new('You must supply a currency', nil, http_status: 400)
  end

  if params[:items]
    unless params[:items].is_a? Array
      raise Stripe::InvalidRequestError.new('You must supply a list of items', nil, http_status: 400)
    end

    unless params[:items].first.is_a? Hash
      raise Stripe::InvalidRequestError.new('You must supply an item', nil, http_status: 400)
    end
  end

  orders[ params[:id] ] = Data.mock_order(order_items, params)

  orders[ params[:id] ]
end

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/stripe_mock/request_handlers/orders.rb', line 59

def pay_order(route, method_url, params, headers)
  route =~ method_url
  order = assert_existence :order, $1, orders[$1]

  if params[:source].blank? && params[:customer].blank?
    raise Stripe::InvalidRequestError.new('You must supply a source or customer', nil, http_status: 400)
  end

  charge_id = new_id('ch')
  charges[charge_id] = Data.mock_charge(id: charge_id)
  order[:charge] = charge_id
  order[:status] = "paid"
  order
end

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stripe_mock/request_handlers/orders.rb', line 36

def update_order(route, method_url, params, headers)
  route =~ method_url
  order = assert_existence :order, $1, orders[$1]

  if params[:metadata]
    if params[:metadata].empty?
      order[:metadata] = {}
    else
      order[:metadata].merge(params[:metadata])
    end
  end

  if %w(created paid canceled fulfilled returned).include? params[:status]
    order[:status] = params[:status]
  end
  order
end