PayPal APIs_ Up and Running_ A Developer's Guide - Michael Balderas [4]
Tip
API operations can change between versions, so when you change a version number, I recommend retesting your application code before going live.
Figure 1-3 outlines the API operation of an NVP request, and Figure 1-4 shows the same transaction with credentials provided.
Figure 1-3. NVP request
Figure 1-4. NVP request with credentials
Putting it together
Now that we have the basic elements laid out, let’s put together a sample URL encoded NVP request via PHP, shown in Examples 1-1 and 1-2.
Example 1-1. developercredentials.php
//PayPal NVP API Test Developer Credentials//
$paypalusername = sdk-three_api1.sdk.com;
$paypalpassword = QFZCWN5HZM8VBG7Q;
$paypalsignature = A-IzJhZZjhg29XQ2qnhapuwxIDzyAZQ92FRP5dqBzVesOkzbdUONzmOU;
$paypalserver = api-3t.sandbox.paypal.com/nvp
?>
Example 1-2. simpletransactionrequestprocessor.php
// PayPal NVP API Simple Transaction Request Processor//
// Include the developercredentials.php file for relevant information
include("../path/outside/webroot/developercredentials.php");
// Build the credentials format of the Request String
$credentials= "USER=$paypaluser&PWD=$paypalpwd&SIGNATURE=$paypalsig";
// Designate the API Method we are calling to have handled
$method = api_method_to_use;
$version = method_version_to_use;
// Build Initial Request string
$request = $method."&".$version."&".$credentials;
// Walk the posted form elements to gather additional information
// to pass URLEncoded to API via the request string
foreach ($_POST as $key => $value){
$value = urlencode(stripslashes($value));
$request. = "&$key=$value";
};
//Build transaction and execute via curl
$ch = curl_init();
// Ensure communication is done via SSL and over a fully verified
// SSL key and certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
// Return response as a string from server
curl_setopt($ch, CURL_RETURNTRANSFER, 1);
// Post values to server via URLEncoded string
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//Execute Request
$response = curl_exec($ch);
?>
Tip
Notice that in Example 1-2, we reference the developercredentials.php file from a path outside the webroot. As stated earlier, this will ensure that no one can access your credentials file directly from their web browser and ensures that this information stays secure. If we were satisfied with this code and wanted to go to production, we would then change this path to the location of our production credentials file.
Parsing an NVP Response
When it comes to parsing an NVP response, your application really has to accomplish only one major step: URL decoding.
URL decoding
URL decoding the response from PayPal is basically just the reverse of URL encoding the values to pass to PayPal. For example:
NAME=John+Doe&Company=Acme+Goods+%26+Services
is decoded as follows:
NAME=John Doe&COMPANY= Acme Goods & Services
As with URL encoding, each application language typically has a URL decode method built into the language. Refer to the list in Table 1-3.
Table 1-3. URL decoding methods
Application language Function Method name
ASP.NET Decode System.Web.HttpUtility.UrlDecode(buffer, Encoding.Default)
Classic ASP Decode No built-in function; several implementation examples are available on the Internet
Java Decode java.net.URLDecoder.decode
PHP Decode urldecode()
ColdFusion Decode URLDecodeurlEncodedString[, charset])
Response format
Each NVP API response is composed of an acknowledgment (or