Sunday, July 1, 2012

Integration with Paypal on PHP

This post will teach you how to to integrate your website with paypal and hope I was able to guide you well.

To start with, you need to apply for a Paypal account. If you just want a test account, you can register for a sandbox account - http://www.sandbox.paypal.com

We need the following account details below:
  • Username
  • Password
  • API Signature
Once you have all the details, we can now do the coding part. Aside from account details, you also need to know the endpoint url and the paypal callback url. Please see details below.

Live:
  • Endpoint - https://api-3t.paypal.com/nvp
  • Callback - https://www.paypal.com/webscr&cmd=_express-checkout&token=
Sandbox:
  • Endpoint - https://api-3t.sandbox.paypal.com/nvp
  • Callback - https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=
Just to get us going, please see below code snippet for checking the environment and initiate the details.

// set up your environment - live or sandbox
$live = "true";

if ($live == "true") {
        // live account details
        $username = 'paypal live username';
        $password = 'paypal live password';
        $signature = 'paypal live api signature';
        $endpoint = "https://api-3t.paypal.com/nvp";
        $url = "https://www.paypal.com/webscr&cmd=_express-checkout&token=";
} else {
        // sandbox account details
        $username = 'paypal sandbox username';
        $password = 'paypal sandbox password';
        $signature = 'paypal sandbox api signature';
        $endpoint = "https://api-3t.sandbox.paypal.com/nvp";
        $url = "https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=";
}


Next, we need the details of transaction to be submitted to paypal api. For the benefit of this post, please see details below.

$itemamt = '40.00'; // item amount
$paymentamt = '50.00'; // total amount
$taxamt = '10.00'; // tax amount
$currencyid = 'CAD'; // 'GBP', 'EUR', 'JPY', 'USD', 'AUD'


Please make sure that $paymentamt = $itemamt + $taxamt

Also, we need the details below to perform payment transaction.

$startdate = urlencode('2012-07-01T18:10:40+08:00'); // payment start date
$billingfreq = '1' // number of months interval;
$paymenttype = 'Authorization'; // or 'Sale' or 'Order'
$description = urlencode('sample description'); // description of transaction


You also need to define your callback url. Please see below.

$returnurl = 'http://www.domain.com/callback/return.html'; // callback url for successful transaction
$cancelurl = 'http://www.domain.com/callback/cancel.html'; // callback url for failed transaction


After defining the parameters, compose the query string. Please see below.

$reqStr = "METHOD=SetExpressCheckout&VERSION=65.2&PWD=$password&USER=$username&SIGNATURE=$signature&RETURNURL=$returnurl&CANCELURL=$cancelurl&REQCONFIRMSHIPPING=0&NOSHIPPING=1&PAYMENTREQUEST_0_CURRENCYCODE=$currencyid&PAYMENTREQUEST_0_AMT=$paymentamt&PAYMENTREQUEST_0_ITEMAMT=$itemamt&PAYMENTREQUEST_0_TAXAMT=$taxamt&PAYMENTREQUEST_0_DESC=$description&PAYMENTREQUEST_0_PAYMENTACTION=$paymenttype&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital&L_PAYMENTREQUEST_0_NAME0=$description&L_PAYMENTREQUEST_0_QTY0=1&L_PAYMENTREQUEST_0_AMT0=$itemamt&L_PAYMENTREQUEST_0_DESC0=$description&L_BILLINGAGREEMENTDESCRIPTION0=$description&L_BILLINGTYPE0=RecurringPayments&MAXFAILEDPAYMENTS='true'";

Setup the curl as below.

// set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

// disable ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

// set the method
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $reqstr);
               
// execute curl
$response = curl_exec($ch);
if (!$response) exit("SetExpressCheckout failed: ".curl_error($ch).'('.curl_errno($ch).')');


Get and parse the http response.

// get and parse the response
$arr_response = explode("&", $response);

$http_response = array();
foreach ($arr_response as $key => $val) {
        $param = explode("=", $val);
        if (sizeof($param) > 1) $http_response[$param[0]] = $param[1];
}

if ((sizeof($http_response) == 0) || !array_key_exists('ACK', $http_response)) {
        exit("SetExpressCheckout failed: " . print_r($arr_response, true));
}


Get the token and pass it to paypal for processing.

