Paypal Payment Gateway For WordPress

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' => sprintf( __( 'Click here to complete the payment on %s', 'wpuf' ), get_bloginfo( '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 );

wp_redirect( $paypal_url );<br>exit;

 

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 post data
$ipn_data = (array) stripslashes_deep( $_POST );
$ipn_data['cmd'] = '_notify-validate';

// Send back post vars to paypal
$params = array(
    'body' => $ipn_data,
    'sslverify' => false,
    'timeout' => 30,
    'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
);

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

$response = wp_remote_post( $paypal_url, $params );

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

if ( !is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && (strcmp( $response['body'], "VERIFIED" ) == 0) ) {
    return true;
} else {
    return false;
}