⽀付宝转账到个⼈账户,查询转账订单
需要准备的东西如下:
应⽤私钥,⽀付宝公钥,应⽤公钥证书、⽀付宝公钥证书、⽀付宝根证书, 应⽤的appId,每个应⽤的appId都有对应的秘钥证书的
通过⽀付宝开放平台开发助⼿app获取秘钥(公钥、私钥),然后拿公钥去⽀付宝开放平台获取⽀付宝公钥
如果还需要加签成证书,那么就得到应⽤公钥证书和⽀付宝公钥证书应⽤证书是可以解析应⽤公钥的,
注意: 应⽤私钥请妥善保管,忘记了只能重新提取,是解析不出来的,⽀付宝公钥证书可以解析⽀付宝公钥的
依赖如下:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
</dependency>
⽀付宝转账到⽀付宝
以下都是使⽤证书的⽅式进⾏调⽤的
⽅式⼀:
新接⼝: ansfer
public Map<String, Object> aliPayTransfer(AliPayTransferParam aliPayTransfer) {
try {
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
certAlipayRequest.SERVER_URL());
certAlipayRequest.APPID());
certAlipayRequest.PAY_PRIVATE_KEY());
certAlipayRequest.FORMAT());
certAlipayRequest.CHARSET());
certAlipayRequest.SIGN_TYPE());
try {
//获取各证书路径
AliPayConfig certPath = getCertPath();
//设置应⽤公钥证书路径
certAlipayRequest.APPLICATION_PUBLIC_KEY());
//设置⽀付宝公钥证书路径
certAlipayRequest.AliPay_PUBLIC_CERT());
//设置⽀付宝根证书路径
certAlipayRequest.ROOT_CERT_PATH());
} catch (Exception e) {
e.printStackTrace();
}
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
AliPay alipay = new AliPay();
//收款⽅信息
PayeeInfo payerInfo = new PayeeInfo();
payerInfo.Identity());
if (Name() != null) {
payerInfo.setIdentity_type("ALIPAY_LOGON_ID");
payerInfo.Name());
}else {
payerInfo.setIdentity_type("ALIPAY_USER_ID");
}
alipay.setPayee_info(payerInfo);
alipay.setOut_biz_OutBizNo());
alipay.setTrans_TransAmount());
alipay.setProduct_code("TRANS_ACCOUNT_NO_PWD");
alipay.setBiz_scene("DIRECT_TRANSFER");
if (OrderTitle() != null) {
alipay.setOrder_OrderTitle());
}
if (Remark() != null) {
alipay.Remark());
}
//转成json格式放⼊
String json = new Gson().toJson(alipay);
request.setBizContent(json);
Map<String, Object> map = new HashMap<String, Object>();
try {
AlipayFundTransUniTransferResponse response = ificateExecute(request);
map.put("code", Code());
map.put("status", Status());
map.put("msg", Msg());
map.put("msg", Msg());
map.put("outBizNo", OutBizNo());
map.put("orderId", OrderId());
map.put("payFundOrderId", PayFundOrderId());
map.put("payTime", TransDate());
if (response.isSuccess()) {
map.put("des", "转账成功");
map.put("success", "true");
} else {
<("转账失败-->{}", SubCode());
map.put("code", Code());
map.put("des", "转账失败");
map.put("success", "false");
map.put("sub_code", SubCode());
map.put("sub_msg", SubMsg());
}
return map;
} catch (AlipayApiException e) {
<("转账出错-->{}", e.getMessage());
e.printStackTrace();
map.put("success", "false");
map.put("des", "转账异常!");
}
} catch (Exception e) {
e.printStackTrace();
}
throw new CustomException("系统繁忙,请稍后重试");
}
注意: identity_type为ALIPAY_LOGON_ID时⼀定要传收款名称(name), identity_type为ALIPAY_USER_ID时,identity传的是pId,也就是⽀付宝的合作伙伴id,以2088开头的。
⽅式⼆
新接⼝: ansfer
public Map<String, Object> aliPaySingleTransfer(AliPayTransferParam aliPayTransfer) {
try {
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
certAlipayRequest.SERVER_URL());
certAlipayRequest.APPID());
certAlipayRequest.PAY_PRIVATE_KEY());
certAlipayRequest.FORMAT());
certAlipayRequest.CHARSET());
certAlipayRequest.SIGN_TYPE());
//获取各证书路径
AliPayConfig certPath = getCertPath();
/
/设置应⽤公钥证书路径
certAlipayRequest.APPLICATION_PUBLIC_KEY());
//设置⽀付宝公钥证书路径
certAlipayRequest.AliPay_PUBLIC_CERT());
//设置⽀付宝根证书路径
certAlipayRequest.ROOT_CERT_PATH());
//⽀付宝客户端
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
request.setBizContent("{" +
"\"out_biz_no\":\"" + OutBizNo() + "\"," +
"\"trans_amount\":" + TransAmount() + "," +
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +
"\"biz_scene\":\"DIRECT_TRANSFER\"," +
"\"order_title\":\"" + OrderTitle() + "\"," +
"\"payee_info\":{" +
"\"identity\":\"" + Identity() + "\"," +
"\"identity_type\":\"ALIPAY_LOGON_ID\"," +
"\"name\":\"" + Name() + "\"" +
" }," +
"\"remark\":\"" + Remark() + "\"" +
" }");
AlipayFundTransUniTransferResponse response = ificateExecute(request);
//请求结束的返回参数
Map<String, Object> map = new HashMap<>();
map.put("code", Code());
map.put("status", Status());
map.put("msg", Msg());
map.put("outBizNo", OutBizNo());
map.put("msg", Msg());
map.put("outBizNo", OutBizNo());
map.put("orderId", OrderId());
map.put("payFundOrderId", PayFundOrderId());
map.put("payTime", TransDate());
if (response.isSuccess()) {
map.put("des", "转账成功");
} else {
map.put("des", "转账失败");
map.put("subCode", SubCode());
map.put("subMsg", SubMsg());
log.info("提现失败-->{}", SubMsg());
}
return map;
} catch (Exception e) {
<("转账异常-->{}", e.getMessage());
e.printStackTrace();
}
throw new CustomException("系统繁忙,请稍后重试");
}
⽅式三
这是旧接⼝,⽀付宝官⽅已不再维护
ansfer
public Map<String, Object> aliPayTransfer(AliPayTransferParam aliPayTransfer) {
try {
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
certAlipayRequest.SERVER_URL());
certAlipayRequest.APPID());
certAlipayRequest.PAY_PRIVATE_KEY());
certAlipayRequest.FORMAT());
certAlipayRequest.CHARSET());
certAlipayRequest.SIGN_TYPE());
try {
//获取各证书路径
AliPayConfig certPath = getCertPath();
//设置应⽤公钥证书路径
certAlipayRequest.APPLICATION_PUBLIC_KEY());
/
/设置⽀付宝公钥证书路径
certAlipayRequest.AliPay_PUBLIC_CERT());
//设置⽀付宝根证书路径
certAlipayRequest.ROOT_CERT_PATH());
} catch (Exception e) {
e.printStackTrace();
}
//构造Client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
//实例化接⼝
AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest (); AliPay alipay = new AliPay();
PayeeInfo payerInfo = new PayeeInfo();
payerInfo.Identity());
payerInfo.setIdentity_type("ALIPAY_LOGON_ID");
payerInfo.Name());
alipay.setPayee_info(payerInfo);
alipay.setPayee_type("ALIPAY_LOGONID");
alipay.setOut_biz_OutBizNo());
alipay.TransAmount());
alipay.setProduct_code("TRANS_ACCOUNT_NO_PWD");
alipay.setBiz_scene("DIRECT_TRANSFER");
alipay.setOrder_title("提现成功");
alipay.setRemark("谢谢您的理解");
//转成json格式放⼊
String json = new Gson().toJson(alipay);
request.setBizContent(json);
AlipayFundTransToaccountTransferResponse response = null;
Map<String, Object> map = new HashMap<String, Object>();
try {
response = ificateExecute(request);
if ("10000".Code())) {
map.put("code", Code());
map.put("success", "true");
return map;
return map;
} else {
map.put("code", Code());
map.put("des", "转账成功");
map.put("sub_code", SubCode());
map.put("success", "false");
map.put("sub_msg", SubMsg());
return map;
}
} catch (AlipayApiException e) {
e.printStackTrace();
map.put("success", "false");
map.put("des", "转账失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
查询⽀付宝转账订单
public Map<String, Object> queryAliPayTransfer(AliPayTransferParam aliPayTransfer) {
try {
//构造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//设置⽹关地址
certAlipayRequest.SERVER_URL());
//设置应⽤Id
certAlipayRequest.APPID());
//设置应⽤私钥
certAlipayRequest.PAY_PRIVATE_KEY());
//设置请求格式,固定值json
certAlipayRequest.FORMAT());
/
/设置字符集
certAlipayRequest.CHARSET());
//设置签名类型
certAlipayRequest.SIGN_TYPE());
//获取各证书路径
AliPayConfig certPath = getCertPath();
//设置应⽤公钥证书路径
certAlipayRequest.APPLICATION_PUBLIC_KEY());
//设置⽀付宝公钥证书路径
certAlipayRequest.AliPay_PUBLIC_CERT());
//设置⽀付宝根证书路径
certAlipayRequest.ROOT_CERT_PATH());
//构造client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest(); request.setBizContent("{" +
"\"out_biz_no\":\"" + OutBizNo() + "\"," +
"\"order_id\":\"" + OrderId() + "\"" +
"}");
AlipayFundTransOrderQueryResponse response = ificateExecute(request); Map<String, Object> map = new HashMap<>();
if (response.isSuccess()) {
map.put("code", Code());
map.put("status", Status());
map.put("msg", Msg());
map.put("outBizNo", OutBizNo());
map.put("orderId", OrderId());
map.put("payTime", PayDate());
} else {
System.out.println("调⽤失败");
map.put("subCode", SubCode());
map.put("subMsg", SubMsg());
<(Msg());
}
return map;
} catch (Exception e) {
e.printStackTrace();
}
e.printStackTrace();
}
throw new CustomException("系统繁忙,请稍后重试");
如何申请支付宝账户}
附加类
转账参数类:AliPay
/**
* 商户单号必选
*/
private String out_biz_no;
/**
* 转账⾦额必选
*/
private String trans_amount;
/**
* 业务产品码,单笔⽆密转账到⽀付宝账户固定为: TRANS_ACCOUNT_NO_PWD;
* 收发现⾦红包固定为: STD_RED_PACKET
* 必选
*/
private String product_code;
/**
* 业务备注可选
*/
private String remark;
/**
* 转账标题可选
*/
private String order_title;
/**
* 描述特定的业务场景,可传的参数如下:
* DIRECT_TRANSFER:单笔⽆密转账到⽀付宝,B2C现⾦红包;
* PERSONAL_COLLECTION:C2C现⾦红包-领红包
* 可选
*/
private String biz_scene;
/**
* 收款信息必选
*/
private PayeeInfo payee_info;
这⾥需要注意的是:为了预防出错,这⾥的参数命名跟按照官⽅⽂档的命名⼀致
收款⼈信息类: PayeeInfo
/**
* ⼿机号码或者邮箱或者PID 参与⽅的唯⼀标识
* pid是合作伙伴⾝份,是⽀付宝账号对应的唯⼀⽤户号ID
*/
private String identity;
/**
* ALIPAY_USER_ID:⽀付宝的会员ID, PID 例如:2088123412341234
* ALIPAY_LOGON_ID: ⽀付宝登录号, ⽀持邮箱和⼿机号格式
*/
private String identity_type;
/
**
* 参与⽅真实姓名,如果⾮空,将校验收款⽀付宝账号姓名⼀致性。当identity_type=ALIPAY_LOGON_ID时,本字段必填。 */
private String name;
⽀付宝转账配置类:AliPayConfig
package fig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
发布评论