廢話不多說(shuō)了,直接給大家貼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 代碼如下:*/ /** * 生成頁(yè)面數(shù)據(jù) * @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=\"確認(rèn)\" 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) { //商戶編號(hào) String merchantid = PropertiesRead.use_classLoador().getProperty("CMBC.pay.id"); //訂單編號(hào) 商戶的交易定單號(hào),由商戶網(wǎng)站生成,最大長(zhǎng)度30 String merorderid = orderNo; //金 額 String amountsum = amt; //商品種類 String subject = PropertiesRead.use_classLoador().getProperty("CMBC.pay.type");//"empty"; //幣 種 01 為cny String currencytype = "01"; //自動(dòng)調(diào)轉(zhuǎn)取貨頁(yè)面0→不跳轉(zhuǎn);1→跳轉(zhuǎn) String autojump = "1"; //跳轉(zhuǎn)等待時(shí)間 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"); /** * 商戶返回確認(rèn): 0→不返回;1→返回 */ String confirm = "1"; //支付銀行 String merbank = "empty"; //支付類型 0→即時(shí)到賬;1→擔(dān)保交易 String tradetype = "0"; //是否在商戶端選擇銀行:0→其他;1→在商戶端選擇銀行 String bankInput = "0"; //接口版本 String strInterface = "5.00"; //備 注 (可選) 支付備注信息,最大長(zhǎng)度50 String remark = "充值"; //支付銀行卡類型 00→借貸混合;01→純借記 String bankcardtype = "00"; //商品描述 String pdtdnm = "虛擬商品"; //商品描述地址 String pdtdetailurl = PropertiesRead.use_classLoador().getProperty("CMBC.pay.return.detail.url"); //支付密鑰(必填): 需在支付平臺(tái)進(jìn)行設(shè)置,可登錄商戶管理系統(tǒng)進(jìn)行維護(hù),用于上送商戶支付及下傳支付結(jié)果加密 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); // 把請(qǐng)求參數(shù)打包成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); //建立請(qǐng)求 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: 商戶不需要進(jìn)行修改</p> * <p>******科技發(fā)展公司 2009. All rights reserved.</p> */ public class Crypto { /** * 功能:MD5加密 * @param strSrc 加密的源字符串 * @return 加密串 長(zhǎng)度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( "系統(tǒng)不支持的MD5算法!"); } return strDes; } /** * 將字節(jié)數(shù)組轉(zhuǎn)為HEX字符串(16進(jìn)制串) * @param bts 要轉(zhuǎn)換的字節(jié)數(shù)組 * @return 轉(zhuǎn)換后的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; } } /** * 支付返回調(diào)用url(返回頁(yè)面) * @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代碼實(shí)現(xiàn)支付充值的通用流程的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!