Paypal PHP is not working. No response received from Sandbox; only Connection: close

I have been with him for 24 hours and am going to pull out my hair. I've checked all over the internet and Stackoverflow and can't find anything to help. I even checked previous posts such as Not getting a response from Paypal IPN IP camera , but the responses for me are not working. I may have missed a post to help me; if I have, please excuse me for repeating the same question, but this question is quite urgent and I am at the end of my rope.

In any case, the problem is this:

I have a PHP cart and it sends customers to Paypal. Everything works perfectly. I built it following Paypal's instructions when it comes to sending them all codes. Everything is fine except when it comes to the last step, return POST

from Paypal back to my server to check everything OK. Even there I copied all the Paypal code as it is and just added functions that will check for all matches from my side. However, I cannot get it to work. I am trying to send the results to the DB and nothing happens. So I put a PHP command Mail

where the last action should be if everything is OK and I don't receive an E-Mail. I put this same command Mail

almost everywhere to try and figure out what's going on and got different clues from different places.

Firstly, when I call to print in E-Mail, you can send Paypal to my server, I receive an E-Mail with all vars except for item_name

and item_number

, which I receive blank; this is weird because up to this point, the Paypal sandbox shows me every product I supposedly buy. I tried to give these vars to PHP code a fixed value in light of the fact that Paypal does not post a value for them, at least at this point. I wanted to see if these errors were caused by the absence of these two values; the error persists, however, even with the set values.

Second, there is a loop while

in Paypal's code (a loop where the answer in the link above says to be replaced with $res=stream_get_contents($fp, 1024);

, however that didn't work for me either), so I put the command there Mail

to see what I got. I asked to print $res

var and payment_status

and received as expected several E-Mails with the following collected results:

    payment_status = Pending
    $res = HTTP/1.0 302 Found
           Location: https://www.sandbox.paypal.com
           Server: BigIP
           Connection: close
           Content-Length: 0

      

So my guess is that the error is happening at this point. Something happens (or doesn't happen) that not only does not return a status Completed

for payment_status

, which is looking for code to continue checking, etc., but it doesn't even return a response INVALID

. So I get no response and show nothing if everything is ok or not. And, strangely, and even worse, the fact that the Sandbox is still fake, accusing me of each of these payment attempts, even though it's clear that there is an error somewhere and the process is not running. (This, of course, creates another dilemma: once this is all resolved, if at some point a server error occurs due to X, Paypal will still charge my client, and I don't even know if anyone bought what what's on my site?)

Well, I suppose as far as I can explain about this. At the moment I do not see where I could go wrong. The code should make this a fairly straightforward task. So I really don't understand how I could mess up the copy and paste. I guess at least there should be no errors if they don't come with the source code. Moreover, since everything else up to this point works 100% perfectly, the only place this error could be is in this PHP file that PayPal should call as soon as the payment process is completed by the user.

ANY idea on this matter would be much appreciated!

Here is the code.

PHP CODE:

    include('../inc/db_fns.inc'); //<--All my DB work is in here.
    include('../inc/shipping.inc');

    $paypal_email = "seller_1360198925_biz@hotmail.com";
    $paypal_currency = 'USD';

    //Here begin the functions I'm using to check everything and pass data to DB.
    function no_paypal_trans_id($trans_id) {
      $connection = db_connect();
      $query = sprintf("SELECT id FROM orders WHERE paypal_trans_id = '%s'", mysql_real_escape_string($trans_id));
      $result = mysql_query($query);
      $num_results = mysql_num_rows($result);
      if($num_results == 0) {
        return true; 
      }
      return false;
    }

    function payment_amount_correct($shipping, $params) {
      $amount = 0.00;   
      for ($i=1;  $i <= $params['num_cart_items']; $i++) {
        $query = sprintf("SELECT precio FROM products WHERE id='%s'", mysql_real_escape_string($params["item_number{$i}"]));
        $result = mysql_query($query);
        if($result) {
          $item_price = mysql_result($result, 0, 'precio');
          $amount += $item_price * $params["quantity{$i}"];
        }
      }
      if(($amount+$shipping) == $params['mc_gross']) {
        return true;    
      } else {
        return false;   
      }
    }

    function create_order($params) {
      db_connect();
      $query =  sprintf("INSERT INTO orders set orders.firstname = '%s', orders.lastname = '%s', orders.email = '%s', orders.country = '%s', orders.address = '%s', orders.city = '%s', orders.zip_code = '%s', orders.state = '%s', orders.status = '%s', orders.amount = '%s', orders.paypal_trans_id = '%s', created_at = NOW()", mysql_real_escape_string($params['first_name']), mysql_real_escape_string($params['last_name']), mysql_real_escape_string($params['payer_email']), mysql_real_escape_string($params['address_country']), mysql_real_escape_string($params['address_street']), mysql_real_escape_string($params['address_city']), mysql_real_escape_string($params['address_zip']), mysql_real_escape_string($params['address_state']), mysql_real_escape_string($params['payment_status']), mysql_real_escape_string($params['mc_gross']), mysql_real_escape_string($params['txn_id']));
      $result = mysql_query($query);
      if(!$result) {
        return false;
      }
      $order_id = mysql_insert_id();
      for ($i=1;  $i <= $params['num_cart_items'] ; $i++) {
        $product = find_product($params["item_number{$i}"]);
        $query = sprintf("INSERT INTO items set order_id = '%s', product_id = '%s', title = '%s', price = '%s', qty = '%s'", mysql_real_escape_string($order_id), mysql_real_escape_string($product['id']), mysql_real_escape_string($product['title']), mysql_real_escape_string($product['price']), mysql_real_escape_string($params["quantity{$i}"]));
        $result = mysql_query($query);
        if(!$result) {
          return false; 
        }
      }
      return true;
    }   

    //Here begins the Paypal code as is
    // 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); //<--Here I also tried out the other response to the question linked above and it actually returned a 'Invalid host' into he $res var-

    // assign posted variables to local variables
    $item_name = $_POST['item_name'];    //<--These are the two vars for which
    $item_number = $_POST['item_number'];//   Paypal Sandbox posts no value.
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];

    if (!$fp) {
      // HTTP ERROR
    } else {
      fputs ($fp, $header . $req);
      while (!feof($fp)) {
        $res = fgets ($fp, 1024);
        if (strcmp ($res, "VERIFIED") == 0) {
        if ($_POST['payment_status'] == 'Completed' && no_paypal_trans_id($_POST['txn_id']) && $paypal_email == $_POST['receiver_email'] && $paypal_currency == $_POST['mc_currency'] && payment_amount_correct($shipping, $_POST)) {
            // process payment
            create_order($_POST);
          }
        } else if (strcmp ($res, "INVALID") == 0) {
          // log for manual investigation
        }
      }
      fclose ($fp);
    }

      

