确认付款后,如何使用Paypal出售东西并让SQL中的东西发生变化?

How do I use Paypal to sell something and have something in SQL change after the payment is confirmed?

我在 PHP 制作了一个网站,我需要销售在线产品。当有人购买此产品时,我需要一种方法来通知我的网站并更改 SQL table 中的一些信息。在过去的五个小时里,我用谷歌搜索了这个,并阅读了有关贝宝 IPN 和贝宝沙箱的内容。我仍然很困惑贝宝 IPN 是什么以及如何使用它。至于贝宝沙箱,我所要求的几个教程都说使用贝宝沙箱。我试过了,但每次我尝试登录我的帐户时,它都说密码错误,当我尝试重新登录时,它会将我重定向到 paypal.com.

我的主要问题是:如何在我的网站上的 SQL table 中使用 paypal 更改信息?


更新:

我一直在尝试使用这个教程:http://www.evoluted.net/thinktank/web-development/paypal-php-integration

我这里有这个 php 代码(根据教程修改):

    <?php // Database variables
$host = "localhost"; //database location
$user = "user"; //database username
$pass = "pass"; //database password
$db_name = "db"; //database name

// PayPal settings
$paypal_email = 'my sandbox business email is here';
$return_url = 'http://painlessnotes.com/';
$cancel_url = 'http://painlessnotes.com/';
$notify_url = 'http://painlessnotes.com/Paypal/payments.php';

$item_name = 'Test Item';
$item_amount = 10.00;

// Include Functions
include("functions.php");

//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){
    echo "<script type='text/javascript'>alert('send start');</script>";

    $querystring = "";

    // Firstly Append paypal account to querystring
    $querystring .= "?business=".urlencode($paypal_email)."&";

    // Append amount& currency (£) to quersytring so it cannot be edited in html

    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
    $querystring .= "item_name=".urlencode($item_name)."&";
    $querystring .= "amount=".urlencode($item_amount)."&";

    //loop for posted values and append to querystring
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $querystring .= "$key=$value&";
    }

    // Append paypal return addresses
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
    $querystring .= "notify_url=".urlencode($notify_url);

    // Append querystring with custom field
    //$querystring .= "&custom=".USERID;

    echo "<script type='text/javascript'>alert('{$querystring}');</script>";

    // Redirect to paypal IPN
    header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
    exit();

}else{
    // Response from Paypal
    mail("my email", "response", "TEST", "From: my email is here");//I am using this to check if it works
    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','%0D%0A',$value);// IPN fix
        $req .= "&$key=$value";
    }

    // assign posted variables to local variables
    $data['item_name']          = $_POST['item_name'];
    $data['item_number']        = $_POST['item_number'];
    $data['payment_status']     = $_POST['payment_status'];
    $data['payment_amount']     = $_POST['mc_gross'];
    $data['payment_currency']   = $_POST['mc_currency'];
    $data['txn_id']             = $_POST['txn_id'];
    $data['receiver_email']     = $_POST['receiver_email'];
    $data['payer_email']        = $_POST['payer_email'];
    $data['custom']             = $_POST['custom'];

    // 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";

    $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

    if (!$fp) {
        // HTTP ERROR
    } else {
                mail('my email', '0', '0');//used to check if it works
        fputs ($fp, $header . $req);
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp ($res, "VERIFIED") == 0) {

                // Validate payment (Check unique txnid & correct price)
                $valid_txnid = check_txnid($data['txn_id']);
                $valid_price = check_price($data['payment_amount'], $data['item_number']);
                // PAYMENT VALIDATED & VERIFIED!
                if($valid_txnid && $valid_price){
                    $orderid = updatePayments($data);
                    if($orderid){
                        // Payment has been made & successfully inserted into the Database
                    }else{
                        // Error inserting into DB
                        // E-mail admin or alert user
                    }
                }else{
                    // Payment made but data has been changed
                    // E-mail admin or alert user
                }

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

                // PAYMENT INVALID & INVESTIGATE MANUALY!
                // E-mail admin or alert user
            }
        }
    fclose ($fp);
    }
}
?>

当我在开发人员中使用 IPN 模拟器时,此代码的接收部分有效。paypal.com 用于 paypal 沙箱。 functions.php 和 html 教程中给出的代码与教程中的代码相同。当我对此进行测试时,出现了 javascript 警报框,这对我来说都是正确的,但由于某种原因,我在发送贝宝沙箱请求后没有收到回复。

我做错了什么,我该如何解决?我已经在沙盒中创建了一个拥有普通贝宝账户和企业账户的买家。

经过更多研究和搜索后,我找到了一个名为 Paypal Integration Wizard 的贝宝页面。

这将生成我所要求的代码。它提供了一个循序渐进的过程,并且易于遵循。阅读代码后,我现在对它的工作原理有了更多的了解。