Paypal Payment Gateway

Paypal payment gateway processing steps are

  1. Send data form your website to paypal
  2. Paypal process your data and return payment information
  3. Send again payment information to paypal for IPN validation
  4. Paypal again return IPN validated status, if status true then insert payment informationin in your database

Step1: Send data form your website to paypal

Sending data arguments are
$paypal_args = array(
    'cmd' => '_xclick',
    'amount' => 50,
    'business' => 'your paypal email. User will be payment this account',
    'item_name' => 'test',
    'item_number' => 20,
    'email' => 'payer email. who is send money to your business account',
    'no_shipping' => '1',
    'no_note' => '1',
    'currency_code' => 'USD',
    'charset' => 'UTF-8',
    'custom' => json_encode( array( 'user_id' => 1, 'product_id' => 20 ) ),
    'rm' => '2',
    'return' => 'www.example.com/paypal.php',
    'notify_url' => 'www.example.com/success_message',
    'cbt' => 'your blog name'
);

//paypal url
if( sandbox ) {
    $paypal_url = 'https://www.sandbox.paypal.com/webscr/';
} else {
    $paypal_url = 'https://www.paypal.com/webscr/';
}

$paypal_url = $paypal_url . '?' . http_build_query( $paypal_args );

header( "location:$paypal_url" );
 Step2: Paypal process your data and return payment information
Step3: Send again payment information to paypal for IPN validation

www.example.com/paypal.php

// Get recieved values from paypal post data
foreach( $_POST as $key => $value ) { 
   $params[$key] = stripslashes( $value ); 
}
$params = http_build_query( $params, null, '&' );

//paypal url
if( sandbox ) {
    $paypal_url = 'https://www.sandbox.paypal.com/webscr/';
} else {
    $paypal_url = 'https://www.paypal.com/webscr/';
}

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $paypal_url);
curl_setopt($ch,CURLOPT_POST, count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS, $params);

//execute post
$response = curl_exec($ch);

//close connection
curl_close($ch);

//Step4: Paypal again return IPN validated status, if status true then insert payment informationin in your database

if ( $response == 'ok' && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && (strcmp( $response['body'], "VERIFIED" ) == 0) ) {
    echo 'Payment successfully you may insert payment information in your database';
} else {
    echo 'Error';
}

 Remember, IPN validation in your localhost always return false. You have to test IPN validation in your server site.

PHP autoload

PHP Autoload Usage:

Suppose you have a lot of php file in “my-class” directory. Each file contain php class. You want to call those class from other php file which is out of “my-class” directory. In this case the better way to use php autoload.

function autoload( $class ) {
    $directorys = array();

    //assing your php file directory which file containg your class
    $directorys = array(
        dirname(__FILE__) . '/your-class-directory',
    );

    if( ! count( $directorys ) ) {
        return;
    } 

    foreach( $directorys as $directory ) {

        if( ! is_dir( $directory ) ) {
            echo $directory .' Direcotry is not exist';
            continue;
        }

        if ( ! ( $handle = opendir( $directory ) ) ) {
            echo 'Fail to open your directory. Please check your directory permission';
            continue;
        }

        while ( false !== ( $entry = readdir( $handle ) ) ) {

            if ( $entry != "." && $entry != "..") {

                $file = $directory .'/'. $entry;
                $contents = file_get_contents( $file );
                $contents = str_replace( ' ', '', $contents ); 
                $contents = strtolower( $contents );
                $search = 'class'.$class.'{';

                if ( strpos( $contents, $search ) !== false ) {
                    require_once $directory .'/'. $entry;
                }
            }   
        }
        closedir( $handle );  
    }    
}

spl_autoload_register( 'autoload' );