Paypal pro integration script using PHP

Hello friends, today I will show you paypal pro integration. PayPal is an American international e-commerce business allowing the faster, safer way to send money, make an online payment. PayPal gives us multiple way for website to integrate it and transfer money from one account to another. Mainly paypal has two types of integration process hosted checkout integration and integrating checkout integration. In hosted checkout integration(Paypal Pro) all the payment related steps are done in your site. But for integrating checkout integration(paypal standard) you will go to paypal site and give your account related information and pay your payment. In this link I develop Paypal standard payment integration using PHP. Now I build paypal pro integration script using PHP.



At first I develop the form where user give all information. I did not validate all form fields but when you use it for your website please validate all form fields then process in next step. I name it index.php

<?php
session_start();
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
$string = '';
$random_string_length = 6;
for ($i = 0; $i < $random_string_length; $i++) {
	  $string .= $characters[rand(0, strlen($characters) - 1)];
}
$_SESSION['order_id'] = $string;
$cards = array(
	array(
		'text'  => 'Visa', 
		'value' => 'VISA'
	),
	array(
		'text'  => 'MasterCard', 
		'value' => 'MASTERCARD'
	),array(
		'text'  => 'Discover Card', 
		'value' => 'DISCOVER'
	),array(
		'text'  => 'American Express', 
		'value' => 'AMEX'
	),array(
		'text'  => 'Maestro', 
		'value' => 'SWITCH'
	),array(
		'text'  => 'Solo', 
		'value' => 'SOLO'
	)
);

$months = array(
	array(
		'text'  => 'January', 
		'value' => '01'
	),
	array(
		'text'  => 'February', 
		'value' => '02'
	),
	array(
		'text'  => 'March', 
		'value' => '03'
	),
	array(
		'text'  => 'April', 
		'value' => '04'
	),
	array(
		'text'  => 'May', 
		'value' => '05'
	),
	array(
		'text'  => 'June', 
		'value' => '06'
	),
	array(
		'text'  => 'July', 
		'value' => '07'
	),
	array(
		'text'  => 'August', 
		'value' => '08'
	),
	array(
		'text'  => 'September', 
		'value' => '09'
	),
	array(
		'text'  => 'October', 
		'value' => '10'
	),
	array(
		'text'  => 'November', 
		'value' => '11'
	),
	array(
		'text'  => 'December', 
		'value' => '12'
	)
);

$year_valid = array(
	array(
		'text'  => '2004', 
		'value' => '2004'
	),
	array(
		'text'  => '2005', 
		'value' => '2005'
	),
	array(
		'text'  => '2006', 
		'value' => '2006'
	),
	array(
		'text'  => '2007', 
		'value' => '2007'
	),
	array(
		'text'  => '2008', 
		'value' => '2008'
	),
	array(
		'text'  => '2009', 
		'value' => '2009'
	),
	array(
		'text'  => '2010', 
		'value' => '2010'
	),
	array(
		'text'  => '2011', 
		'value' => '2011'
	),
	array(
		'text'  => '2012', 
		'value' => '2012'
	),
	array(
		'text'  => '2013', 
		'value' => '2013'
	),
	array(
		'text'  => '2014', 
		'value' => '2014'
	)
);

