|
@@ -43,9 +43,15 @@ 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 com.wechat.pay.java.service.refund.RefundService;
|
|
|
+import com.wechat.pay.java.service.refund.model.AmountReq;
|
|
|
+import com.wechat.pay.java.service.refund.model.CreateRequest;
|
|
|
+import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
|
|
|
+import com.wechat.pay.java.service.refund.model.Refund;
|
|
|
import io.swagger.models.auth.In;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -104,6 +110,9 @@ public class TbWalletAppController {
|
|
|
@Resource
|
|
|
private WxPayProperties wxPayProperties;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RefundService refundService;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 查集合 - 根据条件(参数为空时代表忽略指定条件)
|
|
@@ -157,46 +166,43 @@ public class TbWalletAppController {
|
|
|
* 服务点充值
|
|
|
*/
|
|
|
@RequestMapping("topupSave")
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
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() + ""));
|
|
|
+ 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());
|
|
|
- }
|
|
|
+ // 付款中
|
|
|
+ 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);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -212,7 +218,7 @@ public class TbWalletAppController {
|
|
|
request.setDescription(wxPayProperties.getGoodsDescription());
|
|
|
request.setNotifyUrl(wxPayProperties.getNotifyUrl());
|
|
|
Amount amount = new Amount();
|
|
|
- amount.setTotal(1);
|
|
|
+ amount.setTotal(fee);
|
|
|
amount.setCurrency("CNY");
|
|
|
request.setAmount(amount);
|
|
|
// 调用接口
|
|
@@ -225,6 +231,9 @@ public class TbWalletAppController {
|
|
|
*/
|
|
|
@RequestMapping("topdownSave")
|
|
|
public AjaxJson topdownSave(@RequestParam("amount") Integer amount, @RequestParam("goodsName") String goodsName) {
|
|
|
+ if (!StpAPPUserUtil.isLogin()){
|
|
|
+ return AjaxJson.getError("请先登录!");
|
|
|
+ }
|
|
|
//获取登录人
|
|
|
APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
|
|
|
TbPeople tbPeople = tbPeopleService.getById(appLoginInfo.getFk());
|
|
@@ -232,62 +241,61 @@ public class TbWalletAppController {
|
|
|
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 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 refundCount=tbWalletTopup.getRefundCount();
|
|
|
+ //剩余提现点数
|
|
|
+ int refundMoney = tbWalletTopup.getAmount() - tbWalletTopup.getCutCount() - refundCount;
|
|
|
+ if (refundMoney > amount) {
|
|
|
+ //提不完
|
|
|
+ refundMoney = amount;
|
|
|
+ tbWalletTopup.setRefundCount(amount+refundCount);
|
|
|
+ amount = 0;
|
|
|
+ } else {
|
|
|
+ //一次性提完一个充值单
|
|
|
+ tbWalletTopup.setRefundCount(refundMoney+refundCount);
|
|
|
+ amount = amount - refundMoney;
|
|
|
+ }
|
|
|
+ int total = BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + "");
|
|
|
+ CreateRequest request = new CreateRequest();
|
|
|
+ AmountReq amountReq = new AmountReq();
|
|
|
+ amountReq.setTotal((long) total);
|
|
|
+ amountReq.setCurrency("CNY");
|
|
|
+ amountReq.setRefund(Long.valueOf(BaseWxPayRequest.yuanToFen(refundMoney + "")));
|
|
|
+ request.setAmount(amountReq);
|
|
|
+ request.setTransactionId(tbWalletTopup.getTransactionId());
|
|
|
+ request.setOutTradeNo(tbWalletTopup.getOutTradeNo());
|
|
|
+ request.setReason("服务点提现");
|
|
|
+ String outRefundNo = RandomUtil.randomString(16);
|
|
|
+ tbWalletTopdown.setRefundOutTradeNo(outRefundNo);
|
|
|
+ request.setOutRefundNo(outRefundNo);
|
|
|
+ request.setNotifyUrl(wxPayProperties.getNotifyUrl());
|
|
|
+ Refund refund = refundService.create(request);
|
|
|
+ tbWalletTopdown.setTransactionId(refund.getTransactionId());
|
|
|
+ log.info("refund:{}", JSONUtil.toJsonStr(refund));
|
|
|
+ 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());
|
|
|
}
|
|
|
+ // 成功
|
|
|
+ tbWalletTopdown.setPayStatus(2);
|
|
|
+ tbWalletTopdownService.save(tbWalletTopdown);
|
|
|
+ tbPeopleService.updateById(tbPeople);
|
|
|
+ return AjaxJson.getSuccess();
|
|
|
|
|
|
}
|
|
|
|
|
@@ -323,18 +331,6 @@ public class TbWalletAppController {
|
|
|
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) {
|
|
@@ -357,7 +353,7 @@ public class TbWalletAppController {
|
|
|
result.put("return_code", "SUCCESS");
|
|
|
result.put("return_msg", "OK");
|
|
|
notifyBO.setTradeStatus("SUCCESS");
|
|
|
- tbWalletTopupService.WxNotify(notifyBO);
|
|
|
+ //tbWalletTopupService.WxNotify(notifyBO);
|
|
|
writer.write(XmlUtil.mapToXmlStr(result));
|
|
|
writer.flush();
|
|
|
writer.close();
|