微信支付新增小程序渠道,修复小程序与其他支付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

@ -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