Paypal credit card payment on website in PHP

Paypal credit card payment on website in PHP

PayPal Credit Card, the payment process will do on your website. In this post, I'm goes to explain how you can use PayPal credit card payment to Do Direct payment.

I'm using PayPal sandbox account, for this you have to create a sandbox account with type `BUSINESS` and country `US`.

In live mode Payments only works with PayPal Payments Pro business account and PayPal Payments Pro authorized countries are US, Canada & UK. Means Paypal pro only working in these countries US, Canada and UK.

Before getting started to implement PayPal Pro payment gateway in PHP, take a look at the files structure.

  • index.php
  • paypal_pro.php
  • payment.php

While creating files, update your PayPal credials in `payment.php`

$paypalPro = new paypal_pro('YOUR PAYPAL EMAIL', 'YOUR PAYPAL PASSWORD', 'YOUR PAYPAL SIGNATURE', '', '', FALSE, FALSE );

 

index.php

<form method="POST" action="payment.php" name="perform">
<input type=hidden name=paymentType value="<?=$paymentType?>" />
<table width="600" border="0">
<tr>
<td align=right>First Name:</td>
<td align=left>
<input type=text size=30 maxlength=32 name=firstName value=John>
</td>
</tr>
<tr>
<td align=right>Last Name:</td>
<td align=left>
<input type=text size=30 maxlength=32 name=lastName value=Doe>
</td>
</tr>
<tr>
<td align=right>Card Type:</td>
<td align=left>
<select name="creditCardType ">
<option value=Visa selected>Visa</option>
<option value=MasterCard>MasterCard</option>
<option value=Discover>Discover</option>
<option value=Amex>American Express</option>
</select>
</td>
</tr>
<tr>
<td align=right>Card Number:</td>
<td align=left>
<input type=text size=19 maxlength=19 name=creditCardNumber>
</td>
</tr>
<tr>
<td align=right>Expiration Date:</td>
<td align=left>
<p>
<select name=expDateMonth>
<option value=1>01</option>
<option value=2>02</option>
<option value=3>03</option>
<option value=4>04</option>
<option value=5>05</option>
<option value=6>06</option>
<option value=7>07</option>
<option value=8>08</option>
<option value=9>09</option>
<option value=10>10</option>
<option value=11>11</option>
<option value=12>12</option>
</select>
<select name=expDateYear>
<option value=2021>2021</option>
<option value=2022>2022</option>
<option value=2023>2023</option>
<option value=2024>2024</option>
<option value=2025>2025</option>
<option value=2026>2026</option>
</select>
</p>
</td>
</tr>
<tr>
<td align=right>CVV:</td>
<td align=left>
<input type=text size=3 maxlength=4 name=cvv2Number value=962>
</td>
</tr>
<tr>
<td align=right>
<br><b>Billing Address:</b>
</td>
</tr>
<tr>
<td align=right>Address 1:</td>
<td align=left>
<input type=text size=25 maxlength=100 name=address1 value="1 Main St ">
</td>
</tr>
<tr>
<td align=right>Address 2:</td>
<td align=left>
<input type=text size=25 maxlength=100 name=address2>(optional)</td>
</tr>
<tr>
<td align=right>City:</td>
<td align=left>
<input type=text size=25 maxlength=40 name=city value="San Jose ">
</td>
</tr>
<tr>
<td align=right>State:</td>
<td align=left>
<select id=state name=state>
<option value=></option>
<option value=AK>AK</option>
<option value=AL>AL</option>
<option value=AR>AR</option>
<option value=AZ>AZ</option>
<option value=CA selected>CA</option>
<option value=CO>CO</option>
<option value=CT>CT</option>
<option value=DC>DC</option>
<option value=DE>DE</option>
<option value=FL>FL</option>
<option value=GA>GA</option>
<option value=HI>HI</option>
<option value=IA>IA</option>
<option value=ID>ID</option>
<option value=IL>IL</option>
<option value=IN>IN</option>
<option value=KS>KS</option>
<option value=KY>KY</option>
<option value=LA>LA</option>
<option value=MA>MA</option>
<option value=MD>MD</option>
<option value=ME>ME</option>
<option value=MI>MI</option>
<option value=MN>MN</option>
<option value=MO>MO</option>
<option value=MS>MS</option>
<option value=MT>MT</option>
<option value=NC>NC</option>
<option value=ND>ND</option>
<option value=NE>NE</option>
<option value=NH>NH</option>
<option value=NJ>NJ</option>
<option value=NM>NM</option>
<option value=NV>NV</option>
<option value=NY>NY</option>
<option value=OH>OH</option>
<option value=OK>OK</option>
<option value=OR>OR</option>
<option value=PA>PA</option>
<option value=RI>RI</option>
<option value=SC>SC</option>
<option value=SD>SD</option>
<option value=TN>TN</option>
<option value=TX>TX</option>
<option value=UT>UT</option>
<option value=VA>VA</option>
<option value=VT>VT</option>
<option value=WA>WA</option>
<option value=WI>WI</option>
<option value=WV>WV</option>
<option value=WY>WY</option>
<option value=AA>AA</option>
<option value=AE>AE</option>
<option value=AP>AP</option>
<option value=AS>AS</option>
<option value=FM>FM</option>
<option value=GU>GU</option>
<option value=MH>MH</option>
<option value=MP>MP</option>
<option value=PR>PR</option>
<option value=PW>PW</option>
<option value=VI>VI</option>
</select>
</td>
</tr>
<tr>
<td align=right>ZIP Code:</td>
<td align=left>
<input type=text size=10 maxlength=10 name=zip value=95131>(5 or 9 digits)</td>
</tr>
<tr>
<td align=right>Country:</td>
<td align=left>United States</td>
</tr>
<tr>
<td align=right>
<br>Amount:</td>
<td align=left>
<br>
<input type=text size=4 maxlength=7 name=amount value=10.00>USD</td>
</tr>
<tr>
<td align=right>
<br>Recurring:</td>
<td align=left>
<br>
<input type="checkbox " value="1 " name="recurring ">Enable</td>
</tr>
<tr>
<td>
<input type=Submit value=Submit>
</td>
</tr>
</table>
</form>

