2012-08-09 92 views
-1

我無法將PayPal的IPN集成到我的PHP中,我有以下腳本,並且在PayPal沙箱中進行付款時,它會一直落入默認情況。PayPal IPN集成到PHP

任何幫助,將不勝感激!

$request = "cmd=_notify-validate"; 
    foreach ($_POST as $varname => $varvalue){ 
     $email .= "$varname: $varvalue\n"; 
     if(function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()){ 
      $varvalue = urlencode(stripslashes($varvalue)); 
     } 
     else { 
      $value = urlencode($value); 
     } 
     $request .= "&$varname=$varvalue"; 
    } 
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,"https://www.sandbox.paypal.com/cgi-bin/webscr"); 
    //curl_setopt($ch,CURLOPT_URL,"https://www.paypal.com"); 
    curl_setopt($ch,CURLOPT_POST,true); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$request); 
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    $result = curl_exec($ch); 
    curl_close($ch); 

    switch($result){ 
     case "VERIFIED": 
        mail('[email protected]','worked','worked'); 
      break; 
     case "INVALID": 
      mail('[email protected]','invaild','invaild'); 
      break; 
     default: 
      mail('[email protected]','failed','failed'); 
    } 

如果我發給自己$導致它僅僅是空白。

編輯:我發現這是我的LAMP服務器端問題,但我不確定如何解決它。

注:我確實在服務器上安裝了curl,但我不確定它是否配置正確。

回答

2

我建議使用的var_dump做一些調試和Paypal的測試工具位於:https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session

我能理解它變得困難,並與第三方服務工作時耗費時間。

它可能是值得簡單地抓住POST數據,序列化並將其應用到變量,所以你可以測試沒有貝寶擊中你的回調。

我會這樣做,最初抓住貝寶POST。

<?php 
    file_put_contents(serialize($_POST), 'post.log'); 
    //Now you have the post request serialized we can grab the contents and apply it to a variable for fast testing. 
?> 

的啓動代碼:

<?php 
    $_POST = unserialize(file_get_content('post.log')); 
    //Now you can execute the script via command line or within your browser without requiring PayPal's testing tool. Use var_dump to investigate what's the issue. 
    $request = "cmd=_notify-validate"; 
    foreach ($_POST as $varname => $varvalue){ 
     $email .= "$varname: $varvalue\n"; 
     if(function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()){ 
      $varvalue = urlencode(stripslashes($varvalue)); 
     } 
     else { 
      $value = urlencode($value); 
     } 
     $request .= "&$varname=$varvalue"; 
    } 
?> 

NOW:這是一個比較有效的,高效的,當涉及到測試。在你的例子中,你正在給自己發送電子郵件,但不包括郵件功能體內任何地方的$ result。 http://php.net/manual/en/function.mail.php

PayPal的IPN示例使用fsock,儘管CURL更有效且更易於使用。 PayPal的沙箱也在不斷變化。 https://www.paypal-community.com/t5/Selling-on-your-website/IPN-response-problem/m-p/519862/message-uid/519862#U519862

另外:要確定是什麼主要原因,正如你所說,它看起來像你的LAMP堆棧。通常從他們的日誌目錄(通常是/ var/log /)中檢查你將能夠確定哪些是失敗的。

+0

我解決了這個問題,看起來像貝寶有問題通過SSL通信到我的服務器,我把它放在下面的行,它的工作: curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); – Jeff 2012-08-09 18:14:27