Class: PayGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_gateway.rb

Overview

gemfile中加入 gem ‘iconv’

Constant Summary collapse

URL_UTF8 =

支付接口地址

"https://pay3.chinabank.com.cn/PayGate?encoding=UTF-8"
URL =

图片和css文件地址

"https://pay3.chinabank.com.cn"

Class Method Summary collapse

Class Method Details

.pay(v_mid, key, v_url, remark2, v_oid, v_amount, v_moneytype = "CNY") ⇒ Object

功能:网银在线,网管支付; 参数:

v_mid:商户编号
key:商户md5密钥
v_url:支付成功后跳转的页面
remark2:异步订单状态地址,remark2 = "[url:=http://domain/chinabank/AutoReceive.aspx]"格式必须是[url:=xxx]
v_oid:订单编号
v_amount:订单金额
v_moneytype:支付货币类型


18
19
20
21
22
23
24
25
26
# File 'lib/pay_gateway.rb', line 18

def self.pay(v_mid, key, v_url, remark2, v_oid, v_amount, v_moneytype = "CNY")
  v_md5info = Digest::MD5.hexdigest(v_amount.to_s + v_moneytype + v_oid + v_mid + v_url + key).upcase
  response = Func.post(URL_UTF8,{'v_mid'=>v_mid,'v_oid'=>v_oid,'v_amount'=>v_amount,'v_moneytype'=>v_moneytype,'v_url'=>v_url,'v_md5info'=>v_md5info,'remark2'=>remark2})
  html = Iconv.iconv('utf-8', 'gbk', response.body).first
  html.scan(/[href|src]=("[^"]*.[css|js|gif|jpg]")/).each do |h|
    html = html.gsub(h[0],URL+eval(h[0]).gsub('..',''))
  end
  html
end

.receive(params, key) ⇒ Object

功能:自动验证结果 参数: params参数

v_oid = Request["v_oid"];
v_pstatus = Request["v_pstatus"];
v_pstring = Request["v_pstring"];
v_pmode = Request["v_pmode"];
v_md5str = Request["v_md5str"];
v_amount = Request["v_amount"];
v_moneytype = Request["v_moneytype"];
remark1 = Request["remark1"];
remark2 = Request["remark2"];

key商户的md5密钥 返回:如果支付成功返回订单号,否则返回nil



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pay_gateway.rb', line 42

def self.receive(params,key)
  v_oid = params["v_oid"]
  v_pstatus = params["v_pstatus"]
  v_md5str = params["v_md5str"]  #该参数的MD5字符串的顺序为:v_oid,v_pstatus,v_amount,v_moneytype,key
  v_amount = params["v_amount"]
  v_moneytype = params["v_moneytype"]
  # 20(表示支付成功)
  # 30(表示支付失败)
  if v_pstatus=="20" && v_md5str==Digest::MD5.hexdigest(v_oid+v_pstatus+v_amount+v_moneytype+key)
    return params["v_oid"]
  else
    return nil
  end
end