| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- package com.izouma.weixin.web;
- import com.github.kevinsawicki.http.HttpRequest;
- import com.izouma.awesomeadmin.constant.AppConstant;
- import com.izouma.awesomeadmin.dto.Result;
- import com.izouma.awesomeadmin.model.MemberTicket;
- import com.izouma.awesomeadmin.model.ProductInfo;
- import com.izouma.awesomeadmin.service.MemberTicketService;
- import com.izouma.awesomeadmin.service.ProductInfoService;
- 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.util.WeixinUtil;
- 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.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 javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.util.*;
- @Controller
- @RequestMapping("/lyf")
- public class LeYunFuController {
- private static Logger logger = Logger.getLogger(LeYunFuController.class);
- @Autowired
- private MemberTicketService memberTicketService;
- @Autowired
- private WxpayTempMapper wxpayTempMapper;
- @Autowired
- private ProductInfoService productInfoService;
- @RequestMapping(value = "/notify", method = RequestMethod.POST)
- @ResponseBody
- public String notify(HttpServletRequest request, HttpServletResponse response) {
- logger.error("lyf:notify");
- try {
- Map<String, String> m = notifyMap(request, response);
- 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 checkTemp = new WxpayTemp();
- checkTemp.setOutTradeNo(m.get("out_trade_no"));
- checkTemp = wxpayTempMapper.queryWxpayTemp(checkTemp);
- if (checkTemp != null && "WAIT_BUYER_PAY".equals(checkTemp.getResultCode())) {
- 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"))) {
- 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);
- memberTicketService.recharge(memberTicket);
- } else {
- //TODO 购买VIP
- }
- }
- } catch (Exception e) {
- logger.error("buyProductInfo", e);
- }
- }
- }
- }
- } catch (Exception e) {
- logger.error("支付结果转换异常", e);
- }
- }
- @RequestMapping(value = "/unifiedOrder", method = RequestMethod.GET)
- @ResponseBody
- public String unifiedOrder(@RequestParam(required = true, value = "userId") String userId,
- @RequestParam(required = true, value = "productId") String productId) {
- try {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- String ip = WeixinUtil.getIpAdderss(request);
- double cash = 1;
- ProductInfo productInfo = productInfoService.getProductInfoById(productId);
- cash = productInfo.getMoney().doubleValue();
- int total = 0;
- total = (int) (cash * 100);
- //测试为一分
- total = 1;
- JSONObject attach = new JSONObject();
- attach.put("productId", productId);
- attach.put("userId", userId);
- attach.put("cash", cash);
- attach.put("total", total);
- String out_trade_no = MbappUtil.create_out_trade_no();
- 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("amount", String.valueOf(total)); //金额 单位分
- params.put("body", productInfo.getProductName()); //商品或支付单简要描述
- params.put("out_trade_no", out_trade_no); //商户系统内部的订单号
- params.put("notify_url", PropertiesFileLoader.getProperties("leyunfu_return_url")); //接收支付结果异步通知回调地址
- params.put("spbill_create_ip", ip); //客户端IP
- params.put("return_url", PropertiesFileLoader.getProperties("leyunfu_notify_url")); //页面回调地址
- params.put("payment_type", "trade.weixin.native"); //支付类型
- params.put("attach", attach.toString()); //附加数据
- 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_pay_url"))
- .form(params).body();
- /**
- * 返回示例:
- {"return_code":"SUCCESS",
- "return_msg":"成功",
- "mch_id":"100000007927",
- "payment_type":"trade.qqpay.native",x
- "code_img_url":"http://xxx"}
- */
- 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;
- } catch (Exception e) {
- logger.error("leyunfu下单失败", e);
- }
- return null;
- }
- @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;
- }
- /**
- * 解析微信支付回调结果
- *
- * @param request
- * @param response
- * @return
- */
- public Map<String, String> notifyMap(HttpServletRequest request, HttpServletResponse response) {
- logger.info("payOrder:支付订单");
- Map<String, String> notifyMap = new HashMap<>();
- String resXml = "SUCCESS";
- try {
- 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"));
- notifyMap.put("transaction_id", request.getParameter("transaction_id"));
- notifyMap.put("out_trade_no", request.getParameter("out_trade_no"));
- notifyMap.put("payment_type", request.getParameter("payment_type"));
- notifyMap.put("attach", request.getParameter("attach"));
- notifyMap.put("fee_type", request.getParameter("fee_type"));
- notifyMap.put("nonce_str", request.getParameter("nonce_str"));
- notifyMap.put("amount", request.getParameter("amount"));
- notifyMap.put("sign", request.getParameter("sign"));
- System.out.println("notifyMap:" + notifyMap.toString());
- // 签名正确
- // 进行处理。
- // 注意特殊情况:订单已经退款,但收到了支付结果成功的通知,不应把商户侧订单状态从退款改成支付成功
- if ("SUCCESS".equals(notifyMap.get("result_code"))) {
- //支付成功
- resXml = "SUCCESS";
- } else {
- resXml = "SUCCESS";
- }
- System.out.println("支付回调数据结束");
- HttpSession seesion = request.getSession();
- seesion.setAttribute("notify", "支付回调");
- try {
- BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
- out.write(resXml.getBytes());
- out.flush();
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (Exception e) {
- logger.error("payOrder:支付订单异常", e);
- }
- return notifyMap;
- }
- private String getSignSrc(Map<String, ?> paramMap) {
- StringJoiner signSrc = new StringJoiner("&");
- paramMap.forEach((key, value) -> {
- if (!"sign".equals(key) && value != null && StringUtils.isNotBlank(value.toString())) {
- signSrc.add(key + "=" + value);
- }
- });
- return signSrc.toString();
- }
- }
|