$year_expire = array(
	array(
		'text'  => '2014', 
		'value' => '2014'
	),
	array(
		'text'  => '2015', 
		'value' => '2015'
	),
	array(
		'text'  => '2016', 
		'value' => '2016'
	),
	array(
		'text'  => '2017', 
		'value' => '2017'
	),
	array(
		'text'  => '2018', 
		'value' => '2018'
	),
	array(
		'text'  => '2019', 
		'value' => '2019'
	),
	array(
		'text'  => '2020', 
		'value' => '2020'
	),
	array(
		'text'  => '2021', 
		'value' => '2021'
	),
	array(
		'text'  => '2022', 
		'value' => '2022'
	),
	array(
		'text'  => '2023', 
		'value' => '2023'
	),
	array(
		'text'  => '2024', 
		'value' => '2024'
	)
);
?>
<h2>Credit Card Details</h2>
<div class="content" id="payment">
  <table class="form">
	<tr>
		<td>First Name:</td>
		<td><input type="text" name="first_name" value="" /></td>
	</tr>
	<tr>
		<td>Last Name:</td>
		<td><input type="text" name="last_name" value="" /></td>
	</tr>
	<tr>
		<td>Email Address:</td>
		<td><input type="text" name="email_address" value="" /></td>
	</tr>
	<tr>
		<td>Phone No:</td>
		<td><input type="text" name="phone_no" value="" /></td>
	</tr>
	<tr>
		<td>Address:</td>
		<td><input type="text" name="address" value="" /></td>
	</tr>
	<tr>
		<td>City:</td>
		<td><input type="text" name="city" value="" /></td>
	</tr>
	<tr>
		<td>State:</td>
		<td><input type="text" name="state" value="" /></td>
	</tr>
	<tr>
		<td>Zip COde:</td>
		<td><input type="text" name="zip" value="" /></td>
	</tr>
	<tr>
		<td>Country:</td>
		<td><input type="text" name="country_code" value="" />(Use your country code like US, IN etc)</td>
	</tr>
	<tr>
		<td>Amount:</td>
		<td><input type="text" name="amount" value="" /></td>
	</tr>
    <tr>
      <td>Card Type:</td>
      <td>
        <select name="cc_type">
          <?php foreach ($cards as $card) { ?>
            <option value="<?php echo $card['value']; ?>"><?php echo $card['text']; ?></option>
          <?php } ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Card Number:</td>
      <td><input type="text" name="cc_number" value="" />(Use 4111111111110303)</td>
    </tr>
    <tr>
      <td>Card Valid From Date:</td>
      <td>
        <select name="cc_start_date_month">
          <?php foreach ($months as $month) { ?>
            <option value="<?php echo $month['value']; ?>"><?php echo $month['text']; ?></option>
          <?php } ?>
        </select>
        /
        <select name="cc_start_date_year">
          <?php foreach ($year_valid as $year) { ?>
            <option value="<?php echo $year['value']; ?>"><?php echo $year['text']; ?></option>
          <?php } ?>
        </select>
        (if available)
      </td>
    </tr>
    <tr>
      <td>Card Expiry Date:</td>
      <td>
        <select name="cc_expire_date_month">
          <?php foreach ($months as $month) { ?>
            <option value="<?php echo $month['value']; ?>"><?php echo $month['text']; ?></option>
          <?php } ?>
        </select>
        /
        <select name="cc_expire_date_year">
          <?php foreach ($year_expire as $year) { ?>
            <option value="<?php echo $year['value']; ?>"><?php echo $year['text']; ?></option>
          <?php } ?>
        </select>(Any future date)
      </td>
    </tr>
    <tr>
      <td>Card Security Code (CVV2):</td>
      <td><input type="text" name="cc_cvv2" value="" size="3" />(Use 123)</td>
    </tr>
    <tr>
      <td>Card Issue Number:</td>
      <td><input type="text" name="cc_issue" value="" size="1" />
        (for Maestro and Solo cards only)</td>
    </tr>
  </table>
</div>
<div class="buttons">
  <div class="right">
    <input type="button" value="Pay With Card" id="button-confirm" class="button" />
  </div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript"><!--
$('#button-confirm').bind('click', function() {
	$.ajax({
		url: 'send.php',
		type: 'post',
		data: $('#payment :input'),
		dataType: 'json',		
		beforeSend: function() {
			$('#button-confirm').attr('disabled', true);
			$('#payment').before('<div class="show_message"><img src="loading.gif" alt="" />Please wait!</div>');
		},
		complete: function() {
			$('#button-confirm').attr('disabled', false);
			$('.attention').remove();
		},				
		success: function(json) {
			if (json['error']) {
				alert(json['error']);
				$('.show_message').remove();
			}

			if (json['success']) {
				location = json['success'];
			}
		}
	});
});
//--></script> 

