码迷,mamicode.com
首页 > 移动开发 > 详细

php 做支付宝电脑网站和app支付

时间:2017-10-19 14:03:48      阅读:1794      评论:0      收藏:0      [点我收藏+]

标签:class   技术   enc   uri   eal   url   ppi   通知   color   

首先支付宝的文档真是看得头晕眼花,摸不着北的感觉,网上百度PHP支付宝一堆都是花里胡哨的,看得一脸懵逼。所以自己整理了一下,其实也没那么复杂直接上代码:

 

php 电脑支付的方法:

    /**
     * 支付宝支付
     *$order_sn 单号  $total_amount 价格
     */
    public function alipay($order_sn,$total_amount)
    {
        require_once INCLUDES_PATH.‘class‘.DS.‘Pay‘.DS.‘mobile‘.DS.‘alipaytest/‘.‘AopClient.php‘;
        $aop = new AopClient();//这个类支付宝php服务端 都要用到 (app支付服务端也要用到这个类)
        $aop->gatewayUrl = ‘https://openapi.alipay.com/gateway.do‘;//支付宝网关,也就是接口地址
        $aop->appId = ‘201709010........‘;//APPID
        $aop->rsaPrivateKey =‘‘";//商户的私钥()
        $aop->format = ‘json‘;
        $aop->apiVersion = ‘1.0‘;
        $aop->postCharset= ‘utf-8‘;
        $aop->signType = ‘RSA2‘;
      
        $request = new AlipayTradePagePayRequest();//支付宝电脑网站支付需要用到的类 这个类支付宝SDK可以下载
        $request->setNotifyUrl(HTTP_HOST.‘pay/alipay_notify.php‘);//支付结果的异步通知回调地址
        $request->setReturnUrl(HTTP_HOST.‘pay/alipay_return.php‘);//同步回调回调地址

        $bizcontent = json_encode(array(
            ‘subject‘=>‘商品订单‘.$order_sn,
            ‘out_trade_no‘=>$order_sn,
            ‘body‘=>‘商品订单‘,
            ‘total_amount‘=>sprintf(‘%.2f‘,$total_amount),
            ‘product_code‘=>‘FAST_INSTANT_TRADE_PAY‘,//支付类型,APP和电脑支付 手机支付是不一样的 所以这个类型也是不一样的
        ), JSON_UNESCAPED_UNICODE);



        $request->setBizContent($bizcontent);
        $test = $aop->pageExecute($request,‘post‘);//支付宝返回来的地址,直接是可以跳转的
        echo $test;


    }

 

附上所需要用到的类:

技术分享

 


 


只需要这三个类就好了,支付宝的sdk 满满的一堆 ,但是我们不需要那么多,需要用到类再把它抠出来就好了 好像传不了文件 有需要的可以加我QQ(1031486419)我发给你
 

app 支付 我们PHP 服务端的代码,其实就是生成一堆支付的参数 直接上代码了,其实和上面的电脑支付没什么区别 只不过是参数有所变动:
    /**
     * 生成支付宝app支付参数
     * @param string $title
     * @param string $order_sn
     * @param float $amount
     * @return string
     */
    public function alipay_app($title, $order_sn, $amount)
    {
        require_once BASE_DATA_PATH.‘/vendors/alipay/AopClient.php‘;
        $aop = new AopClient();
        $aop->gatewayUrl = ‘https://openapi.alipay.com/gateway.do‘;
        $aop->appId = ‘20170901。。。‘;
        //$aop->rsaPrivateKey = ‘MIIEpQIBAAKCAQEA0GbLnXksi81/qydbfwM+CJ0JInvHWcgElvIGtBmuDOgq+DHzlPQAGwF5GSEGFo3vMHYS9a8BM1lSP3hdnuodq42YeWjJJVX39SjDitv5jXLnkmtNCynTtPdAwjfu83Xf9kuQTEXFwMGpygSLpOIZ7OcujfJXuDPq7IXGHLfIPAxTo0KSk0BGFjGG1R+lJor8c974jI5QCWSKlvTSGjucHCbFPy6Sw0Bqnev8XGdt6z+NvqZJA7jNZFE3q6xXn84n9d3Jq6r/zu1dt1FN/9ffHeYKIl4p5IhwTh2quYoG3b1YRyNX9Ry33dKkjJNMHnA7dca84sUAkKZFrR1Kao0bwQIDAQABAoIBAQCbHNfSlp1EVSkAMCQWzkf+yztAUlbXiVt5Kh05MZ/tdWR+JVfCdzYY46jJvl20reHo9Qk5mg9Ps9zW5vTv0htRU+u3+fk0897Bt5yLnx0IXgzkWz8V3wT3Pc0SgeU1ineu7yEw1AHTbZG9jlKN38giHxP0jPsjXGkvsIX7XAMKRV6XdJ9H8bXNX4U0Wwc5TaBaAcSSWEBWZjJhAIPG/SFZW92k9ETFLAlXqLln+25/sXNX+c4pL6zkfa6olfsk17H5bVIDwApZMCwb4Sqe8OegU4wwhFuILx1iBTwvP9du6hGrbeZvmbAabI65MeBZp2nZ1OfCi4tG2DJusD1hbgadAoGBAOubK0YfHIgq+GDegkISPebXwBPGtVY0McBIrjD/yKbxIRDphxg6vQcjPT5oJFTA8B0d9KLfUhKC6+GadhmVuRjTssj/rlF1EVsjsZSgXrQIpMbdVvDsQbtfzgft9xXd3GBCc6+ZGGWWNyaZNRf6OBjx1lxcR44tkAX+Kl9fv4UzAoGBAOJwy8INLQ8GAhY5icGAoRE5bFPD/Zq/nCNC0zEi7OGUUKM8rb27z0zvSoXWTRv47j5r7EPVbYalB0srr3yb3yquL/W2mpDxwmnJZSbqzygLCoWRvjGywsvpcVqxhaDklJmLJsql9npy0+iwZJ/jgA2O5f467kBwMxdYvTVGHvM7AoGBAM+Gh+Fl+f2+y7SXiWilJL02gMtqlcNuifvyiOxEAPt7HUMGT6Z55pJo6cMUfBvjLhyXjCoCRwpjI14+bZQvmzBRGAOPon3Bfh4nHH3rnD879dfskm33zBY9Vv8JDkDGgRyQq9GaKjEg1Uc8X9XU4qCmM8h6x9RmMZv1umDzt4CdAoGAGy9nhviF/FPAVGK8AxsqOyMlujjBbZ6rZCBVsoR64+omdDKIuUqe7KGd1MuRnlYYZC4Yw1wQNiYB5ihsfQ1JkZov79PoYpZjvMeM+Usfa7NHpBmHN/g6Irj3/udpH5PRs01dbt7V5IV8o2bp7bgmW3pYalkw2VOfDqJ8tLSiapkCgYEAsKLLzFTpsW60uXWQrxAyelDxzTqoPvFpIcj1qxDnuifCn/22+dKfYw+PzSj8GnGn3p9gdB8jgsQYMekNz0jsta+rweX2ica4buAVRlL2tEYDBQ22lrhwVgUFRXcIRqctqXZ0umBEwnupiFcx73yO7nrcQ+jKuTng489VN7CseWE=‘;//请填写开发者私钥去头去尾去回车,一行字符串
        $aop->rsaPrivateKey = ‘MIIEpQIBAAKCAQEA7hz7/VE2MgVutwjKvN0xMJoTVVmOBOBBMCkXaAXbwrXnFvPOasDkn07ia+ZU2aovCVifaI77rLRTG+Clj7rUU6+zwgsyPa8bH5SQfCnEvvL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Al1iFrdczFJY83ZP137+3Pinq3ZX2xl5lIkA+xxjDBAoGBAMZ74vVYygT67asYRc0ykGPkMhmryNz29+7MXzYh31HXo2iX92nQjf5+tBMnnUAvhvqAQ0pfd9mYkGQXcQAS2/mAhByHun8LV5nTYpmfXr16aax3XCCefeq865DOM8OK2YGnfCDfdOfdQWST6Ub1tIrwArLGqKAMhco+aH/YT79hAoGAP4EZASN9/1LA6Ep0SVm+6e5zCy7FsGsc+ibdDEeYF6De2TLa3I5Q9mf+30bqz2CsoEWST0a2T9JcLsEltkkAQeuy62Q7B8JCeCvFWkSBe/zJDistI8KcMxYTHzhird9xbDL8vD60ntyjWeKIqJFAWSJblUtzUzicvZ56/p8AFr0=‘;
        $aop->format = ‘json‘;
        $aop->charset = ‘UTF-8‘;
        $aop->signType = ‘RSA2‘;
        //$aop->alipayrsaPublicKey = ‘MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAptDyV0pRbSn2A33coQOBhzNIA29hgllWsLxDuai8uDuKxQ4tDpkakqSAu7Ru4fRFrXupCXxLPYFLi9sj3E6q0TjpxvfvWDYbx1iVNAqQXTNwx06ibxSsljTKePRevaVfwFAYSxyGhw7zEbu9YBAQuPo7ER48dWPsVgvgmBu0DEM0ZmrzT5NKnrROiFAKOwruwmQPxEDmhP4fc5ql41sTD+MfPEQPOWi6oTQxys20JhHK7kCwiMaKraaDFJi8zPrBxfXaXV92N8AGzWBodsFLRTJfAscGjhncYYKuygDma7yJS0CEt0gqKz0CgIQvGT43KHdPpeNavAYz1BDs6jDnjwIDAQAB‘;//请填写支付宝公钥,一行字符串
        $aop->alipayrsaPublicKey   = ‘MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlyFPnRahghm70fLEzJ/q4OgEuzYVaoTYlLr2GekhOyrqbte1JMFED1quTkJ7yx+iOXx39jX9sGO+sCJ9ruKfRlJ7vItVhd7Z2a6uTcF1tHrDOwTw45TQXbgFAkPWZEzbWnvTkQ/uR9KXJg1h98WPCHwS2nvoJ53puBfureyFUjmZKdKJjhboUMKd51QeV59It6IAVM8fvxb+1QnZVM7WtdAG12sRS8dD5ZoElIJABTCFie/5pBdoBZymCNYCGP8sUpbUAxCnexetvpG5O7RPUz/Nc7EFz5O1L+qYUfv74rrIRLo7Nl+G6yLRR+LETXlxlb9VLNgKjsxXshzKyLzJCwIDAQAB‘;
        $request = new AlipayTradeAppPayRequest();
        $bizcontent = json_encode(array(
            ‘subject‘=>$title,
            ‘out_trade_no‘=>$order_sn,
            ‘timeout_express‘=>‘30m‘,
            ‘total_amount‘=>sprintf(‘%.2f‘,$amount),
            ‘product_code‘=>‘QUICK_MSECURITY_PAY‘,
        ), JSON_UNESCAPED_UNICODE);
        $request->setNotifyUrl(‘http://xxx/control/alipay_notify.php‘);
        $request->setBizContent($bizcontent);
        $response = $aop->sdkExecute($request);

        return $response;
    }
还是三个类 

技术分享

 

只不过这个类有所变动而已

技术分享

 

再者多了一个应用公钥:

技术分享

电脑支付是不要这个PUBLIC key的

 

好啦 以上就是支付宝电脑网站支付和app 支付的方法  其实手机支付也是差不多的 变得就是支付类型 和支付类

技术分享

 

看不懂的可以加我QQ。一起探讨

 



 

php 做支付宝电脑网站和app支付

标签:class   技术   enc   uri   eal   url   ppi   通知   color   

原文地址:http://www.cnblogs.com/lixiaoquan/p/7691827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!