2014-08-29 85 views
0

我正在尋找如何使用過去2周內的PayPal IPN(即時付款通知),並且無法理解如何在我的代碼中使用它。PayPal支付驗證,IPN如何工作

我使用下面的HTML代碼來創建一個PayPal按鈕

button.html

<form name="paypalForm" action="paypal.php" method="post"> 
<input type="hidden" name="id" value="123"> 
<input type="hidden" name="CatDescription" value="20Percents (12 Credits)"> 
<input type="hidden" name="payment" value="10"> 
<input type="hidden" name="key" value="<? echo md5(date("Y-m-d:").rand()); ?>">   
<input TYPE="image" SRC="http://www.coachsbr.com/images/site/paypal_button.gif" name="paypal" value="Payment via Paypal" > 
</form> 

而且,下面的代碼來處理支付和驗證

貝寶.php

<?php 
require_once('paypal.class.php'); // include the class file 
$p = new paypal_class;    // initiate an instance of the class 
$p->paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; // testing paypal url 
//$p->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';  // paypal url 

// setup a variable for this script (ie: 'http://www.micahcarrick.com/paypal.php') 
$this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; 

// if there is not action variable, set the default action of 'process' 
if (empty($_GET['action'])) $_GET['action'] = 'process'; 

switch ($_GET['action']) { 

    case 'process':  // Process and order... 

     $CatDescription = $_REQUEST['CatDescription']; 
     $payment = $_REQUEST['payment']; 
     $id = $_REQUEST['id']; 
     $key = $_REQUEST['key']; 

     $p->add_field('business', '[email protected]'); 
     $p->add_field('return', $this_script.'?action=success'); 
     $p->add_field('cancel_return', $this_script.'?action=cancel'); 
     $p->add_field('notify_url', $this_script.'?action=ipn'); 
     $p->add_field('item_name', $CatDescription); 
     $p->add_field('amount', $payment); 
     $p->add_field('key', $key); 
     $p->add_field('item_number', $id); 


     $p->submit_paypal_post(); // submit the fields to paypal 
     //$p->dump_fields();  // for debugging, output a table of all the fields 
     break; 

    case 'success':  // Order was successful... 

     echo "<br/><p><b>Payment Successful.</b><br /></p>"; 

     foreach ($_POST as $key => $value) { echo "$key: $value<br>"; }  
     break; 

    case 'cancel':  // Order was canceled... 


     echo "<br/><p><b>The order was canceled!</b></p><br />"; 
    foreach ($_POST as $key => $value) { echo "$key: $value<br>"; } 

     break; 

    case 'ipn':   // Paypal is calling page for IPN validation... 

     if ($p->validate_ipn()) { 

     // For this example, we'll just email ourselves ALL the data. 
     $dated = date("D, d M Y H:i:s", time()); 

     $subject = 'Instant Payment Notification - Recieved Payment'; 
     $to = '[email protected]'; // your email 
     $body = "An instant payment notification was successfully recieved\n"; 
     $body .= "from ".$p->ipn_data['payer_email']." on ".date('m/d/Y'); 
     $body .= " at ".date('g:i A')."\n\nDetails:\n"; 
     $headers = ""; 
     $headers .= "From: Test Paypal \r\n"; 
     $headers .= "Date: $dated \r\n"; 

     $PaymentStatus = $p->ipn_data['payment_status']; 
     $Email  = $p->ipn_data['payer_email']; 
     $id   = $p->ipn_data['item_number']; 

     if($PaymentStatus == 'Completed' or $PaymentStatus == 'Pending'){ 
      $PaymentStatus = '2'; 
     }else{ 
      $PaymentStatus = '1'; 
     } 
     foreach ($p->ipn_data as $key => $value) { $body .= "\n$key: $value"; } 
     fopen("http://www.virtualphoneline.com/admins/TestHMS.php?to=".urlencode($to)."&subject=".urlencode($subject)."&message=".urlencode($body)."&headers=".urlencode($headers)."","r");   
    } 
     break; 
}  
?> 

一個額外的類文件:(可以在這裏找到)http://pastebin.com/auCdYhaR

的代碼工作正常,但我現在有很大的問題,我想確認支付是否成功與否從以下行並執行操作(在會話中爲用戶添加10個信用點)。

case 'success':  // Order was successful... 

     echo "<br/><p><b>Payment Successful.</b><br /></p>"; 

     foreach ($_POST as $key => $value) 

{ 

    /*echo "$key: $value<br>";*/ 
    // Do the required action, **add 10 credits in the database for the user on session** 

}  
     break; 

最後,問題是,當我在按鈕頁面重定向單擊從button.html到Paypal.php頁面,當我進入我的PayPal登錄信息,付款成功後,

,有那麼小的小文本 - >返回發件人的網站我需要點擊它,當我點擊它,它會返回給我帶來PAYPAL.PHP頁面,然後在CASE 'SUCCESS'被激發。如果我付款並且只是關閉PayPal頁面而沒有點擊返回到SENDER的網站並且沒有等到頁面PAYPAL.PHP LOADS網站不能驗證付款並且不能添加信用。

我如何可以自動完成這一過程並添加學分UPON 成功付款不是在成功返回頁面PAYPAL.PHP

感謝

回答

0

這聽起來像你混淆了PDT和IPN。 PDT通過將數據發送到您的返回URL來工作,但這不是處理後付款處理任務的推薦方式。

這就是IPN發揮作用的地方,無論用戶是否返回到您的返回URL,這都是您的監聽器URL的無聲POST數據。這樣的代碼將永遠運行,不管什麼(假設你已經正確配置了所有東西)。

聽起來像我這樣混合二者,並得到混合的結果,因爲它。