tp对接支付宝身份验证


鼎云博客


支付宝sdk包,已经导入了需要用到的类,只需要改下公钥和秘钥就可以用了:

alipay-sdk.zip


直接上代码:


index.html:


<body style="background-color:#fafafa">

    <div class="write">

        <input type="text" placeholder="请输入真实姓名" id="real_name" value="">

        <input type="text" placeholder="请输入身份证号码" id="cer_no" value="">

    </div>

    <div class="write_btn">下一步</div>

    <p class="zj_buy_agreement1">

        <a data-id="1" class="zj_buy_activeYes1"></a>&nbsp;我已阅读并同意<a href="/index.php?g=portal&m=page&a=index&id=10" class="ag1">《飞咻直播入驻协议》</a>

    </p>

</body>

<script src="__PUBLIC__/appapi/authentication/js/jquery.js"></script>

<script src="__PUBLIC__/appapi/authentication/js/layer.js"></script>

<script src="__PUBLIC__/appapi/authentication/js/auth.js"></script>

<script>

    var uid = '{$uid}';

    var token = '{$token}';

</script>


auth.js提交的部分

$(".write_btn").click(function() {

    layer.msg('请稍后..');

    $.ajax({

        url: "./index.php?g=Appapi&m=Authentication&a=authsaveali",

        dataType: "json",

        data: {

            uid: uid,

            token: token,

            real_name: $("#real_name").val(),

            cer_no: $("#cer_no").val(),

        },

        type: "POST",

        success: function(data) {

            if (data.ret == 200) {

                url=encodeURIComponent(data.data.url);

                window.location.href = 'alipays://platformapi/startapp?appId=20000067&url='+url;

                setTimeout(function(){

                    Verification(data.data.certify_id);

                },2000);


            } else {

                layer.msg(data.msg);

            }

        },

        error: function(e) {

            layer.msg(e.msg);

        }

    })

})



function Verification(certify_id) {

    layer.confirm('是否确认已完成支付宝认证?', {

        title: '提示',

        btn: ['取消', '确认'] //按钮

    }, function (index) {

        layer.close(index);

    }, function () {

        $.ajax({

            url: '/index.php?g=appapi&m=Authentication&a=getaliquery',

            data: {

                uid: uid,

                token: token,

                certify_id: certify_id

            },

            type: 'POST',

            dataType: 'json',

            success: function (data) {

                if(data.ret == 200) {

                    layer.msg(data.msg);

                    setTimeout(function () {

                        //window.location.href=document.referrer;

                        goMine();

                    }, 1500);

                }else{

                    layer.msg(data.msg);

                    return false;

                }

            },

            error: function () {

                layer.msg('网络请求失败');

                return !1;

            }

    });

})

}



AlipaytransController.php 业务处理层

<?php

/**

 * 支付宝应用服务

 */

namespace Appapi\Controller;


use Common\Controller\HomebaseController;


Vendor('Alipays.Core');

class AlipaytransController extends HomebaseController

{

    protected $gatewayUrl;

    protected $appId;

    protected $rsaPrivateKey;

    protected $appCertPath;

    protected $alipayCertPath;

    protected $rootCertPath;

    protected $apiVersion;

    protected $signType;

    protected $postCharset;

    protected $format;

    protected $outer_order_no;


    public function _initialize(){

        parent::_initialize();


        //支付接口正式环境地址

        $this->gatewayUrl = 'https://openapi.alipay.com/gateway.do';

        //应用ID

        $this->appId = '20190702******';

        //密钥生成工具生成的应用私钥

        $this->rsaPrivateKey = '******************';

        //应用公钥证书

        $this->appCertPath = SITE_PATH . "simplewind/Core/Library/Vendor/Alipays/crt/appCertPublicKey_2019070265751446.crt";

        //支付宝公钥证书路径

        $this->alipayCertPath = SITE_PATH . "simplewind/Core/Library/Vendor/Alipays/crt/alipayCertPublicKey_RSA2.crt";

        //支付宝根证书

        $this->rootCertPath = SITE_PATH . "simplewind/Core/Library/Vendor/Alipays/crt/alipayRootCert.crt";

        //api版本号

        $this->apiVersion = '1.0';

        //加密方式

        $this->signType = 'RSA2';

        //编码

        $this->postCharset = 'UTF-8';

        //返回格式

        $this->format = 'json';

        //订单号

        $this->outer_order_no = (int)(date("YmdHis",time()).rand(1000,9999));

    }


    /**

     * @Notes:

     * 初始化身份验证

     * @Interface getCertify_id

     * @Author: 133814250@qq.com MengShuai

     * @Date: 2019/12/20   14:42

     */

