1. 程式人生 > >paypal介面對接注意事項 paypal狀態返回的設定

paypal介面對接注意事項 paypal狀態返回的設定

 /**
     * 通過PDT驗證付款後paypal返回的資料
     * @param type $tx 交易流水號,通過Get獲取
     * @param type $pdt_identity_token 商家唯一身份標記
     * @return 訂單明細資料
     * @throws \Exception
     */
    public function verifyReturn($tx, $pdt_identity_token) {

        if(empty($tx))
        {
            throw new \Exception("Unexpected response from PayPal or Others.");
        }
        
        $encoded_data = http_build_query(array
                (
                        'cmd' => '_notify-synch',
                        'tx' => strtoupper($tx),
                        'at' => $pdt_identity_token,
                ));  

//         $encoded_data = 'cmd=_notify-synch&tx=$tx&at=$pdt_identity_token';      
        
        if ($this->use_curl) 
        {
            $this->curlPost($encoded_data); 
        }
        else 
        {
            $this->fsockPost($encoded_data);
        }
        
        $status = strpos($this->response_status, '200');       
        
        // check responses, if first 7 letters are SUCCESS then we're good
        if($this->response_status == 200 && strpos($this->response, "SUCCESS") !== false)
        {
                // get rid of success
                $curlResponse = substr($this->response, 7);
                // decode
                $curlResponse = urldecode($curlResponse);
                // make associative array
                preg_match_all('/^([^=\r\n]++)=(.*+)/m', $curlResponse, $m, PREG_PATTERN_ORDER);
                
                $curlResponse = array_combine($m[1], $m[2]);
                // keysort to keep in order
                ksort($curlResponse);
                
                // end
                return $curlResponse;
        }
        else
        {
                throw new \Exception("Invalid response status: ".$this->response_status);
        }                
    }

九域程式胡靜 2015/12/3 12:13:28

   protected function curlPost($encoded_data) {

        if ($this->use_ssl) {
            $uri = 'https://'.$this->getPaypalHost().'/cgi-bin/webscr';
            $this->post_uri = $uri;
        } else {
            $uri = 'http://'.$this->getPaypalHost().'/cgi-bin/webscr';
            $this->post_uri = $uri;
        }
        
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//         curl_setopt($ch, CURLOPT_CAINFO, 
//         dirname(__FILE__)."/cert/cert_key.pem");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: ".$this->getPaypalHost()));
        
        
//         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        
//         curl_setopt($ch, CURLOPT_HEADER, true);
        
//         if ($this->force_tls_v1) {
//             curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
//         }

        $this->response = curl_exec($ch);
        
        $this->response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
        
        if ($this->response === false || $this->response_status == '0') {
            $errno = curl_errno($ch);
            $errstr = curl_error($ch);
            
            throw new \Exception("cURL error: [$errno] $errstr");
        }
    }