Class: StraightServer::OrdersController

Inherits:
Object
  • Object
show all
Includes:
Goliath::Constants
Defined in:
lib/straight-server/orders_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ OrdersController

Returns a new instance of OrdersController.



11
12
13
14
15
16
17
# File 'lib/straight-server/orders_controller.rb', line 11

def initialize(env)
  @env          = env
  @params       = env.params
  @method       = env['REQUEST_METHOD']
  @request_path = env['REQUEST_PATH'].split('/').delete_if { |s| s.nil? || s.empty? }
  dispatch
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/straight-server/orders_controller.rb', line 9

def response
  @response
end

Instance Method Details

#cancelObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/straight-server/orders_controller.rb', line 114

def cancel
  unless @gateway
    StraightServer.logger.warn "Gateway not found"
    return [404, {}, "Gateway not found"]
  end

  if @gateway.check_signature
    StraightServer::SignatureValidator.new(@gateway, @env).validate!
  end

  if (order = find_order)
    order.status(reload: true)
    order.save if order.status_changed?
    if order.cancelable?
      order.cancel
      [200, {}, '']
    else
      [409, {}, "Order is not cancelable"]
    end
  end
end

#createObject



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/straight-server/orders_controller.rb', line 19

def create

  unless @gateway
    StraightServer.logger.warn "Gateway not found"
    return [404, {}, "Gateway not found" ]
  end

  if @gateway.check_signature
    StraightServer::SignatureValidator.new(@gateway, @env).validate!
  else
    ip = @env['HTTP_X_FORWARDED_FOR'].to_s
    ip = @env['REMOTE_ADDR'] if ip.empty?
    if StraightServer::Throttler.new(@gateway.id).deny?(ip)
      StraightServer.logger.warn message = "Too many requests, please try again later"
      return [429, {}, message]
    end
  end

  begin

    # This is to inform users of previous version of a deprecated param
    # It will have to be removed at some point.
    if @params['order_id']
      return [409, {}, "Error: order_id is no longer a valid param. Use keychain_id instead and consult the documentation." ]
    end

    order_data = {
      amount:           @params['amount'], # this is satoshi
      currency:         @params['currency'],
      btc_denomination: @params['btc_denomination'],
      keychain_id:      @params['keychain_id'],
      callback_data:    @params['callback_data'],
      data:             @params['data'],
      description:      @params['description']
    }

    order = @gateway.create_order(order_data)
    StraightServer::Thread.new(label: order.payment_id) do
      # Because this is a new thread, we have to wrap the code inside in #watch_exceptions
      # once again. Otherwise, no watching is done. Oh, threads!
      StraightServer.logger.watch_exceptions do
        order.start_periodic_status_check
      end
    end
    [200, {}, add_callback_data_warning(order).to_json]
  rescue Sequel::ValidationFailed => e
    StraightServer.logger.warn(
      "VALIDATION ERRORS in order, cannot create it:\n" +
      "#{e.message.split(",").each_with_index.map { |e,i| "#{i+1}. #{e.lstrip}"}.join("\n") }\n" +
      "Order data: #{order_data.inspect}\n"
    )
    [409, {}, "Invalid order: #{e.message}" ]
  rescue Straight::Gateway::OrderAmountInvalid => e
    [409, {}, "Invalid order: #{e.message}" ]
  rescue StraightServer::GatewayModule::GatewayInactive
    StraightServer.logger.warn message = "The gateway is inactive, you cannot create order with it"
    [503, {}, message ]
  end
end

#last_keychain_idObject



136
137
138
139
140
141
142
143
# File 'lib/straight-server/orders_controller.rb', line 136

def last_keychain_id
  unless @gateway
    StraightServer.logger.warn "Gateway not foun"
    return [404, {}, "Gateway not found"]
  end

  [200, {}, {gateway_id: @gateway.id, last_keychain_id: @gateway.last_keychain_id}.to_json]
end

#showObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/straight-server/orders_controller.rb', line 79

def show

  unless @gateway
    StraightServer.logger.warn "Gateway not found"
    return [404, {}, "Gateway not found" ]
  end

  if @gateway.check_signature
    StraightServer::SignatureValidator.new(@gateway, @env).validate!
  end

  order = find_order

  if order
    order.status(reload: true)
    order.save if order.status_changed?
    [200, {}, order.to_json]
  end
end

#websocketObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/straight-server/orders_controller.rb', line 99

def websocket

  order = find_order
  if order
    begin
      @gateway.add_websocket_for_order ws = Faye::WebSocket.new(@env), order
      ws.rack_response
    rescue Gateway::WebsocketExists
      [403, {}, "Someone is already listening to that order"]
    rescue Gateway::WebsocketForCompletedOrder
      [403, {}, "You cannot listen to this order because it is completed (status > 1)"]
    end
  end
end