PHP – PayPal Pro Payment Gateway

The fully customizable way to get paid securely.
PayPal Pro, a market-leading payment gateway, connects your online  store to your existing internet
merchant account. It enables your  business to:
- Accept credit cards, debit cards, etc
- Optimize the customer experience by letting shoppers complete the checkout process without leaving
  your website. 
- Stay secure with access to one of the most powerful risk-management systems.
- Integrate easily. PayPro Pro is pre-integrated into many shopping carts

Code Example: Download Example

index.php
<?php
$paypal_username 	= "youraccount@gmail.com";
$paypal_password 	= "D4sdfFD5G452ZFP9JD22";
$paypal_signature 	= "AMwR710q784XZNXbyEQw5sAvys3421254HJUIKLOsq3liiZMhYFk";

include("paypal_pro_signature.inc.php");

$firstName 			= urlencode("Imran");
$lastName			= urlencode("Phpmoot");
$creditCardType 	= urlencode("Visa");
$creditCardNumber 	= urlencode("4111111111111111");
$expDateMonth 		= urlencode("11");
$padDateMonth 		= str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);
$expDateYear 		= urlencode("2020");
$cvv2Number 		= urlencode("123");
$address1 			= urlencode("test");
$address2 			= urlencode("test");
$city 				= urlencode("test");
$state 				= urlencode("test");
$zip 				= urlencode("12345");
$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')){				
	// success	
}
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>";
}
?>

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;
	}
}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *