服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - 基于Java代码实现支付充值的通用流程

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

2020-05-03 13:49KEEP_MOVING JAVA教程

本文给大家分享一段java核心代码实现支付充值的通用流程,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧

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

具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*支付流程*/
/****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
+"¤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;
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代码实现支付充值的通用流程的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

延伸 · 阅读

精彩推荐