2011-06-14 103 views
1

有沒有人與貝寶IPN?我從PayPalTech.com獲得了一些示例代碼,並進行了編輯以適應我的需求。基本上所有我想要工作的是在訂單完成後發送給客戶的電子郵件。我編輯了PHP代碼以適合我的需求,但由於某種原因,它現在不再適用了。最初的代碼只是爲了回覆來自IPN的所有php變量,並在發送給我自己的電子郵件中結束它。這是我迄今爲止的代碼。PayPal幫助IPN在交易後發送電子郵件

<?php 


// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 

foreach ($_POST as $key => $value) { 
$value = urlencode(stripslashes($value)); 
$req .= "&$key=$value"; 
} 

// post back to PayPal system to validate 

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; 

// If testing on Sandbox use: 
// $header .= "Host: www.sandbox.paypal.com:443\r\n"; 
$header .= "Host: www.paypal.com:443\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

// If testing on Sandbox use: 
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); 




// 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']; 


//transaction ID 
$txn_id = $_POST['txn_id']; 


//ATEKDesign 
$business = $_POST['business']; 

//payer email 
$payer_email = $_POST['payer_email']; 

//create payer name 
$payer_firstname = $_POST['first_name']; 
$payer_lastname = $_POST['last_name']; 
$payer_name = $payer_firstname . " " . $payer_lastname; 

//create payer address 
$name = $_POST['address_name']; 
$street = $_POST['address_street']; 
$city = $_POST['address_city']; 
$state = $_POST['address_state']; 
$zip = $_POST['address_zip']; 

$address = "$name \n $street \n $city $state $zip"; 









if (!$fp) 
{ 
    // HTTP ERROR 
} 

else 
{ 
    fputs ($fp, $header . $req); 
     while (!feof($fp)) 
      { 
       $res = fgets ($fp, 1024); 
        if (strcmp ($res, "VERIFIED") == 0) 
         { 
          // check the payment_status is Completed 
          // check that txn_id has not been previously processed 
          // check that receiver_email is your Primary PayPal email 
          // check that payment_amount/payment_currency are correct 
          // process payment 

          $mail_From = "From: [email protected]"; 
          $mail_To = "[email protected]"; 
          $mail_Subject = "VERIFIED IPN"; 
          $mail_Body = "Hello $payer_ name , thank you for purchasing the $item_name from $buisness , your order ID is as follows: $txn_id; . \n We will be using $payer_email to contact you and will be sending you item to $address"; 
          $emailtext = blank; 



          mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From); 

         } 
+1

你能解釋一下哪些工作不正常嗎?你有錯誤嗎?郵件沒有被髮送?編程是否進入發送郵件的if塊? – Jrod 2011-06-14 17:20:44

+0

正如代碼示例中的註釋所述,在測試時應使用Sandbox設置Paypal開發人員帳戶,以便您可以安全地在代碼上測試假Paypal事務。您可能遇到的一個問題是您的代碼正在接觸到產品Paypal服務器。 我還假定你有最後的'else'和'while'塊的右括號,並且它們在上面的例子中被省略了。 – Mattygabe 2011-06-14 18:26:42

回答

1

你得到了什麼錯誤?如果您刪除郵件部分並返回回顯,會發生什麼情況?

我建議退後一步 - 儘可能多地註釋掉所有的變化,並且仍然有一些基本的功能。然後,開始取消註釋您的代碼 - 一次一個代碼段 - 直到它不起作用。

如果您提供更多詳細信息,我可能會提供幫助。

5

1)確保您已在您的PayPal帳戶中啓用IPN。
           如果未通過將ipn頁面url添加到paypal來啓用IPN。

2)確保paypal將值發佈到您的ipn頁面,以便將以下代碼放入您的ipn文件並完成付款。

<?PHP 
$mail_From = "From: [email protected]"; 
$mail_To = "[email protected]"; 
$mail_Subject = "VERIFIED IPN"; 
$mail_Body = "Post: ".serialize($_POST)."<br>"; 
$emailtext = "blank"; 

mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From); 
?> 

如果您收到電子郵件,請檢查變量及其值。