2

我正在尝试使用 Paypal 和他们的 IPN 系统设置订阅几天。我无法理解大多数教程,最后找到了一个对我有意义的教程。

在 Sean 的帮助下,我稍微更改了代码,但仍然没有运气。

要创建订阅按钮,我使用以下代码:

<form action="https://sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="business" value="info@mydomain.com">
<input type="hidden" name="lc" value="NL">
<input type="hidden" name="item_name" value="Mydomain.com Abonnement">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="notify_url" value="http://www.mydomain.com/pp-ipn-handler.php">
<input type="hidden" name="return" value="http://www.mydomain.com/ppsuccess.php">
<input type="hidden" name="src" value="1">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="bn" value="PP-SubscriptionsBF:btn_subscribeCC_LG.gif:NonHosted">
<input type="hidden" name="custom" value="'. $username .'">
<table>
<tr><td><input type="hidden" name="on0" value="Betaalperiode"><strong>Kies uw betaalperiode:</strong></td></tr><tr><td><select name="os0">
    <option value="Per maand">Per maand : &euro;25.00 EUR</option>
    <option value="Per jaar">Per jaar : &euro;240.00 EUR</option>
</select> </td></tr>
</table>
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="option_select0" value="Per maand">
<input type="hidden" name="option_amount0" value="25.00">
<input type="hidden" name="option_period0" value="M">
<input type="hidden" name="option_frequency0" value="1">
<input type="hidden" name="option_select1" value="Per jaar">
<input type="hidden" name="option_amount1" value="240.00">
<input type="hidden" name="option_period1" value="Y">
<input type="hidden" name="option_frequency1" value="1">
<input type="hidden" name="option_index" value="0">
<input type="image" src="https://www.paypalobjects.com/nl_NL/NL/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="PayPal, de veilige en complete manier van online betalen.">
</form>

为了处理 ipn 响应,我有一个名为 pp-ipn-handler.php 的文件,其中包含以下代码:

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system 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($req) . "\r\n\r\n";
// assign posted variables to local variables
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
// $payment_currency = $_POST['mc_currency'];
$payment_currency = 'EUR';
$ppusername = $_POST['custom'];
$fp = fsockopen ('sandbox.paypal.com', 80, $errno, $errstr, 30);
if (!$fp)
{
}
else
{
fputs ($fp, $header . $req);
while (!feof($fp))
{
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0)
{
// When a payment has gone trough
    //When payment is 25EUR make it a month validity
    if ($ppusername&&($payment_currency=='EUR')&&($payment_amount=='25'))
    {
        $period = '+1 month';       
    }
    //When payment is 240EUR make it a year validity
    else if ($ppusername&&($payment_currency=='EUR')&&($payment_amount=='25'))
    {
        $period = '+1 year';        
    }
    //There is incorrect information in this payment
    else
    {

    }


    //Lets first get the date in mysql format
    $date = date('m/d/Y h:i:s', time());
    $date = date('m/d/Y h:i:s', strtotime("$date -7 hours"));
    //Now lets update the subscription records
    require_once('core/dbconnect.php');
    $subscriptionrecordexists = mysql_query("SELECT * FROM subscriptions WHERE username='$ppusername'");
    if (mysql_num_rows($subscriptionrecordexists))
    {
        //User has purchase before. Adjust his subscription record and ad an invoice.
        require_once('core/dbconnect.php');
        $subscriptionrecord = mysql_query("SELECT * FROM subscriptions WHERE username='$ppusername'");
        $row = mysql_fetch_array($subscriptionrecord);
        //Update subscription record
        $currentvalidity = $row['validity'];
        if ($currentvalidity >= $date)
        {
            //Add period if subscription is still valid
            $newvalidity = date('m/d/Y h:i:s', strtotime("$currentvalidity $period"));
            require_once('core/dbconnect.php');
            mysql_query("UPDATE subscriptions SET validity='$newvalidity' WHERE username='$ppusername'");
            //Add invoice to user
            if ($period == '+1 month')
            {$product = 'NLTVMee.com voor 1 maand';}
            if ($period == '+1 year')
            {$product = 'NLTVMee.com voor 1 jaar';}
            $paymentmethode = 'Paypal';
            $oldvalid = $currentvalidity;
            $newvalid = $newvalidity;
            require_once('core/dbconnect.php');
            mysql_query("INSERT INTO invoices VALUES ('','$date','$ppusername','$product','$paymentmethode','$payment_currency','$payment_amount','$oldvalid','$newvalid')");
        }
        else
        {
            //Add period from current date if validity is not valid anymore
            $newvalidity = date('m/d/Y h:i:s', strtotime("$date $period"));
            require_once('core/dbconnect.php');
            mysql_query("UPDATE subscriptions SET validity='$newvalidity' WHERE username='$ppusername'");
            //Ad invoice to user
            if ($period == '+1 month')
            {$product = 'NLTVMee.com voor 1 maand';}
            if ($period == '+1 year')
            {$product = 'NLTVMee.com voor 1 jaar';}
            $paymentmethode = 'Paypal';
            $newvalid = $newvalidity;
            require_once('core/dbconnect.php');
            mysql_query("INSERT INTO invoices VALUES ('','$date','$ppusername','$product','$paymentmethode','$payment_currency','$payment_amount','','$newvalid')");
        }
    }
    else
    {
        //User has never purchased before. Make new subscription record and add invoice.
        //Add period from current date if validity is not valid anymore
        $newvalidity = date('m/d/Y h:i:s', strtotime("$date $period"));
        require_once('core/dbconnect.php');
        mysql_query("INSERT INTO subscriptions VALUES ('','$ppusername','$newvalidity')");
        //Ad invoice to user
        if ($period == '+1 month')
        {$product = 'NLTVMee.com voor 1 maand';}
        if ($period == '+1 year')
        {$product = 'NLTVMee.com voor 1 jaar';}
        $paymentmethode = 'Paypal';
        $newvalid = $newvalidity;
        require_once('core/dbconnect.php');
        mysql_query("INSERT INTO invoices VALUES ('','$date','$ppusername','$product','$paymentmethode','$payment_currency','$payment_amount','','$newvalid')");
    }

}
//End
else if (strcmp ($res, "INVALID") == 0)
{

}
}
fclose ($fp);
}
?>

