diff --git a/yudao-module-mall/yudao-module-shop-api/src/main/java/cn/iocoder/yudao/module/shop/request/order/OrderPayRequest.java b/yudao-module-mall/yudao-module-shop-api/src/main/java/cn/iocoder/yudao/module/shop/request/order/OrderPayRequest.java index e63c17514..82225c02a 100644 --- a/yudao-module-mall/yudao-module-shop-api/src/main/java/cn/iocoder/yudao/module/shop/request/order/OrderPayRequest.java +++ b/yudao-module-mall/yudao-module-shop-api/src/main/java/cn/iocoder/yudao/module/shop/request/order/OrderPayRequest.java @@ -46,4 +46,6 @@ public class OrderPayRequest { @Schema(description = "下单时小程序的场景值") private Integer scene; + @Schema(description = "openid") + private String openid; } diff --git a/yudao-module-mall/yudao-module-shop-biz/src/main/java/cn/iocoder/yudao/module/shop/service/order/impl/OrderPayServiceImpl.java b/yudao-module-mall/yudao-module-shop-biz/src/main/java/cn/iocoder/yudao/module/shop/service/order/impl/OrderPayServiceImpl.java index 57571ca00..d4dee7897 100644 --- a/yudao-module-mall/yudao-module-shop-biz/src/main/java/cn/iocoder/yudao/module/shop/service/order/impl/OrderPayServiceImpl.java +++ b/yudao-module-mall/yudao-module-shop-biz/src/main/java/cn/iocoder/yudao/module/shop/service/order/impl/OrderPayServiceImpl.java @@ -5,6 +5,9 @@ import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.Constants; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.util.date.DateUtils; +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; +import cn.iocoder.yudao.framework.pay.config.WxPayOneAutoConfiguration; +import cn.iocoder.yudao.framework.pay.properties.WxPayProperties; import cn.iocoder.yudao.module.infra.api.config.ApiConfigApi; import cn.iocoder.yudao.module.member.api.user.MemberUserApi; import cn.iocoder.yudao.module.member.api.user.dto.MemberUserBillDTO; @@ -22,12 +25,18 @@ import cn.iocoder.yudao.module.shop.service.product.StoreProductService; import cn.iocoder.yudao.module.shop.utils.OrderUtil; import cn.iocoder.yudao.module.shop.utils.RedisUtil; import cn.iocoder.yudao.module.shop.vo.order.WxPayJsResultVo; +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 lombok.Data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.support.TransactionTemplate; +import org.springframework.util.Assert; import java.math.BigDecimal; import java.util.HashMap; @@ -86,6 +95,12 @@ public class OrderPayServiceImpl implements OrderPayService { @Autowired private ApiConfigApi apiConfigApi; + @Autowired + private WxPayOneAutoConfiguration wxPayOneAutoConfiguration; + + @Autowired + private WxPayProperties payProperties; + /** * 支付成功处理 @@ -104,7 +119,7 @@ public class OrderPayServiceImpl implements OrderPayService { // billList.add(userBill); // 更新用户下单数量 - // user.setPayCount(user.getPayCount() + 1); + // user.setPayCount(user.getPayCount() + 1); Boolean execute = transactionTemplate.execute(e -> { @@ -135,6 +150,7 @@ public class OrderPayServiceImpl implements OrderPayService { /** * 余额支付 + * * @param storeOrder 订单 * @return Boolean Boolean */ @@ -191,7 +207,7 @@ public class OrderPayServiceImpl implements OrderPayService { storeOrder.setIsChannel(3); } if (orderPayRequest.getPayType().equals(PayConstants.PAY_TYPE_WE_CHAT)) { - switch (orderPayRequest.getPayChannel()){ + switch (orderPayRequest.getPayChannel()) { case PayConstants.PAY_CHANNEL_WE_CHAT_H5:// H5 storeOrder.setIsChannel(2); break; @@ -229,7 +245,7 @@ public class OrderPayServiceImpl implements OrderPayService { // 微信支付,调用微信预下单,返回拉起微信支付需要的信息 if (storeOrder.getPayType().equals(PayConstants.PAY_TYPE_WE_CHAT)) { // 预下单 - Map unifiedorder = unifiedorder(storeOrder, ip); + Map unifiedorder = unifiedorder(storeOrder, ip, orderPayRequest.getOpenid()); response.setStatus(true); WxPayJsResultVo vo = new WxPayJsResultVo(); vo.setAppId(unifiedorder.get("appId")); @@ -266,11 +282,12 @@ public class OrderPayServiceImpl implements OrderPayService { /** * 预下单 + * * @param storeOrder 订单 - * @param ip ip + * @param ip ip * @return 预下单返回对象 */ - private Map unifiedorder(StoreOrder storeOrder, String ip) { + private Map unifiedorder(StoreOrder storeOrder, String ip,String openid) { // 获取用户openId // 根据订单支付类型来判断获取公众号openId还是小程序openId @@ -314,6 +331,25 @@ public class OrderPayServiceImpl implements OrderPayService { // if (storeOrder.getIsChannel() == 2) { // map.put("mweb_url", responseVo.getMWebUrl()); // } + + WxPayService wxPayService = wxPayOneAutoConfiguration.wxPayOneService(); + Assert.notNull(wxPayService, "获取微信支付配置失败!"); + WxPayUnifiedOrderV3Request wxPayRequest = new WxPayUnifiedOrderV3Request(); + int sum = storeOrder.getPayPrice().multiply(new BigDecimal("100")).intValue(); + wxPayRequest.setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(sum)); + wxPayRequest.setDescription("会员充值"); + wxPayRequest.setOutTradeNo(storeOrder.getOrderId()); + wxPayRequest.setNotifyUrl(payProperties.getNotifyUrl()); + wxPayRequest.setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(openid)); + wxPayRequest.setSceneInfo(new WxPayUnifiedOrderV3Request.SceneInfo().setPayerClientIp(ip)); + wxPayRequest.setAppid(payProperties.getAppId()); + wxPayRequest.setMchid(payProperties.getMchId()); + try { + WxPayUnifiedOrderV3Result wxPayUnifiedOrderV3Result = wxPayService.unifiedOrderV3(TradeTypeEnum.JSAPI, wxPayRequest); + map.put("mweb_url",wxPayUnifiedOrderV3Result.getH5Url()); + } catch (Exception e) { + e.getMessage(); + } return map; } }