Paypal Pro Direct Payment Sale Explained - NVP Example

1.Introduction  2.Code and Explanation  3.Download Source Code  4.Comments

Introduction

Paypal direct payment works in a few steps instead of loading the actual paypal page and then logging in and returning to the merchant page. As the name suggests it is direct payment. Just like ajax but in php. There are many options present in this zip file. I have dealt with direct payment in this page. The following explanation are very simple and if you want to make every thing in one single file then go to this link.

The following process involves very few steps like 1 a form to get user details and credit card details, 2. submit that form to a processing page in which you will give three constants (email id of the merchant and apikeys), 3. make a query string form the post values of the form. As you had already included the basic paypal include files you will create a paypal class object using which you will call a function to create an instance of that object. You will pass the email and api keys are parameters. 4. Make a curl connection to the payment gateway url and post your request parameters. Paypal will receive your request and process the payment and returns a response. The response will be converted to an array by paypal code. And according to the result key values pairs you can find whether the payment is success of failure. If success a transaction code is returned and you can store that in your database for future reference about that payment. If failure then you will get an error code and error description.

The code contains few files. I have explained the important parts of that file and you can understand the rest of the code from the code sample very easily.

For a very simple version you can refer this page Direct Payment Simplified

Code and Explanation

Firstly you download the php code from this PAYAPL URL. And then come back to this explanation. In the following i have explained step by step starting with the first page that is the form till processing the response which will not be more than three pages. Extract that zip in your local server folder and run the index.html through the localhost server. You can see a list of links and in that click DoDirectPayment - Sale link. Another page(DoDirectPayment.php?paymentType=Sale) will appear which will show a basic form with the needed fields for payment. Those are the fields you have to include in your code.

When you submit the form it is submited to DoDirectPaymentReceipt.php. Here is where all the process takes place. In this script you can see that a file callerservice.php is included and that file further includes constants.php where you can define the merchant email id and api keys. After including callerservice.php and constants.php this dodirectpaymentreceipt.php fetches the values from post variable.

The constants file will have variables where you can give your merchants details and the paypal payment gateway url for sandbox and for live process.

/**
 * Get required parameters from the web form for the request
 */
$paymentType =urlencode( $_POST['paymentType']);
$firstName =urlencode( $_POST['firstName']);
$lastName =urlencode( $_POST['lastName']);
$creditCardType =urlencode( $_POST['creditCardType']);
$creditCardNumber = urlencode($_POST['creditCardNumber']);
$expDateMonth =urlencode( $_POST['expDateMonth']);

// Month must be padded with leading zero
$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=urlencode($_POST['currency']);
$currencyCode="USD";
$paymentType=urlencode($_POST['paymentType']);
and then it constructs a query string as in the following code
/* Construct the request string that will be sent to PayPal.
   The variable $nvpstr contains all the variables and is a
   name value pair string with & as a delimiter */
$nvpstr="&PAYMENTACTION=$paymentType&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";
Now the post string is posted to the payment gateway and the response is received in the variable $resArray as in the following code.
/* Make the API call to PayPal, using API signature.
   The API response is stored in an associative array called $resArray */
$resArray=hash_call("doDirectPayment",$nvpstr);

For success you can do it like the following

if($ack=="SUCCESS")  
// store the transaction id in the database with other details
else
// show the error message
//besides for testing you can print all the key values pairs to debug

    		foreach($resArray as $key => $value) {
    			
    			echo "<br /> $key: $value";
   			}	
This is it for now. TO DO A DIRECT PAYMENT IN ONE SHOT PLEASE REFER THIS PAGE

Download Source Code

I used the zip file from paypal.com. This is the one i have mentioned above. Click here to download the zip file from paypal for direct payment

Here is a complete source code zip file from paypal:
https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_PHP_WPS_Toolkit.zip

That is all folks. Enjoy.

By -

Comments, Suggestions, Objections, ...