微信支付新增小程序渠道,修复小程序与其他支付appid冲突问题

This commit is contained in:
hupeng
2020-03-12 14:16:30 +08:00
parent 6adedb1687
commit ec863e471e
8 changed files with 184 additions and 16 deletions

View File

@ -18,6 +18,7 @@ import co.yixiang.common.web.vo.Paging;
import co.yixiang.constant.ShopConstants;
import co.yixiang.domain.AlipayConfig;
import co.yixiang.domain.vo.TradeVo;
import co.yixiang.enums.AppFromEnum;
import co.yixiang.enums.BillDetailEnum;
import co.yixiang.enums.BillEnum;
import co.yixiang.enums.OrderInfoEnum;
@ -63,6 +64,7 @@ import co.yixiang.modules.user.web.vo.YxUserQueryVo;
import co.yixiang.modules.user.web.vo.YxWechatUserQueryVo;
import co.yixiang.mp.service.YxPayService;
import co.yixiang.mp.service.YxTemplateService;
import co.yixiang.mp.service.YxMiniPayService;
import co.yixiang.service.AlipayService;
import co.yixiang.utils.OrderUtil;
import com.alibaba.fastjson.JSON;
@ -161,6 +163,8 @@ public class YxStoreOrderServiceImpl extends BaseServiceImpl<YxStoreOrderMapper,
@Autowired
private YxPayService payService;
@Autowired
private YxMiniPayService miniPayService;
@Autowired
private YxTemplateService templateService;
@ -219,8 +223,14 @@ public class YxStoreOrderServiceImpl extends BaseServiceImpl<YxStoreOrderMapper,
}else{
BigDecimal bigDecimal = new BigDecimal("100");
try {
payService.refundOrder(param.getOrderId(),
bigDecimal.multiply(orderQueryVo.getPayPrice()).intValue());
if(OrderInfoEnum.PAY_CHANNEL_1.getValue().equals(orderQueryVo.getIsChannel())){
miniPayService.refundOrder(param.getOrderId(),
bigDecimal.multiply(orderQueryVo.getPayPrice()).intValue());
}else{
payService.refundOrder(param.getOrderId(),
bigDecimal.multiply(orderQueryVo.getPayPrice()).intValue());
}
} catch (WxPayException e) {
log.info("refund-error:{}",e.getMessage());
}
@ -1048,7 +1058,7 @@ public class YxStoreOrderServiceImpl extends BaseServiceImpl<YxStoreOrderMapper,
BigDecimal bigDecimal = new BigDecimal(100);
return payService.wxPay(orderId,wechatUser.getRoutineOpenid(),"小程序商品购买",
return miniPayService.wxPay(orderId,wechatUser.getRoutineOpenid(),"小程序商品购买",
bigDecimal.multiply(orderInfo.getPayPrice()).intValue(),
BillDetailEnum.TYPE_3.getValue());
}
@ -1298,7 +1308,11 @@ public class YxStoreOrderServiceImpl extends BaseServiceImpl<YxStoreOrderMapper,
storeOrder.setSeckillId(seckillId);
storeOrder.setBargainId(bargainId);
storeOrder.setCost(BigDecimal.valueOf(cacheDTO.getPriceGroup().getCostPrice()));
storeOrder.setIsChannel(param.getIsChannel());
if(AppFromEnum.ROUNTINE.getValue().equals(param.getFrom())){
storeOrder.setIsChannel(OrderInfoEnum.PAY_CHANNEL_1.getValue());
}else{
storeOrder.setIsChannel(OrderInfoEnum.PAY_CHANNEL_0.getValue());
}
storeOrder.setAddTime(OrderUtil.getSecondTimestampTwo());
storeOrder.setUnique(key);
storeOrder.setShippingType(param.getShippingType());

View File

@ -29,6 +29,11 @@ public interface ShopConstants {
*/
String YSHOP_WEIXIN_PAY_SERVICE = "yshop_weixin_pay_service";
/**
* 微信支付小程序service
*/
String YSHOP_WEIXIN_MINI_PAY_SERVICE = "yshop_weixin_mini_pay_service";
/**
* 微信公众号service
*/
@ -56,10 +61,6 @@ public interface ShopConstants {
*/
String YSHOP_REDIS_INDEX_KEY = "yshop:index_data";
/**
* redis首页过期时间 单位秒
*/
long YSHOP_REDIS_INDEX_KEY_EXPIRE = 7200;

View File

@ -38,6 +38,9 @@ public enum OrderInfoEnum {
CONFIRM_STATUS_0(0,"正常"),
CONFIRM_STATUS_1(1,"确认"),
PAY_CHANNEL_0(0,"公众号/H5支付渠道"),
PAY_CHANNEL_1(1,"小程序支付渠道"),
SHIPPIING_TYPE_1(1,"快递"),
SHIPPIING_TYPE_2(2,"门店自提");

View File

@ -39,7 +39,7 @@ public class WxPayConfiguration {
WxPayService wxPayService = payServices.get(ShopConstants.YSHOP_WEIXIN_PAY_SERVICE);
if(wxPayService == null || RedisUtil.get(ShopConstants.YSHOP_WEIXIN_PAY_SERVICE) == null) {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(RedisUtil.get("wxpay_appId"));
payConfig.setAppId(RedisUtil.get("wechat_appid"));
payConfig.setMchId(RedisUtil.get("wxpay_mchId"));
payConfig.setMchKey(RedisUtil.get("wxpay_mchKey"));
payConfig.setKeyPath(RedisUtil.get("wxpay_keyPath"));
@ -55,11 +55,39 @@ public class WxPayConfiguration {
return wxPayService;
}
/**
* 获取WxAppPayService
* @return
*/
public static WxPayService getWxAppPayService() {
WxPayService wxPayService = payServices.get(ShopConstants.YSHOP_WEIXIN_MINI_PAY_SERVICE);
if(wxPayService == null || RedisUtil.get(ShopConstants.YSHOP_WEIXIN_PAY_SERVICE) == null) {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(RedisUtil.get("wxapp_appId"));
payConfig.setMchId(RedisUtil.get("wxpay_mchId"));
payConfig.setMchKey(RedisUtil.get("wxpay_mchKey"));
payConfig.setKeyPath(RedisUtil.get("wxpay_keyPath"));
// 可以指定是否使用沙箱环境
payConfig.setUseSandboxEnv(false);
wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
payServices.put(ShopConstants.YSHOP_WEIXIN_MINI_PAY_SERVICE, wxPayService);
//增加标识
RedisUtil.set(ShopConstants.YSHOP_WEIXIN_PAY_SERVICE,"yshop");
}
return wxPayService;
}
/**
* 移除WxPayService
*/
public static void removeWxPayService(){
RedisUtil.del(ShopConstants.YSHOP_WEIXIN_PAY_SERVICE);
payServices.remove(ShopConstants.YSHOP_WEIXIN_PAY_SERVICE);
payServices.remove(ShopConstants.YSHOP_WEIXIN_MINI_PAY_SERVICE);
}
}

View File

@ -0,0 +1,112 @@
package co.yixiang.mp.service;
import cn.hutool.core.util.StrUtil;
import co.yixiang.exception.ErrorRequestException;
import co.yixiang.mp.config.WxPayConfiguration;
import co.yixiang.mp.handler.RedisHandler;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* @ClassName 小程序支付YxPayService
* @Author hupeng <610796224@qq.com>
* @Date 2020/3/12
**/
@Service
@AllArgsConstructor
public class YxMiniPayService {
private final RedisHandler redisHandler;
/**
* 小程序支付
*
* @param orderId
* @param openId 小程序openid
* @param body
* @param totalFee
* @return
* @throws WxPayException
*/
public WxPayMpOrderResult wxPay(String orderId, String openId, String body,
Integer totalFee,String attach) throws WxPayException {
String apiUrl = redisHandler.getVal("api_url");
if (StrUtil.isBlank(apiUrl)) throw new ErrorRequestException("请配置api地址");
WxPayService wxPayService = WxPayConfiguration.getWxAppPayService();
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setTradeType("JSAPI");
orderRequest.setOpenid(openId);
orderRequest.setBody(body);
orderRequest.setOutTradeNo(orderId);
orderRequest.setTotalFee(totalFee);
orderRequest.setSpbillCreateIp("127.0.0.1");
orderRequest.setNotifyUrl(apiUrl + "/api/wechat/notify");
orderRequest.setAttach(attach);
WxPayMpOrderResult orderResult = wxPayService.createOrder(orderRequest);
return orderResult;
}
/**
* 退款
* @param orderId
* @param totalFee
* @throws WxPayException
*/
public void refundOrder(String orderId, Integer totalFee) throws WxPayException {
String apiUrl = redisHandler.getVal("api_url");
if (StrUtil.isBlank(apiUrl)) throw new ErrorRequestException("请配置api地址");
WxPayService wxPayService = WxPayConfiguration.getWxAppPayService();
WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
wxPayRefundRequest.setTotalFee(totalFee);//订单总金额
wxPayRefundRequest.setOutTradeNo(orderId);
wxPayRefundRequest.setOutRefundNo(orderId);
wxPayRefundRequest.setRefundFee(totalFee);//退款金额
wxPayRefundRequest.setNotifyUrl(apiUrl + "/api/notify/refund");
wxPayService.refund(wxPayRefundRequest);
}
/**
* 企业打款
* @param openid
* @param no
* @param userName
* @param amount
* @throws WxPayException
*/
public void entPay(String openid,String no,String userName,Integer amount) throws WxPayException{
WxPayService wxPayService = WxPayConfiguration.getWxAppPayService();
EntPayRequest entPayRequest = new EntPayRequest();
entPayRequest.setOpenid(openid);
entPayRequest.setPartnerTradeNo(no);
entPayRequest.setCheckName("FORCE_CHECK");
entPayRequest.setReUserName(userName);
entPayRequest.setAmount(amount);
entPayRequest.setDescription("提现");
entPayRequest.setSpbillCreateIp("127.0.0.1");
wxPayService.getEntPayService().entPay(entPayRequest);
}
}

View File

@ -19,7 +19,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
/**
* @ClassName YxPayService
* @ClassName 公众号支付YxPayService
* @Author hupeng <610796224@qq.com>
* @Date 2020/3/1
**/
@ -30,10 +30,10 @@ public class YxPayService {
private final RedisHandler redisHandler;
/**
* 微信公众号支付/小程序支付
* 微信公众号支付
*
* @param orderId
* @param openId 公众号/小程序openid
* @param openId 公众号openid
* @param body
* @param totalFee
* @return

View File

@ -61,8 +61,9 @@ public class SystemConfigController {
//重新配置微信相关
if(key.equals("wechat_appid")){
WxMpConfiguration.removeWxMpService();
WxPayConfiguration.removeWxPayService();
}
if(key.equals("wxpay_appId")){
if(key.equals("wxpay_mchId") || key.equals("wxapp_appId")){
WxPayConfiguration.removeWxPayService();
}
RedisUtil.set(key,value.toString(),0);

View File

@ -21,6 +21,7 @@ import co.yixiang.modules.shop.service.YxUserBillService;
import co.yixiang.modules.shop.service.YxUserService;
import co.yixiang.modules.shop.service.dto.*;
import co.yixiang.modules.shop.service.mapper.YxStoreOrderMapper;
import co.yixiang.mp.service.YxMiniPayService;
import co.yixiang.mp.service.YxPayService;
import co.yixiang.utils.OrderUtil;
import co.yixiang.utils.QueryHelp;
@ -57,11 +58,12 @@ public class YxStoreOrderServiceImpl implements YxStoreOrderService {
private final YxStoreOrderStatusService yxStoreOrderStatusService;
private final YxUserService userService;
private final YxPayService payService;
private final YxMiniPayService miniPayService;
public YxStoreOrderServiceImpl(YxStoreOrderRepository yxStoreOrderRepository, YxStoreOrderCartInfoRepository yxStoreOrderCartInfoRepository, YxUserRepository userRepository,
YxStorePinkRepository storePinkRepository, YxStoreOrderMapper yxStoreOrderMapper, YxUserBillService yxUserBillService,
YxStoreOrderStatusService yxStoreOrderStatusService,
YxUserService userService, YxPayService payService) {
YxUserService userService, YxPayService payService, YxMiniPayService miniPayService) {
this.yxStoreOrderRepository = yxStoreOrderRepository;
this.yxStoreOrderCartInfoRepository = yxStoreOrderCartInfoRepository;
this.userRepository = userRepository;
@ -71,6 +73,7 @@ public class YxStoreOrderServiceImpl implements YxStoreOrderService {
this.yxStoreOrderStatusService = yxStoreOrderStatusService;
this.userService = userService;
this.payService = payService;
this.miniPayService = miniPayService;
}
@Override
@ -155,8 +158,14 @@ public class YxStoreOrderServiceImpl implements YxStoreOrderService {
}else{
BigDecimal bigDecimal = new BigDecimal("100");
try {
payService.refundOrder(resources.getOrderId(),
bigDecimal.multiply(resources.getPayPrice()).intValue());
if(OrderInfoEnum.PAY_CHANNEL_1.getValue().equals(resources.getIsChannel())){
miniPayService.refundOrder(resources.getOrderId(),
bigDecimal.multiply(resources.getPayPrice()).intValue());
}else{
payService.refundOrder(resources.getOrderId(),
bigDecimal.multiply(resources.getPayPrice()).intValue());
}
} catch (WxPayException e) {
log.info("refund-error:{}",e.getMessage());
}