企業付款到零錢(微信小程序提現,用戶提現到零錢)

 

pom 增加

    <dependency>
       <groupId>com.github.binarywang</groupId>
       <artifactId>weixin-java-pay</artifactId>
       <version>3.2.0</version>
    </dependency>

 

附上github https://github.com/Wechat-Group/WxJava/wiki

一、證書申請

企業付款需要用到p12證書,證書可以在商戶平臺的 賬戶平臺 - API安全 - API證書 中申請到。(注:需要升級一下)

二、WxPayConfig配置

將 com.github.binarywang.wxpay.service.WxPayService 作爲Bean注入到項目中

實現代碼如下,具體作用讀者可引入pom後自行查看,binarywang作者的註釋非常詳細。有問題可留言討論

我在使用@Autowired 註解時無法注入 所以就注掉了 具體調用方法下面會貼上

/**
 * 企業支付WxPayConfig配置
 * @author hsw
 */
@Configuration
public class WxConfig {

//    @Autowired
//    private WxMpProperties wxMpProperties;

    @Bean
    public WxPayConfig wxPayConfig() {
//        final List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
        WxPayConfig payConfig = new WxPayConfig();
//        payConfig.setAppId(configs.get(1).getAppId());
//        payConfig.setMchId(configs.get(1).getPayMchId());
//        payConfig.setMchKey(configs.get(1).getPaySecretKey());
//        payConfig.setNotifyUrl(configs.get(1).getPayResultUrl());
//        payConfig.setKeyPath(configs.get(1).get);
        payConfig.setTradeType("JSAPI");
        payConfig.setSignType("MD5");
        return payConfig;
    }


    @Bean
    public WxPayService wxPayService(WxPayConfig payConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

 

3、API調用

 /**
     * 進行微信提現操作 (企業付款至用戶零錢)
     * @param openId
     * @param amout     提現金額
     * @param partnerTradeNo    單號
     * @param desc     備註
     */
    private EntPayResult getWithdrawal(String openId, BigDecimal amout,String partnerTradeNo, String desc, HttpServletRequest request){
        final List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
        EntPayRequest entPayRequest = new EntPayRequest();
        entPayRequest.setAppid(configs.get(1).getAppId());
        entPayRequest.setMchId(configs.get(1).getPayMchId());  
        entPayRequest.setPartnerTradeNo(partnerTradeNo);
        entPayRequest.setOpenid(openId);
        entPayRequest.setCheckName("NO_CHECK");
        entPayRequest.setAmount(Integer.valueOf(amout.multiply(new BigDecimal(100)).setScale(0, 		    BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString()));
        entPayRequest.setDescription(desc);
        entPayRequest.setSpbillCreateIp(IpUtils.getIpAddr(request));
        EntPayResult entPayResult = null;
        try {
        	//此處調用=============================================================
            // 通過Java配置來實例化Spring容器
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WxConfig.class);
            // 在Spring容器中獲取Bean對象
            WxConfig wxConfig = context.getBean(WxConfig.class);
            WxPayService wxPayService = wxConfig.wxPayService(wxConfig.wxPayConfig());
            entPayResult = wxPayService.getEntPayService().entPay(entPayRequest);
            log.info("entPayResult : " + entPayResult);
            System.out.println(entPayResult);
        } catch (WxPayException e) {
            log.error("付款失敗,返回報文" + e);
            throw new ApiException(e.getCustomErrorMsg());
        }
        return entPayResult;
    }

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章