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

前端技术 2023/09/09 Java

废话不多说了,直接给大家贴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代码实现支付充值的通用流程的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对phpstudy网站的支持!

本文地址:https://www.stayed.cn/item/25640

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。