Paypal Integration in php

1. Make a new folder in your localhost and Save this file index.php 

<?php
$paypal_username = "arunendra123-facilitator_api1.smartwinzsolutions.com";
$paypal_password = "1394441";
$paypal_signature = "A05YM4frUDrScsYqP299BBa4trgEA3InSTImhusSPEE8pKNdBzC";
include("paypal_pro_signature.inc.php");

$firstName = urlencode("arunbuyer");
$lastName = urlencode("rai");
$creditCardType = urlencode("Visa");
$creditCardNumber = urlencode("4271901174400127");
$expDateMonth = urlencode("03");
$padDateMonth = str_pad($expDateMonth,2,'0',STR_PAD_LEFT);
$expDateYear = urlencode("2019");
$cvv2Number = urlencode("123");
$address1 = urlencode("test");
$address2 = urlencode("test");
$city = urlencode("test");
$state = urlencode("test");
$zip = urlencode("73301");
$amount = urlencode("25.75");
$currencyCode = urlencode("USD");
$paymentAction = urlencode("Sale");
$methodToCall = 'DoDirectPayment';



$nvpstr='&PAYMENTACTION='.$paymentAction.'&AMT='.$amount.'&CREDITCARDTYPE='.$creditCardType.'&ACCT='.$creditCardNumber.'&EXPDATE='.$padDateMonth.$expDateYear.'&CVV2='.$cvv2Number.'&FIRSTNAME='.$firstName.'&LASTNAME='.$lastName.'&STREET='.$address1.'&CITY='.$city.'&STATE='.$state.'&ZIP='.$zip.'&COUNTRYCODE=US&CURRENCYCODE='.$currencyCode;

$environment = 'sandbox'; // or 'beta-sandbox' or live
$paypalPro = new paypal_pro($paypal_username, $paypal_password, $paypal_signature, $environment);
$resArray = $paypalPro->PPHttpPost('DoDirectPayment', $nvpstr);
$ack = strtoupper($resArray["ACK"]);
if(stristr($ack, 'SUCCESS') || stristr($ack, 'SUCCESSWITHWARNING')){
echo 'payment don successfully';
print_r($resArray);   /* this will print respone return by paypal  */
}
else{
echo "<table border='1' cellpadding = 2 cellspacing = '2' align='center' bordercolor='#CCCCCC' width='70%'>",
"<tr><td colspan='2'><font size=4>The Transaction to Paypal Pro Gateway could not be Completed </font></td></tr>\n",
"<tr><td>FinalStatus:</td><td>&nbsp;", $ack, "</td></tr>",
"<tr><td>Response code:</td><td>&nbsp;", $resArray['L_ERRORCODE0'], "</td></tr>",
"<tr><td>Error Message:</td><td>&nbsp;", urldecode($resArray['L_LONGMESSAGE0']), "</td></tr>",
"</table>";
}
?> 

2.  Save this file paypal_pro_signature.inc.php 

<?php
class paypal_pro{
public $API_USERNAME;
public $API_PASSWORD;
public $API_SIGNATURE;
public $API_ENDPOINT;
function __construct($API_USERNAME, $API_PASSWORD, $API_SIGNATURE, $ENVIORNMENT){
$this->API_USERNAME = $API_USERNAME;
$this->API_PASSWORD = $API_PASSWORD;
$this->API_SIGNATURE = $API_SIGNATURE;
if($ENVIORNMENT == 'live'){
$this->API_ENDPOINT = "https://api-3t.paypal.com/nvp";
}
else{
$this->API_ENDPOINT = "https://api-3t.sandbox.paypal.com/nvp";
}
}

function PPHttpPost($methodName_, $nvpStr_) {
$API_UserName = urlencode($this->API_USERNAME);
$API_Password = urlencode($this->API_PASSWORD);
$API_Signature = urlencode($this->API_SIGNATURE);
$API_Endpoint = $this->API_ENDPOINT;
$version = urlencode('51.0');
 
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
 
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
 
// Set the API operation, version, and API signature in the request.
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature&BUTTONSOURCE=".urlencode("PHPMOOT_WPP_DP_US")."$nvpStr_";
 
// Set the request as a POST FIELD for curl.
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
 
// Get response from the server.
$httpResponse = curl_exec($ch);
 
if(!$httpResponse) {
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
}
 
// Extract the response details.
$httpResponseAr = explode("&", $httpResponse);
 
$httpParsedResponseAr = array();
foreach ($httpResponseAr as $i => $value) {
$tmpAr = explode("=", $value);
if(sizeof($tmpAr) > 1) {
$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
}
}
 
if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
}
 
return $httpParsedResponseAr;
}
}
?>

1 comment: