第一步,配置证书。
// 支付宝单笔转账 public function alipay($bank=null){ $pay_param = Db::name('StoreBankInfo')->where(['title'=>'支付宝'])->find(); $alipay_config['app_id'] = 2021001150668881; $alipay_config['public_key'] = ''; // 公钥 $alipay_config['private_key'] = ''; // 应用私钥 $alipay_config['appCertPath'] = ''; //应用公钥证书绝对路径 $alipay_config['alipayCertPath'] = ''; //支付宝公钥证书绝对路径 $alipay_config['rootCertPath'] = ""; // 支付宝根证书绝对路径 // 用户支付宝 $zfb = Db::name('StoreBankuser')->where(['uid'=>$bank['uid'],'type'=>1,'is_deleted'=>0]) ->where('bankusername','neq','')->where('zfbname','neq','')->find(); if(empty($zfb)){ return $this->error('支付宝不存在'); } $res = AliPayTrans([ 'order_sn' => $bank['logno'], // 订单号 'account' => $zfb['zfbname'], // 支付宝登录号,支持邮箱和手机号格式 'name' => $zfb['bankusername'], // 账号姓名 'money' => $bank['realmoney'] ], $alipay_config); return $res; }
第二步,调用接口,建议放在common.php中
/** * 支付宝转账 * @param $params * @param $config */ function AliPayTrans($params,$config) { $biz_content = array(); $biz_content['out_biz_no'] = trim($params['order_sn']); $biz_content['trans_amount'] = $params['money']; //单位为元 $biz_content['product_code'] = 'TRANS_ACCOUNT_NO_PWD' ; //trim($params['product_code']); $biz_content['biz_scene'] = 'DIRECT_TRANSFER'; $biz_content['order_title'] = '提现转账'; $biz_content['payee_info']['identity'] = $params['account']; //付宝登录号,支持邮箱和手机号格式 $biz_content['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID'; //付宝登录号,支持邮箱和手机号格式 $biz_content['payee_info']['name'] = $params['name']; //真实姓名 $biz_content['remark'] = '提现转账,注意查收'; $biz_content = array_filter($biz_content); $config['method'] = 'alipay.fund.trans.uni.transfer'; $config['biz_content'] = json_encode($biz_content,JSON_UNESCAPED_UNICODE); $result = AliPayCertClientTrans($config); $key = str_replace('.', '_', $config['method']) . '_response'; if (($result[$key]['code'] == '10000') && ($result[$key]['status'] == 'SUCCESS')) { return $result[$key]; } return var_dump($result[$key]['code'], $result[$key]['msg'] . ':' . $result[$key]['sub_msg']); } // 支付宝转账 function AliPayCertClientTrans($config) { ini_set('display_errors',1); error_reporting(E_ALL^E_NOTICE); require_once Env::get('app_path').'../vendor/alipaysdk/aop/AopCertClient.php'; require_once Env::get('app_path').'../vendor/alipaysdk/aop/AopCertification.php'; require_once Env::get('app_path').'../vendor/alipaysdk/aop/request/AlipayFundTransUniTransferRequest.php'; /** * 证书类型AopCertClient功能方法使用测试,特别注意支付宝根证书预计2037年会过期,请在适当时间下载更新支付更证书 * 1、execute 证书模式调用示例 * 2、sdkExecute 证书模式调用示例 * 3、pageExecute 证书模式调用示例 */ //1、execute 使用 $aop = new \AliPay\Aop\AopCertClient (); $appCertPath = $config['appCertPath']; //应用公钥证书绝对路径 $alipayCertPath = $config['alipayCertPath']; //支付宝公钥证书绝对路径 $rootCertPath = $config['rootCertPath']; // 支付宝根证书绝对路径 $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $config['app_id']; $aop->rsaPrivateKey = $config['private_key']; $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 \AliPay\Aop\Request\AlipayFundTransUniTransferRequest(); $request->setBizContent($config['biz_content']); $result = $aop->execute ( $request); $result = json_decode(json_encode($result),1); return $result; }
第三部,引入类库并测试
建议放到vendor中 点击下载