将paypal ipn数据保存到数据库中

时间:2013-12-20 10:10:30

标签: php mysql paypal-ipn cpanel

我尝试过不同的脚本,但我似乎遇到了paypal ipn的问题。我想要的只是将返回的数据保存到数据库中,这是我尝试过的第二个脚本。我似乎遇到的另一个问题是如何访问主机服务器我正在使用cpanelx.how我用paypal连接主机ipn.below是我用过的代码。

paypal ipn code

<?php  

    $username = "echi_user";
    $password = "lollipop" ;
    $database = "echi_users";
    $server = "localhost";

    mysql_connect($server,$username,$password) or die(mysql_error());  
    mysql_select_db($database) or die(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 ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);  
    if (!$fp) {  
    // HTTP ERROR  
    } else {  
    fputs ($fp, $header . $req);  
    while (!feof($fp)) {  
    $res = fgets ($fp, 1024);  
    if (strcmp ($res, "VERIFIED") == 0) {  

    // PAYMENT VALIDATED & VERIFIED!  
    $email = $_POST['payer_email']; 
    $gross = $_POST['mc_gross'];  
    $name = $_POST['address_name']; 
    $pupilname = $_POST['item_name']; 
    $pupilno = $_POST['item_number']; 

    mysql_query("INSERT INTO sponsors (email,gross,name,pupilname,pupilno) VALUES('". mysql_real_escape_string($email) ."','". mysql_real_escape_string($gross) ."', '". mysql_real_escape_string($name) ."','". mysql_real_escape_string($pupilname) ."','". mysql_real_escape_string($pupilno) ."' ) ") or die(mysql_error());  
    }  

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

    // PAYMENT INVALID & INVESTIGATE MANUALY!  

    }  
    }  
    fclose ($fp);  
    }  
    ?>  

0 个答案:

没有答案