alipay提现自动转账接口对接笔记!
开放平台秘钥获取:如果后台选的是公钥形式就不要用公钥证书来去验证,会不匹配。反之亦然!
秘钥工具 :涉及转账用证书形式。下载支付宝开放平台开发助手
安装完成后,秘钥工具>>生成秘钥>>获取CSR文件>>点击获取(根据加密形式和开发语言选择不同方式)完善信息,生成应用公钥、应用私钥、CSR文件。然后在商家后台开放平台秘钥处上传CSR文件,然后生成
支付宝证书公钥alipayCertPublicKey_RSA2.crt
、应用证书公钥appCertPublicKey.crt
、支付宝根证书alipayRootCert.crt
下载备用。






签约单笔转账产品,设置秘钥提示有风险,要打
95188
申请。此时可以先用沙箱环境开发,流程没问题到后面直接更换秘钥证书等配置信息,就可达到快速开发目的。涉及转账线上不能用公私钥形式,要用证书形式对接。
开发指南与示例demo
alipay.fund.trans.uni.transfer(单笔转账接口)
alipay.fund.account.query(支付宝资金账户资产查询接口)
ALIPAY_USER_ID
:支付宝会员的用户 ID,可通过 获取会员信息 能力获取。
ALIPAY_LOGON_ID
:支付宝登录号,支持邮箱和手机号格式。
因ALIPAY_USER_ID
需要授权,就选了ALIPAY_LOGON_ID
方式转账。
// 沙箱配置
// public static $url = 'https://openapi.alipaydev.com/gateway.do';//支付宝网关地址
// public static $appId = '2021000119624754';//APPID
// public static $pid = '2088621957939429';//商户PID
// public static $privateKey = 'MIIEowIBxxxx....';//'请填写开发者私钥去头去尾去回车,一行字符串' ;
//正式环境
private static $url = 'https://openapi.alipay.com/gateway.do';//支付宝网关地址
private static $appId = '2021002179641xxx';//APPID
private static $pid = '2088631039593xxx';//商户PID
private static $privateKey = 'MIIEpQIBxxxx....';//'请填写开发者私钥去头去尾去回车,一行字符串' ;
公私钥形式,用官方文档demo改成证书形式。比如查询商家余额接口,把
AopClient
改成AopCertClient
验证。后面的AlipayFundAccountQueryRequest
的流程都一样。
public function query(){
require_once '../aop/AopClient.php';
require_once '../aop/AopCertClient.php';
require_once '../aop/AopCertification.php';
require_once '../aop/AlipayConfig.php';
require_once '../aop/request/AlipayFundAccountQueryRequest.php';
$privateKey = "<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->";
$alipayPublicKey = "<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->";
$alipayConfig = new AlipayConfig();
$alipayConfig->setServerUrl("https://openapi.alipaydev.com/gateway.do");
$alipayConfig->setAppId("<-- 请填写您的AppId,例如:2019091767145019 -->");
$alipayConfig->setPrivateKey($privateKey);
$alipayConfig->setFormat("json");
$alipayConfig->setAlipayPublicKey($alipayPublicKey);
$alipayConfig->setCharset("UTF8");
$alipayConfig->setSignType("RSA2");
$alipayClient = new AopClient($alipayConfig);
$request = new AlipayFundAccountQueryRequest();
$request->setBizContent("{".
"\"alipay_user_id\":\"2088301409188095\",".
"\"account_type\":\"ACCTRANS_ACCOUNT\"".
"}");
$responseResult = $alipayClient->execute($request);
$responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
if(!empty($response->code)&&$response->code==10000){
echo("调用成功");
}
else{
echo("调用失败");
}
}
public function transfer(){
require_once '../aop/AopClient.php';
require_once '../aop/AopCertClient.php';
require_once '../aop/AopCertification.php';
require_once '../aop/AlipayConfig.php';
require_once '../aop/request/AlipayFundTransUniTransferRequest.php';
$privateKey = "";
$alipayConfig = new AlipayConfig();
$alipayConfig->setPrivateKey($privateKey);
$alipayConfig->setServerUrl("https://openapi.alipaydev.com/gateway.do");
$alipayConfig->setAppId("APPID");
$alipayConfig->setCharset("UTF8");
$alipayConfig->setSignType("RSA2");
$alipayConfig->setEncryptKey("");
$alipayConfig->setFormat("json");
$alipayConfig->setAppCertContent("");
$alipayConfig->setAlipayPublicCertContent("");
$alipayConfig->setRootCertContent("");
$alipayClient = new AopCertClient($alipayConfig);
$alipayClient->isCheckAlipayPublicCert = true;
$request = new AlipayFundTransUniTransferRequest();
$request->setBizContent("{" .
"\"out_biz_no\":\"201806300001\"," .
"\"trans_amount\":\"23.00\"," .
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
"\"payee_info\":{" .
"\"identity\":\"2088123412341234\"," .
"\"identity_type\":\"ALIPAY_USER_ID\"," .
"\"bankcard_ext_info\":{" .
"\"inst_name\":\"招商银行\"," .
"\"account_type\":\"1\"" .
"}" .
"}" .
"}");
$responseResult = $alipayClient->execute($request);
$responseApiName = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$response = $responseResult->$responseApiName;
if (!empty($response->code) && $response->code == 10000) {
echo("调用成功");
} else {
echo("调用失败");
}
}
证书形式代码,下载扩展包
aop
放入tp6
下的extend
目录。如果开发场景,引入CSR-test
下的证书,流程通了,可以切回CSR
正式环境。
public function __construct(){
$extend_path = root_path( 'extend'.DIRECTORY_SEPARATOR.'alipay' . DIRECTORY_SEPARATOR);
require_once $extend_path.'aop/AopCertClient.php';
require_once $extend_path.'aop/request/AlipayFundAccountQueryRequest.php';
require_once $extend_path.'aop/request/AlipayFundTransUniTransferRequest.php';
require_once $extend_path.'aop/request/AlipayUserInfoShareRequest.php';
require_once $extend_path.'aop/request/AlipaySystemOauthTokenRequest.php';
$this->c = new \AopCertClient;
$appCertPath = $extend_path."CSR/appCertPublicKey.crt";//"应用证书路径(要确保证书文件可读),例如:/home/admin/cert/appCertPublicKey.crt";
$alipayCertPath = $extend_path."CSR/alipayCertPublicKey_RSA2.crt";//"支付宝公钥证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayCertPublicKey_RSA2.crt";
$rootCertPath = $extend_path."CSR/alipayRootCert.crt";//"支付宝根证书路径(要确保证书文件可读),例如:/home/admin/cert/alipayRootCert.crt";
$this->c->gatewayUrl = self::$url;
$this->c->appId = self::$appId;
$this->c->rsaPrivateKey = self::$privateKey;
$this->c->format = "json";
$this->c->charset= "UTF-8";
$this->c->signType= "RSA2";
$this->c->alipayrsaPublicKey = $this->c->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
$this->c->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$this->c->appCertSN = $this->c->getCertSN($appCertPath);//调用getCertSN获取证书序列号
$this->c->alipayRootCertSN = $this->c->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
}
/**
* 可查询请求方的支付宝账户余额信息。
* 支付宝账户余额查询,适用于单笔转账到支付宝和单笔转账到银行卡场景
* alipay.fund.account.query(支付宝资金账户资产查询接口)
*/
public function account(){
$request = new \AlipayFundAccountQueryRequest();//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.open.public.template.message.industry.modify
//SDK已经封装掉了公共参数,这里只需要传入业务参数
//此次只是参数展示,未进行字符串转义,实际情况下请转义
$request->setBizContent(json_encode([
'alipay_user_id'=>self::$pid,
'account_type'=>'ACCTRANS_ACCOUNT',
]));
$responseResult = $this->c->execute($request);
$responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
if(!empty($response->code)&&$response->code==10000){
$result = ['code'=>1,'msg'=>'查询请求方的支付宝账户余额信息成功!','data'=>['available_amount'=>$response->available_amount,'freeze_amount'=>$response->freeze_amount]];
}
else{
$result = ['code'=>0,'msg'=>'查询请求方的支付宝账户余额信息失败!','data'=>[]];
}
return $result;
}
/**
* 单笔转账到支付宝账户产品的参数说明
* alipay.fund.trans.uni.transfer(单笔转账接口)
*/
public function transfer($data = ['cash_money'=>0,'truename'=>'test','alipay'=>'test@qq.com']){
$account = $this->account();//获取商家余额够不够转账
if(!$account['code']) //获取商家余额出错
return $account;
if((bccomp($data['cash_money'], $account['data']['available_amount'], 2) == 1))
return [
'code'=>0,
'msg'=>"商家余额不足!",
'data'=>[
'cash_money'=>$data['cash_money'],
'available_amount'=>$account['data']['available_amount'],
]
];
$request = new \AlipayFundTransUniTransferRequest();
$request->setBizContent(json_encode([
'out_biz_no'=>'HLWY-'.uniqid(),//订单号
'trans_amount'=>$data['cash_money'],//付款金额
'product_code'=>'TRANS_ACCOUNT_NO_PWD',
'biz_scene'=>'DIRECT_TRANSFER',
'payee_info'=>[
'name'=>$data['truename'], //支付宝实名
'identity'=>$data['alipay'],//支付宝账号
'identity_type'=>'ALIPAY_LOGON_ID',
],
'business_params'=>json_encode(['payer_show_name_use_alias'=>'xxxx企业管理有限公司']),
'remark'=>'业务备注:xxxx!',
]));
$responseResult = $this->c->execute($request);
$responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
if(!empty($response->code)&&$response->code==10000){
return [
'code'=>1,
'msg'=>"商家打款成功!",
'data'=>[
'cash_money'=>$data['cash_money'],
'available_amount'=>$account['data']['available_amount'],
]
];
}
else{
return [
'code'=>0,
'msg'=>"商家打款失败!",
'data'=>[
'cash_money'=>$data['cash_money'],
'available_amount'=>$account['data']['available_amount'],
]
];
}
}