|
@@ -1,13 +1,19 @@
|
|
|
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;
|
|
@@ -18,33 +24,51 @@ 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.util.Date;
|
|
|
-import java.util.List;
|
|
|
+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";
|
|
@@ -56,11 +80,15 @@ public class TbWalletAppController {
|
|
|
@Autowired
|
|
|
TbWalletRecordService tbWalletRecordService;
|
|
|
|
|
|
- /** 底层 Service 对象 */
|
|
|
+ /**
|
|
|
+ * 底层 Service 对象
|
|
|
+ */
|
|
|
@Autowired
|
|
|
TbWalletTopupService tbWalletTopupService;
|
|
|
|
|
|
- /** 底层 Service 对象 */
|
|
|
+ /**
|
|
|
+ * 底层 Service 对象
|
|
|
+ */
|
|
|
@Autowired
|
|
|
TbWalletTopdownService tbWalletTopdownService;
|
|
|
|
|
@@ -70,11 +98,11 @@ public class TbWalletAppController {
|
|
|
@Autowired
|
|
|
TbPeopleService tbPeopleService;
|
|
|
|
|
|
- /**
|
|
|
- * 微信支付 Service 对象
|
|
|
- */
|
|
|
- @Autowired
|
|
|
- private WxPayService wxService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AppServiceExtension appServiceExtension;
|
|
|
+ @Resource
|
|
|
+ private WxPayProperties wxPayProperties;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -85,107 +113,129 @@ public class TbWalletAppController {
|
|
|
@RequestMapping("getList")
|
|
|
public AjaxJson getList() {
|
|
|
SoMap so = SoMap.getRequestSoMap();
|
|
|
- if(StpAPPUserUtil.getAPPLoginInfo().getUserType() == 1){
|
|
|
+ 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("边民用户才能查看服务点交易记录!");
|
|
|
+ } else {
|
|
|
+ return AjaxJson.getError("暂无更多的订单消息!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- /** 查服务点充值 - 根据id */
|
|
|
+ /**
|
|
|
+ * 查服务点充值 - 根据id
|
|
|
+ */
|
|
|
@RequestMapping("getTopUpById")
|
|
|
- public AjaxJson getTopUpById(Long id){
|
|
|
+ public AjaxJson getTopUpById(Long id) {
|
|
|
TbWalletTopup t = tbWalletTopupService.getById(id);
|
|
|
return AjaxJson.getSuccessData(t);
|
|
|
}
|
|
|
|
|
|
|
|
|
- /** 查服务点提现- 根据id */
|
|
|
+ /**
|
|
|
+ * 查服务点提现- 根据id
|
|
|
+ */
|
|
|
@RequestMapping("getTopDownById")
|
|
|
- public AjaxJson getTopDownById(Long id){
|
|
|
+ public AjaxJson getTopDownById(Long id) {
|
|
|
TbWalletTopdown t = tbWalletTopdownService.getById(id);
|
|
|
return AjaxJson.getSuccessData(t);
|
|
|
}
|
|
|
|
|
|
|
|
|
- /** 查服务点扣除 - 根据id */
|
|
|
+ /**
|
|
|
+ * 查服务点扣除 - 根据id
|
|
|
+ */
|
|
|
@RequestMapping("getWalletRecordById")
|
|
|
- public AjaxJson getWalletRecordById(Long id){
|
|
|
+ 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) {
|
|
|
+ public AjaxJson topupSave(@RequestParam("amount") Integer amount, @RequestParam("goodsName") String goodsName) {
|
|
|
//获取登录人
|
|
|
APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
|
|
|
TbPeople tbPeople = tbPeopleService.getById(appLoginInfo.getFk());
|
|
|
- if(StringUtils.isBlank(tbPeople.getWxOpenId())){
|
|
|
- return AjaxJson.getError("未绑定微信用户!");
|
|
|
- }
|
|
|
try {
|
|
|
TbWalletTopup tbWalletTopup = new TbWalletTopup();
|
|
|
Date date = new Date();
|
|
|
- tbWalletTopup.setMchid(this.wxService.getConfig().getMchId());
|
|
|
- tbWalletTopup.setOutTradeNo(CZ_TABLE_NAME+System.currentTimeMillis());
|
|
|
+ String tradeNo = CZ_TABLE_NAME + System.currentTimeMillis();
|
|
|
+ tbWalletTopup.setMchid(wxPayProperties.getMchId());
|
|
|
+ tbWalletTopup.setOutTradeNo(tradeNo);
|
|
|
tbWalletTopup.setGoodsName(goodsName);
|
|
|
- tbWalletTopup.setAmount(amount.toString());
|
|
|
+ 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());
|
|
|
|
|
|
- //调用统一下单接口,并组装生成支付所需参数对象
|
|
|
- WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
|
|
|
- // 商品描述
|
|
|
- request.setBody(tbWalletTopup.getGoodsName());
|
|
|
- // 商户订单编号
|
|
|
- request.setOutTradeNo(tbWalletTopup.getOutTradeNo());
|
|
|
- //支付金额
|
|
|
- request.setTotalFee(BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount()));
|
|
|
- request.setSpbillCreateIp(WebNbUtil.getIP(SpringMVCUtil.getRequest()));
|
|
|
- // 回调地址
|
|
|
- request.setNotifyUrl(wxService.getConfig().getNotifyUrl());
|
|
|
- // 交易类型APP
|
|
|
- request.setTradeType(wxService.getConfig().getTradeType());
|
|
|
- request.setOpenid(tbPeople.getWxOpenId());
|
|
|
- WxPayMpOrderResult data = wxService.createOrder(request);
|
|
|
- // 业务处理
|
|
|
- tbWalletTopup.setTransactionId(data.getPackageValue());
|
|
|
+ PrepayWithRequestPaymentResponse response = prepay(tradeNo, BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + ""));
|
|
|
+// StaticLog.info("response:{}",response);
|
|
|
// 付款中
|
|
|
- tbWalletTopup.setPayStatus(1);
|
|
|
+ tbWalletTopup.setPayStatus(1)
|
|
|
+ .setPrepayid(response.getPrepayId());
|
|
|
tbWalletTopupService.save(tbWalletTopup);
|
|
|
- return AjaxJson.getSuccess();
|
|
|
- } catch (WxPayException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return AjaxJson.getError("微信支付失败," + e.getReturnMsg());
|
|
|
- }catch (Exception e) {
|
|
|
+ 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());
|
|
|
+ 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) {
|
|
|
+ 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("余额不足!");
|
|
|
+ return AjaxJson.getError("可提现点数不足!");
|
|
|
}
|
|
|
+ tbPeople.setWallet(tbPeople.getWallet() - amount);
|
|
|
try {
|
|
|
TbWalletTopdown tbWalletTopdown = new TbWalletTopdown();
|
|
|
Date date = new Date();
|
|
|
- tbWalletTopdown.setMchid(this.wxService.getConfig().getMchId());
|
|
|
+ tbWalletTopdown.setMchid(wxPayProperties.getMchId());
|
|
|
tbWalletTopdown.setOutTradeNo(TX_TABLE_NAME + System.currentTimeMillis());
|
|
|
tbWalletTopdown.setGoodsName(goodsName);
|
|
|
tbWalletTopdown.setAmount(amount.toString());
|
|
@@ -195,40 +245,55 @@ public class TbWalletAppController {
|
|
|
tbWalletTopdown.setCreateBy(appLoginInfo.getLoginId().toString());
|
|
|
tbWalletTopdown.setCreateName(appLoginInfo.getLoginName());
|
|
|
|
|
|
- //调用企业付款到零钱接口,并组装接口所需参数对象
|
|
|
- EntPayRequest request = new EntPayRequest();
|
|
|
- // 商品描述
|
|
|
- request.setDescription(tbWalletTopdown.getGoodsName());
|
|
|
- // 商户订单编号
|
|
|
- request.setPartnerTradeNo(tbWalletTopdown.getOutTradeNo());
|
|
|
- //支付金额
|
|
|
- request.setAmount(BaseWxPayRequest.yuanToFen(tbWalletTopdown.getAmount()));
|
|
|
- request.setSpbillCreateIp(WebNbUtil.getIP(SpringMVCUtil.getRequest()));
|
|
|
- // 交易类型APP
|
|
|
- request.setCheckName(WxPayConstants.CheckNameOption.FORCE_CHECK);
|
|
|
- request.setReUserName(tbPeople.getBankName());
|
|
|
- request.setOpenid(tbPeople.getWxOpenId());
|
|
|
- EntPayResult data = this.wxService.getEntPayService().entPay(request);
|
|
|
- // 业务处理
|
|
|
- tbWalletTopdown.setTransactionId(data.getPaymentNo());
|
|
|
+ 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);
|
|
|
- tbPeople.setWallet(tbPeople.getWallet()-Double.parseDouble(tbWalletTopdown.getAmount()));
|
|
|
+
|
|
|
tbPeopleService.updateById(tbPeople);
|
|
|
return AjaxJson.getSuccess();
|
|
|
- } catch (WxPayException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return AjaxJson.getError("微信支付失败," + e.getReturnMsg());
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- return AjaxJson.getError("下单失败," + e.getMessage());
|
|
|
+ return AjaxJson.getError("提现失败," + e.getMessage());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信支付回调接口
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "/wxPay/notify", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
|
|
@@ -249,7 +314,8 @@ public class TbWalletAppController {
|
|
|
in.close();
|
|
|
//xml数据
|
|
|
String reponseXml = new String(out.toByteArray(), "utf-8");
|
|
|
- WxPayOrderNotifyResult result = wxService.parseOrderNotifyResult(reponseXml);
|
|
|
+// WxPayOrderNotifyResult result = wxService.parseOrderNotifyResult(reponseXml);
|
|
|
+ WxPayOrderNotifyResult result = null;
|
|
|
String resultCode = result.getResultCode();
|
|
|
String outTradeNo = result.getOutTradeNo();
|
|
|
if ("FAIL".equalsIgnoreCase(resultCode)) {
|
|
@@ -258,15 +324,15 @@ public class TbWalletAppController {
|
|
|
} else if ("SUCCESS".equalsIgnoreCase(resultCode)) {
|
|
|
LogUtil.info("订单" + outTradeNo + "支付成功");
|
|
|
// 充值数据处理
|
|
|
- if(outTradeNo.indexOf(CZ_TABLE_NAME)>-1){
|
|
|
+ if (outTradeNo.indexOf(CZ_TABLE_NAME) > -1) {
|
|
|
QueryWrapper<TbWalletTopup> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("outTradeNo", outTradeNo);
|
|
|
- TbWalletTopup tbWalletTopup=tbWalletTopupService.list(queryWrapper).get(0);
|
|
|
+ TbWalletTopup tbWalletTopup = tbWalletTopupService.list(queryWrapper).get(0);
|
|
|
tbWalletTopup.setPayStatus(2);
|
|
|
tbWalletTopupService.updateById(tbWalletTopup);
|
|
|
|
|
|
TbPeople tbPeople = tbPeopleService.getById(tbWalletTopup.getPeopleId());
|
|
|
- tbPeople.setWallet(tbPeople.getWallet()+Double.parseDouble(tbWalletTopup.getAmount()));
|
|
|
+ tbPeople.setWallet(tbPeople.getWallet() + tbWalletTopup.getAmount());
|
|
|
tbPeopleService.updateById(tbPeople);
|
|
|
}
|
|
|
response.getWriter().write(setXml("SUCCESS", "OK"));
|
|
@@ -280,6 +346,22 @@ public class TbWalletAppController {
|
|
|
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();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|