贝宝 IPN 订阅 (php)
Posted
技术标签:
【中文标题】贝宝 IPN 订阅 (php)【英文标题】:Paypal IPN subscription (php) 【发布时间】:2013-03-06 05:54:15 【问题描述】:我几天来一直在尝试使用 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" >
</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 处理程序页面应该会发生变化。
【问题讨论】:
我忘了补充,这两部分代码也在两个不同的页面上。 【参考方案1】:这是一个空白页面,因为您将 ipn 侦听器作为您的 return
值,这是您在完成后发送的位置,但没有发送任何信息。回发到 Paypal 将没有值,您的代码将失败。
(from the docs - 买家完成付款后,PayPal 将他们的浏览器重定向到的 URL。例如,在您的网站上指定一个 URL,显示“感谢您的付款”页面。)
<input type="hidden" name="return" value="http://www.mydomain.com/pp-ipn-handler.php">
您需要使用notify_url
(from the docs - PayPal 以即时付款通知消息的形式发布付款信息的 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
和 $period
连接到 strtotime()
- $currentvalidity.$period
内
$newvalidity = date('m/d/Y h:i:s', strtotime("$currentvalidity $period"));
在第 #91 和 #111 行,您需要将 $date
和 $period
连接到 strtotime()
- $date.$period
中
$newvalidity = date('m/d/Y h:i:s', strtotime("$date $period"));
【讨论】:
还是没有运气。我已将返回值更改为成功 url,然后添加 notify_url 值以转到 ipn 文件,如前所述。它现在按预期转到成功 url,但 ipn 处理程序仍然不起作用,因为我没有将记录添加到数据库中,所以那里仍然有问题。 这让我朝着正确的方向前进,但显然这是我需要添加的内容。能否请您查看 ipn 处理程序代码,看看有什么问题,看看为什么它不响应 Paypal 提供的信息。 您可以做的第一件事是将header('Location: ppcommerror.php');
和header('Location: ppsuccess.php');
更改为mail(...)
或error_handler
,因为此页面/脚本将在后端运行并且没有用户会看到它。这与我所做的类似。我会看一下,但根据这里的内容,我可能不会发现任何其他错误。
我现在已经删除了重新路由 (header('Location...) 行。但这仍然没有帮助。感谢您再看一看。
我在代码中发现了另外 3 个问题。我已在 Edit 下将它们添加到我的答案中以上是关于贝宝 IPN 订阅 (php)的主要内容,如果未能解决你的问题,请参考以下文章