支付宝PC端的支付(PHP)

现在的一个项目支付宝PC支付还是用的以前网关:https://mapi.alipay.com/gateway.do  (是支付宝 2012年7月13日的3.3版本)

所以现在要做下支付宝PC支付的升级,用最新的网关:https://openapi.alipay.com/gateway.do 

与之前的相比,代码上比之前更安全了,有公钥、私钥、APPID、签名、验签等。

支付效果见图:


首先下载好PC支付的sdk,一共有两段代码,代码是基于tp3.2框架写的

第一段:直接用get方式访问下面的这个方法

/*
     * 支付宝支付--new   扫码支付
     * $out_trade_no 是需要你自定义的订单号
     * $subject 是这次订单的标题自定义的
     * $total_fee 是付款的金额 0.01
     * */
    public function alipay_new($out_trade_no, $subject, $total_fee) {
        //配置参数
        $res = array();
        $res['out_trade_no'] = $out_trade_no;
        $res['subject'] = $subject;
        $res['total_amount'] = $total_fee;
        $res['body'] = '';
        //引入核心的支付文件
        vendor('AlipayPc.AopSdk');
        vendor('AlipayPc.aop.AopClient');
        vendor('AlipayNew.aop.request.AlipayTradePagePayRequest');
        //支付宝配置参数
        $config = [
            'app_id' => '这个支付宝管理中心可以找到',
            'merchant_private_key' => '这里面写是私钥,注意要是一行,生成的那个西药文件里面的都有回车',
            'notify_url' =>'异步的回调地址',
            'return_url' => '同步的回调地址',
            'charset' => 'UTF-8',
            'sign_type' => 'RSA2',
            'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
            'alipay_public_key' => '公钥,注意的地方和私钥一样',
            'MaxQueryRetry' => '10',
            'QueryDuration' => '3'
        ];
        //构造参数
        $timeExpress = "5m";
        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018071560647164';
        $aop->rsaPrivateKey = $config['merchant_private_key'];
        $aop->alipayrsaPublicKey = $config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayTradePagePayRequest();
        $request->setNotifyUrl($config['notify_url']);
        $request->setReturnUrl($config['return_url']);
//下面的参数可以去看文档,这里有个问题我还没解决,就是passback_params这在同步返回的时候没有给带回来,有知道的可以给我留言,谢谢了
        $request->setBizContent("{" .
            "\"out_trade_no\":\"".$out_trade_no."\"," .
            "\"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," .
            "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," .
            "\"total_amount\":0.01," .
            "\"subject\":\"".$subject."\"," .
            "\"body\":\"".$subject."\"," .
            "\"timeout_express\":\"5m\"" .
            "}");
        $result = $aop->pageExecute ($request);
        echo $result;exit;//这个结果是html代码直接输出就好了,它是一个form表单,会自动触发,提交数据到网关。
        
    }  

支付完成后,它会自动跳转,会跳转到你的同步回调地址,在同步回调地址中可以用$_GET来获取到传来的信息。

第二段:这里就是获取到信息,来调用下面这个接口

public function alipayReturnNew() {
        //引入核心的支付文件
        vendor('AlipayPc.AopSdk');
        vendor('AlipayPc.aop.AopClient');
        vendor('AlipayNew.aop.request.AlipayTradeQueryRequest');
        //支付宝配置参数
        $config = [
            'app_id' => '',
            'merchant_private_key' => '',
            'notify_url' => '',
            'return_url' => '',
            'charset' => 'UTF-8',
            'sign_type' => 'RSA2',
            'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
            'alipay_public_key' => '',
            'MaxQueryRetry' => '10',
            'QueryDuration' => '3'
        ];
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '';
        $aop->rsaPrivateKey = $config['merchant_private_key'];
        $aop->alipayrsaPublicKey = $config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayTradeQueryRequest ();
        $request->setBizContent("{" .
            "\"out_trade_no\":\"".$_GET['out_trade_no']."\"," .
            "\"trade_no\":\"".$_GET['trade_no']."\"" .
            "}");
        $result = $aop->execute ( $request);
//dump($result);exit;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
//说明支付成功了,下面就是获取需要存储的数据,存到我们的数据库,更新订单的状态了
            $out_user = explode('_',$_GET['out_trade_no']);
            //验证成功
            $out_trade_no = $_GET['out_trade_no'];      //商户订单号
            $trade_no     = $result->$responseNode->trade_no;          //支付宝交易号
            $trade_status = $result->$responseNode->trade_status;      //交易状态
            $total_fee    = $_GET['total_amount'];         //交易金额
            $notify_id    = $_GET['notify_id'];         //通知校验ID
            $notify_time  = $result->$responseNode->send_pay_date;       //通知的发送时间
            $buyer_email  = $result->$responseNode->buyer_logon_id;       //买家支付宝帐号
            $userID       = $out_user[1];       //买家id

            $parameter = [
                "userID"       => $userID,
                "type"         => 'alipay',
                "orderNo"      => $out_trade_no,      //商户订单编号
                "trade_no"     => $trade_no,          //支付宝交易号
                "total_fee"    => $total_fee,         //交易金额
                "trade_status" => $result->$responseNode->trade_status,      //交易状态
                "notify_id"    => $notify_id,         //通知校验ID
                "notify_time"  => $notify_time,       //通知的发送时间
                "buyer_email"  => $buyer_email,       //买家支付宝帐号
            ];

            if ($result->$responseNode->trade_status == 'TRADE_FINISHED' || $result->$responseNode->trade_status == 'TRADE_SUCCESS') {
                if (!checkOrderStatus($out_trade_no)) {
                    orderHandle($parameter);  //进行订单处理,并传送从支付宝返回的参数;
                }
                $this->redirect('写你要跳转的地址');   //跳转到配置项中的支付成功页面;
            } else {
                echo "trade_status=" . $result->$responseNode->trade_status;
                $this->redirect('写你要跳转的地址');     //跳转到配置项中的支付失败页面;
            }
            echo "验证成功<br />";
        } else {
            //验证失败
            echo "验证失败";
        }
    }
View Code

前面说的 passback_params 不起作用,回调时我需要获取用户的id,所以暂时采用了将用户的id拼接进的订单号里面。

还有一个问题回调的时候验签这里通不过,所以我直接给注释了,简单粗暴,至于有什么影响还在看。

有知道原因的留下言,谢谢~~

猜你喜欢

转载自www.cnblogs.com/heiue/p/PAY.html