java微信付款到零钱

317 阅读1分钟

引入jar包:

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

1、登录微信商户平台进行申请。

账户首页 - 微信支付商户平台 (qq.com)

2、提现到微信

/**
     * 提现
     * @author seeklife
     * @param money
     * @param uId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String, Object> payPerson(BigDecimal money, Integer uId, String openId){
        Map<String, Object> map =new HashMap<String,Object>();
        EntPayRequest entPayRequest = EntPayRequest.newBuilder().build();
        entPayRequest.setAppid("自己的");
        entPayRequest.setMchId("自己的");
        entPayRequest.setPartnerTradeNo("withdrawl"+System.currentTimeMillis());
        entPayRequest.setOpenid(openId);
        entPayRequest.setCheckName("NO_CHECK");
        entPayRequest.setAmount(yuanToFee(money));
        entPayRequest.setDescription("二级分销提现");
        entPayRequest.setSpbillCreateIp(IpUtil.getLocalIP());
        EntPayResult entPayResult = null;
        try {
            entPayResult = wxService().getEntPayService().entPay(entPayRequest);
        } catch (WxPayException e) {
            e.printStackTrace();
            map.put("success","error");
            map.put("des","转账失败!");
        }
        if(ObjectUtil.isNotNull(entPayResult)){
            if(entPayResult.getResultCode().equals("SUCCESS")){
                log.info("【提现到微信零钱返回结果】:{}", entPayResult);
                map.put("success","true");
                map.put("des","转账成功!");
            }else{
                map.put("success","false");
                map.put("des","转账失败!");
            }
        }
        return map;
    }

    /**
     * @author seeklife
     * 微信api配置,注意退款需要设置api证书
     */
    public WxPayService wxService(){
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId("自己的");
        payConfig.setMchId("自己的");
        payConfig.setMchKey("自己的");
        InputStream stream1 = getClass().getClassLoader().getResourceAsStream("apiclient_cert.p12");
        File targetFile1 = new File("apiclient_cert.p12");
        try {
            FileUtils.copyInputStreamToFile(stream1, targetFile1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        payConfig.setKeyPath(targetFile1.getPath());
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }