2015-10-23 33 views
0

我一直在遇到問題,當我嘗試從貝寶獲取返回數據。 我在我的Paypal帳戶和表單上設置了Retrun Url。這是表單,這僅用於測試目的。如何獲得貝寶Chekout返回數據

<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr" name="_xclick"> 
    <div align="center"> 
    <input type="hidden" name="return" value="http://{host}/paypal/tnk.php" /> 

      <input type="hidden" name="cancel_return" value="http://{host}/paypal/tnk.php" /> 

      <input type="hidden" name="currency_code" value="USD" />    

      <input type="hidden" value="_xclick" name="cmd"> 

      <input type="hidden" name="business" value="[email protected]"> 

      <input type="hidden" name="item_name" value="Paypal 2015"> 

      <input type="hidden" name="item_number" value="123"> 

      <input type="hidden" name="amount" value="10"> 

      <input type="hidden" name="first_name" value="Test"> 

      <input type="hidden" name="last_name" value="Last Name"> 

      <input type="hidden" name="city" value=""> 

      <input type="hidden" name="state" value=""> 

      <input type="hidden" name="email" value=""> 

      <input type="hidden" name="image_url""> 
      <input type="hidden" name="rm" value="2"> 
     <input type="hidden" name="notify_url" value="http://{host}/paypal/tnk.php"> 

    <input type="submit" value="Proceed To Pay"> 

    </div> 

    </form> 

當我點擊「繼續付款」,我重定向到貝寶,我可以支付價格。 在返回頁面上,我已經把一個文件寫入了Paypal的所有返回數據。

$post   = array('cmd' => '_notify-validate'); 
foreach($_POST as $key=>$value){ 
     $post[$key] = $value; 
     $file = fopen("test.txt","w+"); 
     echo fwrite($file,print_r($_POST)); 

     fclose($file);  
} 
$c = curl_init(); 
curl_setopt_array($c, array(
    CURLOPT_FOLLOWLOCATION => TRUE, 
    CURLOPT_RETURNTRANSFER => TRUE, 
    CURLOPT_CONNECTTIMEOUT => 15, 
    CURLOPT_MAXREDIRS  => 15, 
    CURLOPT_TIMEOUT   => 15, 
    CURLOPT_URL    => 'https://www.paypal.com/cgi-bin/webscr', 
    CURLOPT_POST   => TRUE, 
    CURLOPT_POSTFIELDS  => $post, 
)); 
$res = curl_exec($c); 
curl_close($c); 
$res = trim($res); 

if($res != 'VERIFIED') { 
    exit(); 
} 

但它只在文本文件上寫入值1。 這意味着返回的網址是owrking,但貝寶沒有返回任何關於$_REQUEST的數據。

我做錯了什麼?有點困惑,並嘗試了幾種方法。 希望我已經提供了足夠的細節。

請幫我解決這個問題。 在此先感謝。

回答

0

您必須添加參數「notify_url」,而不是參數「return」。參數「return」用於成功購買頁面。在你的表單中,你有無效的字段,如first_name和last_name!用這個例子:

<form method="post" name="checkout-form" action="https://www.paypal.com/cgi-bin/webscr"> 
<input type="hidden" name="cmd" value="_xclick"> 
<input type="hidden" name="business" value="[email protected]_email.com"> 
<input type="hidden" name="item_name" value="Product Name> 
<input type="hidden" name="amount" value="22" > 
<input type="hidden" name="item_number" value="65"> 
<input type="hidden" name="custom" value="[email protected]#!#[email protected]#@!WDASD"> 
<input type="hidden" name="currency_code" value="USD"> 
<input type="hidden" name="quantity" value="1"> 
<input type="hidden" name="no_shipping" value="1"> 
<input type="hidden" name="return" value="http://zoomthe.me/paypalipn/paypal_ipn.php"> 
<input type="hidden" name="cancel_return" value="http://zoomthe.me/paypal_cancel"> 
<input type="hidden" name="cbt" value="Product Description"> 
<input type="hidden" name="rm" value="2"> 
<input type="hidden" name="notify_url" value="http://zoomthe.me/your_listener_ipn_file.php"> 
</form> 

在你的IPN監聽器文件中最重要的事情是驗證PayPal交易(這只是一個推薦)。這裏是一個(php)的例子:

 $post   = array('cmd' => '_notify-validate'); 
    foreach($_POST as $key=>$value){ 
      $post[$key] = $value; 
    } 
    $c = curl_init(); 
    curl_setopt_array($c, array(
     CURLOPT_FOLLOWLOCATION => TRUE, 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_CONNECTTIMEOUT => 15, 
     CURLOPT_MAXREDIRS  => 15, 
     CURLOPT_TIMEOUT   => 15, 
     CURLOPT_URL    => 'https://www.paypal.com/cgi-bin/webscr', 
     CURLOPT_POST   => TRUE, 
     CURLOPT_POSTFIELDS  => $post, 
    )); 
    $res = curl_exec($c); 
    curl_close($c); 
    $res = trim($res); 

    if($res != 'VERIFIED') { 
     exit(); 
    } 
+0

我已經將錯過的字段添加到文件,但仍在文本文件上打印1。我用最新的代碼編輯了我的問題。你能幫我解決嗎? –

+0

只需使用我的表單發佈!在你的表單中,你有無效的字段,如first_name和last_name! – Pavel