Module: Reapal::Http

Defined in:
lib/reapal/http/decode.rb,
lib/reapal/http/response.rb,
lib/reapal/http/communicate.rb,
lib/reapal/http/error_code/user.rb,
lib/reapal/http/error_code/money.rb,
lib/reapal/http/error_code/order.rb,
lib/reapal/http/error_code/common.rb,
lib/reapal/http/error_code/tender.rb

Defined Under Namespace

Modules: Decode, ErrorCode Classes: Response

Constant Summary collapse

SIGN_TYPE =

0 表示 md5

'0'
VERSION =

版本号

'3.0'

Class Method Summary collapse

Class Method Details

.decode_data(encryptkey, data, config) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reapal/http/communicate.rb', line 69

def self.decode_data(encryptkey, data, config)
  if encryptkey.nil? || data.nil? || encryptkey.empty? || data.empty?
    return {
      resData: {
        errorCode: 'error_decode_params',
        errorMsg: '解密参数不正确'
      },
      data_valid: true,
    }
  end

  # 1. 拿到用来加密的16位随机字符串
  random_key = Encrypt::RSA.decrypt(encryptkey, config[:private_key])

  # 2. 用16位随机字符串解密返回的数据
  data_string = Encrypt::AES.decrypt(data, random_key)
  data = parse_data_string(data_string)
  data[:resData] = Utils.symbolize_keys(JSON.parse(data[:resData]))

  # 3. 验签,错误时候 sign 是没有值的
  if data[:resData][:errorCode] || (data[:sign] && Sign::MD5.verify?(data[:resData].to_json, config[:md5_key], data[:sign]))
    data[:data_valid] = true
  else
    data[:data_valid] = false
  end

  data
end

.get_body(service, params, config, version = VERSION) ⇒ Object

表单的 body



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/reapal/http/communicate.rb', line 51

def self.get_body(service, params, config, version=VERSION)
  data = {
    version: version,
    service: service,
    partner: config[:partner_id],
    sign: Sign::MD5.sign(params.to_json, config[:md5_key]),
    signType: SIGN_TYPE,
    reqData: params.to_json,
  }

  random_key = Reapal::Utils.random_key
  body = {
    merchant_id: config[:merchant_id],
    encryptkey: Encrypt::RSA.encrypt(random_key, config[:public_key]),
    data: Encrypt::AES.encrypt(data.to_json, random_key),
  }
end

.post(service, params, config, post_path, version = VERSION) ⇒ Object



8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
# File 'lib/reapal/http/communicate.rb', line 8

def self.post(service, params, config, post_path, version=VERSION)
  version = VERSION if version.nil?
  post_body = get_body(service, params, config, version)
  uri = URI(config[:server_url] + post_path)

  Reapal.logger.info "[#{service}] 请求内容为:\n#{params}\n"
  Reapal.logger.info "[#{service}(#{uri})] 最终发送内容为:\n#{post_body}\n"

  result = nil

  begin
    response = Net::HTTP.post_form(uri, post_body)
    Reapal.logger.info "[#{service}] 返回的报文为:\n#{response.body.force_encoding('utf-8')}"

    if response.is_a?(Net::HTTPSuccess)
      response_raw_body = unpack_body(response.body, config)
      result = Reapal::Http::Response.new(service: service,
                                          flow_id: params[:orderNo],
                                          http_response: response,
                                          raw_body: response_raw_body,
                                          data: response_raw_body[:resData],
                                          data_valid: response_raw_body[:data_valid])
    else
      # 不成功的不解密, 500, 300, 400
      result = Reapal::Http::Response.new(service: service,
                                          flow_id: params[:orderNo],
                                          http_response: response,
                                          raw_body: response.body,
                                          data_valid: true)
    end
  rescue Net::ReadTimeout
    result = Reapal::Http::Response.new(service: service,
                                        flow_id: params[:orderNo],
                                        http_response: nil,
                                        data_valid: true)
  end

  Reapal.logger.info "[#{service}] 返回结果为:\n#{result}\n\n"

  result
end