Skip to content Skip to sidebar Skip to footer

Integrate Paypal

I have successfully integrated PayPal. All is working fine. But I want my form to redirect to my site after a successful payment. Another question: how to get response from PayPal?

Solution 1:

Just go through this:

What you want is PAYPAL IPN, INSTANT PAYMENT NOTIFICATION which is sent to user's website after payment is completed.

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro

or you can also set path to IPN in your cart where paypal will send notification like this:

<inputtype="hidden" name="notify_url" value="site.com/ipn/index.php" />
<inputtype="hidden" name="return"     value="site.com/ipn/index.php"/>

But you'll need to login to your merchant account on paypal to manually specify the ipn and auto return url. in website payment preferences and instant payment notifications section under profile tab.

Following i've added the sample ipn code which is sent to your website after successfull payment:

// read the post from PayPal system and add 'cmd'$req = 'cmd=_notify-validate';

foreach ($_POSTas$key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// 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 ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

// assign posted variables to local variables$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// 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
}
elseif (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>

You need to add this code on your notify url page. the return page you mention in notify url field of shopping cart i.e.

<inputtype="hidden" name="notify_url" value="site.com/ipn/index.php" />

Put this code in site.com/ipn/index.php

Lets take an example of your shopping cart:

<formaction="https://sandbox.paypal.com/cgi-bin/webscr"method="post"><inputtype="hidden"name="business"value="savife_1314264698_biz@gmail.com "><inputtype="hidden"name="cmd"value="_xclick-subscriptions"><inputtype="hidden"name="item_name"value="Alice's Weekly Digest"><inputtype="hidden"name="item_number"value="DIG Weekly"><inputtype="hidden"name="currency_code"value="USD"><inputtype="hidden"name="notify_url"value="http://localhost/check.php"><inputtype="hidden"name="return"value="http://google.co.in"><inputtype="hidden"name="a3"value="5.00"><inputtype="hidden"name="p3"value="1"><inputtype="hidden"name="t3"value="M"><!-- Display the payment button. --><inputtype="image"name="submit"border="0"src="btn_subscribe_LG.gif"alt="PayPal - The safer, easier way to pay online"><imgalt=""border="0"width="1"height="1"src="https://www.paypal.com/en_US/i/scr/pixel.gif" ></form>

In this case your notify url where paypal will send ipn is http://localhost/check.php.

So put that code in check.php page. After recieving the ipn you can further use it to enter in your database etc.

or visit this link to get an overview of paypal ipn listener:

https://cms.paypal.com/uk/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNImplementation

Hope this helps.

Solution 2:

You can use notify_url for paypal to send a IPN response to a script on your site when a payment is made. Use this hidden field.

<inputtype="hidden" name="notify_url" value="yourwebsite.com/paypal/process" />

Now when a user pays, paypal will send a IPN to your website.

For the Return, you can use:

<input type="hidden" name="return" value="path.to/return/website/"/> // After successful payment
<input type="hidden" name="cancel_return" value=""/> // If user cancels where to redirect to.

Solution 3:

you can set paypal notify_url(IPN Response url) and success url like this way:

<inputtype="hidden" name="notify_url" value="http://localhost/demo/payment.php/ipn_response">                                            
<inputtype="hidden" name="return" value="http://localhost/demo/payment_successful">

Get Paypal response paypal.php

functionipn($ipn_data) {

    define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
    define('SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
    $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
    if (!preg_match('/paypal\.com$/', $hostname)) {
        $ipn_status = 'Validation post isn\'t from PayPal';
        if ($ipn_data == true) {
            //You can send email as well
        }
        returnfalse;
    }

    // parse the paypal URL$paypal_url = ($_REQUEST['test_ipn'] == 1) ? SSL_SAND_URL : SSL_P_URL;
    $url_parsed = parse_url($paypal_url);

    $post_string = '';
    foreach ($_REQUESTas$field => $value) {
        $post_string .= $field . '=' . urlencode(stripslashes($value)) . '&';
    }
    $post_string.="cmd=_notify-validate"; // append ipn command// get the correct paypal url to post request to$paypal_mode_status = $ipn_data; //get_option('im_sabdbox_mode');if ($paypal_mode_status == true)
        $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
    else$fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);

    $ipn_response = '';

    if (!$fp) {
        // could not open the connection. If loggin is on, the error message// will be in the log.$ipn_status = "fsockopen error no. $err_num: $err_str";
        if ($ipn_data == true) {
            echo'fsockopen fail';
        }
        returnfalse;
    } else {
        // Post the data back to paypal
        fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
        fputs($fp, "Host: $url_parsed[host]\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($post_string) . "\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $post_string . "\r\n\r\n");

        // loop through the response from the server and append to variablewhile (!feof($fp)) {
            $ipn_response .= fgets($fp, 1024);
        }
        fclose($fp); // close connection
    }
    // Invalid IPN transaction. Check the $ipn_status and log for details.if (!preg_match("/VERIFIED/s", $ipn_response)) {
        $ipn_status = 'IPN Validation Failed';

        if ($ipn_data == true) {
            echo'Validation fail';
            print_r($_REQUEST);
        }
        returnfalse;
    } else {
        $ipn_status = "IPN VERIFIED";
        if ($ipn_data == true) {
            echo'SUCCESS';
            print_r($_REQUEST);
        }
        returntrue;
    }
}

functionipn_response() {
    //mail("sobhagya1411@gmail.com","My subject",print_r($request,true));$ipn_data = true;
    if ($this->ipn($_REQUEST)) {
        $this->insert_data($_REQUEST);
    }
}

functionissetCheck($post, $key) {
    if (isset($post[$key])) {
        $return = $post[$key];
    } else {
        $return = '';
    }
    return$return;
}


 publicfunctioninsert_data(){

     $post = $_REQUEST;    

    $item_name= $this->issetCheck($post, 'item_name');         
    $amount = $this->issetCheck($post, 'mc_gross');
    $currency = $this->issetCheck($post, 'mc_currency');
    $payer_email = $this->issetCheck($post, 'payer_email');
    $first_name = $this->issetCheck($post, 'first_name');
    $last_name = $this->issetCheck($post, 'last_name');
    $country = $this->issetCheck($post, 'residence_country');
    $txn_id = $this->issetCheck($post, 'txn_id');
    $txn_type = $this->issetCheck($post, 'txn_type');
    $payment_status = $this->issetCheck($post, 'payment_status');
    $payment_type = $this->issetCheck($post, 'payment_type');
    $payer_id = $this->issetCheck($post, 'payer_id');
    $create_date = date('Y-m-d H:i:s');
    $payment_date = date('Y-m-d H:i:s');

    $paypal_sql = "INSERT INTO ipn_data_tbl (item_name,payer_email,first_name,last_name,amount,currency,country,txn_id,txn_type,payer_id,payment_status,payment_type,create_date,payment_date)
    VALUES ($item_name,'$payer_email','$first_name','$last_name','$amount','$currency','$country','$txn_id','$txn_type','$payer_id','$payment_status','$payment_type','$create_date','$payment_date')";
    mysql_query($paypal_sql); 

}

Post a Comment for "Integrate Paypal"