1. 程式人生 > >網站接入銀聯網上支付(B2B)

網站接入銀聯網上支付(B2B)

1、公鑰還有銀行卡號和密碼等銀聯會提供,私鑰按照銀聯提供的文件申請就可以了,需要說明的是申請的時候要用ie,並且在internet 選項-->安全-->自定義級別中,

     將Active X 控制元件和外掛 下的所有禁用都改為啟用,如圖:


2、在銀聯提供的測試網址登陸,如圖:

3、輸入登陸資訊,第一次登陸需要修改密碼,然後重新登陸,選擇機構管理,生成商戶金鑰,如圖:


4、選擇生成的私鑰路徑,儲存後,記得要點提交按鈕,私鑰第二天生效。

5、在src下建立config.properties檔案,裡面存放商戶號、支付閘道器申請、公鑰和私鑰的存放路徑,如:

merid = xxxxxxxxxxxxxxxx
merkeyfilepath = E\:\\key\\XXX
pubkeyfilepath = E\:\\key\\XXX
url = http://XXXXXXXXX

6、申請的servlet:



    private static final String MERID = "merid";
    private static final String PaymentUrl = "url";
    private static final String MERKEYFILEPATH = "merkeyfilepath";

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String MerKeyPath = null;
        String merId = null;

         String msg="";
        try{
            Properties config = Config.getInstance().getProperties();
            MerKeyPath = config.getProperty(MERKEYFILEPATH);
            merId = config.getProperty(MERID);
           
        }catch (Exception e){
            msg="私鑰初始化失敗!";
        }
        if (msg!="") {
            request.setAttribute("errors", msg);
            request.getRequestDispatcher(XXXXX).forward(request, response);
            return;
        }
        String ordId="0000000000000001";//訂單號只能提交一次
        String transAmt="000000000001";//金額
        String curyId="156";//幣種
        String transDate="20150601";//日期
        String transType="0001";//型別
        String version="20070129";//版本
        String bgRetUrl="http://XXXXXXXXX";//後臺action
        String pageRetUrl="http://xxxxx.jsp";//前臺jsp
        String gateId="";
        String priv1 = "";
        String chkValue = null;
        boolean flage= false;
        PrivateKey key = new PrivateKey();
        try{
            flage= key.buildKey(merId, 0, MerKeyPath);
        }catch (Exception e){
            System.out.print("建立失敗");
        }
        //簽名
        String data=merId+ordId+transAmt+curyId+transDate+transType+priv1;
        SecureLink sl = new SecureLink(key);
        chkValue = sl.Sign(data);

//將要提交的資料顯示到頁面
        Bean cbb=new Bean();
        cbb.setMerId(merId);
        cbb.setOrdId(ordId);
        cbb.setTransAmt(transAmt);
        cbb.setCuryId(curyId);
        cbb.setTransDate(transDate);
        cbb.setTransType(transType);
        cbb.setPriv1(priv1);
        cbb.setVersion(version);
        cbb.setBgRetUrl(bgRetUrl);
        cbb.setPageRetUrl(pageRetUrl);
        cbb.setGateId(gateId);
        cbb.setChkValue(chkValue);
        request.setAttribute("cbbInput", cbb);
        request.getRequestDispatcher(xxxxxxxx).forward(request, response);
        return;
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request,response);
    }

7、上面的servlet跳轉到下面的頁面:

<%@ page language="java" import="model.bean.*" pageEncoding="UTF-8"%>

<%@ page import="java.util.*"%>
<jsp:useBean id="cbbInput" scope="request" class="model.Bean" />

<form name="corpor" action="測試提交路徑" method="post">
  <input type="hidden" name="MerId" value=“xxxxx”/>
  <input type="hidden" name="OrdId" value="xxxx"/>
  <input type="hidden" name="TransAmt" value="xxx"/>
  <input type="hidden" name="CuryId" value="xxx"/>
  <input type="hidden" name="TransDate" value="xxx"/>
  <input type="hidden" name="TransType" value="xxx"/>
  <input type="hidden" name="Version" value="xxx"/>
  <input type="hidden" name="BgRetUrl" value="xxx"/>
  <input type="hidden" name="PageRetUrl" value="xxx"/>
  <input type="hidden" name="GateId" value="xxxx"/>
  <input type="hidden" name="Priv1" value=""/>
  <input type="hidden" name="ChkValue" value="xxxx"/>
  <input type="submit" name="submit Button" value="提交" class="cmd"/>
</form>

8、點選提交,就會到企業網銀登陸頁面,如圖:


9、輸入卡號和密碼登陸,會提示交易結果,如圖:


10、PageRetUrl為上面的頁面跳轉回網站的頁面,BgRetUrl 為後臺action,用來驗證和儲存資料,servlet如下:
    private static final String url = "url";
    private static final String MERKEYFILEPATH = "merkeyfilepath";
    private static final String PUBKEYFILEPATH = "pubkeyfilepath";
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        /*
        獲取伺服器返回資料
         */
        String status=request.getParameter("status");
        String merId=request.getParameter("merid");
        String ordno=request.getParameter("orderno");
        String transdate=request.getParameter("transdate");
        String amount=request.getParameter("amount");
        String currencycode=request.getParameter("currencycode");
        String transtype=request.getParameter("transtype");
        String checkvalue=request.getParameter("checkvalue");
        String gateId=request.getParameter("GateId");
        String priv1=request.getParameter("Priv1");
      //  String s=readReqStr(request);
        //此處將上面資料存入資料庫
        //驗籤
        String PubKeyPath = null;
        try {
            Properties config = Config.getInstance().getProperties();
            PubKeyPath = config.getProperty(PUBKEYFILEPATH);
        } catch (Exception e) {
        }
        chinapay.PrivateKey key=new chinapay.PrivateKey();
        chinapay.SecureLink t;
        boolean flage=false;
        boolean flage1=false;
        int int = 0;
        try{
            flage=key.buildKey("999999999999999",int,PubKeyPath);
        }catch (Exception e){


        }
        if(!flage){
            return;
        }
        t=new chinapay.SecureLink(key);
        flage1=t.verifyTransResponse(merId,ordno,amount,currencycode,transdate,transtype,status,checkvalue);
        if(!flage1){
           System.out.print("驗籤失敗");
         //   return;
        }else{
            if(status.equals("1001")){
                System.out.print("支付成功");
            }else {
                System.out.print("支付失敗");
            }
        }
          //告訴銀聯你接收到訊息了,否則銀聯會不停的傳送資訊
          resp.getWriter().write(JSON.toJSONString(cpCyberBankRes));
          resp.getWriter().flush();
    }    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request,response);    } 支付測試程式碼完成!!!!