Merge branch 'develop' of https://gitee.com/likeadmin/likeadmin-java into develop
This commit is contained in:
commit
80e9a04c14
|
|
@ -1,17 +1,27 @@
|
|||
package com.mdd.common.plugin.wechat;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
|
||||
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
|
||||
import com.mdd.common.entity.setting.DevPayConfig;
|
||||
import com.mdd.common.entity.user.UserAuth;
|
||||
import com.mdd.common.enums.ClientEnum;
|
||||
import com.mdd.common.mapper.setting.DevPayConfigMapper;
|
||||
import com.mdd.common.util.ConfigUtils;
|
||||
import com.mdd.common.util.MapUtils;
|
||||
import com.mdd.common.plugin.wechat.request.PaymentRequestV3;
|
||||
import com.mdd.common.plugin.wechat.request.RefundRequestV3;
|
||||
import com.mdd.common.util.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
|
@ -40,6 +50,87 @@ public class WxPayDriver {
|
|||
WxPayDriver.wxPayService = wxPayService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
*
|
||||
* @param requestV3 请求参数
|
||||
* @return WxPayUnifiedOrderV3Result.JsapiResult
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public static WxPayUnifiedOrderV3Result.JsapiResult unifiedOrder(PaymentRequestV3 requestV3) throws Exception {
|
||||
// 订单参数
|
||||
Integer terminal = requestV3.getTerminal();
|
||||
String openId = requestV3.getOpenId();
|
||||
String attach = requestV3.getAttach();
|
||||
String outTradeNo = requestV3.getOutTradeNo();
|
||||
BigDecimal orderAmount = requestV3.getOrderAmount();
|
||||
String description = requestV3.getDescription();
|
||||
|
||||
// 失效时间
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
Long expireTime = System.currentTimeMillis() + (30 * 60 * 1000);
|
||||
String timeExpire = format.format(expireTime) + "+08:00";
|
||||
|
||||
// 订单信息
|
||||
WxPayUnifiedOrderV3Request wxPayUnifiedOrderV3Request = new WxPayUnifiedOrderV3Request();
|
||||
wxPayUnifiedOrderV3Request.setOutTradeNo(outTradeNo);
|
||||
wxPayUnifiedOrderV3Request.setDescription(description);
|
||||
wxPayUnifiedOrderV3Request.setTimeExpire(timeExpire);
|
||||
wxPayUnifiedOrderV3Request.setAttach(attach);
|
||||
wxPayUnifiedOrderV3Request.setNotifyUrl(RequestUtils.uri() + "/api/pay/notifyMnp");
|
||||
|
||||
// 订单金额
|
||||
WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
|
||||
amount.setTotal(AmountUtil.yuan2Fen(orderAmount.toPlainString()));
|
||||
amount.setCurrency("CNY");
|
||||
wxPayUnifiedOrderV3Request.setAmount(amount);
|
||||
|
||||
// 付款人员
|
||||
WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
|
||||
payer.setOpenid(openId);
|
||||
|
||||
// H5平台
|
||||
if (terminal == ClientEnum.H5.getCode()) {
|
||||
WxPayUnifiedOrderV3Request.SceneInfo sceneInfo = new WxPayUnifiedOrderV3Request.SceneInfo();
|
||||
WxPayUnifiedOrderV3Request.H5Info h5Info = new WxPayUnifiedOrderV3Request.H5Info();
|
||||
h5Info.setType("android");
|
||||
sceneInfo.setH5Info(h5Info);
|
||||
sceneInfo.setPayerClientIp(IpUtils.getHostIp());
|
||||
sceneInfo.setDeviceId("1");
|
||||
wxPayUnifiedOrderV3Request.setSceneInfo(sceneInfo);
|
||||
}
|
||||
|
||||
// 发起订单
|
||||
WxPayService wxPayService = WxPayDriver.handler(terminal);
|
||||
wxPayUnifiedOrderV3Request.setPayer(payer);
|
||||
return wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxPayUnifiedOrderV3Request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起退款
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return WxPayRefundV3Result 退款结果
|
||||
* @throws WxPayException 退款异常
|
||||
*/
|
||||
public static WxPayRefundV3Result refund(RefundRequestV3 request) throws WxPayException {
|
||||
WxPayRefundV3Request requestObj = new WxPayRefundV3Request();
|
||||
request.setTransactionId(request.getTransactionId());
|
||||
request.setOutTradeNo(request.getOutRefundNo());
|
||||
request.setOutRefundNo(request.getOutRefundNo());
|
||||
request.setReason(request.getReason());
|
||||
request.setNotifyUrl(request.getNotifyUrl());
|
||||
request.setSubMchid(request.getSubMchid());
|
||||
request.setGoodsDetails(request.getGoodsDetails());
|
||||
|
||||
WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
|
||||
amount.setRefund(request.getRefundAmount());
|
||||
amount.setTotal(request.getTotalAmount());
|
||||
amount.setCurrency(StringUtils.isEmpty(request.getCurrency()) ? "CNY" : request.getCurrency());
|
||||
|
||||
return wxPayService.refundV3(requestObj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象句柄
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
package com.mdd.common.plugin.wechat.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class PaymentRequestV3 implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 支付终端 */
|
||||
private Integer terminal;
|
||||
|
||||
/** 用户身份 */
|
||||
private String openId;
|
||||
|
||||
/** 扩展字段 */
|
||||
private String attach;
|
||||
|
||||
/** 订单编号 */
|
||||
private String outTradeNo;
|
||||
|
||||
/** 订单金额 */
|
||||
private BigDecimal orderAmount;
|
||||
|
||||
/** 订单描述 */
|
||||
private String description;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package com.mdd.common.plugin.wechat.request;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RefundRequestV3 implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1L;
|
||||
|
||||
/** 订单流水号 */
|
||||
private String transactionId;
|
||||
|
||||
/** 订单单号 */
|
||||
private String outTradeNo;
|
||||
|
||||
/** 退款单号 */
|
||||
@SerializedName("out_refund_no")
|
||||
private String outRefundNo;
|
||||
|
||||
/** 退款原因 */
|
||||
private String reason;
|
||||
|
||||
/** 通知接口 */
|
||||
private String notifyUrl;
|
||||
|
||||
/** 退款金额 */
|
||||
private Integer refundAmount;
|
||||
|
||||
/** 订单总额 */
|
||||
private Integer totalAmount;
|
||||
|
||||
/** 退款币种 */
|
||||
private String currency;
|
||||
|
||||
/** 商品信息 */
|
||||
private List<WxPayRefundV3Request.GoodsDetail> goodsDetails;
|
||||
|
||||
/** 子商户号 */
|
||||
private String subMchid;
|
||||
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@ package com.mdd.front.controller;
|
|||
|
||||
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;
|
||||
|
|
@ -11,7 +10,6 @@ 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;
|
||||
|
|
@ -51,11 +49,11 @@ public class PayController {
|
|||
|
||||
@PostMapping("/prepay")
|
||||
@ApiOperation("发起支付")
|
||||
public AjaxResult<Object> prepay(@Validated @RequestBody PaymentValidate paymentValidate) {
|
||||
public AjaxResult<Object> prepay(@Validated @RequestBody PaymentValidate requestObj) {
|
||||
// 接收参数
|
||||
String scene = paymentValidate.getScene();
|
||||
Integer payWay = paymentValidate.getPayWay();
|
||||
Integer orderId = paymentValidate.getOrderId();
|
||||
String scene = requestObj.getScene();
|
||||
Integer payWay = requestObj.getPayWay();
|
||||
Integer orderId = requestObj.getOrderId();
|
||||
Integer terminal = LikeFrontThreadLocal.getTerminal();
|
||||
|
||||
// 订单处理
|
||||
|
|
@ -67,11 +65,11 @@ public class PayController {
|
|||
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());
|
||||
paymentValidate.setDescription("余额充值");
|
||||
requestObj.setUserId(rechargeOrder.getUserId());
|
||||
requestObj.setOutTradeNo(rechargeOrder.getOrderSn());
|
||||
requestObj.setOrderAmount(rechargeOrder.getOrderAmount());
|
||||
requestObj.setDescription("余额充值");
|
||||
requestObj.setAttach("recharge");
|
||||
payStatus = rechargeOrder.getPayStatus();
|
||||
|
||||
rechargeOrder.setPayWay(payWay);
|
||||
|
|
@ -87,22 +85,8 @@ public class PayController {
|
|||
}
|
||||
|
||||
// 发起支付
|
||||
try {
|
||||
switch (payWay) {
|
||||
case 1: // 余额支付
|
||||
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 PaymentException(e.getMessage());
|
||||
}
|
||||
|
||||
throw new PaymentException("发起支付失败");
|
||||
Object result = iPayService.prepay(requestObj, terminal);
|
||||
return AjaxResult.success(result);
|
||||
}
|
||||
|
||||
@NotLogin
|
||||
|
|
|
|||
|
|
@ -22,14 +22,13 @@ public interface IPayService {
|
|||
List<PayWayListedVo> payWay(String from, Integer terminal);
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* 发起支付
|
||||
*
|
||||
* @param params 参数
|
||||
* @param terminal 终端
|
||||
* @return WxPayUnifiedOrderV3Result.JsapiResult
|
||||
* @throws Exception 异常
|
||||
* @return Object
|
||||
*/
|
||||
WxPayUnifiedOrderV3Result.JsapiResult wxPay(PaymentValidate params, Integer terminal) throws Exception;
|
||||
Object prepay(PaymentValidate params, Integer terminal);
|
||||
|
||||
/**
|
||||
* 支付回调处理
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ 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.service.WxPayService;
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.entity.RechargeOrder;
|
||||
import com.mdd.common.entity.setting.DevPayConfig;
|
||||
import com.mdd.common.entity.setting.DevPayWay;
|
||||
|
|
@ -13,6 +14,7 @@ import com.mdd.common.entity.user.UserAuth;
|
|||
import com.mdd.common.enums.ClientEnum;
|
||||
import com.mdd.common.enums.LogMoneyEnum;
|
||||
import com.mdd.common.enums.PaymentEnum;
|
||||
import com.mdd.common.exception.PaymentException;
|
||||
import com.mdd.common.mapper.LogMoneyMapper;
|
||||
import com.mdd.common.mapper.RechargeOrderMapper;
|
||||
import com.mdd.common.mapper.setting.DevPayConfigMapper;
|
||||
|
|
@ -20,6 +22,7 @@ import com.mdd.common.mapper.setting.DevPayWayMapper;
|
|||
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.plugin.wechat.request.PaymentRequestV3;
|
||||
import com.mdd.common.util.*;
|
||||
import com.mdd.front.service.IPayService;
|
||||
import com.mdd.front.validate.PaymentValidate;
|
||||
|
|
@ -30,7 +33,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -92,72 +96,35 @@ public class PayServiceImpl implements IPayService {
|
|||
}
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* 发起支付
|
||||
*
|
||||
* @author fzr
|
||||
* @param params 支付参数
|
||||
* @param params 参数
|
||||
* @param terminal 终端
|
||||
* @throws Exception 异常
|
||||
* @return Object
|
||||
*/
|
||||
@Override
|
||||
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();
|
||||
|
||||
// 查询OpenId
|
||||
String openId = "";
|
||||
UserAuth userAuth = userAuthMapper.selectOne(new QueryWrapper<UserAuth>()
|
||||
.eq("user_id", userId)
|
||||
.eq("terminal", terminal)
|
||||
.last("limit 1"));
|
||||
|
||||
// 设置OpenId
|
||||
if (StringUtils.isNotNull(userAuth)) {
|
||||
openId = userAuth.getOpenid();
|
||||
public Object prepay(PaymentValidate params, Integer terminal) {
|
||||
try {
|
||||
switch (params.getPayWay()) {
|
||||
case 1: // 余额支付
|
||||
String attach = params.getAttach();
|
||||
String orderSn = params.getOutTradeNo();
|
||||
this.handlePaidNotify(attach, orderSn, null);
|
||||
return Collections.emptyList();
|
||||
case 2: // 微信支付
|
||||
PaymentRequestV3 requestV3 = new PaymentRequestV3();
|
||||
requestV3.setTerminal(terminal);
|
||||
requestV3.setOpenId("");
|
||||
requestV3.setAttach(params.getAttach());
|
||||
requestV3.setOutTradeNo(params.getOutTradeNo());
|
||||
requestV3.setOrderAmount(params.getOrderAmount());
|
||||
requestV3.setDescription(params.getDescription());
|
||||
return WxPayDriver.unifiedOrder(requestV3);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new PaymentException(e.getMessage());
|
||||
}
|
||||
|
||||
// 失效时间
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
||||
Long expireTime = System.currentTimeMillis() + (30 * 60 * 1000);
|
||||
String timeExpire = format.format(expireTime) + "+08:00";
|
||||
|
||||
// 订单信息
|
||||
WxPayUnifiedOrderV3Request wxPayUnifiedOrderV3Request = new WxPayUnifiedOrderV3Request();
|
||||
wxPayUnifiedOrderV3Request.setOutTradeNo(orderSn);
|
||||
wxPayUnifiedOrderV3Request.setDescription(description);
|
||||
wxPayUnifiedOrderV3Request.setTimeExpire(timeExpire);
|
||||
wxPayUnifiedOrderV3Request.setAttach(attach);
|
||||
wxPayUnifiedOrderV3Request.setNotifyUrl(RequestUtils.uri() + "/api/pay/notifyMnp");
|
||||
|
||||
// 订单金额
|
||||
WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
|
||||
amount.setTotal(AmountUtil.yuan2Fen(orderAmount.toPlainString()));
|
||||
amount.setCurrency("CNY");
|
||||
wxPayUnifiedOrderV3Request.setAmount(amount);
|
||||
|
||||
// 付款人员
|
||||
WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
|
||||
payer.setOpenid(openId);
|
||||
|
||||
// H5平台
|
||||
if (terminal == ClientEnum.H5.getCode()) {
|
||||
WxPayUnifiedOrderV3Request.SceneInfo sceneInfo = new WxPayUnifiedOrderV3Request.SceneInfo();
|
||||
WxPayUnifiedOrderV3Request.H5Info h5Info = new WxPayUnifiedOrderV3Request.H5Info();
|
||||
h5Info.setType("android");
|
||||
sceneInfo.setH5Info(h5Info);
|
||||
sceneInfo.setPayerClientIp(IpUtils.getHostIp());
|
||||
sceneInfo.setDeviceId("1");
|
||||
wxPayUnifiedOrderV3Request.setSceneInfo(sceneInfo);
|
||||
}
|
||||
|
||||
// 发起订单
|
||||
WxPayService wxPayService = WxPayDriver.handler(terminal);
|
||||
wxPayUnifiedOrderV3Request.setPayer(payer);
|
||||
return wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxPayUnifiedOrderV3Request);
|
||||
throw new PaymentException("支付发起异常");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public class PaymentValidate implements Serializable {
|
|||
private String attach;
|
||||
|
||||
@ApiModelProperty(value = "订单编号", notes = "该参数无需传递")
|
||||
private String orderSn;
|
||||
private String outTradeNo;
|
||||
|
||||
@ApiModelProperty(value = "订单金额", notes = "该参数无需传递")
|
||||
private BigDecimal orderAmount;
|
||||
|
|
|
|||
Loading…
Reference in New Issue