Module: JytPay::Http

Defined in:
lib/jyt_pay/http/ret_code.rb,
lib/jyt_pay/http/communicate.rb

Defined Under Namespace

Modules: RetCode

Class Method Summary collapse

Class Method Details

.post(merchant_id, server_uri, tran_code, xml_str, rsa_private_key, rsa_jyt_public_key) ⇒ Object



5
6
7
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
# File 'lib/jyt_pay/http/communicate.rb', line 5

def self.post(merchant_id, server_uri,
              tran_code, xml_str,
              rsa_private_key, rsa_jyt_public_key)
  body = {
    merchant_id: merchant_id,
    xml_enc: Encrypt::Des.encrypt(xml_str),
    key_enc: Encrypt::Rsa.encrypt(rsa_jyt_public_key),
    sign: Sign::Rsa.sign(rsa_private_key, xml_str)
  }

  puts "\n[#{tran_code}][#{server_uri}] 发送原始 xml 为:\n#{xml_str}\n"

  result = {}

  begin
    response = Net::HTTP.post_form(server_uri, body)

    if response.is_a?(Net::HTTPSuccess) # 返回 200 才处理
      result = unpack_body(response.body, rsa_private_key)
    else
      # 非 200 请求
      result = Http::RetCode.general_error_response(response.code)
    end
  rescue Net::ReadTimeout # 请求超时
    result = Http::RetCode.general_error_response(0)
  end

  puts "\n[#{tran_code}][#{server_uri}] 返回结果为:\n#{result}\n\n"

  result
end