微信支付功能

Posted zcy1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信支付功能相关的知识,希望对你有一定的参考价值。

 

js

wx.request({
url: url + ‘/team!jfchongzhi.action?openid=‘ + openid + ‘&weibi=‘ + weibi,
method: ‘get‘,
header: { ‘Content-Type‘: ‘application/json‘ },
success: function (res) {
console.log(‘预支付回掉:‘ + res.data);
wx.setStorageSync("oid", res.data.res5);
// 发起支付
wx.requestPayment({
‘timeStamp‘: res.data.res3,
‘nonceStr‘: res.data.res4,
‘package‘: res.data.res1,
‘signType‘: ‘MD5‘,
‘paySign‘: res.data.res2,
‘success‘: function (res) {
console.log("支付成功");
wx.request({
url: url + ‘/team!jfschongzhi.action?openid=‘ + openid + ‘&weibi=‘ + weibi + "&oid=" + wx.getStorageSync("oid"),
method: ‘get‘,
header: { ‘Content-Type‘: ‘application/json‘ },
success: function (res) {
console.log("存入数据库")
wx.redirectTo({
url: ‘../team/team‘
})
}
})

},
fail: function (res) {
console.log("res:" + res);
},
complete: function () {
}
})

},
fail: function (res) {
console.log(‘submit fail‘);
},
complete: function (res) {
console.log(‘submit complete‘);
}
})

后台

public void jfschongzhi(){
String openid = request.getParameter("openid");
String pointt = request.getParameter("weibi");
Integer point = Integer.parseInt(pointt);
String oid = request.getParameter("oid");
team=new Team();
List<Team> teams = DAO.findbystring(Team.class, "openId", openid);
for (Team team1 : teams) {
team.setId(team1.getId());
team.setImg(team1.getImg());
team.setNickName(team1.getNickName());
team.setOpenId(team1.getOpenId());
team.setPhone(team1.getPhone());
team.setRegTime(team1.getRegTime());
team.setXinxi(team1.getXinxi());
team.setPoint(team1.getPoint()+point);
Integer Allpoint=point+team1.getAllpoint();
team.setAllpoint(Allpoint);
DAO.update(team);
}
List<WeibiJilu> weibijilus = DAO.findbystring(WeibiJilu.class, "oid", oid);
weibijilu=new WeibiJilu();
for (WeibiJilu weibiJilu1 : weibijilus) {
weibijilu.setId(weibiJilu1.getId());
weibijilu.setMoney(weibiJilu1.getMoney());
weibijilu.setOid(weibiJilu1.getOid());
weibijilu.setOpenid(weibiJilu1.getOpenid());
weibijilu.setPoint(weibiJilu1.getPoint());
weibijilu.setStatus("已付款");
weibijilu.setTeamId(weibiJilu1.getTeamId());
weibijilu.setTime(weibiJilu1.getTime());
weibijilu.setType(weibiJilu1.getType());
DAO.update(weibijilu);
}
}
//添加微币
public String jfchongzhi(){
String pointt = request.getParameter("weibi");
Integer weibi = Integer.parseInt(pointt);
openid = request.getParameter("openid");
award = DAO.findbyid(Award.class, 1);
WeibiJilu weibiJilu = new WeibiJilu();
// weibiJilu.setMoney((double)weibi/award.getDuihuan());
weibiJilu.setOpenid(openid);
weibiJilu.setPoint(weibi);
String orderId = TimeUtil.getOrderId();
orderId= orderId +((int)(Math.random()*9000)+1000);
weibiJilu.setOid(orderId);
double a=weibi/10;
weibiJilu.setMoney(a);
team = (DAO1.findall1V(Team.class, "openId", openid)).get(0);
weibiJilu.setTeamId(team.getId());
weibiJilu.setStatus("待付款");
DAO.save(weibiJilu);
return yuzhifu(openid, orderId, weibiJilu.getMoney(), "https://localhost:8080/wtcbm/notify1!notify3.action");
//return yuzhifu(openid, orderId, weibiJilu.getMoney(), "https://hypfx.xmyunxin.com/notify1!notify3.action");
}