After click submit, using ajax post request all the form data will send to send.php. In send.php page I silently send all the value to paypal site using cURL. But here I need to put my paypal pro cardinal details like username, password, signature. When you create paypal pro account paypal will generate thin things for you. You can use this link also for creating paypal pro account. https://www.paypal.com/webapps/mpp/paypal-payments-pro or http://stackoverflow.com/questions/7820298/how-to-create-paypal-website-payment-pro-sandbox-account

<?php
session_start();
mysql_connect('localhost','root','');
mysql_select_db('test');
//$payment_type = 'Authorization';	
$payment_type = 'Sale';

$request  = 'METHOD=DoDirectPayment';
$request .= '&VERSION=51.0';
$request .= '&USER=abhijitpal.technokrats_api1.gmail.com'; // your paypal pro username
$request .= '&PWD=3434344344324'; //your paypal pro password  
$request .= '&SIGNATURE=AFcWxV21C7fd0v3434324424H3ddw3IxZoGKITTpD-QUUla';  ////your paypal signature password  
$request .= '&CUSTREF=' . (int)$_SESSION['order_id'];
$request .= '&PAYMENTACTION=' . $payment_type;
$request .= '&AMT='.$_POST['amount'];
$request .= '&CREDITCARDTYPE=' . $_POST['cc_type'];
$request .= '&ACCT=' . urlencode(str_replace(' ', '', $_POST['cc_number']));
$request .= '&CARDSTART=' . urlencode($_POST['cc_start_date_month'] . $_POST['cc_start_date_year']);
$request .= '&EXPDATE=' . urlencode($_POST['cc_expire_date_month'] . $_POST['cc_expire_date_year']);
$request .= '&CVV2=' . urlencode($_POST['cc_cvv2']);

if ($_POST['cc_type'] == 'SWITCH' || $_POST['cc_type'] == 'SOLO') { 
	$request .= '&CARDISSUE=' . urlencode($_POST['cc_issue']);
}

$request .= '&FIRSTNAME=' . urlencode($_POST['first_name']);
$request .= '&LASTNAME=' . urlencode($_POST['last_name']);
$request .= '&EMAIL=' . urlencode($_POST['email_address']);
$request .= '&PHONENUM=' . urlencode($_POST['phone_no']);
$request .= '&IPADDRESS=' . urlencode($_SERVER['REMOTE_ADDR']);
$request .= '&STREET=' . urlencode($_POST['address']);
$request .= '&CITY=' . urlencode($_POST['city']);
$request .= '&STATE=' . urlencode($_POST['state']);
$request .= '&ZIP=' . urlencode($_POST['zip']);
$request .= '&COUNTRYCODE=' . urlencode($_POST['country_code']);
$request .= '&CURRENCYCODE=' . urlencode('USD');
	
/* $request .= '&SHIPTONAME=' . urlencode($order_info['shipping_firstname'] . ' ' . $order_info['shipping_lastname']);
$request .= '&SHIPTOSTREET=' . urlencode($order_info['shipping_address_1']);
$request .= '&SHIPTOCITY=' . urlencode($order_info['shipping_city']);
$request .= '&SHIPTOSTATE=' . urlencode(($order_info['shipping_iso_code_2'] != 'US') ? $order_info['shipping_zone'] : $order_info['shipping_zone_code']);
$request .= '&SHIPTOCOUNTRYCODE=' . urlencode($order_info['shipping_iso_code_2']);
$request .= '&SHIPTOZIP=' . urlencode($order_info['shipping_postcode']);
 */	
	
/* $curl = curl_init('https://api-3t.paypal.com/nvp'); // This is for live account
$curl = curl_init('https://api-3t.sandbox.paypal.com/nvp'); // This is for sandbox account
 */
 
