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

作者:KEEP_MOVING 时间:2021-09-04 00:48:45 

废话不多说了,直接给大家贴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代码实现支付充值的通用流程的相关知识网站的支持!

标签:java,支付,充值
0
投稿

猜你喜欢

  • java可以作为第一门编程语言学习吗

    2021-05-25 07:52:37
  • Spring框架构造注入type属性实例详解

    2021-10-04 19:45:19
  • android输入框与文本框加滚动条scrollview示例

    2023-07-29 09:22:42
  • Unity UGUI实现卡片椭圆方向滚动

    2022-06-03 06:02:53
  • WheelView实现上下滑动选择器

    2023-01-02 18:31:03
  • android商户扫码枪读取手机二维码

    2023-11-09 12:58:24
  • Java 数据结构之堆的概念与应用

    2023-11-24 05:36:18
  • C#简单获取时间差的小例子

    2022-02-20 10:12:34
  • Spring生命周期回调与容器扩展详解

    2023-04-05 16:26:31
  • C#简单的向量用法实例教程

    2022-09-27 09:57:29
  • 解读@RequestBody与post请求的关系

    2022-10-07 02:02:51
  • Spring(AbstractRoutingDataSource)实现动态数据源切换示例

    2022-05-23 07:05:08
  • SpringBoot如何动态改变日志级别

    2021-08-29 02:32:17
  • Java编程二项分布的递归和非递归实现代码实例

    2023-08-07 09:38:04
  • springboot FeignClient注解及参数

    2021-07-09 21:59:07
  • C#使用Lambda表达式简化代码的示例详解

    2022-09-16 03:03:36
  • Android中实现TCP和UDP传输实例

    2021-11-08 14:19:59
  • Android开发之保存图片到相册的三种方法详解

    2022-07-26 04:49:10
  • Android Studio自动提取控件Style样式教程

    2022-01-29 14:09:55
  • C#实现简单订单管理程序

    2023-04-08 08:26:34
  • asp之家 软件编程 m.aspxhome.com