企业支付宝转账

17 篇文章 0 订阅

第一步,配置证书。

//    支付宝单笔转账
    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中   点击下载

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2019ab

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值