|
|
@@ -2,30 +2,28 @@ package com.izouma.weixin.web;
|
|
|
|
|
|
import com.github.kevinsawicki.http.HttpRequest;
|
|
|
import com.izouma.awesomeadmin.constant.AppConstant;
|
|
|
-import com.izouma.awesomeadmin.model.MemberCoin;
|
|
|
+import com.izouma.awesomeadmin.dto.Result;
|
|
|
import com.izouma.awesomeadmin.model.MemberTicket;
|
|
|
import com.izouma.awesomeadmin.model.ProductInfo;
|
|
|
-import com.izouma.awesomeadmin.service.MemberCoinService;
|
|
|
import com.izouma.awesomeadmin.service.MemberTicketService;
|
|
|
import com.izouma.awesomeadmin.service.ProductInfoService;
|
|
|
-import com.izouma.awesomeadmin.service.UserInfoService;
|
|
|
import com.izouma.awesomeadmin.util.MbappUtil;
|
|
|
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
|
|
|
import com.izouma.weixin.dao.WxpayTempMapper;
|
|
|
import com.izouma.weixin.model.WxpayTemp;
|
|
|
-import com.izouma.weixin.service.WeiXinService;
|
|
|
import com.izouma.weixin.util.WeixinUtil;
|
|
|
-import com.izouma.weixin.wxpay.WXPayUtil;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
@@ -33,10 +31,7 @@ import javax.servlet.http.HttpSession;
|
|
|
import java.io.BufferedOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.StringJoiner;
|
|
|
-import java.util.TreeMap;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Controller
|
|
|
@RequestMapping("/lyf")
|
|
|
@@ -61,54 +56,82 @@ public class LeYunFuController {
|
|
|
logger.error("lyf:notify");
|
|
|
try {
|
|
|
Map<String, String> m = notifyMap(request, response);
|
|
|
- if (m != null && "SUCCESS".equals(m.get("result_code"))
|
|
|
- && "SUCCESS".equals(m.get("payment_status"))) {//支付成功
|
|
|
+ payResultChange(m);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("微信支付回调异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return "SUCCESS";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付结果
|
|
|
+ *
|
|
|
+ * @param m
|
|
|
+ */
|
|
|
+ public void payResultChange(Map<String, String> m) {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (m != null && "SUCCESS".equals(m.get("return_code"))) {//支付成功
|
|
|
String attach = m.get("attach");
|
|
|
JSONObject attachJson = new JSONObject(attach);
|
|
|
|
|
|
- WxpayTemp wxpayTemp = new WxpayTemp();
|
|
|
- wxpayTemp.setUserId(attachJson.getString("userId"));
|
|
|
- wxpayTemp.setOrderId(attachJson.getString("productId"));
|
|
|
- wxpayTemp.setCash(BigDecimal.valueOf(attachJson.getDouble("cash")));
|
|
|
- wxpayTemp.setTotalAmount(BigDecimal.valueOf(attachJson.getInt("total") / 100.0));
|
|
|
- wxpayTemp.setOutTradeNo(m.get("out_trade_no"));
|
|
|
- wxpayTemp.setTransactionId(m.get("transaction_id"));
|
|
|
+ WxpayTemp checkTemp = new WxpayTemp();
|
|
|
+ checkTemp.setOutTradeNo(m.get("out_trade_no"));
|
|
|
|
|
|
- wxpayTempMapper.updateByOutTradeNo(wxpayTemp);
|
|
|
|
|
|
- logger.info("buyProductInfo");
|
|
|
- try {
|
|
|
- ProductInfo productInfo = productInfoService.getProductInfoById(attachJson.getString("productId"));
|
|
|
- if (productInfo != null) {
|
|
|
- if (AppConstant.ProductType.RECHARGE == productInfo.getTypeFlag()) {//如果是充值类型
|
|
|
+ checkTemp = wxpayTempMapper.queryWxpayTemp(checkTemp);
|
|
|
|
|
|
- MemberTicket memberTicket = new MemberTicket();
|
|
|
- memberTicket.setMoney(productInfo.getCoin());
|
|
|
- memberTicket.setUserId(Integer.valueOf(attachJson.getString("userId")));
|
|
|
- memberTicket.setRemark("购买" + productInfo.getProductName() + "套餐,充值");
|
|
|
- memberTicket.setRechargeType(AppConstant.RechargeType.WEI_XIN);
|
|
|
+ if (checkTemp != null && "WAIT_BUYER_PAY".equals(checkTemp.getResultCode())) {
|
|
|
|
|
|
- memberTicketService.recharge(memberTicket);
|
|
|
+ WxpayTemp wxpayTemp = new WxpayTemp();
|
|
|
+ wxpayTemp.setOutTradeNo(m.get("out_trade_no"));
|
|
|
+ wxpayTemp.setUserId(attachJson.getString("userId"));
|
|
|
+ wxpayTemp.setOrderId(attachJson.getString("productId"));
|
|
|
+ wxpayTemp.setCash(BigDecimal.valueOf(attachJson.getDouble("cash")));
|
|
|
+ wxpayTemp.setTotalAmount(BigDecimal.valueOf(attachJson.getInt("total") / 100.0));
|
|
|
+ wxpayTemp.setOutTradeNo(m.get("out_trade_no"));
|
|
|
+ wxpayTemp.setTransactionId(m.get("transaction_id"));
|
|
|
+ wxpayTemp.setResultCode(m.get("payment_status"));
|
|
|
+ wxpayTempMapper.updateByOutTradeNo(wxpayTemp);
|
|
|
|
|
|
+ if ("SUCCESS".equals(m.get("payment_status"))) {
|
|
|
|
|
|
- } else {
|
|
|
- //TODO 购买VIP
|
|
|
- }
|
|
|
+ logger.info("buyProductInfo");
|
|
|
+ try {
|
|
|
+ ProductInfo productInfo = productInfoService.getProductInfoById(attachJson.getString("productId"));
|
|
|
+ if (productInfo != null) {
|
|
|
+ if (AppConstant.ProductType.RECHARGE == productInfo.getTypeFlag()) {//如果是充值类型
|
|
|
|
|
|
- }
|
|
|
+ MemberTicket memberTicket = new MemberTicket();
|
|
|
+ memberTicket.setMoney(productInfo.getCoin());
|
|
|
+ memberTicket.setUserId(Integer.valueOf(attachJson.getString("userId")));
|
|
|
+ memberTicket.setRemark("购买" + productInfo.getProductName() + "套餐,充值");
|
|
|
+ memberTicket.setRechargeType(AppConstant.RechargeType.WEI_XIN);
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("buyProductInfo", e);
|
|
|
+ memberTicketService.recharge(memberTicket);
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //TODO 购买VIP
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("buyProductInfo", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- logger.error("微信支付回调异常", e);
|
|
|
+ logger.error("支付结果转换异常", e);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- return "SUCCESS";
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -181,11 +204,13 @@ public class LeYunFuController {
|
|
|
|
|
|
WxpayTemp wxpayTemp = new WxpayTemp();
|
|
|
wxpayTemp.setOrderId(productId);
|
|
|
+ wxpayTemp.setUserId(userId);
|
|
|
wxpayTemp.setCash(BigDecimal.valueOf(cash));
|
|
|
wxpayTemp.setTotalAmount(BigDecimal.valueOf(total / 100.0));
|
|
|
wxpayTemp.setOutTradeNo(out_trade_no);
|
|
|
wxpayTemp.setTypeFlag(AppConstant.Aliapi.BUY);
|
|
|
wxpayTemp.setResultCode("WAIT_BUYER_PAY");
|
|
|
+ wxpayTemp.setCreateUser("lyf");
|
|
|
wxpayTempMapper.insertSelective(wxpayTemp);
|
|
|
|
|
|
return response;
|
|
|
@@ -199,6 +224,111 @@ public class LeYunFuController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @RequestMapping(value = "/orderquery", method = RequestMethod.GET)
|
|
|
+ @ResponseBody
|
|
|
+ public String orderquery(@RequestParam(required = true, value = "out_trade_no") String out_trade_no) {
|
|
|
+ try {
|
|
|
+
|
|
|
+
|
|
|
+ String nonceStr = MbappUtil.create_nonce_str();
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, String> params = new TreeMap<>();
|
|
|
+ params.put("mch_id", PropertiesFileLoader.getProperties("leyunfu_mch_id")); //商户号
|
|
|
+ params.put("nonce_str", nonceStr); //随机串
|
|
|
+ params.put("out_trade_no", out_trade_no); //商户系统内部的订单号
|
|
|
+
|
|
|
+
|
|
|
+ String signSrc = getSignSrc(params);
|
|
|
+ String sign = DigestUtils.md5Hex(signSrc + "&key=" + PropertiesFileLoader.getProperties("leyunfu_mch_secret"));
|
|
|
+ params.put("sign_type", "MD5"); //签名方式
|
|
|
+ params.put("sign", sign); //签名
|
|
|
+
|
|
|
+ String response = HttpRequest.post(PropertiesFileLoader.getProperties("leyunfu_query_url"))
|
|
|
+ .form(params).body();
|
|
|
+
|
|
|
+
|
|
|
+ return response;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("leyunfu查询订单失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/autoCheckPayResult", method = RequestMethod.GET)
|
|
|
+ @ResponseBody
|
|
|
+ public Result autoCheckPayResult() {
|
|
|
+ try {
|
|
|
+
|
|
|
+ WxpayTemp param = new WxpayTemp();
|
|
|
+ param.setCreateUser("lyf");
|
|
|
+ param.setResultCode("WAIT_BUYER_PAY");
|
|
|
+ List<WxpayTemp> wxpayTempList = wxpayTempMapper.queryAllWxpayTemp(param);
|
|
|
+
|
|
|
+ for (WxpayTemp wxpayTemp : wxpayTempList) {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ String nonceStr = MbappUtil.create_nonce_str();
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, String> params = new TreeMap<>();
|
|
|
+ params.put("mch_id", PropertiesFileLoader.getProperties("leyunfu_mch_id")); //商户号
|
|
|
+ params.put("nonce_str", nonceStr); //随机串
|
|
|
+ params.put("out_trade_no", wxpayTemp.getOutTradeNo()); //商户系统内部的订单号
|
|
|
+
|
|
|
+
|
|
|
+ String signSrc = getSignSrc(params);
|
|
|
+ String sign = DigestUtils.md5Hex(signSrc + "&key=" + PropertiesFileLoader.getProperties("leyunfu_mch_secret"));
|
|
|
+ params.put("sign_type", "MD5"); //签名方式
|
|
|
+ params.put("sign", sign); //签名
|
|
|
+
|
|
|
+ String response = HttpRequest.post(PropertiesFileLoader.getProperties("leyunfu_query_url"))
|
|
|
+ .form(params).body();
|
|
|
+
|
|
|
+ JSONObject responseJson = new JSONObject(response);
|
|
|
+
|
|
|
+ Map<String, String> notifyMap = new HashMap<>();
|
|
|
+
|
|
|
+ notifyMap.put("return_code", responseJson.getString("return_code"));
|
|
|
+ notifyMap.put("return_msg", responseJson.getString("return_msg"));
|
|
|
+ notifyMap.put("mch_id", responseJson.getString("mch_id"));
|
|
|
+ notifyMap.put("payment_status", responseJson.getString("payment_status"));
|
|
|
+ notifyMap.put("transaction_id", responseJson.getString("transaction_id"));
|
|
|
+ notifyMap.put("out_trade_no", responseJson.getString("out_trade_no"));
|
|
|
+ notifyMap.put("payment_type", responseJson.getString("payment_type"));
|
|
|
+ notifyMap.put("attach", responseJson.getString("attach"));
|
|
|
+ notifyMap.put("fee_type", responseJson.getString("fee_type"));
|
|
|
+ notifyMap.put("nonce_str", responseJson.getString("nonce_str"));
|
|
|
+ notifyMap.put("amount", String.valueOf(responseJson.getInt("amount")));
|
|
|
+ notifyMap.put("sign", responseJson.getString("sign"));
|
|
|
+
|
|
|
+ payResultChange(notifyMap);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("查询支付结果异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return new Result(true, "自动检测支付结果成功");
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("leyunfu查询订单失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 解析微信支付回调结果
|
|
|
@@ -215,7 +345,7 @@ public class LeYunFuController {
|
|
|
try {
|
|
|
|
|
|
|
|
|
- notifyMap.put("result_code", request.getParameter("result_code"));
|
|
|
+ notifyMap.put("return_code", request.getParameter("return_code"));
|
|
|
notifyMap.put("return_msg", request.getParameter("return_msg"));
|
|
|
notifyMap.put("mch_id", request.getParameter("mch_id"));
|
|
|
notifyMap.put("payment_status", request.getParameter("payment_status"));
|