paypal_pro.php

<?php
class paypal_pro
{
    public $API_USERNAME;
    public $API_PASSWORD;
    public $API_SIGNATURE;
    public $API_ENDPOINT;
    public $USE_PROXY;
    public $PROXY_HOST;
    public $PROXY_PORT;
    public $PAYPAL_URL;
    public $VERSION;
    public $NVP_HEADER;
    function __construct($API_USERNAME, $API_PASSWORD, $API_SIGNATURE, $PROXY_HOST, $PROXY_PORT, $IS_ONLINE = false, $USE_PROXY = false, $VERSION = '57.0')
    {
        $this->API_USERNAME = $API_USERNAME;
        $this->API_PASSWORD = $API_PASSWORD;
        $this->API_SIGNATURE = $API_SIGNATURE;
        $this->API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp';
        $this->USE_PROXY = $USE_PROXY;
        if ($this->USE_PROXY == true)
        {
            $this->PROXY_HOST = $PROXY_HOST;
            $this->PROXY_PORT = $PROXY_PORT;
        }
        else
        {
            $this->PROXY_HOST = '127.0.0.1';
            $this->PROXY_PORT = '808';
        }
        if ($IS_ONLINE == false)
        {
            $this->PAYPAL_URL = 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=';
        }
        else{

            $this->PAYPAL_URL = 'https://www.paypal.com/webscr&cmd=_express-checkout&token=';

        }
        $this->VERSION = $VERSION;
    }
    function hash_call($methodName, $nvpStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->API_ENDPOINT);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        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);

        if ($this->USE_PROXY)
        {
            curl_setopt($ch, CURLOPT_PROXY, $this->PROXY_HOST . ":
                " . $this->PROXY_PORT);
            }
            $nvpreq = "METHOD = " . urlencode($methodName) . " & VERSION = " . urlencode($this->VERSION) . " & PWD = " . urlencode($this->API_PASSWORD) . " & USER = " . urlencode($this->API_USERNAME) . " & SIGNATURE = " . urlencode($this->API_SIGNATURE) . $nvpStr;

            curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
            $response = curl_exec($ch);
            $nvpResArray = $this->deformatNVP($response);
            $nvpReqArray = $this->deformatNVP($nvpreq);
            $_SESSION['nvpReqArray'] = $nvpReqArray;

            if (curl_errno($ch))
            {
                die("CURLsendaerrorduringperformoperation:
                    " . curl_errno($ch));
                }
                else
                {
                    curl_close($ch);
                }
                return $nvpResArray;
            }

            function deformatNVP($nvpstr)
            {
                $intial = 0;
                $nvpArray = array();

                while (strlen($nvpstr))
                {
                    $keypos = strpos($nvpstr, '=');
                    $valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr);
                    $keyval = substr($nvpstr, $intial, $keypos);
                    $valval = substr($nvpstr, $keypos + 1, $valuepos - $keypos - 1);
                    $nvpArray[urldecode($keyval) ] = urldecode($valval);
                    $nvpstr = substr($nvpstr, $valuepos + 1, strlen($nvpstr));
                }
                return $nvpArray;
            }
            function __destruct()
            {
            }
     }      

