基于Java代码实现支付充值的通用流程
废话不多说了,直接给大家贴java代码了。
具体代码如下所示:
/*支付流程*/
/****Controller.java代码如下:*/
@RequestMapping(value="/paySubmit.htm",method=RequestMethod.POST)
publicModelAndViewpaySubmit(HttpServletRequestrequest,
HttpServletResponseresponse,@RequestParamMap<String,Object>maps){
ModelAndViewmodel=newModelAndView("***/submit");
/**
*代码块
*/
returnmodel;
}
/*submit.jsp代码如下:*/
<%@pagecontentType="text/html;charset=UTF-8"language="java"trimDirectiveWhitespaces="true"%>
<%@pageimport="com.***.util.PayUtil"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>支付</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
Stringtype=(String)request.getAttribute("type");
StringsHtmlText="";
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);
%>
</body>
</html>
/*PayUtil.java代码如下:*/
/**
*生成页面数据
*@paramurl三方支付的URL
*@paramsPara
*@paramstrMethod
*@return
*/
publicstaticStringbuildRequest(Stringurl,Map<String,String>sPara,StringstrMethod){
ArrayListkeys=newArrayList(sPara.keySet());
StringBuffersbHtml=newStringBuffer();
sbHtml.append("<formid=\"paySubForm\"name=\"paySubForm\"action=\""+url+"\"method=\""+strMethod+"\">");
for(inti=0;i<keys.size();++i){
Stringname=(String)keys.get(i);
Stringvalue=(String)sPara.get(name);
sbHtml.append("<inputtype=\"hidden\"name=\""+name+"\"value=\""+value+"\"/>");
}
sbHtml.append("<inputtype=\"submit\"name=\"b1\"value=\"确认\"style=\"display:none;\"></form>");
sbHtml.append("<script>document.forms[\'paySubForm\'].submit();</script>");
returnsbHtml.toString();
}
/**
*以民生支付为例
*@paramorderNo
*@paramamt
*@paramtype
*@return
*/
publicstaticStringbuildForm(StringorderNo,Stringamt,Stringtype){
//商户编号
Stringmerchantid=PropertiesRead.use_classLoador().getProperty("CMBC.pay.id");
//订单编号商户的交易定单号,由商户网站生成,最大长度30
Stringmerorderid=orderNo;
//金额
Stringamountsum=amt;
//商品种类
Stringsubject=PropertiesRead.use_classLoador().getProperty("CMBC.pay.type");//"empty";
//币种01为cny
Stringcurrencytype="01";
//自动调转取货页面0→不跳转;1→跳转
Stringautojump="1";
//跳转等待时间
Stringwaittime="0";
//商户取货URL
Stringmerurl=PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.page.url");
//是否通知商户:0→不通知;1→通知
Stringinformmer="1";
//商户通知URL
Stringinformurl=PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.notify.url");
/**
*商户返回确认:0→不返回;1→返回
*/
Stringconfirm="1";
//支付银行
Stringmerbank="empty";
//支付类型0→即时到账;1→担保交易
Stringtradetype="0";
//是否在商户端选择银行:0→其他;1→在商户端选择银行
StringbankInput="0";
//接口版本
StringstrInterface="5.00";
//备注(可选)支付备注信息,最大长度50
Stringremark="充值";
//支付银行卡类型00→借贷混合;01→纯借记
Stringbankcardtype="00";
//商品描述
Stringpdtdnm="虚拟商品";
//商品描述地址
Stringpdtdetailurl=PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.detail.url");
//支付密钥(必填):需在支付平台进行设置,可登录商户管理系统进行维护,用于上送商户支付及下传支付结果加密
StringMD5key=PropertiesRead.use_classLoador().getProperty("CMBC.pay.pwd");
//拼接加密的源字符串
Stringmac_src="merchantid="+merchantid+"&merorderid="+merorderid
+"&amountsum="+amountsum+"&subject="+subject
+"¤cytype="+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;
Stringmac=Crypto.GetMessageDigest(mac_src);
//把请求参数打包成map
Map<String,String>sParaTemp=newHashMap<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);
//建立请求
StringsHtmlText=buildRequest(PropertiesRead.use_classLoador().getProperty("CMBC.pay.url"),sParaTemp,"post");
logger.info("McPayrequest:{}",sHtmlText);
returnsHtmlText;
}
/"Crypto.java代码如下"/
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
/**
*<p>Title:MD5加密算法</p>
*<p>Description:商户不需要进行修改</p>
*<p>******科技发展公司2009.Allrightsreserved.</p>
*/
publicclassCrypto{
/**
*功能:MD5加密
*@paramstrSrc加密的源字符串
*@return加密串长度32位
*/
publicstaticStringGetMessageDigest(StringstrSrc){
MessageDigestmd=null;
StringstrDes=null;
finalStringALGO_MD5="MD5";
byte[]bt=strSrc.getBytes();
try{
md=MessageDigest.getInstance(ALGO_MD5);
md.update(bt);
strDes=bytes2Hex(md.digest());
}catch(NoSuchAlgorithmExceptione){
thrownewIllegalStateException(
"系统不支持的MD5算法!");
}
returnstrDes;
}
/**
*将字节数组转为HEX字符串(16进制串)
*@parambts要转换的字节数组
*@return转换后的HEX串
*/
publicstaticStringbytes2Hex(byte[]bts){
Stringdes="";
Stringtmp=null;
for(inti=0;i<bts.length;i++){
tmp=(Integer.toHexString(bts[i]&0xFF));
if(tmp.length()==1){
des+="0";
}
des+=tmp;
}
returndes;
}
}
/**
*支付返回调用url(返回页面)
*@paramsession
*@paramrequest
*@return
*/
@RequestMapping(value="/allPayReturn.htm",method=RequestMethod.POST)
publicModelAndViewallInPayReturnCall(HttpServletRequestrequest,
HttpServletResponseresponse,@RequestParamMap<String,Object>maps){
ModelAndViewmodel=newModelAndView("***/payReturn");
/**
*代码块
*/
returnmodel;
}
以上所述是小编给大家介绍的基于Java代码实现支付充值的通用流程的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!