473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

why doesn't my paypal ipn work ?

This script does not upload to the database, but it always says verified and pays ok through paypal. i have enabled ipn in sandbox, i think my payments.php page is wrong but not sure. I really dont know where to start, my connection to my database is correct and im using php 5

index.php....

Expand|Select|Wrap|Line Numbers
  1. <form id="contactForm" class="paypal" action="paypal/payments.php" method="post">
  2. <input name="cmd" type="hidden" value="_xclick" />
  3.  
  4.                                                                                     <input name="no_note" type="hidden" value="1" />
  5.  
  6.                                                                                     <input name="lc" type="hidden" value="UK" />
  7.  
  8.                                                                                     <input name="currency_code" type="hidden" value="GBP" />
  9.  
  10.                                                                                     <input name="bn" type="hidden" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" />
  11.  
  12.                                                                                     <input name="item_number" type="hidden" value="123456" />
  13.  
  14.                                                                                     <input type="submit"  value="Continue" />
  15.  
  16.                                                                                     <img style="padding-left: 10px; padding-top: 5px;" class="paypal_btn" alt="Pay with PayPal" 
  17.                                                                                   src="http://bytes.com/images/payWithPaypal.jpg" title="">
  18.  
payments.php.....

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Database variables
  4. $host = "*****"; //database location
  5. $user = "*****"; //database username
  6. $pass = "*****"; //database password
  7. $db_name = "*****"; //database name
  8.  
  9.  
  10.  
  11. // PayPal settings
  12. $paypal_email = 'sandboxpaypalemail';
  13. $return_url = 'http://www.mysite.info/i/payment-successful.php';
  14. $cancel_url = 'http://www.mysite.info/i/payment-cancelled.html';
  15. $notify_url = 'http://www.mysite.info/i/paypal/payments.php';
  16.  
  17. $item_name = 'Test Item';
  18.  
  19. // Payment Type
  20. $PaymentType = $_POST['paymentAmount'];           
  21.  
  22.  
  23. $item_amount = $PaymentType;
  24.  
  25.  
  26.  
  27. //Database Connection
  28. $link = mysql_connect($host, $user, $pass);
  29. mysql_select_db($db_name);
  30.  
  31. // Include Functions
  32. include("functions.php");
  33.  
  34.  
  35.  
  36.            // Check if paypal request or response
  37. if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){
  38.  
  39.     // Firstly Append paypal account to querystring
  40.     $querystring .= "?business=".urlencode($paypal_email)."&";    
  41.  
  42.     // Append amount& currency (£) to quersytring so it cannot be edited in html
  43.  
  44.     //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
  45.     $querystring .= "item_name=".urlencode($item_name)."&";
  46.     $querystring .= "amount=".urlencode($item_amount)."&";
  47.  
  48.     //loop for posted values and append to querystring
  49.     foreach($_POST as $key => $value){
  50.         $value = urlencode(stripslashes($value));
  51.         $querystring .= "$key=$value&";
  52.     }
  53.  
  54.     // Append paypal return addresses
  55.     $querystring .= "return=".urlencode(stripslashes($return_url))."&";
  56.     $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
  57.     $querystring .= "notify_url=".urlencode($notify_url);
  58.  
  59.     // Append querystring with custom field
  60.     //$querystring .= "&custom=".USERID;
  61.  
  62.     // Redirect to paypal IPN
  63.     header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
  64.     exit();
  65.  
  66. }else{
  67.  
  68.     // Response from Paypal
  69.  
  70.     // read the post from PayPal system and add 'cmd'
  71.     $req = 'cmd=_notify-validate';
  72.     foreach ($_POST as $key => $value) {
  73.         $value = urlencode(stripslashes($value));
  74.         $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
  75.         $req .= "&$key=$value";
  76.     }
  77.  
  78.     // post back to PayPal system to validate
  79.     $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
  80.     $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  81.     $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
  82.  
  83.     $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);    
  84.  
  85.     // assign posted variables to local variables
  86.     $data['item_name']            = $_POST['item_name'];
  87.     $data['item_number']         = $_POST['item_number'];
  88.     $data['payment_status']     = $_POST['payment_status'];
  89.     $data['payment_amount']     = $_POST['mc_gross'];
  90.     $data['payment_currency']    = $_POST['mc_currency'];
  91.     $data['txn_id']                = $_POST['txn_id'];
  92.     $data['receiver_email']     = $_POST['receiver_email'];
  93.     $data['payer_email']         = $_POST['payer_email'];
  94.    /* $data['custom']             = $_POST['custom']; */
  95.  
  96.  
  97.  
  98.     if (!$fp) {
  99.         // HTTP ERROR
  100.      echo "http error";// HTTP ERROR
  101.     } else {    
  102.  
  103.         fputs ($fp, $header . $req);
  104.         while (!feof($fp)) {
  105.             $res = fgets ($fp, 1024);
  106.             if (strcmp($res, "VERIFIED") == 0) {
  107.  
  108.                 // Used for debugging
  109.                 @mail("myemail.co.uk", "PAYPAL DEBUGGING", "Verified Response<br />data = <pre>".print_r($post, true)."</pre>");
  110.  
  111.                 // Validate payment (Check unique txnid & correct price)
  112.                 $valid_txnid = check_txnid($data['txn_id']);
  113.                 $valid_price = check_price($data['payment_amount'], $data['item_number']);
  114.                 // PAYMENT VALIDATED & VERIFIED!
  115.                 if($valid_txnid && $valid_price){                
  116.                     $orderid = updatePayments($data);        
  117.                     if($orderid){                    
  118.                         // Payment has been made & successfully inserted into the Database                                
  119.                     }else{  
  120.  
  121.                     @mail("myemail@hotmail.co.uk", "PAYPAL DEBUGGING", "Verified Response<br />data = <pre>".print_r($post, true)."</pre>");
  122.  
  123.                         // Error inserting into DB
  124.                         // E-mail admin or alert user
  125.                     }
  126.                 }else{                    
  127.                     // Payment made but data has been changed
  128.                     // E-mail admin or alert user
  129.                 }                        
  130.  
  131.             }else if (strcmp ($res, "INVALID") == 0) {
  132.  
  133.                 // PAYMENT INVALID & INVESTIGATE MANUALY! 
  134.                 // E-mail admin or alert user
  135.  
  136.                 // Used for debugging
  137.                 @mail("myemail.com", "PAYPAL DEBUGGING", "Invalid Response<br />data = <pre>".print_r($post, true)."</pre>");
  138.             }        
  139.         }        
  140.     fclose ($fp);
  141.     }    
  142. }
  143. ?>
  144.  
  145.  
