首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在付款确认后使用Paypal销售某些内容并在SQL中进行一些更改?

如何在付款确认后使用Paypal销售某些内容并在SQL中进行一些更改?
EN

Stack Overflow用户
提问于 2015-05-02 22:02:53
回答 1查看 419关注 0票数 0

我有一个网站,我正在用PHP和我需要销售一个在线产品。当有人购买此产品时,我需要一种方法来通知我的网站,并在SQL表中更改一些信息。我已经谷歌了这五个小时,我读到有关贝宝IPN和贝宝沙箱的东西。我仍然非常困惑什么贝宝IPN是和如何使用它。至于贝宝沙箱,几个教程,我要求说,使用贝宝沙箱。我试过了,但是每次我尝试登录到我的帐户时,它都会说密码是错误的,当我尝试做另一个密码时,它会将我重定向到paypal.com。

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

更新:

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

这里有这个php代码(从教程中修改):

代码语言:javascript
复制
    <?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);
    }
}
?>

当我在developer.paypal.com中为paypal沙箱使用IPN模拟器时,此代码的接收部分工作。本教程中给出的functions.php和html代码与本教程中的相同。当我测试它时,我会收到javascript警告框,这在我看来都是正确的,但是由于某种原因,在发送paypal沙箱请求之后,我没有得到响应。

我做错了什么?我该如何解决?我已经创建了一个买家与一个正常的贝宝帐户在沙箱和一个商业帐户。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-17 03:14:45

在做了更多的研究和搜索之后,我发现了一个名为Paypal集成向导的paypal页面。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30007986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档