|
@@ -0,0 +1,368 @@
|
|
|
+package com.pj.tb_wallet_record;
|
|
|
+
|
|
|
+import cn.dev33.satoken.spring.SpringMVCUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.XmlUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import cn.hutool.log.StaticLog;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.github.binarywang.wxpay.bean.ecommerce.RefundsRequest;
|
|
|
+import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
|
|
|
+import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
|
|
|
+import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
|
|
+import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
|
|
+import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
|
|
|
+import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
|
|
+import com.github.binarywang.wxpay.config.WxPayConfig;
|
|
|
+import com.github.binarywang.wxpay.constant.WxPayConstants;
|
|
|
+import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
+import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
+import com.pj.common.core.utils.StringUtils;
|
|
|
+import com.pj.current.dto.APPLoginUserInfo;
|
|
|
+import com.pj.current.satoken.StpAPPUserUtil;
|
|
|
+import com.pj.enummj.DeleteStatus;
|
|
|
+import com.pj.tb_banner.TbBanner;
|
|
|
+import com.pj.tb_people.TbPeople;
|
|
|
+import com.pj.tb_people.TbPeopleService;
|
|
|
+import com.pj.tb_wallet_record.vo.NotifyBO;
|
|
|
+import com.pj.tb_wallet_record.vo.WalletRecordAppVo;
|
|
|
+import com.pj.tb_wallet_topdown.TbWalletTopdown;
|
|
|
+import com.pj.tb_wallet_topdown.TbWalletTopdownService;
|
|
|
+import com.pj.tb_wallet_topup.TbWalletTopup;
|
|
|
+import com.pj.tb_wallet_topup.TbWalletTopupService;
|
|
|
+import com.pj.utils.LogUtil;
|
|
|
+import com.pj.utils.cache.RedisUtil;
|
|
|
+import com.pj.utils.sg.AjaxJson;
|
|
|
+import com.pj.utils.sg.WebNbUtil;
|
|
|
+import com.pj.utils.so.SoMap;
|
|
|
+import com.wechat.pay.java.core.Config;
|
|
|
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
+import com.wechat.pay.java.service.payments.app.AppService;
|
|
|
+import com.wechat.pay.java.service.payments.app.AppServiceExtension;
|
|
|
+import com.wechat.pay.java.service.payments.app.model.Amount;
|
|
|
+import com.wechat.pay.java.service.payments.app.model.PrepayRequest;
|
|
|
+import com.wechat.pay.java.service.payments.app.model.PrepayResponse;
|
|
|
+import com.wechat.pay.java.service.payments.app.model.PrepayWithRequestPaymentResponse;
|
|
|
+import io.swagger.models.auth.In;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.crypto.Mac;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.Writer;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Controller: tb_wallet_record -- 服务点交易记录
|
|
|
+ *
|
|
|
+ * @author plj
|
|
|
+ */
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/app/WalletManage/")
|
|
|
+@Slf4j
|
|
|
+public class TbWalletAppController {
|
|
|
+
|
|
|
+ public static final String CZ_TABLE_NAME = "topup";
|
|
|
+ public static final String TX_TABLE_NAME = "topdown";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 底层 Service 对象
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ TbWalletRecordService tbWalletRecordService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 底层 Service 对象
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ TbWalletTopupService tbWalletTopupService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 底层 Service 对象
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ TbWalletTopdownService tbWalletTopdownService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 底层 Service 对象
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ TbPeopleService tbPeopleService;
|
|
|
+
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AppServiceExtension appServiceExtension;
|
|
|
+ @Resource
|
|
|
+ private WxPayProperties wxPayProperties;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查集合 - 根据条件(参数为空时代表忽略指定条件)
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("getList")
|
|
|
+ public AjaxJson getList() {
|
|
|
+ SoMap so = SoMap.getRequestSoMap();
|
|
|
+ Integer userType = StpAPPUserUtil.getAPPLoginInfo().getUserType();
|
|
|
+ if (userType == 1 || userType == 2) {
|
|
|
+ List<WalletRecordAppVo> list = tbWalletRecordService.getAPPList(so.startPage());
|
|
|
+ if (list.size() == 0) return AjaxJson.getSuccess("暂无更多的订单消息!");
|
|
|
+ return AjaxJson.getPageData(so.getDataCount(), list);
|
|
|
+ } else {
|
|
|
+ return AjaxJson.getError("暂无更多的订单消息!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查服务点充值 - 根据id
|
|
|
+ */
|
|
|
+ @RequestMapping("getTopUpById")
|
|
|
+ public AjaxJson getTopUpById(Long id) {
|
|
|
+ TbWalletTopup t = tbWalletTopupService.getById(id);
|
|
|
+ return AjaxJson.getSuccessData(t);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查服务点提现- 根据id
|
|
|
+ */
|
|
|
+ @RequestMapping("getTopDownById")
|
|
|
+ public AjaxJson getTopDownById(Long id) {
|
|
|
+ TbWalletTopdown t = tbWalletTopdownService.getById(id);
|
|
|
+ return AjaxJson.getSuccessData(t);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查服务点扣除 - 根据id
|
|
|
+ */
|
|
|
+ @RequestMapping("getWalletRecordById")
|
|
|
+ public AjaxJson getWalletRecordById(Long id) {
|
|
|
+ TbWalletRecord t = tbWalletRecordService.getById(id);
|
|
|
+ return AjaxJson.getSuccessData(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 服务点充值
|
|
|
+ */
|
|
|
+ @RequestMapping("topupSave")
|
|
|
+ public AjaxJson topupSave(@RequestParam("amount") Integer amount, @RequestParam("goodsName") String goodsName) {
|
|
|
+ //获取登录人
|
|
|
+ APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
|
|
|
+ TbPeople tbPeople = tbPeopleService.getById(appLoginInfo.getFk());
|
|
|
+ try {
|
|
|
+ TbWalletTopup tbWalletTopup = new TbWalletTopup();
|
|
|
+ Date date = new Date();
|
|
|
+ String tradeNo = CZ_TABLE_NAME + System.currentTimeMillis();
|
|
|
+ tbWalletTopup.setMchid(wxPayProperties.getMchId());
|
|
|
+ tbWalletTopup.setOutTradeNo(tradeNo);
|
|
|
+ tbWalletTopup.setGoodsName(goodsName);
|
|
|
+ tbWalletTopup.setAmount(amount);
|
|
|
+ tbWalletTopup.setBeforeAmount(tbPeople.getWallet());
|
|
|
+ tbWalletTopup.setAfterAmount(tbPeople.getWallet() + amount);
|
|
|
+ tbWalletTopup.setPeopleId(tbPeople.getId().toString());
|
|
|
+ tbWalletTopup.setTopupTime(date);
|
|
|
+ tbWalletTopup.setCreateTime(date);
|
|
|
+ tbWalletTopup.setCreateBy(appLoginInfo.getLoginId().toString());
|
|
|
+ tbWalletTopup.setCreateName(appLoginInfo.getLoginName());
|
|
|
+
|
|
|
+ PrepayWithRequestPaymentResponse response = prepay(tradeNo, BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + ""));
|
|
|
+// StaticLog.info("response:{}",response);
|
|
|
+ // 付款中
|
|
|
+ tbWalletTopup.setPayStatus(1)
|
|
|
+ .setPrepayid(response.getPrepayId());
|
|
|
+ tbWalletTopupService.save(tbWalletTopup);
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("appid", response.getAppid());
|
|
|
+ result.put("noncestr", response.getNonceStr());
|
|
|
+ result.put("package", response.getPackageVal());
|
|
|
+ result.put("partnerid", response.getPartnerId());
|
|
|
+ result.put("prepayid", response.getPrepayId());
|
|
|
+ result.put("timestamp", response.getTimestamp());
|
|
|
+ result.put("sign", response.getSign());
|
|
|
+ RedisUtil.forListAdd(wxPayProperties.getMchId() + "-" + wxPayProperties.getAppId(), tradeNo + "-" + System.currentTimeMillis() / 1000);
|
|
|
+ return AjaxJson.getSuccessData(result);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return AjaxJson.getError("微信支付失败," + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * APP支付下单
|
|
|
+ */
|
|
|
+ public PrepayWithRequestPaymentResponse prepay(String tradeNo, Integer fee) {
|
|
|
+
|
|
|
+ PrepayRequest request = new PrepayRequest();
|
|
|
+ // 调用request.setXxx(val)设置所需参数,具体参数可见Request定义
|
|
|
+ request.setAppid(wxPayProperties.getAppId());
|
|
|
+ request.setMchid(wxPayProperties.getMchId());
|
|
|
+ request.setOutTradeNo(tradeNo);
|
|
|
+ request.setDescription(wxPayProperties.getGoodsDescription());
|
|
|
+ request.setNotifyUrl(wxPayProperties.getNotifyUrl());
|
|
|
+ Amount amount = new Amount();
|
|
|
+ amount.setTotal(1);
|
|
|
+ amount.setCurrency("CNY");
|
|
|
+ request.setAmount(amount);
|
|
|
+ // 调用接口
|
|
|
+ return appServiceExtension.prepayWithRequestPayment(request);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 服务点提现
|
|
|
+ */
|
|
|
+ @RequestMapping("topdownSave")
|
|
|
+ public AjaxJson topdownSave(@RequestParam("amount") Integer amount, @RequestParam("goodsName") String goodsName) {
|
|
|
+ //获取登录人
|
|
|
+ APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
|
|
|
+ TbPeople tbPeople = tbPeopleService.getById(appLoginInfo.getFk());
|
|
|
+ if (tbPeople.getWallet() < amount) {
|
|
|
+ return AjaxJson.getError("可提现点数不足!");
|
|
|
+ }
|
|
|
+ tbPeople.setWallet(tbPeople.getWallet() - amount);
|
|
|
+ try {
|
|
|
+ TbWalletTopdown tbWalletTopdown = new TbWalletTopdown();
|
|
|
+ Date date = new Date();
|
|
|
+ tbWalletTopdown.setMchid(wxPayProperties.getMchId());
|
|
|
+ tbWalletTopdown.setOutTradeNo(TX_TABLE_NAME + System.currentTimeMillis());
|
|
|
+ tbWalletTopdown.setGoodsName(goodsName);
|
|
|
+ tbWalletTopdown.setAmount(amount.toString());
|
|
|
+ tbWalletTopdown.setPeopleId(tbPeople.getId().toString());
|
|
|
+ tbWalletTopdown.setTopdownTime(date);
|
|
|
+ tbWalletTopdown.setCreateTime(date);
|
|
|
+ tbWalletTopdown.setCreateBy(appLoginInfo.getLoginId().toString());
|
|
|
+ tbWalletTopdown.setCreateName(appLoginInfo.getLoginName());
|
|
|
+
|
|
|
+ List<TbWalletTopup> list = tbWalletTopupService.findRefundList(tbPeople.getId());
|
|
|
+
|
|
|
+ for (TbWalletTopup tbWalletTopup : list) {
|
|
|
+ //剩余提现点数
|
|
|
+ int refundMoney = tbWalletTopup.getAmount() - tbWalletTopup.getCutCount() - tbWalletTopup.getRefundCount();
|
|
|
+ if (refundMoney > amount) {
|
|
|
+ //提不完
|
|
|
+ refundMoney = amount;
|
|
|
+ tbWalletTopup.setRefundCount(amount);
|
|
|
+ amount=0;
|
|
|
+ } else {
|
|
|
+ tbWalletTopup.setRefundCount(refundMoney);
|
|
|
+ amount = amount - refundMoney;
|
|
|
+ }
|
|
|
+ int total = BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + "");
|
|
|
+ RefundsRequest request = new RefundsRequest();
|
|
|
+ RefundsRequest.Amount refund = RefundsRequest.Amount.builder()
|
|
|
+ .total(total)
|
|
|
+ .currency("CNY")
|
|
|
+ .refund(BaseWxPayRequest.yuanToFen(refundMoney + ""))
|
|
|
+ .build();
|
|
|
+ request.setAmount(refund);
|
|
|
+ request.setTransactionId(tbWalletTopup.getTransactionId());
|
|
|
+ request.setOutTradeNo(tbWalletTopup.getOutTradeNo());
|
|
|
+ request.setReason("服务点提现");
|
|
|
+ request.setSpAppid(wxPayProperties.getAppId());
|
|
|
+ request.setSubMchid(wxPayProperties.getMchId());
|
|
|
+ request.setNotifyUrl(wxPayProperties.getNotifyUrl());
|
|
|
+ tbWalletTopupService.updateById(tbWalletTopup);
|
|
|
+ if (amount == 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 成功
|
|
|
+ tbWalletTopdown.setPayStatus(2);
|
|
|
+ tbWalletTopdownService.updateById(tbWalletTopdown);
|
|
|
+
|
|
|
+ tbPeopleService.updateById(tbPeople);
|
|
|
+ return AjaxJson.getSuccess();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return AjaxJson.getError("提现失败," + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信支付回调接口
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/wxPay/notify", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
|
|
|
+ public void notify(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType("text/html;charset=UTF-8");
|
|
|
+ response.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
+ InputStream in = request.getInputStream();
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ in.close();
|
|
|
+ //xml数据
|
|
|
+ String reponseXml = new String(out.toByteArray(), "utf-8");
|
|
|
+// WxPayOrderNotifyResult result = wxService.parseOrderNotifyResult(reponseXml);
|
|
|
+ WxPayOrderNotifyResult result = null;
|
|
|
+ String resultCode = result.getResultCode();
|
|
|
+ String outTradeNo = result.getOutTradeNo();
|
|
|
+ if ("FAIL".equalsIgnoreCase(resultCode)) {
|
|
|
+ LogUtil.info("订单" + outTradeNo + "支付失败");
|
|
|
+ response.getWriter().write(setXml("SUCCESS", "OK"));
|
|
|
+ } else if ("SUCCESS".equalsIgnoreCase(resultCode)) {
|
|
|
+ LogUtil.info("订单" + outTradeNo + "支付成功");
|
|
|
+ // 充值数据处理
|
|
|
+ if (outTradeNo.indexOf(CZ_TABLE_NAME) > -1) {
|
|
|
+ QueryWrapper<TbWalletTopup> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("outTradeNo", outTradeNo);
|
|
|
+ TbWalletTopup tbWalletTopup = tbWalletTopupService.list(queryWrapper).get(0);
|
|
|
+ tbWalletTopup.setPayStatus(2);
|
|
|
+ tbWalletTopupService.updateById(tbWalletTopup);
|
|
|
+
|
|
|
+ TbPeople tbPeople = tbPeopleService.getById(tbWalletTopup.getPeopleId());
|
|
|
+ tbPeople.setWallet(tbPeople.getWallet() + tbWalletTopup.getAmount());
|
|
|
+ tbPeopleService.updateById(tbPeople);
|
|
|
+ }
|
|
|
+ response.getWriter().write(setXml("SUCCESS", "OK"));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //返回微信服务
|
|
|
+ public static String setXml(String returnCode, String returnMsg) {
|
|
|
+ return "<xml><return_code><![CDATA[" + returnCode + "]]></return_code><return_msg><![CDATA[" + returnMsg + "]]></return_msg></xml>";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "notify")
|
|
|
+ public void notify(@RequestBody NotifyBO notifyBO, HttpServletResponse response) throws Exception {
|
|
|
+ log.info("wx pay notify;{}", JSONUtil.toJsonStr(notifyBO));
|
|
|
+ response.setContentType("application/json");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ Writer writer = response.getWriter();
|
|
|
+ Map<String, String> result = new HashMap<>();
|
|
|
+ result.put("return_code", "SUCCESS");
|
|
|
+ result.put("return_msg", "OK");
|
|
|
+ notifyBO.setTradeStatus("SUCCESS");
|
|
|
+ tbWalletTopupService.WxNotify(notifyBO);
|
|
|
+ writer.write(XmlUtil.mapToXmlStr(result));
|
|
|
+ writer.flush();
|
|
|
+ writer.close();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|