paypal ipn micah 的 php 类

Posted

技术标签:

【中文标题】paypal ipn micah 的 php 类【英文标题】:paypal ipn micah's php class 【发布时间】:2012-01-20 07:50:24 【问题描述】:

我知道这个主题有很多问题,我现在用 paypal IPN 玩了将近 3 个小时,但没有成功。这是一个很长的问题,如果有人能读完,我真的很感激。

我正在尝试创建一个简单的在线服务,用户访问我的网站,单击贝宝付款按钮,然后将被带到贝宝进行付款。付款后,他们将被带回到验证他们是否付款的页面。如果付费,则显示在同一(php文件)中的注册页面,如果没有,用户将被重定向回主页。

我按照这里的教程进行操作: http://www.micahcarrick.com/paypal-ipn-with-php.html

我把所有东西都放在同一个目录中,(ipnlistener.php、ipn.php、ipn-errors.log)和一个 index.html,其中存储了 BUY NOW 按钮 代码: p>

<html>
<title>Test IPN</title>
<form name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr" 
    method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="testmailsandbox@domain.com">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="item_name" value="Item">
    <input type="hidden" name="amount" value="50">
    <input type="hidden" name="return" value="http://mysite.com/ipn/">
    <input type="hidden" name="notify_url" value="http://mysite.com/ipn/ipn.php">
    <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" 
        border="0" name="submit" >
</form>
</html>

我没有触及ipnlistener.php 文件,因为它是ipn.php 使用的类

我的 ipn.php 文件:

/**
 *  @package    PHP-PayPal-IPN
 *  @author     Micah Carrick
 *  @copyright  (c) 2011 - Micah Carrick
 *  @license    http://opensource.org/licenses/gpl-3.0.html
 */

    ini_set('log_errors', true);
    ini_set('error_log','ipn_errors.log');


    // instantiate the IpnListener class
    include('ipnlistener.php');
    $listener = new IpnListener();


    /*
    When you are testing your IPN script you should be using a PayPal "Sandbox"
    account: https://developer.paypal.com
    When you are ready to go live change use_sandbox to false.
    */
    $listener->use_sandbox = true;


    try 
        $listener->requirePostMethod();
        $verified = $listener->processIpn();
     catch (Exception $e) 
        error_log($e->getMessage());
        exit(0);
    


    /*
    The processIpn() method returned true if the IPN was "VERIFIED" and false if it
    was "INVALID".
    */
    if ($verified) 
        // 1. Make sure the payment status is "Completed" 
        if ($_POST['payment_status'] != 'Completed')  
            // simply ignore any IPN that is not completed
            exit(0); 
        

        // 2. Make sure seller email matches your primary account email.
        if ($_POST['receiver_email'] != 'testmailsandbox@domain.com') 
            $errmsg .= "'receiver_email' does not match: ";
            $errmsg .= $_POST['receiver_email']."\n";
        

        // 3. Make sure the amount(s) paid match
        if ($_POST['mc_gross'] != '50') 
            $errmsg .= "'mc_gross' does not match: ";
            $errmsg .= $_POST['mc_gross']."\n";
        

        // 4. Make sure the currency code matches
        if ($_POST['mc_currency'] != 'USD') 
            $errmsg .= "'mc_currency' does not match: ";
            $errmsg .= $_POST['mc_currency']."\n";
        

        // TODO: Check for duplicate txn_id

        if (!empty($errmsg)) 

            // manually investigate errors from the fraud checking
            $body = "IPN failed fraud checks: \n$errmsg\n\n";
            $body .= $listener->getTextReport();
            mail('myemail@domain.com', 'IPN Fraud Warning', $body);

         else 

        //verified
        include 'connect.php';

            $sql = "INSERT INTO order VALUES 
                ('$txn_id', '$payer_email', '$mc_gross')";

           if (!mysql_query($sql)) 
              error_log(mysql_error());
              exit(0);
            
        

     else 
        /*
        An Invalid IPN *may* be caused by a fraudulent transaction attempt. It's
        a good idea to have a developer or sys admin manually investigate any 
        invalid IPN.
        */
        mail('myemail@domain.com', 'Invalid IPN', $listener->getTextReport());
    

    ?>

我已将我的商家 acc IPN 设置为收听“ipn.php”文件。 如您所见,付款是否已验证,我进行数据库查询以将新行插入 ORDER 表。

我转到 index.html 并单击“立即购买”按钮,使用我的买家 ID 付款。

然而,

我在错误日志中不断收到“无效的 HTTP 请求方法”错误。

任何人都可以帮助我发现问题还是我做错了? 谢谢,祝你有美好的一天。

【问题讨论】:

【参考方案1】:

我认为这是因为通知 url 和返回 url 都没有指向真实的网站。虽然我来到这里是因为我发现了同样的问题(我正在使用本地机器地址在沙盒环境中进行测试,所以也需要找到解决这个问题的方法)

http://mysite.com/

【讨论】:

【参考方案2】:

试试这个 ipn 监听器

 <?php
   //Build the data to post back to Paypal
    $postback = 'cmd=_notify-validate'; 

     // go through each of the posted vars and add them to the postback variable
     foreach ($_POST as $key => $value) 
          $value = urlencode(stripslashes($value));
          $postback .= "&$key=$value";
      


     // Send to paypal or the sandbox depending on whether you're live or developing
     //$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//open the connection
       $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) 

    // HTTP ERROR Failed to connect
    //error handling or email here

else // if we've connected OK

    fputs ($fp, $header . $postback);//post the data back
    while (!feof($fp)) 
    
        $response = fgets ($fp, 1024);

        if (strcmp ($response, "VERIFIED") == 0) //It's verified
        
           //if verified do something 
        
        else if (strcmp ($response, "INVALID") == 0) 
         
            //the Paypal response is INVALID, not VERIFIED
            // This implies something is wrong 
        
     //end of while
    fclose ($fp);

?>

【讨论】:

【参考方案3】:

这里是PHP-PayPal-IPN !这肯定会帮助你

【讨论】:

以上是关于paypal ipn micah 的 php 类的主要内容,如果未能解决你的问题,请参考以下文章

PHP Paypal IPN:交易未确认

在 PHP 中使用 IPN 的 Paypal 无法在新的 Beta 开发人员沙箱中运行

PayPal IPN PHP 握手错误

PayPal IPN 脚本未重定向到 ipn.php

与paypal IPN(PHP)相关的问题

PHP PayPal IPN代码段