正如你所看到的,它现在配置了 Paypal 沙箱 URL 来试用它,但是当我这样做并付款时,数据库中没有任何变化,正如你所见,IPN 处理程序页面应该会改变一些事情。

4

1 回答 1

1

这是一个空白页面,因为您将 ipn 侦听器作为您的return值,这是完成后发送给您的地方,但没有发送任何信息。回发到 Paypal 将没有任何值,您的代码将失败。
来自文档-买家完成付款后,PayPal 将他们的浏览器重定向到的 URL。例如,在您的网站上指定一个显示“感谢您的付款”页面的 URL。

<input type="hidden" name="return" value="http://www.mydomain.com/pp-ipn-handler.php">

您需要使用 (来自文档- PayPal 以即时付款通知消息的形式发布有关付款信息的 URL。notify_url

<input type="hidden" name="notify_url" value="http://www.mydomain.com/pp-ipn-handler.php">
<input type="hidden" name="return" value="http://www.mydomain.com/payment_complete_thank_message.php">

编辑
我在代码中看到 3 个额外的错误

您在第 65 行的查询是$subscriptionrecord,但在第 66 行您正在使用$userinfo

    $subscriptionrecord = mysql_query("SELECT * FROM subscriptions WHERE username='$ppusername'");
    $row = mysql_fetch_array($userinfo);

更正
以下这些不正确,因为将它们括在引号"$currentvalidity $period"中与$currentvalidity.$period

在第 72 行,你需要在里面连接$currentvalidity&$periodstrtotime()$currentvalidity.$period

$newvalidity = date('m/d/Y h:i:s', strtotime("$currentvalidity $period"));

在第 #91 和 #111 行,你需要在里面连接$date-$periodstrtotime()$date.$period

$newvalidity = date('m/d/Y h:i:s', strtotime("$date $period"));
于 2013-03-17T23:55:38.373 回答