Then, based on the value of $sandbox we’re setting values to other variables for our API information. You’ll just want to fill in those placeholders with your own details accordingly. Now we’re ready to build our credit card processing script. ## Step 3: Create an API Request Now we can begin to build our process-credit-card.php page. The first thing we need to do here is include our config file. Next, we need to build a name-value-pair string that includes all of the data we need to send PayPal in order to process this payment. A name-value-pair string looks just like something you might see when passing data via URL parameters. We just need to make sure our parameter names are in all caps. So, you might be thinking to yourself “How do I know what to use for my variable names in my string?” The good news is PayPal provides very good documentation on this. We can see all of the possible variables that we can pass PayPal including customer details, order item details, and credit card information. Some of this information is required in order to process a payment, but many of the variables available are optional. For demonstration purposes, we’ll keep this pretty simple and just pass the required information. We’ll store all of our request parameters in an array so that we can loop through this array to easily generate our NVP string. All requests require the following parameters by default: • METHOD – The name of the API call you’re making. • USER – The API username • PWD – The API password • SIGNATURE – The API signature • VERSION – The API version Then you can refer to the PayPal documentation for any API request you’d like to make to see what other parameters should be included. For the sake of this demonstration, our array will be built as follows. You’ll notice we’re using our config variables from config.php, and then I’m simply loading static data for the other values. In a standard project, though, you’ll most likely be populating these values with form data, session data, or some other form of dynamic data. Now we can simply loop through this array to generate our NVP string. The value of $nvp_string is now:

This string is what we'll send to PayPal for our request.

## Step 4: Send the HTTP Request to PayPal

Now that our NVP string is ready to go we need to send this to the PayPal server to be processed accordingly.  To do this, we’ll use PHP’s CURL methods.

Here you can see that we’ve setup CURL with a few simple options and we’re using our $api_endpoint and $nvp_string variables accordingly.

This data will be sent over to PayPal and we will receive the API response back in our $result variable so that we can see the result and send the user to a successful or failure page based on whether or not the call succeeded or not. ## Step 5: Parse the API Response The value that we get back in $result from the previous step will be an NVP string just like the one we generated and sent to PayPal.  When we run our current script we get a successful response back that looks like this:

One very simple way to parse this result is to use PHP’s parse_str() function.  This will load all of the response data into PHP variables matching the names and values returned in the response.  For example, if we do the following:

• $TIMESTAMP • $CORRELATIONID
• $ACK • $VERSION
• $BUILD • $AMT
• $CURRENCYCODE • $AVSCODE
• $CVV2MATCH • $TRANSACTIONID

We can then proceed to use these variables to present information back to our customer, populate values in email receipts we’d like to generate, update database information, or anything else we need to do once an order is completed.

The $ACK value is what will tell us whether or not the API call was successful or not. Values for $ACK can be:

• Success
• SuccessWithWarning
• Failure
• FailureWithWarning
•

You can simply redirect your user where they need to go and show them information based on this value.

A failing API call will result in additional parameters that provide information about why the transaction failed.  If I run this test again with an invalid credit card number, for example, I get the following response back from PayPal:

Now, when we use parse_str() we end up with the following PHP variables available to us:

• $TIMESTAMP • $CORRELATIONID
• $ACK • $VERSION
• $BUILD • $L_ERRORCODE0
• $L_SHORTMESSAGE0 • $L_LONGMESSAGE0
• $L_SEVERITYCODE0 • $AMT
• $CURRENCYCODE In this case, $ACK shows a Failure so we know the call did not succeed and we can check the error parameters for more details about what went wrong.

While the previous method of parsing the response works just fine, I personally prefer to work with data arrays.  As such, I use the following function to convert the PayPal response into an array.

This allows me to see all of the response data available by simply looking at the contents of the array:

If I run my script again now I get the following result on screen:

And If I were to cause an error again I get the following:

You can see this is a nice, easy to navigate result array that contains everything we might need to move the user through our application and update data sources as necessary.

## Conclusion

As you can see, processing credit cards using PayPal Payments Pro is actually a very simple procedure.  It just involves a few standard steps for working with API web services, and a basic knowledge of working with array data can help as well.

Good luck, and happy coding!