netpayclinet.jar
|
根據專案工程的需要放置對應路徑下
|
用於提供數字簽名的方法呼叫
|
MerPrk.key
|
可以放置到任意路徑下,但是需要呼叫方法指定檔案位置和名稱
|
商戶簽名私鑰
|
PgPubk.key
|
可以放置到任意路徑下,但是需要呼叫方法指定檔案位置和名稱
|
ChinaPay簽名公鑰
|
把jar包放進專案lib裡後直接呼叫方法:
1.建立公/私鑰物件buildKey
chinapay.PrivateKey key=new chinapay.PrivateKey();
chinapay.SecureLink t;
boolean flag;
//buildKey用於建立私/公鑰的物件,用於簽名或者驗證簽名,該方法在類chinapay. PrivateKey中。
//第一個引數:MerId 商戶號,長度為15個位元組的數字串,由ChinaPay分配。
//第二個引數:使用私/公鑰的方式,固定為 0
//第三個引數:私/公鑰的檔案路徑(包含檔名稱)baseDiskPath,
File.separator(不同系統自適應路徑)
String baseDiskPath = request.getSession().getServletContext().getRealPath(File.separator);
flag=key.buildKey(MerId,0,baseDiskPath+File.separator+”chinapaykey”+File.separator+”MerPrK.key”);
if (flag==false)
{
System.out.println(“build key error!”);
}else {
System.out.println(“build key ok!”);
}
2.訂單簽名函式signOrder 該方法在類chinapay. SecureLink中
String MerId, OrdId, TransAmt, CuryId, TransDate, TransType,ChkValue;
// 對訂單的簽名,引數說明見頁面表單
ChkValue= t.signOrder(MerId, OrdId, TransAmt, CuryId, TransDate, TransType) ;
//將訂單資料送往頁面提交
request.setAttribute(“ChkValue”, ChkValue);
request.setAttribute(“TransAmt”, TransAmt);
request.setAttribute(“OrdId”, OrdId);
request.setAttribute(“TransDate”, TransDate);
3.//頁面接收引數並自動提交
//這裡action的內容為提交交易資料的URL地址 http://payment-test.chinapay.com/pay/TransGet為測試地址
<form name=”form1″ action=”https://payment.chinapay.com/pay/TransGet”
METHOD=POST>
//MerId為ChinaPay統一分配給商戶的商戶號,15位長度,必填
<input type=hidden name=”MerId” value=””/>
//商戶提交給ChinaPay的交易訂單號,16位長度,必填
<%String OrdId = (String)request.getAttribute(“OrdId”); %>
<input type=hidden name=”OrdId” value=<%=OrdId %>>
//訂單交易金額,單位為分,12位長度,左補0,必填
<%String TransAmt = (String)request.getAttribute(“TransAmt”); %>
<input type=hidden name=”TransAmt” value=<%=TransAmt %>>
//訂單交易幣種,3位長度,固定為人民幣156, 必填
<input type=hidden name=”CuryId” value=”156″/>
//訂單交易日期,8位長度,必填
<%String TransDate = (String)request.getAttribute(“TransDate”); %>
<input type=hidden name=”TransDate” value=<%=TransDate %>>
//交易型別,4位長度,必填,取值範圍為:“0001”和“0002”, 其中“0001”表示消費交易,“0002”表示退貨交易
<input type=hidden name=”TransType” value=”0001″/>
//支付接入版本號,必填,20040916的版本中,如果商戶為二級商戶,訂單號從第5位到第9位必須和商戶號的第11位到第15位相同
<input type=hidden name=”Version” value=”20040916″/>
//後臺交易接收URL,必填,長度不要超過80個位元組
<% String basePath = request.getScheme()+”://”+request.getServerName();
String BgRetUrl = basePath+”/返回頁面”; %>
<input type=hidden name=”BgRetUrl” value=<%=BgRetUrl %>>
//頁面交易接收URL,長度不要超過80個位元組,必填
<input type=hidden name=”PageRetUrl” value=<%=basePath %>>
//支付閘道器號,可選
<input type=hidden name=”GateId” value=””>
//商戶私有域,長度不要超過60個位元組,可選
<%String Priv1 = (String)request.getAttribute(“Priv1”); %>
<input type=hidden name=”Priv1″ value=<%=Priv1 %>>
//256位元組長的ASCII碼,為此次交易提交關鍵資料的數字簽名,必填
<%String ChkValue = (String)request.getAttribute(“ChkValue”); %>
<input type=hidden name=”ChkValue” value=<%=ChkValue %>>
</form>
<script language=”JavaScript”
type=”text/JavaScript”>
form1.submit();
</script>
4.驗證交易應答函式verifyTransResponse 該方法在類chinapay. SecureLink中
//後臺返回頁驗證
chinapay.PrivateKey key=new chinapay.PrivateKey();
chinapay.SecureLink t;
boolean flag;
boolean flag1;
String MerId, OrdId, TransAmt, CuryId, TransDate, TransType,ChkValue,OrderStatus,Priv1;
String plainData, ChkValue2;
MerId = request.getParameter(“merid”);
OrdId = request.getParameter(“orderno”);
TransAmt = request.getParameter(“amount”);
CuryId = request.getParameter(“currencycode”);
TransDate = request.getParameter(“transdate”);
TransType = request.getParameter(“transtype”);
OrderStatus = request.getParameter(“status”);
ChkValue = request.getParameter(“checkvalue”);
Priv1 = request.getParameter(“Priv1”);
String baseDiskPath = request.getSession().getServletContext().getRealPath(File.separator);
flag=key.buildKey(“999999999999999″,0,baseDiskPath+File.separator+”chinapaykey”+File.separator+”PgPubk.key”);
if (flag==false)
{
//System.out.println(“build key error!”);
}else {
//System.out.println(“build key ok!”);
}
t=new chinapay.SecureLink (key);
flag1=t.verifyTransResponse(MerId,OrdId, TransAmt, CuryId, TransDate, TransType, OrderStatus, ChkValue);
————
demo
<form id=”form1″ runat=”server”>
<div>
<table cellpadding=”0″ cellspacing=”0″ border=”1″ style=”border-collapse:collapse” bordercolor=”#cccccc”>
<tr>
<td>訂單號</td>
<td>
<asp:TextBox ID=”txtOrderNo” runat=”server”>0700004350100289</asp:TextBox></td>
</tr>
<tr>
<td>訂單數量</td>
<td><asp:TextBox ID=”txtOrderQty” runat=”server”></asp:TextBox></td>
</tr>
<tr>
<td>金額</td>
<td><asp:TextBox ID=”txtAmount” runat=”server”></asp:TextBox></td>
</tr>
<tr>
<td colspan=”2″>
<form name=”frm1″ action=”http://payment.chinapay.com:8081/pay/TransGet” METHOD=”POST” >
<input type=hidden name=”MerId” value=”808080010500435″ >
<input type=hidden name=”OrdId” value=”0700004350100289″ >
<input type=hidden name=”TransAmt” value=”000000106000″ >
<input type=hidden name=”CuryId” value=”156″ >
<input type=hidden name=”TransDate” value=”20070703″ >
<input type=hidden name=”TransType” value=”0001″ >
<input type=hidden name=”Version” value=”20040916″>
<input type=hidden name=”BgRetUrl” value=”accept.aspx”>
<input type=hidden name=”PageRetUrl” value=”chinapayback.aspx”>
<input type=hidden name=”Priv1″ value=”Memo”>
<input type=hidden name=”ChkValue” value=<%= keyValue %> >
<input id=”Button1″ type=”submit” class=”button” value=”進入銀聯支付平臺” /></form>
</td>
</tr>
</table>
</div>
</form>
get方式:
http://payment-test.chinapay.com/pay/TransGet?MerId=001320554110008&OrdId=0001320554110008&TransAmt=000000001234&CuryId=156&TransDate=20130410&TransType=0001&Version=20040916&BgRetUrl=http://www.baidu.com&PageRetUrl=http://www.baidu.com&ChkValue=
00132055411000800132055411000800132055411000800
13205541100080013205541100080013205541100081000800
13205541100081000800132055411000810008001320554110008
18018018011801801801180180013205541100080013205541100
08001320554110008001320554110008001320554110008000080