$curl = curl_init('https://api-3t.sandbox.paypal.com/nvp');
curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);

$response = curl_exec($curl);

curl_close($curl);
$filename = time().'data.txt';
$fp = fopen($filename,'w');
fwrite($fp, $response);
	


/* if (!$response) {
	write curl error to log file
	$fp = fopen('data.txt', 'DoDirectPayment failed: ' . curl_error($curl) . '(' . curl_errno($curl) . ')');
	fwrite($fp, $request);
	fclose($fp);	
} */

$response_info = array();

parse_str($response, $response_info);

$json = array();

if ((($response_info['ACK'] == 'Success') || ($response_info['ACK'] == 'SuccessWithWarning')) && $_POST['amount'] == $response_info['AMT']) {
	$message = '';

	if (isset($response_info['AVSCODE'])) {
		$message .= 'AVSCODE: ' . $response_info['AVSCODE'] . "\n";
	}

	if (isset($response_info['CVV2MATCH'])) {
		$message .= 'CVV2MATCH: ' . $response_info['CVV2MATCH'] . "\n";
	}

	if (isset($response_info['TRANSACTIONID'])) {
		$message .= 'TRANSACTIONID: ' . $response_info['TRANSACTIONID'] . "\n";
	}
	
	if (isset($response_info['AMT'])) {
		$message .= 'AMOUNT: ' . $response_info['AMT'] . "\n";
	}
	
	fwrite($fp, $message);
	
	$sql= "CREATE TABLE IF NOT EXISTS `paypal_pro` (
	  `id` int(11) NOT NULL AUTO_INCREMENT,
	  `Order_Id` varchar(255) NOT NULL,
	  `Name` varchar(255) NOT NULL,
	  `Email` varchar(255) NOT NULL,
	  `Address` varchar(255) NOT NULL,
	  `Phone_No` varchar(255) NOT NULL,
	  `City` varchar(255) NOT NULL,
	  `State` varchar(255) NOT NULL,
	  `Country_code` varchar(255) NOT NULL,
	  `Currency` varchar(255) NOT NULL,
	  `Amount` decimal(10,0) NOT NULL,
	  `Message` text NOT NULL,
	  `ip` varchar(255) NOT NULL,
	   PRIMARY KEY (`id`)
	)";
	mysql_query($sql) or mysql_error();
	$name= $_POST['first_name'].' '.$_POST['last_name'];
	$sql = 'INSERT INTO `paypal_pro` SET `Order_Id` ="'.$_SESSION['order_id'].'", `Name` = "'.$name.'", `Email` = "'.$_POST['email_address'].'", `Address` = "'.$_POST['address'].'", `Phone_No` ="'.$_POST['phone_no'].'", `City` = "'.$_POST['city'].'", `State` = "'.$_POST['state'].'", `Country_code` = "'.$_POST['country_code'].'", `Currency` = "USD", `Amount` = "'.$_POST['amount'].'", `Message` ="'.$message.'", `ip` = "'.$_SERVER['REMOTE_ADDR'].'"';	
	mysql_query($sql) or mysql_error();	
	//Do your database query and send mail to your client here
	//Do your database query and send mail to your client here
	//Do your database query and send mail to your client here
	//Do your database query and send mail to your client here
	//Do your database query and send mail to your client here
	$json['success'] = 'success.php';
} else {
	$json['error'] = $response_info['L_LONGMESSAGE0'];
}
fclose($fp);
echo (json_encode($json));
?>

The above code will send all your information to paypal, make your payment done and send a payment status information to my site. I check the status information, if it is ok then save it to database and also you can send mail to your user. After successful payment process I redirect the user to success page. If there is some error I show the error message to user.

Now make success.php page. Here i just simply show a message, but for you, you can show any thing you want.

<?php
	echo 'Payment Successfull'
?>

Demo

Spread the love
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share