// get the token and pass to paypal for processing
if (strtoupper($http_response["ACK"]) == "SUCCESS" || strtoupper($httpParsedResponseAr["ACK"]) == "SUCCESSWITHWARNING") {
        // redirect to paypal to confirm and process transaction
        $token = urldecode($http_response["TOKEN"]);
        $paypalurl .= $token;

        if (isset($paypalurl)) redirect($paypalurl);
        exit;
} else  {
        exit('SetExpressCheckout failed: ' . print_r($http_response, true));
}


This is all for now. Next post will be processing after Paypal. You can also check out the full script below. Happy coding!!

<?php

// set up your environment - live or sandbox
$live = "true";

if ($live == "true") {
        // live account details
        $username = 'paypal live username';
        $password = 'paypal live password';
        $signature = 'paypal live api signature';
        $endpoint = "https://api-3t.paypal.com/nvp";
        $url = "https://www.paypal.com/webscr&cmd=_express-checkout&token=";
} else {
        // sandbox account details
        $username = 'paypal sandbox username';
        $password = 'paypal sandbox password';
        $signature = 'paypal sandbox api signature';
        $endpoint = "https://api-3t.sandbox.paypal.com/nvp";
        $url = "https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=";
}

$itemamt = '40.00'; // item amount
$paymentamt = '50.00'; // total amount
$taxamt = '10.00'; // tax amount
$currencyid = 'CAD'; // or 'GBP', 'EUR', 'JPY', 'USD', 'AUD'

$startdate = urlencode('2012-07-01T18:10:40+08:00'); // payment start date - 2012-07-01T18:10:40+08:00
$billingfreq = '1' // number of months interval;
$paymenttype = 'Authorization'; // or 'Sale' or 'Order'
$description = urlencode('sample description'); // description of transaction

$returnurl = 'http://www.domain.com/callback/return.html'; // callback url for successful transaction
$cancelurl = 'http://www.domain.com/callback/cancel.html'; // callback url for failed transaction

$reqstr = "METHOD=SetExpressCheckout&VERSION=65.2&PWD=$password&USER=$username&SIGNATURE=$signature&RETURNURL=$returnurl&CANCELURL=$cancelurl&REQCONFIRMSHIPPING=0&NOSHIPPING=1&PAYMENTREQUEST_0_CURRENCYCODE=$currencyid&PAYMENTREQUEST_0_AMT=$paymentamt&PAYMENTREQUEST_0_ITEMAMT=$itemamt&PAYMENTREQUEST_0_TAXAMT=$taxamt&PAYMENTREQUEST_0_DESC=$description&PAYMENTREQUEST_0_PAYMENTACTION=$paymenttype&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital&L_PAYMENTREQUEST_0_NAME0=$description&L_PAYMENTREQUEST_0_QTY0=1&L_PAYMENTREQUEST_0_AMT0=$itemamt&L_PAYMENTREQUEST_0_DESC0=$description&L_BILLINGAGREEMENTDESCRIPTION0=$description&L_BILLINGTYPE0=RecurringPayments&MAXFAILEDPAYMENTS='true'";

// set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

// disable ssl verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

// set the method
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $reqstr);

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

if (!$response) exit("SetExpressCheckout failed: ".curl_error($ch).'('.curl_errno($ch).')');

// get and parse the response
$arr_response = explode("&", $response);

$http_response = array();
foreach ($arr_response as $key => $val) {
        $param = explode("=", $val);
        if (sizeof($param) > 1) $http_response[$param[0]] = $param[1];
}

if ((sizeof($http_response) == 0) || !array_key_exists('ACK', $http_response)) {
        exit("SetExpressCheckout failed: " . print_r($arr_response, true));
}

// get the token and pass to paypal for processing
if (strtoupper($http_response["ACK"]) == "SUCCESS" || strtoupper($httpParsedResponseAr["ACK"]) == "SUCCESSWITHWARNING") {
        // redirect to paypal to confirm and process transaction
        $token = urldecode($http_response["TOKEN"]);
        $paypalurl .= $token;

        if (isset($paypalurl)) redirect($paypalurl);
        exit;
} else  {
        exit('SetExpressCheckout failed: ' . print_r($http_response, true));
}

?>

No comments:

Post a Comment

Leadership 101


  • Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
  • Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
  • Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
  • Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
  • Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
  • Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.