Zend Framework and PayPal API – Part 1 of 2

Zend Framework and Paypal. Leveraging the Zend Framework to use the PayPal NVP API is accomplished using the Zend_Http_Client class, and PayPal API credentials. The following article series shows how to complete a transaction using DoDirectPayment (Website Payments Pro), and how to DoExpressCheckout (Express Checkout) using the Zend_Http_Client class. Much of this information was compiled using the PayPal Name-Value Pair API Developer Guide, and PayPal’s examples, along with the Zend Framework documentation.

In part 1, I will be showing you how to accomplish a call to the DoDirectPayment PayPal NVP API using the Zend_Http_Client class.

The PayPal API Client

First off, let’s build a PayPal API client that will extend the Zend_Http_Client. This will help keep our code properly organized, and object-oriented.

You will need to register at https://developer.paypal.com/ for test API credentials.

class Paypal_Client extends Zend_Http_Client
{
      private $_api_version = '56.0';
      private $_api_username = 'your_user_name'; // These differ depending if you're in live mode or test mode.
      private $_api_password = 'your_password'; // I'm certain you can come up with something to take that into account. :-)
      private $_api_signature = 'your_api_signature';
 
}

Next, we will want to add methods that call ‘doExpressCheckout’ and call ‘doDirectPayment’ via PayPal’s NVP API. But before we do that, let’s make our life a little easier and ensure our HTTP client always adds required fields for us before sending any API calls. To accomplish this, we can simply override the class __construct() method.