    public function getCertify_id($cert_name='',$cert_no='',$CallbackUrl='')

    {

        if(!$cert_name || !$cert_no)return false;

        $aop = new \AopCertClient ();


        $aop->gatewayUrl = $this->gatewayUrl;

        $aop->appId = $this->appId;

        $aop->rsaPrivateKey = $this->rsaPrivateKey;

        $aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayCertPath);

        $aop->appCertSN = $aop->getCertSN($this->appCertPath);

        $aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);

        $aop->apiVersion = $this->apiVersion;

        $aop->signType = $this->signType;

        $aop->postCharset = $this->postCharset;

        $aop->format = $this->format;



        $CallbackUrl='tencent101652996://';

        $request = new \AlipayUserCertifyOpenInitializeRequest ();

        $outer_order_no=$this->outer_order_no;

        $data="{" .

            "\"outer_order_no\":\"{$outer_order_no}\"," .

            "\"biz_code\":\"FACE\"," .

            "identity_param:{\"identity_type\":\"CERT_INFO\",

            cert_type:\"IDENTITY_CARD\",

            cert_name:\"{$cert_name}\",

            cert_no:\"{$cert_no}\"}," .

            "merchant_config:{\"return_url\":\"{$CallbackUrl}\"}  }";

        

        $request->setBizContent($data);


        $result = $aop->execute ($request);


        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        $resultCode = $result->$responseNode->code;

        $certify_id = $result->$responseNode->certify_id;

//        if(!empty($resultCode)&&$resultCode == 10000){

//            echo "成功";

//        } else {

//            echo "失败";

//        }


       return $certify_id ?: '';

    }



    /**

     * @Notes:

     * 开始认证 获取认证url

     * @Interface open

     * @Author: 133814250@qq.com MengShuai

     * @Date: 2019/12/20   14:43

     */

    public function open($cert_name='',$cert_no='',$CallbackUrl='https://www.baidu.com'){

        if(!$cert_name || !$cert_no)return false;

        $certify_id = $this->getCertify_id($cert_name,$cert_no,$CallbackUrl);


        $aop = new \AopCertClient () ?:'';

        $aop->gatewayUrl = $this->gatewayUrl;

        $aop->appId = $this->appId;

        $aop->rsaPrivateKey = $this->rsaPrivateKey;

        $aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayCertPath);

        $aop->appCertSN = $aop->getCertSN($this->appCertPath);

        $aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);

        $aop->apiVersion = $this->apiVersion;

        $aop->signType = $this->signType;

        $aop->postCharset = $this->postCharset;

        $aop->format = $this->format;


        $request = new \AlipayUserCertifyOpenCertifyRequest ();

        $request->setBizContent("{" .

            "\"certify_id\":\"{$certify_id}\"" .

            "  }");

        $result = $aop->pageExecute ( $request,"GET");


        $data=[

            'url' => $result ?:'',

            'certify_id' => $certify_id ?:'',

            'time' => $this->outer_order_no,

        ];

        return $data;

//        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

//        $resultCode = $result->$responseNode->code;

//        var_export($result);

//        if(!empty($resultCode)&&$resultCode == 10000){

//            echo "成功";

//        } else {

//            echo "失败";

//        }

    }


    /**

     * @Notes:

     * 查询认证结果

     * @Interface query

     * @Author: 133814250@qq.com MengShuai

     * @Date: 2019/12/20   16:06

     */

    public function query($cert_no='094619b9266aca9b7945387eafdd60e6'){

        $aop = new \AopCertClient () ?:'';

        $aop->gatewayUrl = $this->gatewayUrl;

        $aop->appId = $this->appId;

        $aop->rsaPrivateKey = $this->rsaPrivateKey;

        $aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayCertPath);

        $aop->appCertSN = $aop->getCertSN($this->appCertPath);

        $aop->alipayRootCertSN = $aop->getRootCertSN($this->rootCertPath);

        $aop->apiVersion = $this->apiVersion;

        $aop->signType = $this->signType;

        $aop->postCharset = $this->postCharset;

        $aop->format = $this->format;


        $request = new \AlipayUserCertifyOpenQueryRequest ();

        $request->setBizContent("{" .

            "\"certify_id\":\"{$cert_no}\"" .

            "  }");

        $result = $aop->execute ( $request);

//        var_export($result);


        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";

        $resultCode = $result->$responseNode->code;


        return $result->$responseNode;

//        var_export($result->$responseNode);

//        if(!empty($resultCode)&&$resultCode == 10000){

//            echo "成功";

//        } else {

//            echo "失败";

//        }

    }


 


鼎云博客
  • 最新评论
  • 总共0条评论