functions.php....

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.   // functions.php    // table customerdetails
  4. function check_txnid($tnxid){
  5.     global $link;
  6.     return true;
  7.     $valid_txnid = true;
  8.     //get result set
  9.     $sql = mysql_query("SELECT * FROM payments WHERE txnid = '$tnxid'", $link)or die(mysql_error());
  10.     if($row = mysql_fetch_array($sql)) {
  11.         $valid_txnid = false;
  12.     }
  13.     return $valid_txnid;
  14. }
  15.  
  16. function check_price($price, $id){
  17.     $valid_price = false;
  18.  
  19.     return true;
  20. }
  21.  
  22. function updatePayments($data){
  23.     global $link;
  24.     if(is_array($data)){
  25.         $sql = mysql_query("INSERT INTO payments (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
  26.                 '".$data['txn_id']."' ,
  27.                 '".$data['payment_amount']."' ,
  28.                 '".$data['payment_status']."' ,
  29.                 '".$data['item_number']."' ,
  30.                 '".date("Y-m-d H:i:s")."'
  31.                 )", $link)or die(mysql_error());
  32.     return mysql_insert_id($link)or die(mysql_error());;
  33.     }
  34. }
  35.  
  36. ?>
  37.  
  38.  
Aug 18 '11 #1
0 1542

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chewy509 | last post by:
Hi Everyone, I'll just start, and say I am not a PHP developer (I'm a sysadmin, who has gotten lumped with a non-working website). But since I like to do this type of stuff, I though I might...
0
by: emes | last post by:
hi all, i'm developing an application in gtk (+glade). i have to intercept any modification of gtk.TextView widget contents. using glade, i connected following signals to callback method: ...
4
by: Chris Lount | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm pretty new to c++ . I'm trying to work out why the following code doesn't work. I've just learned about cin.get() and written the following...
3
by: OM | last post by:
Why doesn't a onmouseover function work in a function? (It's prob due to my code being wrong more than anything else!) I've got the following code (snippet): <!-- Begin var image0 = new...
6
by: JustSomeGuy | last post by:
unsigned short x; ifstream cin; // opened in binary mode. cin >> x; // Doesn't work. yet cin.read((char *) &x, sizeof(x)); works...
3
by: Iver Erling Årva | last post by:
Can anyone please tell me why this doesn't work? The sign changes when I hit the button, and I get no error messages, but the textarea doesn't disappear/reappear. <html> <head> <title>New...
10
by: Brett | last post by:
This code is supposed to work in Netscape 4+ and IE 4+. It works fine in IE but in Netscape 7.2, I get a blank page. Any suggestions? Thanks, Brett <html> <head>
3
by: MeNotHome | last post by:
I am trying to automate web browser navigation and form fill out in vb.net Why doesn't this work? AxWebBrowser1.Document.Forms(0).All("action-download").click() I also tried...
3
by: Joey | last post by:
I am working on an asp.net 1.1 web app in C#. I downloaded some sample code that is supposed to allow for me to persist session state. The code is as follows: private void PersistSessionState()...
7
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/xml.html The first alert() shows the XML that the server is returning. The second alert() shows a particular elements nodeValue and, as you can see,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.