【问题标题】:PayPal Payment Verification, How the IPN worksPayPal 付款验证,IPN 的工作原理
【发布时间】:2014-10-23 05:39:16
【问题描述】:

我正在寻找如何使用过去 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 登录详细信息时,支付成功

来了一个小文本 -> 返回发件人的网站我需要点击它,当我点击它时,它会带我回到 PAYPAL.PHP strong> 页面,然后CASE 'SUCCESS' 被解雇。如果我付款后只是关闭了 PAYPAL 页面而不点击返回发件人的网站并且没有等到页面 PAYPAL.PHP 加载网站无法验证付款且无法添加积分。

我如何使这个过程自动化并在成功付款而不是成功返回页面 PAYPAL.PHP 时添加信用。

谢谢

【问题讨论】:

    标签: php paypal paypal-ipn


    【解决方案1】:

    在我看来,您在混淆 PDT 和 IPN。 PDT 的工作原理是向您的返回 URL 发送数据,但这不是处理付款后处理任务的推荐方式。

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

    在我看来,您似乎是在将两者混为一谈,并因此而得到不同的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-24
      • 2012-09-17
      • 2014-03-01
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2012-11-07
      相关资源
      最近更新 更多