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
Live:
- Endpoint - https://api-3t.paypal.com/nvp
- Callback - https://www.paypal.com/webscr&cmd=_express-checkout&token=
- Endpoint - https://api-3t.sandbox.paypal.com/nvp
- Callback - https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=
// 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