PayPal付款验证,IPN如何运作

时间:2014-08-29 08:23:02

标签: php paypal paypal-ipn

我正在搜索过去2周内如何使用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>

并且,以下代码处理付款并验证

paypal.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', 'info@aoptrading.com');
      $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 = 'info@aoptrading.com';    //  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登录详细信息时,付款成功

有一个小小的文字 - &gt;返回 SENDER&#39; S网站我需要点击它,当我点击它时,它会让我回到 PAYPAL.PHP PAGE然后{ {1}}被解雇了。如果我付款并且只是关闭PayPal页面而不点击返回 SENDER&#39; S网站并且DIDN等待直到页面 PAYPAL.PHP 负载网站有权确认支付费用并增加信用额度。

我如何自动完成此流程,并在成功付款后不再成功返回页面PAYPAL.PHP

感谢

1 个答案:

答案 0 :(得分:0)

听起来我觉得你混淆了PDT和IPN。 PDT的工作原理是将数据发送到您的返回URL,但这不是处理后付款处理任务的推荐方法。

IPN发挥作用的地方,无论用户是否将其返回到您的返回网址,都是向您的侦听器网址静默POST的数据。这样,代码将始终运行,无论如何(假设您已正确配置所有内容。)

听起来像你一样,混合两者并因此而得到混合结果。