EDIT 1: Give one more move: no luck!

I just tried the original, untouched Paypal code. I just copied and pasted it into this PHP and then started buying Sandbox. The same result. No answer, no data inserted into tables, no way I know, someone actually bought something from my cart, but Paypal is still fake - charges the purchase. This confirms my suspicion that the error occurs in the original code and has nothing to do with the features I added. Hope someone can figure it out soon. You thank this person in advance!

EDIT 2: After the 1st answer.

The kind user who has now deleted their answer was kind enough to give it a first try. He / she recommended that Paypal documentation mention https://www.sandbox.paypal.com/cgi-bin/webscr

where I have http://www.sandbox.paypal.com

. Maybe I have an older version of the code, so I tried his / her recommendation. Now I will comment on what happened:

First of all, thanks a lot for your answer. Unfortunately, he was out of luck. Substituting one address for another, now the process does not fall into the loop while

, it stops at fsockopen

. I have placed the command Mail

for printing $errno

and $errstr

contained in this command. With https://www.sandbox.paypal.com/cgi-bin/webscr

substituting http://www.sandbox.paypal.com

I get Unable to find socket transfer "https" - did you forget to enable it when configuring PHP? ... So I stripped the "https: //" and tried again. Now I am getting php_network_getaddresses: getaddrinfo failed: name or service unknown . Any ideas?

EDIT 3: After my conversation with @hexacyanide in the comment section:

    $req = cmd=_notify-validate&test_ipn=1&payment_type=instant&payment_date=20%3A55%3A22+Feb+08%2C+2013+PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123%2C+any+street&receiver_email=seller%40paypalsandbox.com&receiver_id=TESTSELLERID1&residence_country=US&item_name1=something&item_number1=AK-1234&quantity1=1&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross_1=9.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=2229455&notify_version=2.4&custom=xyz123&invoice=abc1234&charset=windows-1252&verify_sign=AsdNkKD2ktCz.aUB.9WYWy-g8MHoAa-TsvSjUgGstseJVdUhQTq3aCwW

      

Sorry it was $req

, not $res

