paypal将用户数据存储到数据库中

时间:2012-12-27 13:01:13

标签: php paypal

我有一个帐户系统的网站。我在卖一些服务。我使用paypal动态订阅按钮进行销售。我的问题是我怎么知道,当从paypal发回IPN时,用户付款了什么?我有一个工作脚本,将付款信息发送到paypal和另一个从paypal接收信息并将其插入数据库的脚本,但我不知道如何知道用户发送了什么付款。我正在寻找如何实现这一目标的想法。

发送按钮:

<form name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr method="post">
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="business" value="user@example.com">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="no_shipping" value="1">
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="a3" value="<?php echo $pay_value; ?>">
<input type="hidden" name="p3" value="1">
<input type="hidden" name="t3" value="M">
<input type="hidden" name="src" value="1">
<input type="hidden" name="sra" value="1">
</form>

ipn script:

<?php

// tell PHP to log errors to ipn_errors.log in this directory
ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__).'/ipn_errors.log');

// intantiate the IPN listener
include('include/ipnlistener.php');
include('include/conn.php');
$listener = new IpnListener();

// tell the IPN listener to use the PayPal test sandbox
$listener->use_sandbox = true;

// try to process the IPN POST
try {
    $listener->requirePostMethod();
    $verified = $listener->processIpn();
} catch (Exception $e) {
    error_log($e->getMessage());
    exit(0);
}

if ($verified) {

    $errmsg = '';   // stores errors from fraud checks

    // Make sure the payment status is "Completed" 
    if ($_POST['payment_status'] != 'Completed') { 
        }
        exit(0); 
    }

     if ($_POST['txn_type'] == 'subscr_eot') {
            $link = mysql_connect($conn_host,$conn_user,$conn_pass) or die('Connection to mysql failed!');
            mysql_select_db($conn_db,$link) or die('Connection to database failed!');

            $sql = mysql_query("UPDATE user_data SET paid='0.00' WHERE uid='$userID'");

            mysql_close($link);

    if (!empty($errmsg)) {

        // manually investigate errors from the fraud checking
        $body = "IPN failed fraud checks: \n$errmsg\n\n";
        $body .= $listener->getTextReport();
        mail('user@example.com', 'IPN Fraud Warning', $body);

    } else {

         $link = mysql_connect($conn_host,$conn_user,$conn_pass) or die('Connection to mysql failed!');
         mysql_select_db($conn_db,$link) or die('Connection to database failed!');

         $sql = mysql_query("UPDATE user_data SET paid='$paid' WHERE uid='$userID'");

         mysql_close($link);
    }

} else {
    // manually investigate the invalid IPN
    mail('user@example.com', $listener->getTextReport());
}

?>

1 个答案:

答案 0 :(得分:0)

您可以将表单中的加密用户ID作为自定义变量

发送
<input type="hidden" name="custom" value="put user id here " />

IPN会向您发送此自定义代码,以便您可以更新数据库。

您将在IPN验证页面中使用$ _POST ['custom']获取值。

请在下面找到完整的变量集。

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  <input type="hidden" name="cmd" value="_xclick-subscriptions">
  <input type="hidden" name="business" value="nora@paypal.com">
  <input type="hidden" name="item_name" value="Baseball Hat Monthly">
  <input type="hidden" name="item_number" value="123">
  <input type="hidden" name="image_url" value="https://www.yoursite.com/logo.gif">
  <input type="hidden" name="no_shipping" value="1">
  <input type="hidden" name="return" value="http://www.yoursite.com/thankyou.htm">
  <input type="hidden" name="cancel_return" value="http://www.yoursite.com/cancel.htm">
  <input type="hidden" name="a1" value="0">
  <input type="hidden" name="p1" value="1">
  <input type="hidden" name="t1" value="W">
  <input type="hidden" name="a2" value="5.00">
  <input type="hidden" name="p2" value="2">
  <input type="hidden" name="t2" value="M">
  <input type="hidden" name="a3" value="50.00">
  <input type="hidden" name=”p3" value="1">
  <input type="hidden" name="t3" value="Y">
  <input type="hidden" name="src" value="1">
  <input type="hidden" name="sra" value="1">
  <input type="hidden" name="srt" value="5">
  <input type="hidden" name="no_note" value="1">
  <input type="hidden" name="custom" value="customcode">
  <input type="hidden" name="invoice" value="invoicenumber">
  <input type="hidden" name="usr_manage" value="1">
  <input type="image" src="http://images.paypal.com/images/x-click-but01.gif" border="0" name="submit" alt="Make payments with PayPal - it’s fast, free and secure!">

我希望这会有所帮助。

感谢。