Java实现单次转账到到个人支付宝账户(新版接口)

前言:支付宝5月21日发布通知单次转账接口升级,老版接口不再维护

1.首先需要认证企业支付宝账户

2.准入条件

该能力仅对企业支付宝账户开放;

企业支付宝账户需满足注册满 90 天,且已实名认证。

3.去绑定(创建)你的应用

java 转账 java转账接口_后端


4.去能力列表,能力管理签约转账到支付宝账号

java 转账 java转账接口_后端_02


准备工作完成进入开发阶段

1.alipay.fund.trans.toaccount.transfer (旧版转账到支付宝账户接口/阿里已经不维护了不建议这个)

链接: https://opendocs.alipay.com/open/309/alipay.fund.trans.toaccount.transfer.

2.alipay.fund.trans.uni.transfer(单笔转账接口/这是新版)

本人用的是证书签名验证的新版转账接口,下面是代码的实现(如果不了解什么是证书签名验证点击查看链接: https://opendocs.alipay.com/open/291/105974.)

需要的maven依赖

<dependency>
   <groupId>com.alipay.sdk</groupId>
   <artifactId>alipay-sdk-java</artifactId>
   <version>4.8.73.ALL</version>
</dependency>

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
</dependency>

Controller层

@RequestMapping("/updatewithdrawal")
@ResponseBody
@CrossOrigin(origins = "*", maxAge = 3600)//跨域
public ResultMap updatewithdrawal(String id, Integer mType) throws AlipayApiException {
	//根据提现订单id查询出提现记录对象
    Withdraw w = userService.getwithdrawalsById(id);
    if (w.getmManner() == 1) {
    	//调用转账到个人支付宝
        Map<String, Object> map = alipayService.alipayWithdraw(id, w.getmMoney(),w.getuId(), w.getAccountpayeename(), w.getAccountpayee());
        return ResultMap.ok(200, "success").put("data",map);
    } else if (w.getmManner() == 2) {
    //这是微信提现
        Map<String, String> b = userService.updatewithdrawal(id,mType);
       return ResultMap.ok(200, "success").put("data",b);
    }
    return ResultMap.error(305, "提现异常");
}

Service层

/**
 * 支付宝提现
 * @param wOrderNumber  自定义订单号
 * @param price   提现金额
 * @param zfbName 支付宝账户
 * @param name   真实姓名
 * @throws Exception
 */
Map<String, Object> alipayWithdraw(String wOrderNumber, Double price,String uId,String name, String zfbName )


/**
 * 支付宝提现
 * @param wOrderNumber  自定义订单号
 * @param price   提现金额
 * @param zfbName 支付宝账户
 * @param name   真实姓名
 * @throws Exception
 */
@Override
public Map<String, Object> alipayWithdraw(String wOrderNumber,Double price,String uId,String name,String zfbName ) throws AlipayApiException {
    //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
    //构造client
    CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

    //设置网关地址https://openapi.alipay.com/gateway.do
   certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
//设置应用AppId
    certAlipayRequest.setAppId("应用AppId");
//设置应用私钥
    certAlipayRequest.setPrivateKey("应用私钥");
    certAlipayRequest.setFormat("json");
//设置字符集
    certAlipayRequest.setCharset("UTF-8");
//设置签名类型
    certAlipayRequest.setSignType("RSA2");
//设置应用公钥证书路径
    certAlipayRequest.setCertPath("");
//设置支付宝公钥证书路径
  certAlipayRequest.setAlipayPublicCertPath("");
//设置支付宝根证书路径
    certAlipayRequest.setRootCertPath("");

    //构造Client
    AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
   //实例化接口
    AlipayFundTransUniTransferRequest request=new AlipayFundTransUniTransferRequest();
    Alipay alipay = new Alipay();
    alipay.setOut_biz_no(wOrderNumber);//商户自定义的唯一订单号
    alipay.setTrans_amount(price);//金额
    PayeeInfo payerInfo=new PayeeInfo();
    payerInfo.setIdentity(zfbName);//收款方支付宝账号
    payerInfo.setIdentity_type("ALIPAY_LOGON_ID");
    payerInfo.setName(name);
    alipay.setProduct_code("TRANS_ACCOUNT_NO_PWD");
    alipay.setBiz_scene("DIRECT_TRANSFER");
    alipay.setPayee_info(payerInfo);
    alipay.setOrder_title("标题");
    alipay.setRemark("备注理由");
    //转成json格式放入
    String json = new Gson().toJson(alipay);
    request.setBizContent(json);
    AlipayFundTransUniTransferResponse response=null;
    AlipayFundTransOrderQueryResponse response1=null;
    Map<String, Object> map = new HashMap<String,Object>();
    try{
        response = alipayClient.certificateExecute(request);
        if (response.isSuccess()){
            if("10000".equals(response.getCode())){
                Boolean b = userMapper.updatewithdrawal(wOrderNumber,1);
                if(b){
                    map.put("code", response.getCode());
                    map.put("success","true");
                    map.put("des", "转账成功");
                }else {
                    map.put("code", response.getCode());
                    map.put("success","false");
                    map.put("des", "转账成功,修改状态失败!");
                }

            } else {
                map.put("code", response.getCode());
                map.put("sub_code", response.getSubCode());//详情状态码
                map.put("success", "false");
                map.put("sub_msg", response.getSubMsg());//详情原因
            }
            return map;
        }else {
        //调用查询接口查询数据
            AlipayFundTransOrderQueryRequest request1 = new AlipayFundTransOrderQueryRequest();
            Alipay alipay1 = new Alipay();
            alipay1.setOut_biz_no(wOrderNumber);
            //转成json格式放入
            String json1 = new Gson().toJson(alipay1);
            request1.setBizContent(json1);
            response1=alipayClient.certificateExecute(request1);
            if(response1.isSuccess()){
                if("10000".equals(response1.getCode())){
                    map.put("code", response1.getCode());
                    map.put("success","true");
                    map.put("des", "已经转账成功,无需重复请求!");
                } else {
                    map.put("code", response1.getCode());
                    map.put("sub_code", response1.getSubCode());//详情状态码
                    map.put("success", "false");
                    map.put("sub_msg", response1.getSubMsg());//详情原因
                }
                return map;
            }
            else {
                map.put("success", "false");
                map.put("des", "订单查询结果失败!");
                return map;
            }

        }

    }catch(AlipayApiException e){
        e.printStackTrace();
        map.put("success", "false");
        map.put("des", "转账失败!");

    }
    return map;
}

大致看了看,目前可查的支付宝单笔转账的代码实现基本都是老版本接口,以上是新版本(单笔转账到支付宝账号/查询转账订单)的代码实现(基础版代码,可以思考如何去优化) 好记性不如烂笔头~