我正在尝试使用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 : €25.00 EUR</option>
<option value="Per jaar">Per jaar : €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沙盒网址以进行尝试,但是当我这样做并付款时,数据库中没有任何更改,因为您可以看到IPN处理程序页面应该更改内容。
答案 0 :(得分:1)
这是一个空白页面,因为您的ipn侦听器是return
值,这是您在完成后发送的地方,但不会发送任何信息。 Paypal的回发将没有值,您的代码将失败
(from the docs - PayPal在完成付款后重定向买家浏览器的网址。例如,在您的网站上指定显示“感谢您付款”页面的网址。 )
<input type="hidden" name="return" value="http://www.mydomain.com/pp-ipn-handler.php">
您需要使用notify_url
(from the docs - PayPal以即时付款通知邮件的形式发布付款信息的网址。)
<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
&amp; $period
内的strtotime()
- $currentvalidity.$period
$newvalidity = date('m/d/Y h:i:s', strtotime("$currentvalidity $period"));
在线#91&amp; #111你需要连续$date
&amp; $period
内的strtotime()
- $date.$period
$newvalidity = date('m/d/Y h:i:s', strtotime("$date $period"));