function __construct($uri = null, $options = null)
{
      parent::__construct($uri, $options);
 
      // NOTE: Parameters must always be url encoded, as per PayPal documentation.
      $this->setParameterGet('USER', urlencode($this->_api_username));
      $this->setParameterGet('PWD', urlencode($this->_api_password);
      $this->setParameterGet('SIGNATURE', urlencode($this->_api_signature));
      $this->setParameterGet('VERSION', urlencode($this->_api_version));
}

DoDirectPayment

The DoDirectPayment method allows us to authorize and capture funds without the need of a customer signing in to paypal. The customer simply fills out our form with relevant fields, and we submit our data via the PayPal DoDirectPayment NVP API call. The upside of this method is the ease of implementation. Send the API call, save the transaction ID PayPal sends back, and continue on our merry way. The downside (or, other upside, depending on your monthly profits) is, your PayPal account must be configured with Website Payments Pro, which has a fixed monthly fee.

Here is the method that belongs within our class in order to handle the PayPal DoDirectPayment NVP API call using the inherited Zend_Http_Client functionality:

/**
 * Calls the 'DoDirectPayment' API call. Note - Keep track of the
 * transaction ID on success! You'll need it to get transaction details
 * at a later date.
 *
 * @param float $amount
 * @param string $credit_card_type
 * @param string $credit_card_number
 * @param string $expiration_month
 * @param string $expiration_year
 * @param string $cvv2
 * @param string $first_name
 * @param string $last_name
 * @param string $address1
 * @param string $address2
 * @param string $city
 * @param string $state
 * @param string $zip
 * @param string $country
 * @param string $currency_code
 * @param string $ip_address
 * @param string $payment_action Can be 'Authorization' (default) or 'Sale'
 *
 * @return Zend_Http_Response
 * @throws Zend_Http_Client_Exception
 */
function doDirectPayment(
      $amount,
      $credit_card_type,
      $credit_card_number,
      $expiration_month,
      $expiration_year,
      $cvv2,
      $first_name,
      $last_name,
      $address1,
      $address2,
      $city,
      $state,
      $zip,
      $country,
      $currency_code,
      $ip_address,
      $payment_action = 'Sale'
) {
      $this->setParameterGet('METHOD', 'DoDirectPayment');
 
      $expiration_date = str_pad($expiration_month, 2, STR_PAD_LEFT) .
            $expiration_year;
 
      $this->setParameterGet('PAYMENTACTION', urlencode($payment_action)); // Can be 'Authorization', or 'Sale'
      $this->setParameterGet('AMT', urlencode($amount));
      $this->setParameterGet('CREDITCARDTYPE', urlencode($credit_card_type));
      $this->setParameterGet('ACCT', urlencode($credit_card_number));
      $this->setParameterGet('EXPDATE', urlencode($expiration_date));
      $this->setParameterGet('CVV2', urlencode($cvv2));
      $this->setParameterGet('FIRSTNAME', urlencode($first_name));
      $this->setParameterGet('LASTNAME', urlencode($last_name));
      $this->setParameterGet('STREET', urlencode($address1));
 
      if (!empty($address2)) {
            $this->setParameterGet('STREET2', urlencode($address2));
      }
 
      $this->setParameterGet('CITY', urlencode($city));
      $this->setParameterGet('STATE', urlencode($state));
      $this->setParameterGet('ZIP', urlencode($zip));
      $this->setParameterGet('COUNTRYCODE', urlencode($country));
      $this->setParameterGet('CURRENCYCODE', urlencode($currency_code));
      $this->setParameterGet('IPADDRESS', urlencode($ip_address));
 
      return $this->request(Zend_Http_Client::GET);
 
}

Now that we have our code in place, we can try our stuff out. Lets make a PHP script to handle our form.

<?php
if (isset($_REQUEST['submit'])
{
      // Live URL: https://api-3t.paypal.com/nvp
      // Test URL: https://api-3t.sandbox.paypal.com/nvp
      $url = 'https://api-3t.paypal.com/nvp';
 
      $amount = 10.00; // Obviously, we would sum up the contents of some cart to fill in this value.
      $credit_card_type = $_REQUEST['credit_card_type'];
      $credit_card_number = $_REQUEST['credit_card_number'];
      $expiration_month = $_REQUEST['expiration_month'];
      $expiration_year = $_REQUEST['expiration_year'];
      $cvv2 = $_REQUEST['cvv2'];
 
      $first_name = $_REQUEST['first_name'];
      $last_name = $_REQUEST['last_name'];
      $address1 = $_REQUEST['address1'];
      $address2 = $_REQUEST['address2'];
      $city = $_REQUEST['city'];
      $state = $_REQUEST['state'];
      $zip = $_REQUEST['zip'];
 
      $country = 'US'; // Assuming we are only accepting transactions within the United States.
      $currency_code = 'USD'; // Assuming we are using the United States Dollar
      $ip_address = $_SERVER['REMOTE_ADDR']; // Get the IP Address, assuming we are in a LAMP environment
 
      // Create an instance of our PayPal NVP client
      $client = new PayPal_Client($url);
 
      // Send our API request!
      $result = $client->doDirectPayment(
            $amount,
            $credit_card_type,
            $credit_card_number,
            $expiration_month,
            $expiration_year,
            $cvv2,
            $first_name,
            $last_name,
            $address1,
            $address2,
            $city,
            $state,
            $zip,
            $country,
            $currency_code,
            $ip_address);
 
      // Remember to store the transaction ID! You'll need it 
      // to lookup the transaction details. For now, let's just
      // display the results.
      echo $result->getBody();
}
?>
<form>
      <ul>
            <li><label>First Name:</label><input type="" name="" value="" /></li>
            <li><label>Last Name:</label><input type="" name="" value="" /></li>
            <li><label>Address, Line 1:</label><input type="" name="" value="" /></li>
            <li><label>Address, Line 2:</label><input type="" name="" value="" /></li>
            <li><label>City:</label><input type="" name="" value="" /></li>
            <li><label>State (2-Letter):</label><input type="" name="" value="" /></li>
            <li><label>Zip:</label><input type="" name="" value="" /></li>
      </ul>
      <ul>
            <li><label>Card Type</label><input type="" name="" value="" /></li>
            <li><label></label><input type="" name="" value="" /></li>
      </ul>
</form>

…And that concludes part 1 of 2.

Stay tunned for Part 2 of this article series: Zend Framework and the PayPal API – Part 2 of 2.

NEXT: Zend Framework and the PayPal API – Part 2 of 2

NOTE: These samples assume you have configured auto-loading within your environment. Needless to say, you should fill in the API credentials with your own data before attempting to run these samples. Additionally, you could also move credential information into a different location instead of hard-coding credentials into a class. If you find something that can be improved, feel free to leave a comment. Thanks!

7 thoughts on “Zend Framework and PayPal API – Part 1 of 2

  1. Hi Alex,

    Thanks for the code. Can you shed more light on how I can integrate this into a zend project please. Your prompt response will be highly appreciated.

  2. Pingback: Zend Framework and PayPal API – Part 2 of 2 | Alex Venture Project

  3. Oh hey Alex. I’m sorry I got confused. Karl was just a commenter right? Sorry about that.

    Do you think you could help us out with the problem we’re having? We are developing a PayPal app that allows users to donate to Website Payments Pro right on a Facebook Canvas iFrame, so you’re tutorial is perfect for this.

    We can donate some money to you if we can get it up and running…

    Thanks again Alex!

  4. Lol…If your commenting about Karl’s tutorial on my tutorial page, I’m flattered (this is actually my tutorial)… :P

  5. Hey there Karl. I’m trying to set up a PayPal app using your tutorial and am getting a method not found error, even though it is in the file:

    I load Zend at the top:

    require_once ‘Zend/Loader.php’;
    require_once ‘Zend/Http/Client.php’;

    Then extend Zend_Http_Client with Paypal_Client like above.

    Then the __construct and the doDirectPayment function.

    Yet when I submit, it gives a “Fatal error: Call to undefined method Paypal_Client::doDirectPayment() in…” pointing to the line: $result = $client->doDirectPayment( …

    I’ve got everything just like you did above. Do you know what I’m doing wrong?

    Thanks in advance if you are able to help on this!

    -Luke

  6. // NOTE: Parameters must always be url encoded, as per PayPal documentation.
    $this->setParameterGet(‘USER’, urlencode($this->_api_username));

    No need to encode here as the setParameterGet encodes input automaically!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">