使用第三方sdk进行微信支付

  • Post author:
  • Post category:其他


先导入依赖

<dependency>
            <groupId>cn.springboot</groupId>
            <artifactId>best-pay-sdk</artifactId>
            <version>1.1.0</version>
</dependency>

spring启动的时候从yml注入相应的配置

@Data
@Component
@ConfigurationProperties(prefix = "wx.mp.configs")
public class WeChatAccountConfig {

    private String appId;                   //公众号的appid

    private String secret;                  //公众号的AppSecret

    private String mchId;                   //商户号

    private String mchKey;                  //商户秘钥

    private String keyPath;                 //商户证书路径

	private String notifyUrl;               //微信支付异步通知地址

}

初始化支付service的相关配置

@Component
public class WeChatPayConfig {

    @Autowired
    @Qualifier("weChatAccountConfig")
    private WeChatAccountConfig weChatAccountConfig;

    @Bean
    public BestPayServiceImpl bestPayService(){
        BestPayServiceImpl bestPayService = new BestPayServiceImpl();
        bestPayService.setWxPayH5Config(wxPayH5Config());
        return bestPayService;
    }

    WxPayH5Config wxPayH5Config(){
        WxPayH5Config wxPayH5Config = new WxPayH5Config();
        wxPayH5Config.setAppId(weChatAccountConfig.getAppId());
        wxPayH5Config.setAppSecret(weChatAccountConfig.getSecret());
        wxPayH5Config.setKeyPath(weChatAccountConfig.getKeyPath());
        wxPayH5Config.setMchId(weChatAccountConfig.getMchId());
        wxPayH5Config.setMchKey(weChatAccountConfig.getMchKey());
        wxPayH5Config.setNotifyUrl(weChatAccountConfig.getNotifyUrl());                	   
        return wxPayH5Config;
    }

}

实现预支付的service,这的openid是公众号的openid,

注意这里的create只是预支付

@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME="微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;

    @Override
    public void create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        //payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOpenid("oTgZpwTeEFsGDyUbhPvrS4y2mXgU");
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.ALIPAY_APP);  //支付类型
        log.info("[微信支付] request={}",payRequest);

        PayResponse payResponse = bestPayService.pay(payRequest);
        log.info("[微信支付] response={}",payResponse);

    }
}


请求参数和返回参数demo,注意这里返回的参数只是预支付参数,并不是直接支付

2020-02-06 16:24:10,282 - [微信支付] request={
  "payTypeEnum": "ALIPAY_APP",
  "orderId": "1213835492500574208",
  "orderAmount": 0.01,
  "orderName": "微信点餐订单",
  "openid": "oTgZpwTeEFsGDyUbhPvrS4y2mXgU"
}
2020-02-06 16:24:10,516 - [微信支付] response={
  "appId": "wxd898fcb01713c658",
  "timeStamp": "1580977450",
  "nonceStr": "GRi76tjjwkgnHP4U",
  //\u003d是=号
  "packAge": "prepay_id\u003dwx061624126949551e10003e9c1976267200",
  "signType": "MD5",
  "paySign": "3242460917D3763B5CF9B051CFD13071"
}

详情可见第三方依赖的github

文档


微信支付的文档


在这里插入图片描述



版权声明:本文为weixin_43944305原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。