var. $res

var is returned by an outline while

. I put a command there Mail

and I get multiple emails with values $res

. It:

    X-Frame-Options: SAMEORIGIN
    HTTP/1.0 200 OK
    Strict-Transport-Security: max-age=14400
    INVALID 
    Content-Type: text/html; charset=UTF-8
    Date: Sat, 09 Feb 2013 05:44:33 GMT
    Connection: close
    Content-Length: 7

      

EDIT 4: Absolutely stunned: Even the simple IPN Paypal code straight from the Paypal developer website and tested with the Paypal developer website. IPN tester is working ...

  • I used the Paynal IPN script generator found here .
  • I decided to create a script that would send me a VALID or INVALID response.
  • I copied it in its entirety and pasted it into a PHP file that I saved on my server.
  • I checked and checked again that some of the code was not left over.
  • I targeted this PHP file on my server using the IPN simulator I found here .
  • The simulator shows the receipt and says: "IPN was successfully sent".
  • I go to check the mail to see what answer I had, whether it was VALID or INVALID ... I didn't get a response at all. So the IPN reaches the PHP purchase, at some point the process is interrupted, so it doesn't even hit the VALID / INVALID functions. However, this is the same code that Paypal gives you and is tested with the very sim he designed for this. So what's going on?

DAY 2 THIS ODYSSEY

OK It goes without saying: no luck yet. But now I decided to exhaustively track everything that goes into PHP code and Paypal. The idea is that the response VALID

occurs when this code sends back to Paypal those initial values โ€‹โ€‹that Paypal inserted into it and in the same order (this is according to Paypal's documentation).

The first problem is that I didn't get any response from the code. Therefore, there was an assumption that the code did not work at the very beginning. It so happened that the first problem was that the Sandbox links were not working. (I got to this point with the help of @hexacyanide, who very kindly followed up yesterday with my progress or lack thereof. Whenever I get enough reputation to positively mark his answer, I will.) So I pulled myself off sandbox.

and, voilร  , it was returning an answer, but now the answer was INVALID

.

From that point on, I realized that the IPN is only for merchant-side transaction verification and data logging. Paypal doesn't care about IPN results. So my shopping cart is working 100%. A user can buy something and Paypal charges him a user fee. The IPN will only check me if the purchase information actually comes from Paypal and everything is fine there, and then (in my script) will send all this information to the DB so that I can execute my part of the service. Thus, even though the transaction is successful without an IPN, the IPN will be an invaluable mechanism to facilitate the overall sales process. However, all this latest information confirms my suspicion that my entire shopping cart code is OK and that the problem is ONLY here with this code or the way Paypal handles it. (The fact that "virgin "Paypal code went through Paypal's own IPN simulator, does not return a response, but the same error in the initial state of not being able to connect to the Paypal server makes me think the problem is something wrong on the Paypal side.)

So a log of all the inputs and outputs from this PHP. I placed commands Mail

at every important point in the code to create breakpoints. Inside the command, foreach()

I included an incremental variable so that I also have an ordered list of incoming data. And this is what I got:

This is the data that is POSTED entered into the Paypal code (in the order it was sent)

    test_ipn=1
    payment_type=instant
    payment_date=08%3A42%3A23+Feb+09%2C+2013+PST
    payment_status=Completed
    payer_status=verified
    first_name=John
    last_name=Smith
    payer_email=buyer%40paypalsandbox.com
    payer_id=TESTBUYERID01
    business=seller%40paypalsandbox.com
    receiver_email=seller%40paypalsandbox.com
    receiver_id=TESTSELLERID1
    residence_country=US
    item_name1=something
    item_number1=AK-1234
    quantity1=1
    tax=2.02
    mc_currency=USD
    mc_fee=0.44
    mc_gross=15.34
    mc_gross_1=12.34
    mc_handling=2.06
    mc_handling1=1.67
    mc_shipping=3.02
    mc_shipping1=1.02
    txn_type=cart
    txn_id=23291642
    notify_version=2.4
    custom=xyz123
    invoice=abc1234
    charset=windows-1252

      

At first glance, I seem to be getting all the data I need to get from Paypal. And this is the order in which PP sends it. So if I were to send this data in that order, and the PP actually checks it for that same order, I should theoretically get a response VALID

.

So, what the code does is add a "cmd = _notify-validate", which is the only supplement (PP documentation clearly states that it is a necessary addition) and transmits these data in var, referred to $req

as follows: A=X&B=Y&C=Z...

