How to integrate Paypal Pro in PHP?

Paypal is best known payment gateway all over the world. Here we are implementing paypal pro. This is really simple example to implement.
paypal_pro.inc.php file need to be included to the form processing logic.

form.php


<form action="process-paypal.php" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="first-name"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last-name"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td>State</td>
<td><input type="text" name="state"></td>
</tr>
<tr>
<td>Zip Code</td>
<td><input type="text" name="zipcode"></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city"></td>
</tr>
<tr>
<td>Cardtype</td>
<td>
<select name="cardtype">
<option name="Mastercard">Mastercard</option>
<option name="Visa">Visa</option>
</select>
</td>
</tr>
<tr>
<td>Card Number</td>
<td><input type="text" name="cardno"></td>
</tr>
<tr>
<td>CVV</td>
<td><input type="text" name="cvv"></td>
</tr>
<tr>
<td>Expiry date</td>
<td>
<select name="exp_month">
<option name="01">January</option>
<option name="02">February</option>
<option name="03">March</option>
<option name="04">April</option>
<option name="05">May</option>
</select>
<select name="exp_yr">
<option name="2015">2015</option>
<option name="2016">2016</option>
<option name="2017">2017</option>
<option name="2018">2018</option>
<option name="2019">2019</option>
</select>
</td>
</tr>
</table>
</form>

process-paypal.php


<?php
$path = "paypal_pro.inc.php";
include_once( $path );
$first_name = $_REQUEST['first-name'];
$last_name = $_REQUEST['last-name'];
$creditCardType = $_REQUEST['cardtype'];
$creditCardNumber = $_REQUEST['cardno'];
$API_USERNAME =  "YOUR_API_USERNAME";
$API_PASSWORD = "YOUR_API_USERPASSWORD";
$API_SIGNATURE ="YOUR_API_SIGNATURE";
$amount = $_REQUEST['payment-amount'];
$card_expire_month = $_REQUEST['exp_month'];
$card_expire_yr = $_REQUEST['exp_yr'];
$card_verfi_no = $_REQUEST['cvv'];//CVV
$address = $_REQUEST['address'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip_code = $_REQUEST['zipcode'];

$currencyCode = "USD";
$paymentAction = "Sale";
$methodToCall = 'doDirectPayment';

$nvpstr='&PAYMENTACTION='.$paymentAction.'&AMT='.$amount.'&CREDITCARDTYPE='.$creditCardType.'&ACCT='.$creditCardNumber.'&EXPDATE='.$card_expire_month.$card_expire_yr.'&CVV2='.$card_verfi_no.'&FIRSTNAME='.$first_name.'&LASTNAME='.$last_name.'&STREET='.$address.'&CITY='.$city.'&STATE='.$state.'&ZIP='.$zip_code.'&COUNTRYCODE=US&CURRENCYCODE='.$currencyCode;

$paypalPro = new paypal_pro($API_USERNAME, $API_PASSWORD, $API_SIGNATURE, '', '', FALSE, FALSE );
$resArray = $paypalPro->hash_call($methodToCall,$nvpstr);

$ack = strtoupper($resArray["ACK"]);
//if payment done successfully
if($ack == "SUCCESS")
{
echo "your payment has been receive successfully. Note your transaction ID for future reference ".$resArray["TRANSACTIONID"];
}
?>

Please download the source code here.Download Code

Leave a Reply