为什么这个Paypal IPN脚本没有完全执行?

时间:2012-05-15 17:32:17

标签: php paypal paypal-ipn

我有以下IPN脚本,我用它来处理“立即购买”按钮的付款。当我在Paypal Sandbox中运行脚本时,它会在正确接收时返回。我收到一封电子邮件,其中列出了所有已发布的变量(此时仅用于调试),但是它在我在DB中创建的IPN错误表中发布了一个条目,错误号为0且没有错误消息。似乎没有其他代码被执行(例如调用process_payment函数)。我已经运行了它自己的流程支付功能,它运行正常,我知道解析到它的变量也是正确的。脚本如下所示:

<?php
class Paypal extends Controller
{
    public function __construct()
    {
        // Load the Registration Model
        $this->load_model('Registration');

        // Load the Error Model
        $this->load_model('Ipn_Error');
    }

    public function process_payment($Post)
    {
        if (!empty($Post['custom']))
        {
            // This is the User ID
            $User_ID = $Post['custom'];

            // Check that the User Exists
            if ($this->registration->check_user_by_id($User_ID))
            {
                if (!empty($Post['item_number']))
                {
                    if ($Post['item_number'] == 'M') {
                        // Set the Package Type
                        $Package_Type = 'M';

                        // Process the Payment
                        $this->registration->process_initial_payment($User_ID, $Package_Type);
                    }
                    else if ($Post['item_number'] == 'Y') {
                        // Set the Package Type
                        $Package_Type = 'Y';

                        // Process the Payment
                        $this->registration->process_initial_payment($User_ID, $Package_Type);
                    }
                }
            }
        }
    }

    public function index()
    {
        // First, check that we're dealing with a Posted Request
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            // Create a Blank Header
            $Header = '';

            // Create a Request String
            $Request_String = 'cmd=_notify-validate';

            $eml = "";

            // Loop through and add the Variables
            foreach($_POST as $Key=>$Value)
            {
                $eml .= "$Key : $Value \n";

                // URL Encode the Value
                $Value = urlencode($Value);

                // Add it to the Request String
                $Request_String .= '&'.$Key.'='.$Value;
            }

            mail('me@andypopsmedia.com', 'RESPONSE', $eml);

            // Post back to PayPal to validate 
            $Header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; 
            $Header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
            $Header .= "Content-Length: ".strlen($Request_String)."\r\n\r\n"; 

            // Use a Socket Connect to Send Data back to Paypal
            $FP = fsockopen('ssl://'.paypal_ipn, 80, $Error_No, $Error_Str, 30);

            // Begin Handling the Transaction Stuff
            if (!$FP)
            {
                // HTTP ERROR
                // HANDLE WITH CARE
                $this->ipn_error->log_error($Error_No, $Error_Str);
            }
            else
            {
                // Post the Data back
                fwrite($FP, $Header.$Request_String);

                $this->ipn_error->log_error(3, 'Response Obtained');

                // Get the Response and Handle it
                while (!feof($FP))
                {
                    // Get the Result String
                    $Result_String = fgets($FP, 1024);

                    $this->ipn_error->log_error(3, $Result_String);

                    // Check if it was Successful
                    if (strcmp($Result_String, 'VERIFIED') == 0)
                    {
                        // Check the Transaction Type
                        switch ($_POST['txn_type'])
                        {
                            case ('web_accept'):
                                $this->ipn_error->log_error(123, 'Transaction done.');
                                $this->process_payment($_POST);
                            break;
                        }
                    }
                    else
                    {
                        // VERIFICATION ERROR
                        // HANDLE WITH CARE
                        mail('me@andypopsmedia.com', 'RESPONSE FAILED', 'it did not work');
                    }
                }
            }
        }
        // ELSE NOT POSTED
        else
        {
            echo 'the file works';
            //$this->registration->process_initial_payment(1, 'M');
            $this->ipn_error->log_error(123, 'File Accessed');
        }
    }
};
?>

0 个答案:

没有答案