PayPal IPN 帮助(不是更新数据库)

标签 paypal paypal-ipn

大家好,大家已经尝试让这个工作大约 2 天了。付款没有问题,但它没有更新数据库:(

代码如下:

<?php

require_once(WWW_DIR."/lib/users.php");


            // Connect to database
$con = mysql_connect("localhost","root","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// 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";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$txn_id = $_POST['txn_id'];
$payer_email = $_POST['payer_email'];
$name = $_POST['first_name'] . " " . $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$address_city = $_POST['address_city'];
$contact_phone = $_POST['contact_phone'];
$email = $_POST['payer_email'];
$uid = $users->currentUserId();
//timezone set
date_default_timezone_set('Europe/London');

if (!$fp)
{
    // HTTP ERROR
} else
{
    fputs($fp, $header . $req);
    while (!feof($fp))
    {
        $res = fgets($fp, 1024);
        if (strcmp($res, "VERIFIED") == 0)  {
if($payment_status != "Completed"){
                if($payment_amount == 2.00)
                {
                    $role = 6;
                }
                else
                if($payment_amount == 5.00)
                {
                    $role = 8;
                }
                else
                if($payment_amount == 8.00)
                {
                    $role = 9;
                }

                //update user records
$sql = sprintf("UPDATE users SET role = %d WHERE ID = %d", $role, $uid);
mysql_query($sql);  


 //log payment
            //mysql_query("INSERT INTO payments (email, item_name, payment_status, txn_id, payment_ammount) VALUES('" . mysql_escape_string($email) . "', '" . $item_name . "', '" . $payment_status . "', '" . $txn_id . "', '" . $payment_amount . "' ) ") or die(mysql_error());
      }} else
            if (strcmp($res, "INVALID") == 0)
            {
                // log for manual investigation
            }
    }
    fclose($fp);
} 
?>

如果你有任何问题尽管问我会尽快回答如果你能使这个工作我会很高兴

谢谢

最佳答案

改变:

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

收件人:

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";

改变:

if (strcmp($res, "VERIFIED") == 0)  {

收件人:

if (strcmp(trim($res), "VERIFIED") == 0)  {

最后,更改:

if (strcmp($res, "INVALID") == 0)

收件人:

if (strcmp(trim($res), "INVALID") == 0)

原因:
PayPal 沙箱配置为拒绝未指定主机 header 的 HTTP 1.0 请求,因此您当前的脚本永远无法正确验证。

关于PayPal IPN 帮助(不是更新数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825015/

相关文章:

paypal - 如果客户在完成付款后关闭浏览器,如何获得 PayPal PDT?

paypal - 'Review your information page' Paypal 页面上的送货/账单地址问题

php - 如何从 IPN 访问事务数组值

html - 下拉菜单中的样式悬停状态(PayPal)

php - 如何密码保护整个网站但允许公众访问 1 个特定文件

php - 是否可以使用任何 html 加密器来加密 paypal html 代码?

Paypal IPN 消息

PayPal Sandbox API SSL 握手错误 HTTPS 请求

paypal - 在 Paypal 中禁用项目数量和购物车

php - PHP 中的 Paypal Masspay API