//小程序预支付,直接跳转过来。
public String yuzhifu(String Openid, String oid,Double money,String url){
//日志对象
// System.out.println(money);
Log log = LogFactory.getLog(this.getClass());

//总金额以分为单位,不带小数点
if(money<0.01)
money = 0.01;

String finalmoney = (int)(money*100)+"";

// finalmoney = "1";//测试临时用1分

String out_trade_no = oid;
// 获取openId后调用统一支付接口https://api.mch.weixin.qq.com/pay/unifiedorder
String currTime = TenpayUtil.getCurrTime();
// 8位日期
String strTime = currTime.substring(8,currTime.length());
// 四位随机数
String strRandom = TenpayUtil.buildRandom(4) + "";
// 10位序列号,可以自行调整。
String strReq = strTime + strRandom;
peizhi = DAO.findbyid(Peizhi.class,1);
// 商户号
String mch_id = peizhi.getPartner();
String appid = peizhi.getAppid();
String appsecret = peizhi.getAppsecret();
String partnerkey = peizhi.getPartnerkey();
String nonce_str = strReq;
// 商品描述根据情况修改(无要求就暂用公众号名称)
String body = "XXXXXX";
// 商户订单号
// 订单生成的机器 IP
String spbill_create_ip = org.apache.struts2.ServletActionContext
.getRequest().getRemoteAddr();
// = ((ServletRequest) request).getRemoteAddr();
// 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。
String notify_url = url;
String trade_type = "JSAPI";

SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", appid);
packageParams.put("mch_id", mch_id);
packageParams.put("nonce_str", nonce_str);
packageParams.put("body", body);
packageParams.put("out_trade_no", out_trade_no);
packageParams.put("total_fee", finalmoney);//finalmoney
packageParams.put("spbill_create_ip", spbill_create_ip);
packageParams.put("notify_url", notify_url);
packageParams.put("trade_type", trade_type);
packageParams.put("openid", openid);

HttpServletResponse HttpServletResponse = null;
RequestHandler reqHandler = new RequestHandler(request,
HttpServletResponse);
reqHandler.init(appid, appsecret, partnerkey);

String sign = reqHandler.createSign(packageParams);
String xml = "<xml>" + "<appid>" + appid + "</appid>" + "<mch_id>"
+ mch_id + "</mch_id>" + "<nonce_str>" + nonce_str
+ "</nonce_str>" + "<sign>" + sign + "</sign>"
+ "<body><![CDATA[" + body + "]]></body>"
+ "<out_trade_no>" + out_trade_no + "</out_trade_no>"
+ "<total_fee>" + finalmoney + "</total_fee>"//finalmoney
+ "<spbill_create_ip>" + spbill_create_ip
+ "</spbill_create_ip>" + "<notify_url>" + notify_url
+ "</notify_url>" + "<trade_type>" + trade_type
+ "</trade_type>" + "<openid>" + openid + "</openid>"
+ "</xml>";
String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
String prepay_id = "";
try {
prepay_id = new GetWxOrderno().getPayNo(createOrderURL, xml);
if (prepay_id.equals("")) {
msg = "统一支付接口获取预支付订单出错";
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
log.error("统一支付接口获取预支付订单出错:"+e1);
}
SortedMap<String, String> finalpackage = new TreeMap<String, String>();
String timestamp = Sha1Util.getTimeStamp();
String nonceStr2 = nonce_str;
String prepay_id2 = "prepay_id=" + prepay_id;
String packages = prepay_id2;

finalpackage.put("appId", appid);
finalpackage.put("timeStamp", timestamp);
finalpackage.put("nonceStr", nonceStr2);
finalpackage.put("package", packages);
finalpackage.put("signType", "MD5");
String finalsign = reqHandler.createSign(finalpackage);

/*session.put("package", packages);
session.put("sign", finalsign);
session.put("appid", appid);
session.put("timeStamp", timestamp);
session.put("nonceStr", nonceStr2);*/
jsonresult = new JsonResult();
jsonresult.setRes1(packages);
jsonresult.setRes2(finalsign);
jsonresult.setRes3(timestamp);
jsonresult.setRes4(nonceStr2);
jsonresult.setRes5(out_trade_no);
return "jsonresult";
}

//微币充值回掉
public void notify3() throws Exception {
//获取request与response的输出输入流
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
//声明用于存放request解析结果的Map
Map<String, String> requestMap;
PrintWriter out = response.getWriter();
BufferedReader br = new BufferedReader(new InputStreamReader(
(ServletInputStream) request.getInputStream()));

//获取当前时间字符串time2
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time2 = df1.format(new Date());
//解析request存入map
requestMap = MessageUtil.parseXml1(request);
//获取结果状态码和,什么订单编号
String result_code = requestMap.get("result_code");
String out_trade_no = requestMap.get("out_trade_no");
String openid = requestMap.get("openid");
//通过订单编号查询订单
team = (DAO1.findall1V(Team.class, "openId", openid)).get(0);

WeibiJilu weibiJilu = (DAO1.findall1V(WeibiJilu.class, "oid", out_trade_no)).get(0);

if (weibiJilu != null) {
if (result_code.equals("SUCCESS")) {
String status = weibiJilu.getStatus();

if ("已付款".equals(status)) {
String huifu = "<xml> <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg> </xml>";
out.println(huifu);
out.close();
} else {
weibiJilu.setStatus("已付款");
DAO.update(weibiJilu);
// team.setAllpoint(team.getAllpoint()+weibiJilu.getPoint());
team.setPoint(team.getPoint()+weibiJilu.getPoint());
DAO.update(team);
TeamAward teamAward = new TeamAward();
teamAward.setPoint(weibiJilu.getPoint());
teamAward.setTeamId(team.getId());
teamAward.setYjcontent("充值");
DAO.save(teamAward);
String huifu = "<xml> <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg> </xml>";
out.println(huifu);
out.close();
}
} else {
String huifu = "<xml> <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg> </xml>";
out.println(huifu);
out.close();
}

}

}

























































































































































































































































以上是关于微信支付功能的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序商城如何接入微信支付的功能?

如何开通微信小程序支付功能

微信支付2.0和3.0有啥区别?

h5微信支付功能封装

微信h5支付和jsapi支付的区别

iOS Scheme以及微信、支付宝等第三方功能的实现