2014-08-29 88 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数据。这样的代码将永远运行,不管什么(假设你已经正确配置了所有东西)。

听起来像我这样混合二者,并得到混合的结果,因为它。