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.