payment.php

<table border='0' width='40%' cellspacing='2' cellpadding='2' align="center">
<?php
require_once ("paypal_pro . php");
$firstName = urlencode($_POST['firstName']);
$lastName = urlencode($_POST['lastName']);
$creditCardType = urlencode($_POST['creditCardType']);
$creditCardNumber = urlencode($_POST['creditCardNumber']);
$expDateMonth = urlencode($_POST['expDateMonth']);
$padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);
$expDateYear = urlencode($_POST['expDateYear']);
$cvv2Number = urlencode($_POST['cvv2Number']);
$address1 = urlencode($_POST['address1']);
$address2 = urlencode($_POST['address2']);
$city = urlencode($_POST['city']);
$state = urlencode($_POST['state']);
$zip = urlencode($_POST['zip']);
$amount = urlencode($_POST['amount']);
$currencyCode = "USD";
$paymentAction = urlencode("Sale");

if ($_POST['recurring'] == 1) // For Recurring
{
    $profileStartDate = urlencode(date('Y-m-d h:i:s'));
    $billingPeriod = urlencode($_POST['billingPeriod']); // or "Day", "Week", "SemiMonth", "Year"
    $billingFreq = urlencode($_POST['billingFreq']); // combination of this and billingPeriod must be at most a year
    $initAmt = $amount;
    $failedInitAmtAction = urlencode("ContinueOnFailure");
    $desc = urlencode("Recurring $" . $amount);
    $autoBillAmt = urlencode("AddToNextBilling");
    $profileReference = urlencode("Anonymous");
    $methodToCall = 'CreateRecurringPaymentsProfile';
    $nvpRecurring = '&BILLINGPERIOD=' . $billingPeriod . '&BILLINGFREQUENCY=' . $billingFreq . '&PROFILESTARTDATE=' . $profileStartDate . '&INITAMT=' . $initAmt . '&FAILEDINITAMTACTION=' . $failedInitAmtAction . '&DESC=' . $desc . '&AUTOBILLAMT=' . $autoBillAmt . '&PROFILEREFERENCE=' . $profileReference;

}
else{
    $nvpRecurring = ";
    $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 . $nvpRecurring;
$paypalPro = new paypal_pro('YOUR PAYPAL EMAIL', 'YOUR PAYPAL PASSWORD', 'YOUR PAYPAL SIGNATURE', ", ", false, false);
$resArray = $paypalPro->hash_call($methodToCall, $nvpstr);
$ack = strtoupper($resArray["ACK"]);

if ($ack != "SUCCESS"){
    echo '<tr>';
    echo '<td colspan="2" style="font-weight:bold;color:red;" align="center">Error! Please check that u will provide all information correctly :(</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td align="right">Ack:</td>';
    echo '<td>' . $resArray["ACK"] . '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td align="right">Correlation ID:</td>';
    echo '<td>' . $resArray['CORRELATIONID'] . '</td>';
    echo '</tr>';
}
else{
    echo '<tr>'
    echo '<td colspan="2" style="font-weight:bold;color:Green" align="center">Thank You For Your Payment :)</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td align="right"> Transaction ID:</td>';
    echo '<td>' . $resArray["TRANSACTIONID"] . '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td align="right"> Amount:</td>';
    echo '<td>' . $currencyCode . $resArray['AMT'] . '</td>';
    echo '</tr>';
}
?>
</table>

 

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Mohammad Nadeem
Mohammad Nadeem
17 Dec 2020

I found this plugin in 50 bucks. You save my time and money 😊

jack roley
jack roley
20 Dec 2020

I did not try with live credentials, but now working fine with sandbox. Thanks

Sadiq Hussain
Sadiq Hussain
21 Dec 2020

Thanks for the code...

Add your comment