Class: RockFintech::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rock_fintech/http/request.rb

Constant Summary collapse

SIGN_TYPE =
'MD5'
ENCODE =
'UTF-8'
VERSION =
'2.0.0'

Instance Method Summary collapse

Constructor Details

#initialize(params, config, service, version = VERSION) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
# File 'lib/rock_fintech/http/request.rb', line 10

def initialize(params, config, service, version=VERSION)
  @params = params
  @config = config
  @uri = @config[:uri]
  @service = service
  @version = version || VERSION
  @response = nil
end

Instance Method Details

#flow_idObject



66
67
68
# File 'lib/rock_fintech/http/request.rb', line 66

def flow_id
  @params[:out_serial_no] || @params[:serial_no] || @params[:order_no] || @params[:batch_no]
end

#identifierObject



70
71
72
# File 'lib/rock_fintech/http/request.rb', line 70

def identifier
  "[#{@service} - #{flow_id}] "
end

#postObject



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
# File 'lib/rock_fintech/http/request.rb', line 19

def post
  RockFintech.logger.info "#{identifier} 请求内容为:\n#{@params.to_json}\n"
  # 1. api params

  # 2. sign
  sign_body = {
    service: @service,
    timestamp: Time.now.to_i,
    uuid: Utils.get_uuid,
    sign_type: SIGN_TYPE,
    encode: ENCODE,
    version: @version,
  }.merge(@params)

  sign_str = Sign.sign(sign_body, @config)

  # 3. merge sign
  post_body = sign_body.merge({sign: sign_str})

  # 4. encrypt
  request_data = Encrypt::RSA.encrypt(post_body.to_json, @config[:public_key])

  # 5. send http request
  header = {
    'rft-key' => @config[:rft_key],
    'rft-token' => @config[:rft_token],
    'rft-org' => @config[:rft_org],
  }
  RockFintech.logger.info "#{identifier} 发送的报文为:\n#{request_data}\n"
  http_response = RestClient.post(@uri, request_data, header)
  RockFintech.logger.info "#{identifier} 返回的报文为:\n#{http_response.body.force_encoding('utf-8')}\n"

  # 6. decode http response
  result_str = Encrypt::RSA.decrypt(http_response.body, @config[:private_key])
  res = Utils.symbolize_keys(JSON.parse(result_str))

  RockFintech.logger.info "#{identifier} 返回的数据为:\n#{res}\n"

  # 7. create response
  @response = RockFintech::Http::Response.new(service: @service,
                                              flow_id: flow_id,
                                              http_response: http_response,
                                              raw_body: http_response.body,
                                              data: res,
                                              data_valid: Sign.verify(res, @config))
end