在付款确认后,我如何使用Paypal销售某些东西并在SQL中更改某些内容?

时间:2022-10-14 16:18:18

I have a website I am making in PHP and I need to sell an online product. When someone buys this product I need a way to have my website notified and change some information in an SQL table. I have googled this for the past five hours and I read things about paypal IPN and paypal sandbox. I am still very confused about what paypal IPN is and how to use it. As for paypal sandbox, several tutorials for what I am asking say to use paypal sandbox. I have tried, but every time I try to login to my account it says the password is wrong, and when I try to make another it redirects me to paypal.com.

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

My main question is: How do I make buying something on paypal change information in an SQL table on my website?

我的主要问题是:如何在我网站上的SQL表格中购买paypal更改信息?


Update:

I have been trying to use this tutorial: http://www.evoluted.net/thinktank/web-development/paypal-php-integration

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

I have this php code here (modified from the tutorial):

我在这里有这个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','${1}%0D%0A${3}',$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);
    }
}
?>

The receiving part of this code works when I use IPN simulator in developer.paypal.com for paypal sandbox. The functions.php and the html code giving in the tutorial is the same as what is in the tutorial. When I test this I get the javascript alert boxes coming up and it all looks correct to me, but for some reason I am not getting a response after sending the paypal sandbox request.

当我在developer.paypal.com中使用IPN模拟器用于paypal沙箱时,此代码的接收部分可以正常工作。本教程中的functions.php和html代码与本教程中的内容相同。当我测试这个时,我会收到javascript警告框,这一切对我来说都是正确的,但由于某种原因,我在发送paypal沙盒请求后没有收到回复。

What am I doing wrong and how do I fix it? I have create a buyer with a normal paypal account in the sandbox and a business account.

我做错了什么,我该如何解决?我在沙盒和商业帐户中创建了一个普通paypal帐户的买家。

1 个解决方案

#1


After doing more research and searching I found a paypal page called Paypal Integration Wizard.

经过更多的研究和搜索,我发现了一个名为Paypal Integration Wizard的paypal页面。

This will generate the code needed for what I have asked. It gives a step by step process and is easy to follow. After reading the code I now understand more on how it works.

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

#1


After doing more research and searching I found a paypal page called Paypal Integration Wizard.

经过更多的研究和搜索,我发现了一个名为Paypal Integration Wizard的paypal页面。

This will generate the code needed for what I have asked. It gives a step by step process and is easy to follow. After reading the code I now understand more on how it works.

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