app提现至微信(微信企业付款到个人微信号)

1.申请一个商户号,并开通企业付款功能,需要下载api证书

2.在微信开放平台申请app授权登录的账号(带有appid),开通支付功能,在商户号中如下图所示,在appId授权管理中绑定该appId,这步做完前端可以获取到openid

3.如果有小程序提现到微信的业务,申请小程序并开通支付功能,并在商户号中绑定,前端静默获取code,传给后端,后端用code获取openid,存入数据库

4.企业付款代码参考微信官方网站

https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2

 

//下边是参考代码,时间久远,以备参考。

withdrawByWX.java

package com.naryou.controller.smallAndApp.live;

import com.naryou.config.ConfigUtil;
import com.naryou.utils.HmfAppJsonData;
import com.naryou.utils.HttpUtil;
import com.naryou.utils.payCorn.WXPayUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class withdrawByWX {

    public static String MCH_ID = ConfigUtil.getProperty("shanghuhao.MCH_ID");;
    public static String key = ConfigUtil.getProperty("shanghuhao.key");//秘钥
    public static String zfbAppid = ConfigUtil.getProperty("zfb.appid");//找房豹小程序
    public static String appAppid = ConfigUtil.getProperty("app.appid");//找房豹app
    //提现
    //参考 https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
    @RequestMapping(value = "smallWithdrawByWX")
    @ResponseBody
    public Object smallWithdrawByWX(HttpServletRequest request, Double money) throws Exception {
        try{
            //将金额转化为分
            BigDecimal bd = new BigDecimal(String.valueOf(money)).multiply(new BigDecimal("100"));
            String amountPay = bd.stripTrailingZeros().toPlainString();
            String openid="";
            //获取订单号
            String orderNo  = UUID.randomUUID().toString().replace("-", "");
            Map<String, String> reqData = new HashMap<String, String>();
            reqData.put("mch_appid", zfbAppid);// APPID
            reqData.put("mchid", MCH_ID);//商户ID
            reqData.put("nonce_str", WXPayUtil.generateUUID());
            reqData.put("partner_trade_no",orderNo);//订单号
            reqData.put("openid",openid);//openId
            reqData.put("check_name","NO_CHECK");//openId
            reqData.put("amount",amountPay);//企业付款金额,单位为分
            String desc = "红包提现"+money+"元";
            reqData.put("desc",desc);//企业付款备注,必填。注意:备注中的敏感词会被转成字符*
            String sign =  WXPayUtil.generateSignature(reqData,key);//API密钥
            reqData.put("sign",sign);//签名
            String param = WXPayUtil.mapToXml(reqData);
            //查询我的提现余额并锁表
            CloseableHttpResponse response = HttpUtil.sendPostForCert("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", param,true);
            String resultString = EntityUtils.toString(response.getEntity(), "utf-8");
//            String resultString  = HttpUtil.sendPostForCert2("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", param);
            Map resultMap = WXPayUtil.xmlToMap(resultString);
//            LOGGER.info("resultMap = " + resultMap);
            String return_code =  resultMap.get("return_code").toString();
            String return_msg =  resultMap.get("return_msg").toString();
            Map returnMap = new HashMap();
            if(return_code.equals("SUCCESS")){
                //return_code为SUCCESS的时候有返回
                String result_code =(String)resultMap.get("result_code");
                String err_code =(String)resultMap.get("err_code");
                if(result_code.equals("SUCCESS")){
                    //以下字段在return_code 和result_code都为SUCCESS的时候有返回
                    String payment_no =(String)resultMap.get("payment_no");//微信付款单号
                    String payment_time =(String)resultMap.get("payment_time");//付款成功时间
                    System.out.println("payment_no = " + payment_no);
                }else{
                    return HmfAppJsonData.FAIL(err_code);
                }
            }else{
                return HmfAppJsonData.FAIL(return_msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return HmfAppJsonData.FAIL("亲,请重试");
    }
}

HttpUtil.java

package com.naryou.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmEx
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值