Jump to content

Paypal's IPN, PDT and how it all comes together....


tlflow

Recommended Posts

I've been reading some of the other posts on this subject, and I can tell already that this project is going to be FUN.  We want to do something like this, but I'm just trying to wrap my mind around how to do it. 

 

Question 1) For all the other posts I think the flow has been like this:

  • Step1- customer enters info
  • Step 2- info is written to database
  • Step 3- paypal processes request
  • Step 4- sends thank you

 

but, I was wondering...what happens if the order doesnt go through?  So, shouldnt it be like this:

 

  • Step1- customer enters info
  • Step 2- paypal processes request
  • Step 3- info is written to database
  • Step 4- sends thank you

 

This is really just a matter of what's best - by theory...like I said, I'm trying to figure this out before I really get going.

 

Question 2) I tried just using Paypal's code, putting my token in, but I'm not getting any variables posted back after the transaction goes through (except for the merchant_return_link).  At least I get that, so I'm optimistic...but I'm missing all the other variables (first_name, last_name, etc...).

 

<?php

                // Check to see if variables are at least coming over
                var_dump($_POST,$_GET);
                
                // read the post from PayPal system and add 'cmd'
                $req = 'cmd=_notify-synch';
                
                $tx_token = $_GET['tx'];
                $auth_token = "***my live token - should I have a test token since Im using sandbox?****";
                $req .= "&tx=$tx_token&at=$auth_token";
                
                // 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.paypal.com', 80, $errno, $errstr, 30);
                // If possible, securely post back to paypal using HTTPS
                // Your PHP server will need to be SSL enabled
                // $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

							if (!$fp) {
                // HTTP ERROR
                } else {
                fputs ($fp, $header . $req);
                // read the body data 
                $res = '';
                $headerdone = false;
                while (!feof($fp)) {
                $line = fgets ($fp, 1024);
                if (strcmp($line, "\r\n") == 0) {
                // read the header
                $headerdone = true;
                }
                else if ($headerdone)
                {
                // header has been read. now read the contents
                $res .= $line;
                }
                }
                
                // parse the data
                $lines = explode("\n", $res);
                $keyarray = array();
                if (strcmp ($lines[0], "SUCCESS") == 0) {
                for ($i=1; $i<count($lines);$i++){
                list($key,$val) = explode("=", $lines[$i]);
                $keyarray[urldecode($key)] = urldecode($val);
                }
                // check the payment_status is Completed
                // check that txn_id has not been previously processed
                // check that receiver_email is your Primary PayPal email
                // check that payment_amount/payment_currency are correct
                // process payment
                $firstname = $keyarray['first_name'];
                $lastname = $keyarray['last_name'];
                $itemname = $keyarray['item_name'];
                $amount = $keyarray['payment_gross'];
                
                echo ("<p><h3>Thank you for your purchase!</h3></p>");
                
                echo ("<b>Payment Details</b><br>\n");
                echo ("<li>Name: $firstname $lastname</li>\n");
                echo ("<li>Item: $itemname</li>\n");
                echo ("<li>Amount: $amount</li>\n");
                echo ("");
                }
                else if (strcmp ($lines[0], "FAIL") == 0) {
                // log for manual investigation
                }
                
                }
                
                fclose ($fp);
                
                ?>

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.