.

Here is the message from the next breakpoint, added just after the command was closed foreach()

.

    $req = cmd=_notify-validate&test_ipn=1&payment_type=instant&payment_date=08%3A42%3A23+Feb+09%2C+2013+PST&payment_status=Completed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=TESTSELLERID1&residence_country=US&item_name1=something&item_number1=AK-1234&quantity1=1&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=15.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=23291642&notify_version=2.4&custom=xyz123&invoice=abc1234&charset=windows-1252&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31ACyRxUQ6LVDwUz.i78mjQLsN9aKb

      

See note verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31ACyRxUQ6LVDwUz.i78mjQLsN9aKb

at the end. I cannot explain this piece of data because it was not spitting out as an E-Mail during the loop foreach()

. So where did it come from? Could this be the culprit for the data returning the response INVALID

? Open question. But at the moment all this data is transmitted by the code at the "DATA COMING IN" checkpoint; so if this data comes in and my code sends it back then everything should be fine.

So the next checkpoint will be "DATA COMING OUT". I have set this breakpoint right before the code fputs ($fp, $header . $req);

that writes $req

back to PP. And this is what comes out of the code:

    $req = cmd=_notify-validate&test_ipn=1&payment_type=instant&payment_date=08%3A42%3A23+Feb+09%2C+2013+PST&payment_status=Completed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=TESTSELLERID1&residence_country=US&item_name1=something&item_number1=AK-1234&quantity1=1&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=15.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=23291642&notify_version=2.4&custom=xyz123&invoice=abc1234&charset=windows-1252&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31ACyRxUQ6LVDwUz.i78mjQLsN9aKb

      

HUM! Am I missing something? Could my eyes miss the smallest hiccup that makes this code path? The result seems to be exactly the same as the input. Suppose PP worked on his code in the order that the POSTED data is converted to a string in such a way that PP expects and therefore checks for existence ... if all this, what could be wrong?

Let's see more ...: open questions:

  • Could this be a title issue? According to the documentation, this is the header that should be used ... so if they are not turned off then it should work fine.
  • Could there be a problem with the URL provided? OK, this is actually a problem, or rather, WAS at some point. There are at least 6 or 7 different URLs on the internet that should and should not work. Some of them worked for some people and not others, some never seemed to work, etc. Even the PPs themselves are not sure which url should be used as it changes in their own documentation. It is clear that using URLs sandbox.

    will not return anything - you have to assume that they are broken.
  • Could this be an encoding issue? Could there be incompatibility? This just happened to me as I saw the data coming in with hex codes. The point is, that should make it compatible, right? However, yesterday when I was checking what was going on in the loop while()

    and catching the information sent from PP to var $res

    , I didnโ€™t get Hex codes, as you can see further on in this will.

Next control point is a classic in the if()

VALID and if()

INVALID. And of course I get the one that tells me the whole thing got INVALID again.

Hopefully this detailed information I give makes someone shine a light bulb bright! As for me, I am still stunned.

Thanks in advance!


THANKS A LOT FOR YOUR ANSWERS!

I haven't tried the second one, although it sounds pretty reasonable. It looks like it might have been a problem with the source code I got from PayPal. If I have time to try it and see if the code works with this, I'll post my review here. Hope someone finds all of this helpful.

After a week, banging my head against the walls and a little time to cool the problem in my brain, I finally found another way to solve the problem somewhere on the Net a couple of days ago, which is very similar to the second part of the answer to hexacyanide. I am posting it below for those following this line. However, I have to say the PayPal code is pretty buggy. At least the one I received. Since then, I have had several other problems that I had to solve with workarounds. Worst of all, PayPal doesn't support AT ALL. I even called three times: first, the person hung up on me; then they told me that it might be a virus problem (typical response when they really don't know what's going on).

Anyway, I wish I could evaluate your answers, but for that I need more than +15 points, so I will praise them verbally. I tried Hexacynide's answer code as it is, but it didn't work or me, so I can't mark it as tested. If I have time to try another in the future and it works, I will mark it as correct.

Until then, here is my code update using CURL.

    $ch = curl_init('https://www.paypal.com/pe/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    if( !($res = curl_exec($ch)) ) {
        // error_log("Got " . curl_error($ch) . " when processing IPN data");
        curl_close($ch);
        exit;
    }
    curl_close($ch);

      

NOTE: Here it is aiming at the real thing because it works and works now, however using it with sandbox.

also works.

+3


source to share


3 answers


I'm new to Stackoverflow so I didn't get it that one question can be answered. Anyway, I answered this at the end of the original post by editing it last week. Now I am including it here, so anyone with this problem can come back to this and figure that this is how I was able to fix the problem.

I tried the first solution given here by a co-author, but unfortunately it didn't work for me. The second solution came up as soon as I was already able to solve it in a different way, so I didn't try. Perhaps I will visit him sometime in the future. But if anyone tries, please leave your comments here for other benefits from wisdom.

Here is my solution:

After a week, banging my head against the walls and a little time to cool the problem in my brain, I finally found another way to solve the problem somewhere on the Net a couple of days ago, which is very similar to the second part of the answer to hexacyanide. I am posting it below for those following this line. However, I must say, the PayPal code is pretty buggy. At least the one I received. Since then, I have had several other problems that I had to solve with workarounds. Worst of all, PayPal doesn't support AT ALL. I even called three times: first, the person hung up on me; then they told me that it might be a virus problem (typical response when they really don't know what's going on).



Anyway, I wish I could evaluate your answers, but for that I need more than +15 points, so I will praise them verbally. I tried Hexacynide's answer code as it is, but it didn't work or me, so I can't mark it as tested. If I have time to try another in the future and it works, I will mark it as correct.

Until then, here is my code update using CURL.

    $ch = curl_init('https://www.paypal.com/pe/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    if( !($res = curl_exec($ch)) ) {
        // error_log("Got " . curl_error($ch) . " when processing IPN data");
        curl_close($ch);
        exit;
    }
    curl_close($ch);

      

NOTE: Here it is aiming at the real thing because it works and works now, however using it with help sandbox.

also works.

+4


source


Try to change this:

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

      

For this:

// 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 .= "Connection: close\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);

      



Essentially, PayPal is dropping support for HTTP 1.0. I believe it has already been discontinued on Sandbox, but it is still supported on the site (but not much longer). This article has more information on this: https://www.paypal-notify.com/eventnotification/event_details?eventId=2891

In addition, you must send a message to the SSL enabled host. My point is that PayPal will refuse to validate the IPN on the HTTP URL (and just redirect to the PayPal homepage on an SSL enabled server), but I'm not 100% sure about that.

Finally, to answer your question - IPN is an asynchronous process and it doesn't start until the payment is complete. Therefore, if you do not receive an IPN for any reason, the payment has already been completed and will not be canceled just because you did not receive an IPN. If you want something that will definitely scan your site before the payment is made, I would suggest looking at Express Checkout.

+3


source


I just went through this. You must ACCEPT the command to respond, not add it. Once you have done this, you should receive the desired "VERIFIED" response from paypal. For your convenience, I posted the code that worked for me. I was where you are, brother, and decided that I would rather answer than close my browser and go to bed (I also had a long day!). Anyway, without further adieu, here's the 'tis:

function verifyPayment($request)
{
    if($request->is('post') && !empty($request->data))
    {
        //build response string
        $response = '';
        foreach($request->data as $field => $value)
        {
            //build response string
            $response .= $field . '=' . urlencode(stripslashes($value)) . '&';
        }
        if(empty($id))
        {
            return false; //invalid transaction
        }
        //append notify validate command
        $response .= "cmd=_notify-validate"; 
        //respond to paypal - open connection
        $parsedUrl = parse_url('https://www.sandbox.paypal.com/cgi-bin/webscr');
        $sock = fsockopen('ssl://' . $parsedUrl['host'], "443", $errorNumber, $error, 30);
        if(!$sock)
        {
            return false; //could not open connection - must be able to verify
        }
        //post data back to paypal
        fputs($sock, "POST $parsedUrl[path] HTTP/1.1\r\n");
        fputs($sock, "Host: $parsedUrl[host]\r\n");
        fputs($sock, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($sock, "Content-length: " . strlen($response) . "\r\n");
        fputs($sock, "Connection: close\r\n\r\n");
        fputs($sock, $response . "\r\n\r\n");
        //loop through response and append
        $ipnResponse = '';
        while(!feof($sock))
        {
            $ipnResponse .= fgets($sock, 1024);
        }
        //close connection
        fclose($sock);
        if(!eregi("VERIFIED", $ipnResponse))
        {
            return false; //paypal reports invalid transaction
        }
        //if we made it here, transaction is valid
        return true;
    }
}

      

I sincerely hope this helps you and everyone else dealing with PayPal frustration!

Hooray! Jay the Good

+1


source







All Articles