前言
注意
- <?php
- header(‘Content-type:text/html; Charset=utf-8’);
- // sdk 单笔转账接口 https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer
- Class AliTransferPay
- {
- public $appId;
- public $privateKey;
- public $charset ;
- public $sign_type ;
- public function __construct($appId, $privateKey)
- {
- $this->appId = $appId;
- $this->privateKey = $privateKey;
- $this->charset=‘utf-8’;
- $this->sign_type=‘RSA2’;
- }
- /**
- * 查询余额,需要签约改功能,如果不能签约的话 会提示 ISV权限不足,建议在开发者中心检查签约是否已经生效
- * home.php?mod=space&uid=155549 mixed
- */
- public function queryBalance()
- {
- $reqParams = [
- ‘app_id’ => $this->appId,
- ‘method’ => ‘alipay.data.bill.balance.query’,
- ‘format’ => ‘JSON’,
- ‘charset’ => $this->charset,
- ‘sign_type’ => $this->sign_type,
- ‘timestamp’ => date(‘Y-m-d H:i:s’),
- ‘version’ => ‘1.0’,
- ];
- //格式化编码
- $bizParmsStr = $this->getSignContent($reqParams);
- //计算签名
- $sign = $this->getSign($bizParmsStr, $this->sign_type);
- $reqParams[‘sign’] = $sign;
- //发送请求
- $result = $this->curlPost(‘https://openapi.alipay.com/gateway.do?charset=’.$this->charset, $reqParams);
- return json_decode($result,true);
- }
- /**
- * 转账方法
- * home.php?mod=space&uid=952169 $order_id string 订单号
- * @param $price string 价格
- * @param $account string 收款方账户账号
- * @param $payee_real_name string 收款方账户姓名 可以不传
- * @param string $remark string 支付备注
- * @param string $payer_show_name 转账企业支付时显示的名称
- * @return mixed
- */
- public function pay($order_id, $price, $account, $payee_real_name, $remark = ‘测试’, $payer_show_name = ‘测试’)
- {
- $bizParms = [
- ‘out_biz_no’ => $order_id,
- ‘payee_type’ => ‘ALIPAY_LOGONID’,//固定值,老接口
- ‘payee_account’ => $account,
- ‘payee_real_name’ => $payee_real_name, //收款方姓名(选填)
- ‘amount’ => $price, //转账金额
- ‘remark’ => $remark, //转账备注(选填)
- ‘payer_show_name’ => $payer_show_name, //支付方名称,比如我不想显示公司名称太长了,可以自定义这个名称
- ];
- $reqParams = [
- ‘app_id’ => $this->appId,
- ‘method’ => ‘alipay.fund.trans.toaccount.transfer’,
- ‘format’ => ‘JSON’,
- ‘charset’ => $this->charset,
- ‘sign_type’ => $this->sign_type,
- ‘timestamp’ => date(‘Y-m-d H:i:s’),
- ‘version’ => ‘1.0’,
- ‘biz_content’ => json_encode($bizParms),
- ];
- //格式化编码
- $bizParmsStr = $this->getSignContent($reqParams);
- //计算签名
- $sign = $this->getSign($bizParmsStr, $this->sign_type);
- $reqParams[‘sign’] = $sign;
- //发送请求
- $result = $this->curlPost(‘https://openapi.alipay.com/gateway.do?charset=’.$this->charset, $reqParams);
- return json_decode($result, true);
- }
- /**
- * 根据加密类型,进行参数加密
- * @param $params string
- * @param string $sign_type 该函数只支持 RSA 和 RSA2两种方式
- * @return string
- */
- public function getSign($params, $sign_type = “RSA”)
- {
- $privateKey = $this->privateKey;
- $formatPrivateKey = “—–BEGIN RSA PRIVATE KEY—–\n” .
- wordwrap($privateKey, 64, “\n”, true) .
- “\n—–END RSA PRIVATE KEY—–“;
- ($formatPrivateKey) or die(‘您使用的私钥格式错误,请检查RSA私钥配置’);
- if ($sign_type == ‘RSA2’) {
- openssl_sign($params, $sign, $formatPrivateKey, version_compare(PHP_VERSION, ‘5.4.0’, ‘<‘) ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
- } else {
- openssl_sign($params, $sign, $formatPrivateKey);
- }
- return base64_encode($sign);
- }
- /**
- * 获取待签名的字符串
- * @param $params
- * @return string
- */
- public function getSignContent($params)
- {
- ksort($params);
- $stringToBeSigned = “”;
- $i = 0;
- foreach ($params as $k => $v) {
- if (false === $this->checkEmpty($v) && “@” != substr($v, 0, 1)) {
- // 转换成目标字符集
- $v = $this->transCoding($v, $this->charset);
- if ($i == 0) {
- $stringToBeSigned .= “$k” . “=” . “$v”;
- } else {
- $stringToBeSigned .= “&” . “$k” . “=” . “$v”;
- }
- $i++;
- }
- }
- unset ($k, $v);
- return $stringToBeSigned;
- }
- /**
- * 转换字符集编码
- * @param $data
- * @param $targetCharset
- * @return string
- */
- function transCoding($data, $targetCharset)
- {
- if (!empty($data)) {
- $fileType = $this->charset;
- if (strcasecmp($fileType, $targetCharset) != 0) {
- $data = mb_convert_encoding($data, $targetCharset, $fileType);
- }
- }
- return $data;
- }
- /**
- * 校验参数是否为空
- * @param $value
- * @return bool
- */
- protected function checkEmpty($value)
- {
- return empty($value) == true;
- }
- /**
- * curl请求,注意https如果没有证书,需要设置不验证证书和host
- * @param string $url
- * @param string $postData
- * @param array $options
- * @param bool $isssl
- * @return mixed
- */
- public function curlPost($url = ”, $postData = ”, $options = [], $isssl = true)
- {
- if (is_array($postData)) {
- $postData = http_build_query($postData);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- if (!empty($options)) {
- curl_setopt_array($ch, $options);
- }
- //https请求 不验证证书和host
- if ($isssl) {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
- }
- //转账
- function invoke()
- {
- //调用测试
- $appid = ”;
- $order_id = uniqid();
- $price = 0.1;
- $saPrivateKey=”;
- $account = ”;//收款方账号
- $payee_real_name = ”;//收款方真实姓名
- $aliPay = new AliTransferPay($appid, $saPrivateKey);
- $result = $aliPay->pay($order_id, $price, $account, $payee_real_name);
- $result = $result[‘alipay_fund_trans_toaccount_transfer_response’];
- if ($result[‘code’] && $result[‘code’] == ‘10000’) {
- echo ‘转账成功’;
- } else {
- echo $result[‘msg’] . ‘ : ‘ . $result[‘sub_msg’];
- }
- }
- invoke();
打赏
- 支付宝扫一扫
- 微信扫一扫