支付回调处理
This commit is contained in:
parent
42928372f6
commit
1088d305c5
|
|
@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
|
|||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
@ApiModel("用户信息实体")
|
||||
|
|
@ -39,6 +40,9 @@ public class User implements Serializable {
|
|||
@ApiModelProperty("用户电话")
|
||||
private String mobile;
|
||||
|
||||
@ApiModelProperty("用户钱包")
|
||||
private BigDecimal money;
|
||||
|
||||
@ApiModelProperty("注册渠道")
|
||||
private Integer channel;
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ public enum HttpEnum {
|
|||
TOKEN_EMPTY(332, "token参数为空"),
|
||||
TOKEN_INVALID(333, "token参数无效"),
|
||||
CAPTCHA_ERROR(334, "验证码错误"),
|
||||
PAYMENT_ERROR(335, "发起支付失败"),
|
||||
|
||||
NO_PERMISSION(403, "无相关权限"),
|
||||
REQUEST_404_ERROR(404, "请求接口不存在"),
|
||||
|
|
|
|||
|
|
@ -15,4 +15,5 @@ public class OperateException extends BaseException {
|
|||
public OperateException(String msg, Integer errCode) {
|
||||
super(errCode, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package com.mdd.common.exception;
|
||||
|
||||
import com.mdd.common.enums.HttpEnum;
|
||||
|
||||
/**
|
||||
* 支付失败异常
|
||||
*/
|
||||
public class PaymentException extends BaseException {
|
||||
|
||||
public PaymentException(String msg) {
|
||||
super(HttpEnum.PAYMENT_ERROR.getCode(), msg);
|
||||
}
|
||||
|
||||
public PaymentException(String msg, Integer errCode) {
|
||||
super(errCode, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,20 +1,24 @@
|
|||
package com.mdd.front.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import com.mdd.common.aop.NotLogin;
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.entity.RechargeOrder;
|
||||
import com.mdd.common.enums.ClientEnum;
|
||||
import com.mdd.common.enums.PaymentEnum;
|
||||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.exception.PaymentException;
|
||||
import com.mdd.common.mapper.RechargeOrderMapper;
|
||||
import com.mdd.common.plugin.wechat.WxPayDriver;
|
||||
import com.mdd.front.LikeFrontThreadLocal;
|
||||
import com.mdd.front.service.IPayService;
|
||||
import com.mdd.front.validate.PaymentValidate;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.models.auth.In;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
|
@ -38,13 +42,10 @@ public class PayController {
|
|||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 预支付
|
||||
*
|
||||
* @return AjaxResult<Object>
|
||||
*/
|
||||
@PostMapping("/prepay")
|
||||
@ApiOperation("发起支付")
|
||||
public AjaxResult<Object> prepay(@Validated @RequestBody PaymentValidate paymentValidate) {
|
||||
// 接收参数
|
||||
String scene = paymentValidate.getScene();
|
||||
Integer payWay = paymentValidate.getPayWay();
|
||||
Integer orderId = paymentValidate.getOrderId();
|
||||
|
|
@ -54,13 +55,12 @@ public class PayController {
|
|||
int payStatus = 0;
|
||||
switch (scene) {
|
||||
case "recharge":
|
||||
RechargeOrder rechargeOrder = rechargeOrderMapper.selectOne(
|
||||
new QueryWrapper<RechargeOrder>()
|
||||
.eq("id", orderId)
|
||||
.last("limit 1"));
|
||||
RechargeOrder rechargeOrder = rechargeOrderMapper.selectById(orderId);
|
||||
|
||||
Assert.notNull(rechargeOrder, "订单不存在");
|
||||
Assert.isTrue(payWay.equals(PaymentEnum.WALLET_PAY.getCode()), "支付类型不被支持");
|
||||
|
||||
paymentValidate.setAttach("recharge");
|
||||
paymentValidate.setOrderSn(rechargeOrder.getOrderSn());
|
||||
paymentValidate.setUserId(rechargeOrder.getUserId());
|
||||
paymentValidate.setOrderAmount(rechargeOrder.getOrderAmount());
|
||||
|
|
@ -68,7 +68,6 @@ public class PayController {
|
|||
payStatus = rechargeOrder.getPayStatus();
|
||||
break;
|
||||
case "order":
|
||||
// todo 其它订单处理
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -81,38 +80,38 @@ public class PayController {
|
|||
try {
|
||||
switch (payWay) {
|
||||
case 1: // 余额支付
|
||||
iPayService.walletPay();
|
||||
String attach = paymentValidate.getAttach();
|
||||
String orderSn = paymentValidate.getOrderSn();
|
||||
iPayService.handlePaidNotify(attach, orderSn, null);
|
||||
break;
|
||||
case 2: // 微信支付
|
||||
WxPayUnifiedOrderV3Result.JsapiResult result = iPayService.wxPay(paymentValidate, terminal);
|
||||
return AjaxResult.success(result);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new OperateException(e.getMessage());
|
||||
throw new PaymentException(e.getMessage());
|
||||
}
|
||||
|
||||
return AjaxResult.success();
|
||||
throw new PaymentException("发起支付失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付回调
|
||||
*
|
||||
* @return AjaxResult<Object>
|
||||
*/
|
||||
@NotLogin
|
||||
@PostMapping("/notifyMnp")
|
||||
@ApiOperation("微信支付回调")
|
||||
public AjaxResult<Object> notifyMnp(@RequestBody String jsonData, HttpServletRequest request) throws WxPayException {
|
||||
SignatureHeader signatureHeader = this.getWxRequestHeader(request);
|
||||
iPayService.handlePaidNotify(jsonData, signatureHeader);
|
||||
WxPayService wxPayService = WxPayDriver.handler(ClientEnum.MNP.getCode());
|
||||
WxPayOrderNotifyV3Result.DecryptNotifyResult notifyResult = wxPayService.parseOrderNotifyV3Result(jsonData, signatureHeader).getResult();
|
||||
|
||||
String transactionId = notifyResult.getTransactionId();
|
||||
String outTradeNo = notifyResult.getOutTradeNo();
|
||||
String attach = notifyResult.getAttach();
|
||||
|
||||
iPayService.handlePaidNotify(attach, outTradeNo, transactionId);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付回调签名相关
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @return SignatureHeader
|
||||
*/
|
||||
@ApiOperation("微信支付回调签名相关")
|
||||
private SignatureHeader getWxRequestHeader(HttpServletRequest request) {
|
||||
String signature = request.getHeader("wechatpay-signature");
|
||||
String nonce = request.getHeader("wechatpay-nonce");
|
||||
|
|
|
|||
|
|
@ -10,11 +10,6 @@ import com.mdd.front.validate.PaymentValidate;
|
|||
*/
|
||||
public interface IPayService {
|
||||
|
||||
/**
|
||||
* 余额支付
|
||||
*/
|
||||
void walletPay();
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
*
|
||||
|
|
@ -28,9 +23,10 @@ public interface IPayService {
|
|||
/**
|
||||
* 支付回调处理
|
||||
*
|
||||
* @param jsonData 回调数据
|
||||
* @param signatureHeader 请求头
|
||||
* @param attach 场景码
|
||||
* @param outTradeNo 订单编号
|
||||
* @param transactionId 流水号
|
||||
*/
|
||||
void handlePaidNotify(String jsonData, SignatureHeader signatureHeader) throws WxPayException;
|
||||
void handlePaidNotify(String attach, String outTradeNo, String transactionId) throws WxPayException;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
package com.mdd.front.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
|
||||
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import com.mdd.common.entity.RechargeOrder;
|
||||
import com.mdd.common.entity.user.User;
|
||||
import com.mdd.common.entity.user.UserAuth;
|
||||
import com.mdd.common.enums.ClientEnum;
|
||||
import com.mdd.common.enums.PaymentEnum;
|
||||
import com.mdd.common.mapper.RechargeOrderMapper;
|
||||
import com.mdd.common.mapper.user.UserAuthMapper;
|
||||
import com.mdd.common.mapper.user.UserMapper;
|
||||
import com.mdd.common.plugin.wechat.WxPayDriver;
|
||||
import com.mdd.common.util.AmountUtil;
|
||||
import com.mdd.common.util.IpUtils;
|
||||
|
|
@ -30,20 +30,15 @@ import java.text.SimpleDateFormat;
|
|||
@Service
|
||||
public class PayServiceImpl implements IPayService {
|
||||
|
||||
@Resource
|
||||
UserMapper userMapper;
|
||||
|
||||
@Resource
|
||||
UserAuthMapper userAuthMapper;
|
||||
|
||||
@Resource
|
||||
RechargeOrderMapper rechargeOrderMapper;
|
||||
|
||||
/**
|
||||
* 零钱支付
|
||||
*/
|
||||
@Override
|
||||
public void walletPay() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
*
|
||||
|
|
@ -55,6 +50,7 @@ public class PayServiceImpl implements IPayService {
|
|||
public WxPayUnifiedOrderV3Result.JsapiResult wxPay(PaymentValidate params, Integer terminal) throws Exception {
|
||||
// 订单参数
|
||||
Integer userId = params.getUserId();
|
||||
String attach = params.getAttach();
|
||||
String orderSn = params.getOrderSn();
|
||||
BigDecimal orderAmount = params.getOrderAmount();
|
||||
String description = params.getDescription();
|
||||
|
|
@ -66,6 +62,7 @@ public class PayServiceImpl implements IPayService {
|
|||
.eq("terminal", terminal)
|
||||
.last("limit 1"));
|
||||
|
||||
// 设置OpenId
|
||||
if (StringUtils.isNotNull(userAuth)) {
|
||||
openId = userAuth.getOpenid();
|
||||
}
|
||||
|
|
@ -80,6 +77,7 @@ public class PayServiceImpl implements IPayService {
|
|||
wxPayUnifiedOrderV3Request.setOutTradeNo(orderSn);
|
||||
wxPayUnifiedOrderV3Request.setDescription(description);
|
||||
wxPayUnifiedOrderV3Request.setTimeExpire(timeExpire);
|
||||
wxPayUnifiedOrderV3Request.setAttach(attach);
|
||||
wxPayUnifiedOrderV3Request.setNotifyUrl("https://likeadmin-java-api.yixiangonline.com/api/pay/notifyMnp");
|
||||
|
||||
// 订单金额
|
||||
|
|
@ -111,38 +109,58 @@ public class PayServiceImpl implements IPayService {
|
|||
|
||||
/**
|
||||
* 支付回调处理
|
||||
*
|
||||
* @author fzr
|
||||
* @param attach 场景码
|
||||
* @param outTradeNo 订单编号
|
||||
* @param transactionId 流水号
|
||||
*/
|
||||
public void handlePaidNotify(String jsonData, SignatureHeader signatureHeader) throws WxPayException {
|
||||
log.info("\n\n微信传来的json-------");
|
||||
log.info(jsonData);
|
||||
log.info("\nsignatureHeader------------");
|
||||
log.info(signatureHeader.toString());
|
||||
@Override
|
||||
public void handlePaidNotify(String attach, String outTradeNo, String transactionId) {
|
||||
switch (attach) {
|
||||
case "order":
|
||||
break;
|
||||
case "recharge":
|
||||
this.rechargeCallback(outTradeNo, transactionId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
WxPayService wxPayService = WxPayDriver.handler(ClientEnum.MNP.getCode());
|
||||
WxPayOrderNotifyV3Result.DecryptNotifyResult notifyResult = wxPayService.parseOrderNotifyV3Result(jsonData, signatureHeader).getResult();
|
||||
/**
|
||||
* 余额充值回调
|
||||
*
|
||||
* @author fzr
|
||||
* @param outTradeNo 订单号
|
||||
* @param transactionId 流水号
|
||||
*/
|
||||
private void rechargeCallback(String outTradeNo, String transactionId) {
|
||||
for (int i=0; i<=0; i++) {
|
||||
RechargeOrder rechargeOrder = rechargeOrderMapper.selectOne(
|
||||
new QueryWrapper<RechargeOrder>()
|
||||
.eq("order_sn", outTradeNo)
|
||||
.last("limit 1"));
|
||||
|
||||
String transactionId = notifyResult.getTransactionId();
|
||||
String outTradeNo = notifyResult.getOutTradeNo();
|
||||
if (StringUtils.isNull(rechargeOrder)) {
|
||||
log.error("充值订单不存在: {} : {}", outTradeNo, transactionId);
|
||||
break;
|
||||
}
|
||||
|
||||
RechargeOrder rechargeOrder = rechargeOrderMapper.selectOne(
|
||||
new QueryWrapper<RechargeOrder>()
|
||||
.eq("order_sn", outTradeNo)
|
||||
.last("limit 1"));
|
||||
if (rechargeOrder.getPayStatus().equals(PaymentEnum.OK_PAID.getCode())) {
|
||||
log.error("充值订单已支付: {} : {}", outTradeNo, transactionId);
|
||||
break;
|
||||
}
|
||||
|
||||
if (StringUtils.isNotNull(rechargeOrder)) {
|
||||
rechargeOrder.setPayStatus(1);
|
||||
rechargeOrder.setTransactionId(transactionId);
|
||||
rechargeOrder.setPayTime(System.currentTimeMillis() / 1000);
|
||||
rechargeOrder.setUpdateTime(System.currentTimeMillis() / 1000);
|
||||
rechargeOrderMapper.updateById(rechargeOrder);
|
||||
} else {
|
||||
log.info("=======###订单不存在#####=====");
|
||||
}
|
||||
|
||||
log.info("transactionId-------\n");
|
||||
log.info(transactionId);
|
||||
log.info("outTradeNo-------\n");
|
||||
log.info(outTradeNo);
|
||||
User user = new User();
|
||||
user.setMoney(rechargeOrder.getOrderAmount());
|
||||
user.setUpdateTime(System.currentTimeMillis() / 1000);
|
||||
userMapper.update(user, new QueryWrapper<User>().eq("id", rechargeOrder.getUserId()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,12 +26,19 @@ public class PaymentValidate implements Serializable {
|
|||
@ApiModelProperty("订单ID")
|
||||
private Integer orderId;
|
||||
|
||||
@ApiModelProperty(value = "用户ID", notes = "该参数无需传递")
|
||||
private Integer userId;
|
||||
|
||||
@ApiModelProperty(value = "订单类型", notes = "该参数无需传递")
|
||||
private String attach;
|
||||
|
||||
@ApiModelProperty(value = "订单编号", notes = "该参数无需传递")
|
||||
private String orderSn;
|
||||
|
||||
@ApiModelProperty(value = "订单金额", notes = "该参数无需传递")
|
||||
private BigDecimal orderAmount;
|
||||
|
||||
@ApiModelProperty(value = "用户描述", notes = "该参数无需传递")
|
||||
private String description;
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue