支付寶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拼接進的訂單號里面。

還有一個問題回調的時候驗簽這里通不過,所以我直接給注釋了,簡單粗暴,至於有什么影響還在看。

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM