JAVA-支付宝单笔转账

使用支付宝AIP向个人转账

支付宝证书及配置

https://opendocs.alipay.com/open/291/twngcd/

官方文档

https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer/

支付宝jdk版本

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

主流程代码

证书地址如果是本地运行,可以放在项目包里的根目录下面就可以了,直接读取相对路径就可以了。
如果是放在服务器上面可能会报找不到证书地址,这时候需要把证书地址在docker中镜像一个地址,之后在代码中读取镜像之后的地址。


import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
/**
*out_biz_no 商户订单号
*identity 转账对象的手机号 参与方的唯一标识
*name 转账对象的真实姓名
*trans_amount 转账金额
*/
public Map aliPayWithdraw(String out_biz_no, String identity, String name, BigDecimal trans_amount) {
        try {
            if (new BigDecimal(0.1).compareTo(trans_amount) == 1) {
                return RestResult.result(RespCode.CODE_2.getValue(), "提现金额小于最低值");
            }
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");//服务器网址
            certAlipayRequest.setAppId("");//支付宝分配给开发者的应用ID
            certAlipayRequest.setPrivateKey("");//生成的私钥
            certAlipayRequest.setFormat("json");//请求格式
            certAlipayRequest.setCharset("UTF-8");//编码
            certAlipayRequest.setSignType("RSA2");//商户生成签名字符串所使用的签名算法类型
//证书地址            
certAlipayRequest.setCertPath("appCertPublicKey_2014072300007148.crt");
            certAlipayRequest.setAlipayPublicCertPath("alipayCertPublicKey_RSA2.crt");
            certAlipayRequest.setRootCertPath("alipayRootCert.crt");
            DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            request.setBizContent("{" +
                    "\"out_biz_no\":\"" + out_biz_no + "\"," +
                    "\"trans_amount\":" + trans_amount + "," +
                    "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +
                    "\"biz_scene\":\"DIRECT_TRANSFER\"," +
                    "\"order_title\":\"转账\"," +
                    "\"payee_info\":{" +
                    "\"identity\":\"" + identity + "\"," +
                    "\"identity_type\":\"ALIPAY_LOGON_ID\"," +
                    "\"name\":\"" + name + "\"," +
                    "    }," +
                    "  }");
            AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
            //请求结束的返回参数
            Map<String, Object> map = new HashMap<>();
            map.put("code", response.getCode());
            map.put("status", response.getStatus());
            map.put("msg", response.getMsg());
            map.put("subCode", response.getSubCode());
            map.put("subMsg", response.getSubMsg());
            map.put("tradingNo", response.getOutBizNo());
            map.put("payOrderId", response.getOrderId());
            map.put("payFundOrderId", response.getPayFundOrderId());
            map.put("payTime", response.getTransDate());
            if (response.isSuccess()) {
                logger.error(response.getMsg());
                logger.info("提现成功");
                return map;
            } else {
                logger.error(response.getMsg());
                logger.info("提现失败");
                return map;
            }
        } catch (Exception e) {
            logger.error("提现出错", e);
        }
    }
关于提交代码至服务器端时无法读取到项目目录

可以把证书放置docker镜像文件,然后读取镜像之后的证书地址

请求返回的参数示例
{
    "alipay_fund_trans_uni_transfer_response": {
        "code": "10000",
        "msg": "Success",
        "out_biz_no": "201808080001",
        "order_id": "20190801110070000006380000250621",
        "pay_fund_order_id": "20190801110070001506380000251556",
        "status": "SUCCESS",
        "trans_date": "2019-08-21 00:00:00"
    },
    "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

异常

公共异常类型

https://opendocs.alipay.com/open/common/105806

{
    "alipay_fund_trans_uni_transfer_response": {
        "code": "20000",
        "msg": "Service Currently Unavailable",
        "sub_code": "isp.unknow-error",
        "sub_msg": "系统繁忙"
    },
    "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

支付宝其余功能AIP示例

https://opendocs.alipay.com/open/309/106237

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值