123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- package com.pj.api.wx.service;
- import cn.hutool.cache.CacheUtil;
- import cn.hutool.cache.impl.TimedCache;
- import cn.hutool.core.net.URLDecoder;
- import cn.hutool.core.util.NumberUtil;
- import cn.hutool.core.util.RandomUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.core.util.XmlUtil;
- import cn.hutool.crypto.SecureUtil;
- import cn.hutool.http.HttpUtil;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.alibaba.fastjson.JSON;
- import com.pj.api.wx.WxUtils;
- import com.pj.api.wx.bo.*;
- import com.pj.api.wx.vo.PrePayVO;
- import com.pj.current.config.MyConfig;
- import com.pj.current.config.PartConfig;
- import com.pj.current.config.WxConfig;
- import com.pj.project.tb_account.TbAccountService;
- import com.pj.project.tb_business.TbBusiness;
- import com.pj.project.tb_business.TbBusinessService;
- import com.pj.project.tb_business_car.TbBusinessCar;
- import com.pj.project.tb_business_car.TbBusinessCarService;
- import com.pj.project.tb_business_item.TbBusinessItem;
- import com.pj.project.tb_business_item.TbBusinessItemService;
- import com.pj.project.tb_charge_record.TbChargeRecord;
- import com.pj.project.tb_costomer.TbCostomer;
- import com.pj.project.tb_costomer.TbCostomerService;
- import com.pj.project.tb_fee_details.TbFeeDetailsService;
- import com.pj.project.tb_fee_statistics.TbFeeStatisticsService;
- import com.pj.project.tb_pay_record.TbPayRecord;
- import com.pj.project.tb_pay_record.TbPayRecordService;
- import com.pj.utils.cache.RedisUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import java.math.BigDecimal;
- import java.nio.charset.Charset;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- @Service
- @Transactional
- @Slf4j
- public class WxService {
- @Resource
- @Lazy
- private TbBusinessService tbBusinessService;
- @Resource
- WxConfig wxConfig;
- @Resource
- MyConfig myConfig;
- @Resource
- PartConfig partConfig;
- @Resource
- WxUtils wxUtils;
- @Resource
- TbPayRecordService tbPayRecordService;
- @Resource
- TbCostomerService tbCostomerService;
- @Resource
- TbBusinessCarService tbBusinessCarService;
- @Resource
- @Lazy
- TbBusinessItemService tbBusinessItemService;
- @Resource
- TbFeeStatisticsService tbFeeStatisticsService;
- @Resource
- private TbAccountService tbAccountService;
- @Resource
- TbFeeDetailsService tbFeeDetailsService;
- /**
- * 统一下单接口
- */
- public Map<String, String> prePay(HttpServletRequest request) throws Exception {
- String tradeType = request.getParameter("tradeType");
- String money = request.getParameter("money");
- String openid = request.getParameter("openid");
- String desc = request.getParameter("desc");
- // String openid = "oDWvn5w-hVkzUuKeY7OBXBV_l1rU";
- String businessId = request.getParameter("b");
- String c = request.getParameter("c");
- String a = request.getParameter("a");
- Attach atchMap = new Attach();
- atchMap.setC(c).setB(businessId).setA(a);
- String out_trade_no = RandomUtil.randomString(32);
- Map<String, String> params = new HashMap<>();
- params.put("appid", wxConfig.getAppId());
- params.put("mch_id", wxConfig.getMachId());
- params.put("openid", openid);
- params.put("nonce_str", RandomUtil.randomString(32));
- params.put("body", desc);
- params.put("out_trade_no", out_trade_no);
- params.put("attach", JSONUtil.toJsonStr(atchMap));
- String total_free = partConfig.isTestEnv() ? "1" : NumberUtil.mul(money, 100 + "").intValue() + "";
- log.info("pay free:{}", total_free);
- params.put("total_fee", total_free);
- // params.put("total_fee", "1");
- params.put("spbill_create_ip", getIpAddress(request));
- params.put("notify_url", myConfig.getDomain() + "/wx/notify");
- params.put("trade_type", tradeType);
- params.put("scene_info", SceneInfoBO.getSceneInfo(myConfig.getDomain(), wxConfig.getTitle()));
- String sign = wxUtils.sign(params, wxConfig.getKey());
- log.info("wx pay sign result:{}", sign);
- params.put("sign", sign);
- String prePayUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
- String xmlParam = XmlUtil.mapToXmlStr(params);
- log.info("wx pay xml params:{}", xmlParam);
- String result = HttpUtil.createPost(prePayUrl).header("Content-Type", "text/xml").body(xmlParam)
- .execute().body();
- Map<String, Object> resultMap = XmlUtil.xmlToMap(result);
- log.info("request pre pay url result:{}", resultMap);
- PrePayVO vo = new PrePayVO();
- if ("SUCCESS".equals(resultMap.get("return_code")) && "SUCCESS".equals(resultMap.get("result_code"))) {
- log.info("生成预支付订单成功,mweb_url:{}", resultMap.get("mweb_url"));
- Object webUrl = resultMap.get("mweb_url");
- Object prePayId = resultMap.get("prepay_id");
- String preId = prePayId == null ? "" : prePayId.toString();
- vo.setPayUrl(webUrl == null ? "" : webUrl.toString())
- .setPrePayId(preId)
- .setTradeType(tradeType);
- return getPayParams(openid, preId);
- }
- throw new Exception("支付信息有误");
- }
- public Map<String, String> getPayParams(String openid, String prePayId) {
- Map<String, String> signMap = new HashMap<>(10);
- signMap.put("appId", wxConfig.getAppId());
- signMap.put("timeStamp", System.currentTimeMillis() / 1000 + "");
- signMap.put("nonceStr", RandomUtil.randomString(32));
- signMap.put("signType", "MD5");
- signMap.put("package", "prepay_id=" + prePayId);
- String sign = wxUtils.sign(signMap, wxConfig.getKey());
- signMap.put("paySign", sign);
- signMap.put("openId", openid);
- return signMap;
- }
- public Map<String, String> getPayP(String timeStamp, String nonceStr, String openid, String pack) {
- Map<String, String> signMap = new HashMap<>(10);
- signMap.put("appId", wxConfig.getAppId());
- signMap.put("timeStamp", timeStamp);
- signMap.put("nonceStr", nonceStr);
- signMap.put("signType", "MD5");
- signMap.put("package", pack);
- String sign = wxUtils.sign(signMap, wxConfig.getKey());
- signMap.put("paySign", sign);
- signMap.put("openId", openid);
- return signMap;
- }
- public static String getIpAddress(HttpServletRequest request) {
- String ip = request.getHeader("x-forwarded-for");
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- if (ip.contains(",")) {
- return ip.split(",")[0];
- } else {
- return ip;
- }
- }
- TimedCache<String, Integer> NotifyCache = CacheUtil.newTimedCache(360000);
- @Async
- public void WxNotify(NotifyBO notifyBO) throws Exception {
- String outTradeNo = notifyBO.getOutTradeNo();
- if (NotifyCache.get(outTradeNo) != null) {
- return;
- }
- NotifyCache.put(outTradeNo, 1);
- String total_fee = notifyBO.getTotalFee();
- BigDecimal money = new BigDecimal(total_fee).divide(new BigDecimal(100), 2, BigDecimal.ROUND_UP);
- log.info("wx pay notify;{}", JSONUtil.toJsonStr(notifyBO));
- String tradeNo = notifyBO.getOutTradeNo();
- tradeNo = StrUtil.subBefore(tradeNo, "_", true);
- String attachStr = notifyBO.getAttach();
- String transactionId = notifyBO.getTransactionId();
- Date now = new Date();
- if (StrUtil.isNotEmpty(attachStr)) {
- Attach attach = JSONUtil.toBean(attachStr, Attach.class);
- List<PriceBO> cars = JSONUtil.toList(attach.getC(), PriceBO.class);
- tbFeeStatisticsService.countParkFee(cars);//停车费日统计
- for (PriceBO bo1 : cars) {
- TbBusinessCar car = tbBusinessCarService.getById(bo1.getId());
- BigDecimal price = bo1.getP();
- car.setPay(1).setMoney(car.getMoney().add(price)).setPayTime(now);
- tbBusinessCarService.updateById(car);
- TbBusiness business = tbBusinessService.getById(car.getBusinessId());
- if (business != null) {
- business.setPayMoney(business.getPayMoney().add(price));
- business.setPayStatus(business.getPayMoney().equals(business.getTotalMoney()) ? 3 : 4);
- tbBusinessService.updateById(business);
- }
- }
- tbFeeDetailsService.chargeParkFee(cars, transactionId);//添加cars的收费明细
- String businessId = attach.getB();
- if (StrUtil.isNotEmpty(businessId)) {
- List<TbBusinessItem> items = tbBusinessItemService.findByBusinessId(businessId);
- tbFeeStatisticsService.countBusinessFee(items, transactionId);//业务费日统计
- items.forEach(tbBusinessItem -> tbBusinessItem.setPayStatus(1).setPayTime(now));
- tbBusinessItemService.updateBatchById(items);
- TbBusiness tbBusiness = tbBusinessService.getById(businessId);
- tbBusiness.setPayTime(now).setPayType(3).setConfirmInput(1).setConfirmInputTime(now)
- .setPayMoney(tbBusiness.getPayMoney().add(tbBusiness.getItemPrice()))
- .setPayNo(transactionId);
- tbBusiness.setPayStatus(tbBusiness.getPayMoney().equals(tbBusiness.getTotalMoney()) ? 3 : 4);
- tbBusinessService.updateById(tbBusiness);
- TbPayRecord payRecord = new TbPayRecord();
- TbCostomer tbCostomer = tbCostomerService.getById(tbBusiness.getCustomerId());
- payRecord.setCreateTime(now)
- .setOutTradeNo(tradeNo)
- .setTransactionId(transactionId)
- .setPayMoney(money);
- if (tbCostomer != null) {
- payRecord.setCustomerId(tbCostomer.getId()).setCustomerName(tbCostomer.getName());
- }
- tbPayRecordService.save(payRecord);
- }
- String a = attach.getA();
- if (StrUtil.isNotEmpty(a)) {
- AccountChargeBO chargeBO = JSONUtil.toBean(a, AccountChargeBO.class);
- tbAccountService.recharge(chargeBO.getId(), chargeBO.getC(), TbChargeRecord.Ch.WECHAT.getType(), money);
- }
- }
- }
- public String getRedirectUrl(String path, String state) {
- String redirectUrl = myConfig.getWebDomain() + path;
- String encoderUrl = URLDecoder.decode(redirectUrl, Charset.forName("utf-8"));
- String url = wxConfig.getAuthLoginUrl().replace("REDIRECT_URI", encoderUrl);
- if (StrUtil.isNotEmpty(state)) {
- url = url.replace("STATE", state);
- }
- log.info("get redirect url:{}", url);
- return url;
- }
- public String getOpenidByCode(String code, String openid) {
- String openidUrl = wxConfig.getOpenidUrl().replace("CODE", code);
- String resp = HttpUtil.get(openidUrl);
- JSONObject result = JSONUtil.parseObj(resp);
- log.info("get openid result:{}", resp);
- String newOpenid = result.getStr("openid");
- if (StrUtil.isNotEmpty(newOpenid)) {
- return newOpenid;
- }
- return openid;
- }
- public SortedMap getWxConfig(String url) {
- Long nowTime = System.currentTimeMillis() / 1000;
- String jsTicket = RedisUtil.get(wxConfig.getJsApiTicketKey());
- SortedMap<String, Object> params = new TreeMap<>();
- params.put("noncestr", RandomUtil.randomString(32));
- params.put("jsapi_ticket", jsTicket);
- params.put("timestamp", nowTime);
- params.put("url", url);
- String sign = SecureUtil.sha1(params.toString().substring(1, params.toString().lastIndexOf("}")).replaceAll(", ", "&"));
- params.put("sign", sign);
- params.put("appId", wxConfig.getAppId());
- return params;
- }
- @Async
- public void sendTemplateMsg(String templateId, String openid, MsgDataBO data) {
- String accessToken = RedisUtil.get(wxConfig.getAccessTokenKey());
- BaseTemplate baseTemplate = new BaseTemplate(openid, templateId, data);
- String json = JSON.toJSONString(baseTemplate);
- String url = wxConfig.getSendMsgUrl().replace("ACCESS_TOKEN", accessToken);
- String resp = HttpUtil.post(url, json);
- log.info("send wx msg{},{},{};return :{}", templateId, openid, JSONUtil.toJsonStr(data), resp);
- }
- @Async
- public void sendTemplateMsg(String templateId, String openid, MsgDataBO data, String detailUrl) {
- String accessToken = RedisUtil.get(wxConfig.getAccessTokenKey());
- BaseTemplate baseTemplate = new BaseTemplate(openid, templateId, detailUrl, data);
- String json = JSON.toJSONString(baseTemplate);
- String url = wxConfig.getSendMsgUrl().replace("ACCESS_TOKEN", accessToken);
- String resp = HttpUtil.post(url, json);
- log.info("send wx msg{},{},{};return :{}", templateId, openid, JSONUtil.toJsonStr(data), resp);
- }
- }
|