微信支付后台:
https://pay.weixin.qq.com
只有这里设置了,程序中才能使用这个key
/**
* 微信支付统一下单接口
*
* @param userOrder 订单信息
* @return
* @throws Exception
*/
public WebResult weixinPay(UserOrder userOrder, String payType, String ip) throws Exception {
System.out.println("请求参数如下:");
System.out.println("订单信息:"+userOrder.toString());
System.out.println("支付方式payType:"+payType);
System.out.println("-----------------------传递来的真实ip:------------" + ip);
// 账号信息
String appId = Configure.getAppid();
// 商业号
String mchId = Configure.getMchid();
// key
String key = Configure.getKey();
// 随机字符串
String currTime = PayCommonUtil.getCurrTime();
String strTime = currTime.substring(8, currTime.length());
String strRandom = PayCommonUtil.buildRandom(4) + "";
String nonceStr = strTime + strRandom;
// 商品名称
String body = "LOVZVZU-商城";//这里非常重要,腾讯有格式要求
// 价格 注意:价格的单位是分
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
//多比子订单
Double totalprice = 0d;
List userOrders = orderService.select(1, 1,
null, null, null,
null, null, null,
userOrder.getOrderno(), null);
for (int i = 0; i < userOrders.size(); i++) {
UserOrder userOrder1 = userOrders.get(i);
totalprice += userOrder1.getPrice();
}
System.out.println("订单总额度:"+totalprice);
List userByWxid = adminUserService.findUserByWxid(userOrder.getCustomeropenid());
if (userByWxid != null&&userByWxid.size()>0) {
AdminUser adminUser = userByWxid.get(0);
Double balance = adminUser.getBalance();
System.out.println("用户余额:"+balance);
if (balance>=totalprice){
totalprice = 0d;
}else {
totalprice = totalprice - balance;
}
}
System.out.println("最后需要支付额度:"+totalprice);
/**
* 1、交易金额
* 交易金额默认为人民币交易,接口中参数支付金额单位为【分】,参数值不能带小数。对账单中的交易金额单位为【元】。
*
* 外币交易的支付金额精确到币种的最小单位,参数值不能带小数点。
*/
String orderPrice = nf.format(totalprice.doubleValue()*100);
// 查询订单数据表获取订单信息
// 回调接口
String notify_url = "http://xxxxxx/payWeixin/weixinNotify";
String time_start = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
Calendar ca = Calendar.getInstance();
ca.setTime(new Date());
ca.add(Calendar.DATE, 1);
String time_expire = new SimpleDateFormat("yyyyMMddHHmmss").format(ca.getTime());
SortedMap
下面是一个示例:
请求xml::::
{
"appid":"wxsdsdfsfs",
"body":"ZU-商城",
"mch_id":"sdsdfsdf",
"nonce_str":"2148063991",
"notify_url":"http://xxxxxxxx/payWeixin/weixinNotify",
"openid":"o_XBZ6AS9TzzJ19odkw310UHXNyY",
"out_trade_no":"201905162148060001",
"sign":"ECAD32EDBBCB0F69482CC85818093262",
"spbill_create_ip":"101.30.55.30",
"time_expire":"20190517214806",
"time_start":"20190516214806",
"total_fee":"10",
"trade_type":"JSAPI"
}
返回xml::::
{
"return_code":"SUCCESS",
"return_msg":"OK",
"appid":"wx39sdsdsdfdsf8",
"mch_id":"15sdfsdfs521",
"nonce_str":"pz8rjFwzwP3SmIAu",
"sign":"F9E748398DE5AD4E64FADB5B38988028",
"result_code":"SUCCESS",
"prepay_id":"wx16214807119260b53874258e3836952547",
"trade_type":"JSAPI"
}
/**
* 微信支付回调方法
*
* @param request
* @param response
* @throws Exception
*/
public void weixin_notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("++++++++++微信支付回调+++++++++++++");
// 读取参数
InputStream inputStream;
StringBuffer sb = new StringBuffer();
inputStream = request.getInputStream();
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
while ((s = in.readLine()) != null) {
sb.append(s);
}
in.close();
inputStream.close();
// 解析xml成map
Map m = new HashMap();
m = XMLUtil.doXMLParse(sb.toString());
// 过滤空 设置 TreeMap
SortedMap packageParams = new TreeMap();
Iterator it = m.keySet().iterator();
while (it.hasNext()) {
String parameter = (String) it.next();
String parameterValue = m.get(parameter);
String v = "";
if (null != parameterValue) {
v = parameterValue.trim();
}
packageParams.put(parameter, v);
}
// 账号信息
String key = Configure.getKey(); // key
String out_trade_no = (String) packageParams.get("out_trade_no");
// logger.info(packageParams);
// 判断签名是否正确
if (PayCommonUtil.isTenpaySign("UTF-8", packageParams, key)) {
// 处理业务开始
String resXml = "";
if ("SUCCESS".equals((String) packageParams.get("result_code"))) {
System.out.println("回调:支付成功");
System.out.println(" 返回参数如下:");
String mch_id = (String) packageParams.get("mch_id");
String openid = (String) packageParams.get("openid");
String is_subscribe = (String) packageParams.get("is_subscribe");
String bank_type = (String) packageParams.get("bank_type");
String total_fee = (String) packageParams.get("total_fee");
String transaction_id = (String) packageParams.get("transaction_id");
System.out.println("mch_id:" + mch_id);
System.out.println("openid:" + openid);
System.out.println("is_subscribe:" + is_subscribe);
System.out.println("out_trade_no:" + out_trade_no);
System.out.println("total_fee:" + total_fee);
System.out.println("bank_type:" + bank_type);
System.out.println("transaction_id:" + transaction_id);
// 执行自己的业务逻辑
// UserOrder userOrder = this.orderService.selectById(Integer.parseInt(out_trade_no));
System.out.println("///执行自己的业务逻辑//");
List userOrders = orderService.select(1, 100,
null, null, null,
null, null,
null, out_trade_no, null);
Double totalprice = 0d;
Boolean canpay = true;
String customeropenid = null;
if (userOrders != null && userOrders.size() > 0) {
for (int i = 0; i < userOrders.size(); i++) {
UserOrder userOrder = userOrders.get(i);
if (userOrder.getCustomeropenid()!=null&&!userOrder.getCustomeropenid().equals("")){
customeropenid = userOrder.getCustomeropenid();
}
totalprice += userOrder.getPrice();
//状态检查
// if (!userOrder.getStatus().equals(UserOrder.STATUS_WAITFORPAY)) {
// canpay = false;
// }
}
List userByWxid = adminUserService.findUserByWxid(customeropenid);
if (userByWxid != null&&userByWxid.size()>0) {
AdminUser adminUser = userByWxid.get(0);
Double balance = adminUser.getBalance();
if (balance>=totalprice){
totalprice = 0d;
}else {
totalprice = totalprice - balance;
}
}
if (canpay) {// 说明还没被处理过orderPrice
System.out.println("订单都处于待支付状态,可正常支付");
//我这里的业务,应该是多个单加在一起
double orderPrice = totalprice;
double wxPayPrice = Double.parseDouble(total_fee) / 100;//获取支付金额
System.out.println("该订单应支付:"+orderPrice+" 用户微信支付:"+wxPayPrice);
if (wxPayPrice == orderPrice) {
System.out.println("微信同步通知交易金额验证通过- 订单号:" + out_trade_no + " 订单价:" + orderPrice + "微信返回支付:" + total_fee);
// payAlipayService.pay(out_trade_no, openid, transaction_id, request, "10");
//自己的业务逻辑
for (int i = 0; i < userOrders.size(); i++) {
UserOrder userOrder = userOrders.get(i);
OrderController orderController = new OrderController();
boolean b = orderController.payOrderCallback(userOrder);
if (!b) {
System.out.println("订单回调业务出错,下面是订单信息");
System.out.println(userOrder.toString());
}
}
resXml = ""
+ " "
+ " "
+ " ";
} else {
System.out.println("微信同步通知交易金额不一致-- 订单号:" + out_trade_no + " 订单价:" + orderPrice + "微信返回支付:" + total_fee);
resXml = ""
+ " "
+ " "
+ " ";
}
} else {
System.out.println("微信同步通知处理结果-订单状态已经被更改" + out_trade_no);
resXml = ""
+ " "
+ " "
+ " ";
}
} else {
System.out.println("微信同步通知该订单号不存在-" + out_trade_no);
resXml = ""
+ " "
+ " "
+ " ";
}
} else {
System.out.println("支付失败,错误信息:" + packageParams.get("err_code") + "-----订单号:::" + out_trade_no
+ "*******支付失败时间::::" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
String err_code = (String) packageParams.get("err_code");
resXml = ""
+ " "
+ " " + " ";
}
// 处理业务完毕
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
} else {
System.out.println("通知签名验证失败---时间::::" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}