2015-12-23 125 views
0

我想實現paypal IPN監聽器。問題是,無論我嘗試什麼,我仍然從IPN模擬器獲得INVALID答案。我搜索了很多論壇,但沒有任何幫助。有誰知道,可能是什麼問題?PayPal IPN模擬器返回INVALID答案

請參閱下面的代碼。 YII_DEBUG是真的 USE_SANDBOX是真的 我試着用或不用cacert.pem的實現,沒有任何工作。

public function actionProcessPayment() 
{ 
    // Read POST data 
    // reading posted data directly from $_POST causes serialization 
    // issues with array data in POST. Reading raw POST data from input stream instead. 
    $raw_post_data = file_get_contents('php://input'); 
    if(YII_DEBUG == true) { 
     Yii::log(date('[Y-m-d H:i e] '). "Original data: " . $raw_post_data . PHP_EOL, 'warning'); 
    } 

    $req = 'cmd=_notify-validate&'.$raw_post_data; 


    // Post IPN data back to PayPal to validate the IPN data is genuine 
    // Without this step anyone can fake IPN data 
    if(self::USE_SANDBOX == true) { 
     $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; 
    } else { 
     $paypal_url = "https://www.paypal.com/cgi-bin/webscr"; 
    } 

    $ch = curl_init($paypal_url); 
    if ($ch == FALSE) { 
     return FALSE; 
    } 
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
    if(YII_DEBUG == true) { 
     curl_setopt($ch, CURLOPT_HEADER, 1); 
     curl_setopt($ch, CURLINFO_HEADER_OUT, 1); 
    } 

    // Set TCP timeout to 30 seconds 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); 

    $cert = dirname(__FILE__).'/../components/cacert.pem'; 
    curl_setopt($ch, CURLOPT_CAINFO, $cert); 

    $res = curl_exec($ch); 
    if (curl_errno($ch) != 0) // cURL error 
    { 
     if(YII_DEBUG == true) { 
      Yii::log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 'warning'); 
     } 
     curl_close($ch); 
     exit; 
    } else { 
     // Log the entire HTTP response if debug is switched on. 
     if(YII_DEBUG == true) { 
      Yii::log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 'warning'); 
      Yii::log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 'warning'); 
     } 
     curl_close($ch); 
    } 

    // Inspect IPN validation result and act accordingly 
    // Split response headers and payload, a better way for strcmp 
    $tokens = explode("\r\n\r\n", trim($res)); 
    $res = trim(end($tokens)); 
    if (strcmp ($res, "VERIFIED") == 0) { 
     // check whether the payment_status is Completed 
     // check that txn_id has not been previously processed 
     // check that receiver_email is your PayPal email 
     // check that payment_amount/payment_currency are correct 
     // process payment and mark item as paid. 
     // assign posted variables to local variables 
     //$item_name = $_POST['item_name']; 
     //$item_number = $_POST['item_number']; 
     //$payment_status = $_POST['payment_status']; 
     //$payment_amount = $_POST['mc_gross']; 
     //$payment_currency = $_POST['mc_currency']; 
     //$txn_id = $_POST['txn_id']; 
     //$receiver_email = $_POST['receiver_email']; 
     //$payer_email = $_POST['payer_email']; 
     if(isset($_POST['custom'], $_POST['payment_status'])) 
      if($_POST['payment_status'] == 'Completed') 
       Order::processPayment($_POST['custom']); 

     if(YII_DEBUG == true) { 
      Yii::log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 'warning'); 
     } 
    } else if (strcmp ($res, "INVALID") == 0) { 
     // log for manual investigation 
     // Add business logic here which deals with invalid IPN messages 
     if(YII_DEBUG == true) { 
      Yii::log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 'warning'); 
     } 
    } 

    Yii::app()->end(); 
} 

回答

0

問題是因爲IPN模擬器自動填寫領域PAYMENT_DATE與localy(捷克)格式化日期(StředníEVROPA(běžnýCAS)字符串,我更換這IPN模擬器(GMT標準時間)和everythink作品現在好...