下面是支付宝文档,跟着文档做好开发前的准备:接入准备 | 网页&移动应用
SDK我下载的是PHP通用版,使用支付宝沙箱环境进行调试:沙箱调试 | 网页&移动应用
业务规则:
下面是完整代码:
/**
* @notes 公共请求参数
*/
public function config()
{
return [
'app_id' => '',//APPID
'charset' => 'UTF-8',//请求和签名使用的字符编码格式,支持 GBK和 UTF-8
'sign_type' => 'RSA2',//商户生成签名字符串所使用的签名算法类型,目前支持 RSA2 和 RSA,推荐商家使用 RSA2
'server_url' => 'https://openapi.alipaydev.com/gateway.do',//支付宝网关(固定)https://openapi.alipay.com/gateway.do(正式环境)
https://openapi.alipaydev.com/gateway.do(沙箱环境)
'private_key' => "",//应用私钥
'app_cert_path' => '',//应用公钥证书路径
'alipay_public_cert_path' => '',//支付宝公钥证书文件路径
'alipay_root_cert_path' => '',//支付宝CA根证书文件路径
];
}
/**
* @notes 转账到支付宝账号
*/
public function transfer($withdraw)
{
//公共请求参数
$config = $this->config();
//请求参数
$data = [
'out_biz_no' => $withdraw['sn'],//商家侧唯一订单号,由商家自定义。对于不同转账请求,商家需保证该订单号在自身系统唯一。
'trans_amount' => $withdraw['left_money'],//订单总金额,单位为元,不支持千位分隔符,精确到小数点后两位
'product_code' => 'TRANS_ACCOUNT_NO_PWD',//销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD。
'biz_scene' => 'DIRECT_TRANSFER',//业务场景。单笔无密转账固定为 DIRECT_TRANSFER。
'order_title' => '',//转账业务的标题
'payee_info' => [//收款方信息
'identity' => $withdraw['account'],//参与方的标识 ID。当 identity_type=ALIPAY_USER_ID 时,填写支付宝用户 UID;当 identity_type=ALIPAY_LOGON_ID 时,填写支付宝登录号。
'identity_type' => 'ALIPAY_LOGON_ID',//参与方的标识类型。ALIPAY_USER_ID:支付宝会员的用户 ID;ALIPAY_LOGON_ID:支付宝登录号;
'name' => $withdraw['real_name'],//参与方真实姓名。如果非空,将校验收款支付宝账号姓名一致性。当 identity_type=ALIPAY_LOGON_ID 时,本字段必填。
],
'remark' => '',//业务备注
];
$alipayConfig = new AlipayConfig();
$alipayConfig->setPrivateKey($config['private_key']);
$alipayConfig->setServerUrl($config['server_url']);
$alipayConfig->setAppId($config['app_id']);
$alipayConfig->setCharset($config['charset']);
$alipayConfig->setSignType($config['sign_type']);
$alipayConfig->setAppCertPath($config['app_cert_path']);
$alipayConfig->setAlipayPublicCertPath($config['alipay_public_cert_path']);
$alipayConfig->setRootCertPath($config['alipay_root_cert_path']);
$alipayClient = new AopCertClient($alipayConfig);
$alipayClient->isCheckAlipayPublicCert = true;
$request = new AlipayFundTransUniTransferRequest();
$request->setBizContent(json_encode($data,JSON_UNESCAPED_UNICODE));
$responseResult = $alipayClient->execute($request);
$responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
if(isset($response->code) && $response->code==10000 && isset($response->status) && $response->status=='SUCCESS'){//转账成功
halt("调用成功");
} else {//转账失败
if (isset($response->sub_code) && $response->sub_code=='SYSTEM_ERROR') {//转账异常,不能直接作失败处理,需要调用转账业务单据查询接口查询转账结果
halt('转账异常');
}
halt("调用失败");
}
}
/**
* @notes 转账业务单据查询接口
*/
public function query($withdraw)
{
//公共请求参数
$config = $this->config();
//请求参数
$data = [
'out_biz_no' => $withdraw['sn'],//商户转账唯一订单号:发起转账来源方定义的转账单据 ID。
'product_code' => 'TRANS_ACCOUNT_NO_PWD',//销售产品码,如果传了 out_biz_no,则该字段必传。单笔无密转账固定为TRANS_ACCOUNT_NO_PWD。
'biz_scene' => 'DIRECT_TRANSFER',//描述特定的业务场景,如果传递了out_biz_no 则该字段为必传。单笔无密转账固定为DIRECT_TRANSFER。
];
$aopAlipayClient = new AopCertClient;
$aopAlipayClient->gatewayUrl = $config['server_url'];
$aopAlipayClient->appId = $config['app_id'];
$aopAlipayClient->rsaPrivateKey = $config['private_key'];
$aopAlipayClient->charset= $config['charset'];
$aopAlipayClient->signType= $config['sign_type'];
$aopAlipayClient->alipayrsaPublicKey = $aopAlipayClient->getPublicKey($config['alipay_public_cert_path']);//调用getPublicKey从支付宝公钥证书中提取公钥
$aopAlipayClient->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$aopAlipayClient->appCertSN = $aopAlipayClient->getCertSN($config['app_cert_path']);//调用getCertSN获取证书序列号
$aopAlipayClient->alipayRootCertSN = $aopAlipayClient->getRootCertSN($config['alipay_root_cert_path']);//调用getRootCertSN获取支付宝根证书序列号
$request = new AlipayFundTransCommonQueryRequest();
$request->setBizContent(json_encode($data,JSON_UNESCAPED_UNICODE));
$responseResult = $aopAlipayClient->execute($request);
$responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
if(!empty($response->code) && $response->code==10000){
if(isset($response->status) && $response->status=='SUCCESS'){//转账成功
halt("转账成功");
} elseif(isset($response->status) && $response->status=='FAIL') {//转账失败
halt("转账失败");
}
}
}