|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require_once "aop/AopCertClient.php";
|
|
|
|
|
require_once "aop/request/AlipayFundTransUniTransferRequest.php";
|
|
|
|
|
require_once "aop/request/AlipayFundTransOrderQueryRequest.php";
|
|
|
|
|
require_once "aop/request/AlipayFundAccountQueryRequest.php";
|
|
|
|
|
|
|
|
|
|
class Fund {
|
|
|
|
|
public $appCertPath = __DIR__ . "/cert/appCertPublicKey_2021001150621489.crt";
|
|
|
|
|
public $alipayCertPath = __DIR__ . "/cert/alipayCertPublicKey_RSA2.crt";
|
|
|
|
|
public $rootCertPath = __DIR__ . "/cert/alipayRootCert.crt";
|
|
|
|
|
public $gatewayUrl = "https://openapi.alipay.com/gateway.do";
|
|
|
|
|
public $appId = "";
|
|
|
|
|
public $alipayUserId = ''; // pid
|
|
|
|
|
public $rsaPrivateKey = '';
|
|
|
|
|
public $is_init = false;
|
|
|
|
|
public function __construct($config)
|
|
|
|
|
{
|
|
|
|
|
if(empty($config)){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->appId = $config['appId'];
|
|
|
|
|
$this->alipayUserId = $config['alipayUserId'];
|
|
|
|
|
$this->rsaPrivateKey = $config['rsaPrivateKey'];
|
|
|
|
|
$this->is_init = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function transfer($aliAccount, $realName, $orderId, $amount, $title='') {
|
|
|
|
|
|
|
|
|
|
$aop = new AopCertClient ();
|
|
|
|
|
//1、execute 使用
|
|
|
|
|
$appCertPath = $this->appCertPath;
|
|
|
|
|
$alipayCertPath = $this->alipayCertPath;
|
|
|
|
|
$rootCertPath = $this->rootCertPath;
|
|
|
|
|
|
|
|
|
|
$aop->gatewayUrl = $this->gatewayUrl;
|
|
|
|
|
$aop->appId = $this->appId;
|
|
|
|
|
$aop->rsaPrivateKey = $this->rsaPrivateKey;
|
|
|
|
|
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
|
|
|
|
|
$aop->apiVersion = '1.0';
|
|
|
|
|
$aop->signType = 'RSA2';
|
|
|
|
|
$aop->postCharset='utf-8';
|
|
|
|
|
$aop->format='json';
|
|
|
|
|
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
|
|
|
|
|
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
|
|
|
|
|
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
|
|
|
|
|
|
|
|
|
|
$request = new AlipayFundTransUniTransferRequest();
|
|
|
|
|
$arrData = [
|
|
|
|
|
'out_biz_no' => $orderId, // 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一。
|
|
|
|
|
// 单笔无密转账到支付宝账户固定
|
|
|
|
|
// TRANS_ACCOUNT_NO_PWD 产品取值范围[0.1,100000000]
|
|
|
|
|
// STD_RED_PACKET [0.01,100000000]
|
|
|
|
|
'trans_amount' => $amount, // 订单总金额,单位为元,精确到小数点后两位,
|
|
|
|
|
'product_code' => 'TRANS_ACCOUNT_NO_PWD',
|
|
|
|
|
|
|
|
|
|
'biz_scene' => 'DIRECT_TRANSFER', // B2C现金红包、单笔无密转账到支付宝/银行卡
|
|
|
|
|
'order_title' => $title, // 转账业务的标题,用于在支付宝用户的账单里显示
|
|
|
|
|
'payee_info' => [
|
|
|
|
|
'identity' => $aliAccount,// 参与方的唯一标识
|
|
|
|
|
'identity_type' => 'ALIPAY_LOGON_ID', // 支付宝登录号,支持邮箱和手机号格式
|
|
|
|
|
'name' => $realName, // 参与方真实姓名,如果非空,将校验收款支付宝账号姓名一致性。当identity_type=ALIPAY_LOGON_ID时,本字段必填。
|
|
|
|
|
],
|
|
|
|
|
'remark' => $title
|
|
|
|
|
];
|
|
|
|
|
$request->setBizContent(json_encode($arrData,JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$result = $aop->execute($request);
|
|
|
|
|
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
|
|
|
|
|
return $result->$responseNode;
|
|
|
|
|
// $resultCode = $result->$responseNode->code;
|
|
|
|
|
// if(!empty($resultCode)&&$resultCode == 10000){
|
|
|
|
|
// return 0;
|
|
|
|
|
// } else {
|
|
|
|
|
// return -1;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 失败返回-1
|
|
|
|
|
* 成功返回 order_id order_fee pay_date status ali_order_id
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function order($orderId) {
|
|
|
|
|
$aop = new AopCertClient ();
|
|
|
|
|
//1、execute 使用
|
|
|
|
|
$appCertPath = $this->appCertPath;
|
|
|
|
|
$alipayCertPath = $this->alipayCertPath;
|
|
|
|
|
$rootCertPath = $this->rootCertPath;
|
|
|
|
|
|
|
|
|
|
$aop->gatewayUrl = $this->gatewayUrl;
|
|
|
|
|
$aop->appId = $this->appId;
|
|
|
|
|
$aop->rsaPrivateKey = $this->rsaPrivateKey;
|
|
|
|
|
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
|
|
|
|
|
// echo ($aop->alipayrsaPublicKey);die;
|
|
|
|
|
$aop->apiVersion = '1.0';
|
|
|
|
|
$aop->signType = 'RSA2';
|
|
|
|
|
$aop->postCharset='utf-8';
|
|
|
|
|
$aop->format='json';
|
|
|
|
|
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
|
|
|
|
|
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
|
|
|
|
|
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
|
|
|
|
|
// alipay.fund.trans.order.query
|
|
|
|
|
Vendor('Alipay2020.Aop.Request.AlipayFundTransOrderQueryRequest');
|
|
|
|
|
$request = new AlipayFundTransOrderQueryRequest();
|
|
|
|
|
$arrData = [
|
|
|
|
|
'out_biz_no' => $orderId, // 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一。
|
|
|
|
|
'order_id' => ''
|
|
|
|
|
];
|
|
|
|
|
$request->setBizContent(json_encode($arrData,JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$result = $aop->execute($request);
|
|
|
|
|
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
|
|
|
|
|
$resultCode = $result->$responseNode->code;
|
|
|
|
|
if(!empty($resultCode)&&$resultCode == 10000){
|
|
|
|
|
$ret['order_id'] = $result->alipay_fund_trans_order_query_response->out_biz_no;
|
|
|
|
|
$ret['order_fee'] = $result->alipay_fund_trans_order_query_response->order_fee;
|
|
|
|
|
$ret['pay_date'] = $result->alipay_fund_trans_order_query_response->pay_date;
|
|
|
|
|
$ret['status'] = $result->alipay_fund_trans_order_query_response->status;
|
|
|
|
|
$ret['ali_order_id'] = $result->alipay_fund_trans_order_query_response->order_id;
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function account() {
|
|
|
|
|
|
|
|
|
|
$aop = new AopCertClient();
|
|
|
|
|
//1、execute 使用
|
|
|
|
|
$appCertPath = $this->appCertPath;
|
|
|
|
|
$alipayCertPath = $this->alipayCertPath;
|
|
|
|
|
$rootCertPath = $this->rootCertPath;
|
|
|
|
|
|
|
|
|
|
$aop->gatewayUrl = $this->gatewayUrl;
|
|
|
|
|
$aop->appId = $this->appId;
|
|
|
|
|
$aop->rsaPrivateKey = $this->rsaPrivateKey;
|
|
|
|
|
$aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥
|
|
|
|
|
$aop->apiVersion = '1.0';
|
|
|
|
|
$aop->signType = 'RSA2';
|
|
|
|
|
$aop->postCharset='utf-8';
|
|
|
|
|
$aop->format='json';
|
|
|
|
|
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
|
|
|
|
|
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
|
|
|
|
|
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
|
|
|
|
|
$request = new AlipayFundAccountQueryRequest();
|
|
|
|
|
$arrData = [
|
|
|
|
|
'alipay_user_id' => $this->alipayUserId, // 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一。
|
|
|
|
|
// 'account_product_code' => 'DING_ACCOUNT',
|
|
|
|
|
// 'account_scene_code' => 'SCENE_000_000_000',
|
|
|
|
|
'account_type' => 'ACCTRANS_ACCOUNT'
|
|
|
|
|
];
|
|
|
|
|
$request->setBizContent(json_encode($arrData,JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$result = $aop->execute($request);
|
|
|
|
|
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
|
|
|
|
|
$resultCode = $result->$responseNode->code;
|
|
|
|
|
if(!empty($resultCode)&&$resultCode == 10000){
|
|
|
|
|
$ret['amount'] = $result->alipay_fund_account_query_response->available_amount;
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
?>
|