欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Java代码实现支付充值的通用流程

程序员文章站 2024-03-11 17:41:25
废话不多说了,直接给大家贴java代码了。 具体代码如下所示: /*支付流程*/ /****controller.java 代码如下:*/ @request...

废话不多说了,直接给大家贴java代码了。

具体代码如下所示:

/*支付流程*/
/****controller.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" %>
<!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>
<%
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);
%>
</body>
</html>
/* payutil.java 代码如下:*/
/**
* 生成页面数据
* @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;
}

以上所述是小编给大家介绍的基于java代码实现支付充值的通用流程的相关知识,希望对大家有所帮助