tp5对接支付宝支付简单集成

对于每个刚开始工作的新手来说,无论支付宝支付还是微信支付都是跑不掉的一个小门槛。

在加上本人比较技术比较渣(比较懒导致的),不太喜欢引用那么大的SDK,于是就简单集成了一下支付宝的支付。

但也只是只有支付而已哦,其他的一些功能还是自己用自己在集成吧,嘻嘻(#^.^#)还是懒。

废话不多说了,直接上代码:

/**
 * 支付宝支付
 */
class  Alipay
{
    private $config =[ 
        "app_id"        => "************",   //支付宝APPid
        "rsaPrivateKey" => "************",   //支付宝私钥
        "rsaPublicKey"  => "************",   //支付宝公钥
        "charset"       => "utf-8",
        "sign_type"     => "RSA2",
        "version"       => "1.0",
    ];
    private $submitUrl = "https://openapi.alipay.com/gateway.do" ;//提交地址
    public $calltype = "app";//pc电脑端 app 手机和app端公用
    public $submit_auto=true;
    
    public function index($data)
    {
        $calltype=$this->calltype;
        return $this->$calltype($data);
    } 
    public function app($data)
    {
        $return_url=$data['return_url'];
        $notify_url=$data['notify_url'];
        $subject=$data['subject'];
        $order_sn=$data['order_sn'];
        $total=$data['total'];

        $biz_content=[
            'body'=>'',
            'subject'=>$subject,
            'out_trade_no'=>$order_sn,
            'timeout_express'=>'30m',
            'total_amount'=>$total,
            'product_code'=>'QUICK_MSECURITY_PAY',
        ];
        $postdata=[
            'app_id'=>$this->config['app_id'],
            'method'=>'alipay.trade.app.pay',
            'format'=>'json',
            'charset'=>$this->config['charset'],
            'sign_type'=>$this->config['sign_type'],
            'sign'=>'',
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>'1.0',
            'notify_url'=>$notify_url,
            'biz_content'=>json_encode($biz_content),
        ];

        $signdata=$this->alisign($postdata);
        $str=$this->keyandvalStr($signdata,1);
        //返回链接不能带官方https链接
        return $str;
        
    }
    public function pc($data)
    {
        $return_url=$data['return_url'];
        $notify_url=$data['notify_url'];
        $biz_content=[
            "total_amount"=>$data['total'],
            "product_code"=>"FAST_INSTANT_TRADE_PAY",
            "subject"=>$data['subject'],
            "out_trade_no"=>$data['order_sn'],
            "goods_type"=>1,//商品类型 0虚拟商品 1实物商品
        ];

        $post=[
            'app_id'=>$this->config['app_id'],
            'method'=>'alipay.trade.page.pay',
            'format'=>'JSON',
            'return_url'=>$return_url,
            'charset'=>$this->config['charset'],
            'sign_type'=>$this->config['sign_type'],
            // 'sign'=>$sign,
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>$this->config['version'],
            'notify_url'=>$notify_url,
            'biz_content'=>json_encode($biz_content),
        ];

        $signdata=$this->alisign($post);

        $html=$this->formsubmithtml($this->submitUrl,$signdata);

        return $html;
    }
    public function mobile($data)
    {
        $return_url=$data['return_url'];
        $notify_url=$data['notify_url'];
        $biz_content=[
            "total_amount"=>$data['total'],
            "product_code"=>"QUICK_WAP_WAY",
            "subject"=>$data['subject'],
            "out_trade_no"=>$data['order_sn'],
            "goods_type"=>1,//商品类型 0虚拟商品 1实物商品
        ];

        $post=[
            'app_id'=>$this->config['app_id'],
            'method'=>'alipay.trade.wap.pay',
            'format'=>'JSON',
            'return_url'=>$return_url,
            'charset'=>$this->config['charset'],
            'sign_type'=>$this->config['sign_type'],
            // 'sign'=>$sign,
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>$this->config['version'],
            'notify_url'=>$notify_url,
            'biz_content'=>json_encode($biz_content),
        ];

        $signdata=$this->alisign($post);
        $str=$this->keyandvalStr($signdata,1);

        $url="https://openapi.alipay.com/gateway.do?".$str;

        return $url;
    }
    public function alisign($data)
    {
        $rsaPrivateKey=$this->config['rsaPrivateKey'];
        $buff = "";
        ksort($data);
        $buff=$this->keyandvalStr($data);
        $str  = chunk_split($rsaPrivateKey, 64, "\n");
        $res = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n";

        $sign=$this->ras2Sign($buff,$res);
        $data['sign']=$sign;
        return $data;
    }
    function keyandvalStr($data,$flg=0)
    {
        $buff="";
        foreach ($data as $k => $v) {
            if($flg!=0 && $v != "" && !is_array($v))
            {
                $v=urlencode($v);
                $buff .= $k . "=" . $v . "&";
            }else{
                if($k != "sign" && $v != "" && !is_array($v)){
                    $buff .= $k . "=" . $v . "&";
                }
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
    function ras2Sign($buff,$res)
    {
        $sign='';
        openssl_sign($buff, $sign, $res, OPENSSL_ALGO_SHA256);
        $sign = base64_encode($sign);
        return $sign;
    }
    function formsubmithtml($action,$data)
    {
        $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$action."' method='POST'>";
        foreach ($data as $key => $value) {
            $val = str_replace("'","&apos;",$value);
            $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";     
        }
        $sHtml = $sHtml."<input type='submit' class='paysubmit' value='ok' ></form>";
        if($this->submit_auto==true)
        {
            $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
        }
        
        return $sHtml;
    }

}

调用方法:

use pay\Alipay;

    $callD=[
        'return_url' => '支付完成跳转路径',
        'notify_url' => '回调路径',
        'total'      => '支付总额',
        'subject'    => '支付说明',
        'order_sn'   => '生成订单号',
    ];
    $url=$this->Alipay($callD);


    public function Alipay($data,$flg=true)
    {
        $pay = new Alipay();
        $pay->calltype='app';
        $pay->submit_auto=$flg;
        $pay=$pay->index($data);
        return $pay;
    }

以上即是本次分享的内容。

感谢您的观看。

2019年05月10日

posted @ 2019-05-10 16:59  御风琊穹  阅读(1844)  评论(0编辑  收藏  举报

春风十里,我喜欢你。可是你喜欢的人不喜欢你,哈哈....
人来人往,莫失莫忘。最终还是选择淡忘于回忆之中...
落日余晖,待你而归。但你终究不是为我而归..
一米阳光,温暖安放;心若向阳,无谓悲伤;轻安喜乐,次第花开 。