java實現支付充值的通用流程

/*支付流程*/html

/****Controller.java 代碼以下:*/java

@RequestMapping(value = "/paySubmit.htm", method = RequestMethod.POST)
public ModelAndView paySubmit(HttpServletRequest request,
        HttpServletResponse response, @RequestParam Map<String, Object> maps){
        ModelAndView model = new ModelAndView("***/submit");
        /**
        * 代碼塊
        */
        return model;
}算法


/*submit.jsp 代碼以下:*/數組


<%@ page contentType="text/html;charset=UTF-8" language="java" trimDirectiveWhitespaces="true" %>
<%@ page import="com.***.util.PayUtil" %>session

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>支付</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
  </head>
  
  <body>app

  <%
      request.setCharacterEncoding("UTF-8");
      String type =  (String) request.getAttribute("type");
      String sHtmlText = "";
      if ("1".equals(type)){
          sHtmlText = PayUtil.buildForm(
                  (String) request.getAttribute("orderNo"),
                  (String) request.getAttribute("amt"),type);
      }else{
          sHtmlText = PayUtil.allInPaybuildForm(
                  (String) request.getAttribute("orderNo"),
                  (String) request.getAttribute("amt"),type,request);
      }
      out.println(sHtmlText);
  %>jsp

  </body>post

</html>網站


/* PayUtil.java 代碼以下:*/ui

/**
 * 生成頁面數據
 * @param url 三方支付的URL
 * @param sPara
 * @param strMethod
 * @return
 */
public static String buildRequest(String url, Map<String, String> sPara, String strMethod) {
    ArrayList keys = new ArrayList(sPara.keySet());
    StringBuffer sbHtml = new StringBuffer();
    sbHtml.append("<form id=\"paySubForm\" name=\"paySubForm\" action=\"" + url + "\" method=\"" + strMethod + "\">");

    for(int i = 0; i < keys.size(); ++i) {
        String name = (String)keys.get(i);
        String value = (String)sPara.get(name);
        sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
    }

    sbHtml.append("<input type=\"submit\" name=\"b1\" value=\"確認\" style=\"display:none;\"></form>");
    sbHtml.append("<script>document.forms[\'paySubForm\'].submit();</script>");
    return sbHtml.toString();
}


/**
 * 以民生支付爲例
 * @param orderNo
 * @param amt
 * @param type
 * @return
 */
public static String buildForm(String orderNo, String amt,String type) {

    //商戶編號
    String merchantid = PropertiesRead.use_classLoador().getProperty("CMBC.pay.id");
    //訂單編號 商戶的交易定單號,由商戶網站生成,最大長度30
    String merorderid = orderNo;
    //金  額
    String amountsum = amt;
    //商品種類
    String subject = PropertiesRead.use_classLoador().getProperty("CMBC.pay.type");//"empty";
    //幣  種 01 爲cny
    String currencytype = "01";
    //自動調轉取貨頁面0→不跳轉;1→跳轉
    String autojump = "1";
    //跳轉等待時間
    String waittime    = "0";
    
    //商戶取貨URL    
    String merurl = PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.page.url");      
    //是否通知商戶: 0→不通知;1→通知
    String informmer = "1";
    //商戶通知URL
    String informurl = PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.notify.url");
    
    /**
     *  商戶返回確認:    0→不返回;1→返回
    */
    String confirm = "1";
    
    //支付銀行
    String merbank = "empty";
    //支付類型 0→即時到帳;1→擔保交易
    String tradetype = "0";

    //是否在商戶端選擇銀行:0→其餘;1→在商戶端選擇銀行
    String bankInput = "0";
    //接口版本
    String strInterface = "5.00";
    
    //備  注 (可選) 支付備註信息,最大長度50
    String remark = "充值";
    //支付銀行卡類型 00→借貸混合;01→純借記
    String bankcardtype = "00";
    
    //商品描述
    String pdtdnm = "虛擬商品";
    //商品描述地址
    String pdtdetailurl = PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.detail.url");
    //支付密鑰(必填): 需在支付平臺進行設置,可登陸商戶管理系統進行維護,用於上送商戶支付及下傳支付結果加密
    String MD5key = PropertiesRead.use_classLoador().getProperty("CMBC.pay.pwd");
    
   //拼接加密的源字符串
   String mac_src="merchantid="+merchantid+"&merorderid="+merorderid
                +"&amountsum="+amountsum+"&subject="+subject
                +"&currencytype="+currencytype+"&autojump="+autojump
                + "&waittime=" + waittime +"&merurl="+merurl
                + "&informmer=" + informmer +"&informurl=" +informurl
                + "&confirm=" + confirm + "&merbank=" + merbank
                + "&tradetype=" + tradetype + "&bankInput=" + bankInput
                + "&interface=" + strInterface + "&bankcardtype=" + bankcardtype
                + "&pdtdetailurl=" + pdtdetailurl + "&merkey="+MD5key;
   
   
    String mac = Crypto.GetMessageDigest(mac_src);
    
    // 把請求參數打包成map
    Map<String, String> sParaTemp = new HashMap<String,String>();
    sParaTemp.put("merchantid", merchantid);
    sParaTemp.put("merorderid", merorderid);
    sParaTemp.put("amountsum", amountsum);
    sParaTemp.put("subject", subject);
    sParaTemp.put("currencytype", currencytype);
    sParaTemp.put("autojump", autojump);
    sParaTemp.put("waittime", waittime);
    sParaTemp.put("merurl", merurl);
    sParaTemp.put("informmer", informmer);
    sParaTemp.put("informurl", informurl);
    sParaTemp.put("confirm", confirm);
    sParaTemp.put("merbank", merbank);
    sParaTemp.put("tradetype", tradetype);
    sParaTemp.put("bankInput", bankInput);
    sParaTemp.put("interface", strInterface);
    sParaTemp.put("remark", remark);
    sParaTemp.put("bankcardtype", bankcardtype);
    sParaTemp.put("pdtdnm", pdtdnm);
    sParaTemp.put("pdtdetailurl", pdtdetailurl);
    sParaTemp.put("mac", mac);

    //創建請求
    String sHtmlText = buildRequest(PropertiesRead.use_classLoador().getProperty("CMBC.pay.url"), sParaTemp, "post");

    logger.info("McPay request: {}", sHtmlText);

    return sHtmlText;
}

/" Crypto.java 代碼以下 "/
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * <p>Title: MD5加密算法</p>
 * <p>Description: 商戶不須要進行修改</p>
 * <p>******科技發展公司 2009. All rights reserved.</p>
 */
public class Crypto {

    /**
     * 功能:MD5加密
     * @param strSrc 加密的源字符串
     * @return 加密串 長度32位
     */
    public static String GetMessageDigest(String strSrc) {
        MessageDigest md = null;
        String strDes = null;
        final String ALGO_MD5 = "MD5";

        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance(ALGO_MD5);
            md.update(bt);
            strDes = bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(
                "系統不支持的MD5算法!");
        }
        return strDes;
    }

    /**
     * 將字節數組轉爲HEX字符串(16進制串)
     * @param bts 要轉換的字節數組
     * @return 轉換後的HEX串
     */
    public static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }

}

         /**      * 支付返回調用url(返回頁面)      * @param session      * @param request      * @return      */     @RequestMapping(value = "/allPayReturn.htm", method = RequestMethod.POST)     public ModelAndView allInPayReturnCall(HttpServletRequest request,             HttpServletResponse response, @RequestParam Map<String, Object> maps){         ModelAndView model = new ModelAndView("***/payReturn");         /**         * 代碼塊         */         return model;     }            

相關文章
相關標籤/搜索