thenetimp: (Default)
I've only used Paypal IPN a couple times. Both time were in systems where the IPN was already set up. It didn't take much work for me to actually do anything from there. I am working on a new WordPress module. Part of which includes functionality where I needed to do some paypal processing. While searching through the internet I found a lot of sample code, but nothing that was a simple object. After some headaches caused by a typo, I have a php5 class for Paypal IPN validation.



define('PAYPAL_IPN_LISTENER_VERIFIED_STATUS','VERIFIED');
define('PAYPAL_IPN_LISTENER_INVALID_STATUS','INVALID');

class PaypalIPNListener
{
    protected $status = PAYPAL_IPN_LISTENER_INVALID_STATUS;
    protected $payment_status = false;
    private $sandboxUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    private $productionUrl = "https://www.paypal.com/cgi-bin/webscr";
    private $serviceUrl = null;
    private $serviceData = "";
    
    public function __construct($postData = array(), $sandbox=false)
    {
		// Set the initial serviceUrl
		$serviceUrl = $productionUrl;

		// If sandbox change the serviceUrl
		if($sandbox == true) $serviceUrl = $sandboxUrl;

        // Save the post data for later use.
        $this->postData = $postData;

        // If we are debugging switch the URL
        $serviceUrl =  ($sandbox) ? $sandboxUrl : $productionUrl;
        
        // Add the validate notify command to the post data.
        $postData['cmd'] = '_notify-validate';
        
        // Create a querystring from the data.
        foreach($postData as $key => $val)
        {
                if($serviceData != "") $responseData .="&";
                $serviceData .= $key . "=" . urlencode($val);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->serviceUrl);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $output = curl_exec($ch);
        
        // If the payment is verified then set the status as verified.
        if($output == "VERIFIED")
        {
            $this->status = PAYPAL_IPN_LISTENER_VERIFIED_STATUS;
            $this->payment_status = $postData['payment_status'];
        }
        
    }

    public function getPaymentStatus()
    {
    	return $this->payment_status;
    }

    public function __toString()
    {
        return $this->status;
    }
}




The code makes creating an IPN script simple for anyone.



// Create the IPN Listener object. (passing true as the 2nd parameter
// sets you up with the sandbox for development.
$ipn = new PaypalIPNListener($_POST, true);



Once you create the object there is nothing left for you to do for the validation process to begin. It has already begun, and ready for you to use. How do you use it? Like this.

// IPN Listener Object
$ipn = new PaypalIPNListener($_POST, true);

if($ipn == PAYPAL_IPN_LISTENER_VERIFIED_STATUS)
{
    // Payment is verified.   We use the magic method __toString to return the validation status.
    // Then it's as simple as checking the $_POST data for the ['payment_status'] to make sure they
   // weren't denied or that something didn't go wrong on paypals side.
}
else
{
    // something went wrong with the validation process.  You should handle it here.
    // at least log it so that you have a record of the failed validation.
}



To go into production mode either pass "false" as the second parameter or remove it entirely only passing the post data. Once you do that you are GOOD TO GO!

thenetimp: (Default)
I am sure people have written something similar 1000 times, but with time they fade from the internet. Today I will share with you my Random String generating php class. Are you ready?


<?php

    /**************************************************************************  
    Copyright 2009  James Andrews  (email : contact at jamesmandrews dot com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation version 2 of the License

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     **************************************************************************  */ 

    class RandomString 
    {
    
        /* static variables needed to create the random string */
        private static $alphas = "abcedfghijklmnopqrstuvwxyz";
        private static $alphasUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static $numbers = "1234567890";
        private static $specialChars = "!@#$%^&*()-_+=[]{}|:;>.<,/?";
    
        /*
         * Function to generate a random string of roman characters
         */
        public static function generateRandomAlphaString($stringLength, $type = "lower")
        {
            switch($type)
            {
                case 'upper':
                    $stringBase = self::$alphasUpper;
                    break;
                case 'mixed':
                    $stringBase = self::$alphasUpper . self::$alphas;
                    break;
                default:
                    $stringBase = self::$alphas;
            }

            return self::generateRandomStringWithLengthFromString($stringLength, $stringBase);

        }

        /*
         * Function to generate a random string of numbers 0-9
         */
        public static function generateRandomNumericString($stringLength)
        {
            return self::generateRandomStringWithLengthFromString($stringLength, self::$numbers);
        }


        /*
         * Function to generate a random string of numbers 0-9 and roman characters
         */
        public static function generateRandomAlphaNumericString($stringLength, $type = "lower")
        {
            switch($type)
            {
                case 'upper':
                    $stringBase = self::$alphasUpper . self::$numbers;
                    break;
                case 'mixed':
                    $stringBase = self::$alphasUpper . self::$alphas . self::$numbers;
                    break;
                default:
                    $stringBase = self::$alphas . self::$numbers;
            }
            return self::generateRandomStringWithLengthFromString($stringLength, $stringBase);
        }
    
        /*
         * Function to generate a random string of numbers 0-9 and roman characters and other special characters
         */
        public static function generateRandomStringWithAll($stringLength)
        {
            $stringBase = self::$alphasUpper . self::$alphas . self::$numbers . self::$specialChars;
            return self::generateRandomStringWithLengthFromString($stringLength, self::$numbers);
        }

        /*
         * Function takes a string length, and a "string from", and generates a random string
         * of stringlength, with characters withing "string from"
         */
        private static function generateRandomStringWithLengthFromString($stringLength = 0, $fromString = "")
        {
            $fromStrLen = strlen($fromString);
            $returnString = "";
        
            for($count = 0; $count < $stringLength; $count++)
            {
                $random = (rand() % $fromStrLen);
                $random = rand(0,$fromStrLen);
                $returnString .= substr($fromString, $random, 1);
            }

            return $returnString;
        }
    }





This class is useful anytime you need a random string of any length. All the functions are called staticly, so no need to create an object just call the class.

Example 1:
Create a random string of 12 lowercase-alphabet characters

print RandomString::generateRandomAlphaString(12);


Example 2:
Create a random string of 12 uppercase-alphabet characters

print RandomString::generateRandomAlphaString(12, 'upper');


Example 3:
Create a random string of 12 mixed-case-alphabet characters

print RandomString::generateRandomAlphaString(12, 'mixed');


Example 4:
Create a random string of 12 number characters

print RandomString::generateRandomNumericString(12);


Example 5:
Create a random string of 12 number characters or lower-case alphabet characters

print RandomString::generateRandomAlphaNumericString(12);


Example 6:
Create a random string of 12 number characters or upper-case alphabet characters

print RandomString::generateRandomAlphaNumericString(12, 'upper');


Example 7:
Create a random string of 12 number characters or mixed-case alphabet characters

print RandomString::generateRandomAlphaNumericString(12, 'mixed');


Example 8:
Create a random string of 12 number characters, mixed-case alphabet characters, or special characters

print RandomString::generateRandomStringWithAll(12);


This class will pretty much fill all your random string needs. Feel free to use it if you like, or make suggestions to changes, to make it better.




Profile

thenetimp: (Default)
thenetimp

July 2014

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 23rd, 2025 04:06 pm
Powered by Dreamwidth Studios