Paypal とその IPN システムを使用してサブスクリプションをセットアップしようとしています。ほとんどのチュートリアルを理解できませんでしたが、最終的に自分にとって意味のあるチュートリアルを見つけました。
ここでショーンの助けを借りて、コードを少し変更しましたが、まだうまくいきません。
サブスクリプション ボタンを作成するには、次のコードを使用します。
<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 サンドボックスの URL が構成されていますが、支払いを行ってもデータベース内で何も変更されておらず、IPN ハンドラー ページが変更されていることがわかります。