基于Java代碼實(shí)現(xiàn)支付充值的通用流程
廢話不多說(shuō)了,直接給大家貼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 代碼如下:*/
/**
* 生成頁(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ǎng)站的支持!
相關(guān)文章
springboot創(chuàng)建線程池的兩種方式小結(jié)
這篇文章主要介紹了springboot創(chuàng)建線程池的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Eclipse中@SpringBootTest注解報(bào)紅的解決方案
這篇文章主要介紹了Eclipse中@SpringBootTest注解報(bào)紅的解決方案,文中給出了原因分析和解決方案,并通過(guò)圖文結(jié)合的方式介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Java特性?Lambda?表達(dá)式和函數(shù)式接口
這篇文章主要介紹了Java特性?Lambda?表達(dá)式和函數(shù)式接口,Lambda表達(dá)式基于函數(shù)式編程思想,也可以稱為閉包,是Java?8引入的重要新特性,?Lambda允許把函數(shù)作為一個(gè)方法的參數(shù)2022-06-06
Spring實(shí)戰(zhàn)之緩存使用condition操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用condition操作,結(jié)合實(shí)例形式分析了Spring緩存使用condition具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-01-01
使用maven構(gòu)建java9 service實(shí)例詳解
本篇文章主要介紹了使用maven構(gòu)建java9 service實(shí)例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
java基礎(chǔ)之接口組成更新的實(shí)現(xiàn)
本文主要介紹了java基礎(chǔ)之接口組成更新的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
java實(shí)現(xiàn)簡(jiǎn)單的搜索引擎
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的搜索引擎的相關(guān)資料,需要的朋友可以參考下2016-02-02

