PayPal Direct Payment API Component

2 : Usage Examples

By Mariano Iglesias (mariano)
Useful component that provides a wrapper for PayPal's Direct Payment API, allowing any cake based application to accept payments via Direct Payment (processing credit cards and payments without leaving your website) and Express Checkout (allowing users to use their PayPal account to pay)

Doing a Direct Payment



$order = array(
    'action' => CAKE_COMPONENT_PAYPAL_ORDER_TYPE_SALE,
    'description' => 'CakePHP Component',
    'total' => 100.00,
    'buyer' => array (
        'first' => 'Mariano',
        'last' => 'Iglesias',
        'address1' => '123 CakePHP Street',
        'address2' => 'Apartment A',
        'city' => 'San Diego',
        'state' => 'CA',
        'zip' => 92014,
        'country' => 'US'
    ),
    'cc' => array (
        'type' => 'Visa', // Can be: Visa, MasterCard, Amex, Discover
        'number' => '0000000000000000',
        'expiration' => '01/2010',
        'cvv2' => '999',
        'owner' => array (
            'first' => 'Mariano',
            'last' => 'Iglesias'
        )
    )
);

$this->Paypal->setEnvironment(CAKE_COMPONENT_PAYPAL_ENVIRONMENT_SANDBOX);
$this->Paypal->setUser('ApiUser');
$this->Paypal->setPassword('ApiPassword');
$this->Paypal->setCertificate('cert_perm.txt');
$this->Paypal->setOrder($order);

// Make payment via PayPal

$result = $this->Paypal->directPayment();

// Check PayPal status

if ($result === false)
{
    switch($this->Paypal->getErrorCode())
    {
        case CAKE_COMPONENT_PAYPAL_ERROR_INVALID_CREDIT_CARD:
            echo 'INVALID CREDIT CARD';
            exit;
            break;
        case CAKE_COMPONENT_PAYPAL_ERROR_INVALID_CVV2:
            echo 'INVALID Credit Card Verification Number.';
            exit;
            break;
        default:
            echo 'ERROR: ' . $this->Paypal->getError();
            exit;
            break;
    }
}
else
{
    echo 'Woha! Got the money!';
    echo '<pre>'; print_r($result); echo '</pre>';
    exit;
}


Doing an Express Checkout



Here it gets a little tricky. Remember that I said that you achieve this in two steps:

  1. First call to expressCheckout() after which user gets redirected to PayPal
  2. PayPal calls us back using the URL we specified and we do then another expressCheckout() to perform the actual sale


So let's suppose we're doing this on an action called express() on a controller called Payments. We'll set it to receive one optional parameter indicating if we are receiving PayPal's callback. I'll also add the code here that stores and restores the session:

function express($callback = null)
{
    if (isset($callback) && isset($_REQUEST['csid']))
    {
        // Restore session
        
        if (!$this->Paypal->restoreSession($_REQUEST['csid']))
        {
            $this->redirect('/');
            exit;
        }
    }
    
    // Neither buyer nor credit card information since it
    // is handled by PayPal
    
    $order = array(
        'action' => CAKE_COMPONENT_PAYPAL_ORDER_TYPE_SALE,
        'description' => 'CakePHP Component',
        'total' => 100.00
    );
    
    // Set up common component's parameters
    
    $this->Paypal->setEnvironment(CAKE_COMPONENT_PAYPAL_ENVIRONMENT_SANDBOX);
    $this->Paypal->setUser('ApiUser');
    $this->Paypal->setPassword('ApiPassword');
    $this->Paypal->setCertificate('cert_perm.txt');
    $this->Paypal->setOrder($order);
    
    if (!isset($callback))
    {
        // First call, user gets redirected to PayPal
    
        $this->Paypal->setTokenUrl('http://www.server.com/payments/express/pay?csid=' . session_id());
        $this->Paypal->setCancelUrl('http://www.server.com/payments/express/cancel?csid=' . session_id());
        
        // Save current session
        
        $this->Paypal->storeSession();
    
        // Make payment via PayPal
        
        $result = $this->Paypal->expressCheckout();
        
        if ($result === false)
        {
            echo 'ERROR: ' . $this->Paypal->getError();
            exit;
        }
    }
    else if ($callback == 'cancel')
    {
        echo 'SNIFF... Why not?';
        exit;
    }
    else if ($callback == 'pay')
    {
        // Second call, make payment via PayPal
        
        $result = $this->Paypal->expressCheckout();
        
        // Check PayPal status
        
        if ($result === false)
        {
            echo 'ERROR: ' . $this->Paypal->getError();
            exit;
        }
        else
        {
            echo 'Woha! Got the money!';
            echo '<pre>'; print_r($result); echo '</pre>';
            exit;
        }
    }
}

Page 3: Feedback

Comments 168

CakePHP Team Comments Author Comments
 

Comment

1 PayPal Direct Payment API Version

Hi,

Is the API used the US version for paypal pro?

I am wondering as paypal pro api is now avaliable in the uk but doesn't use the same sdk as the US version.

Would it be easy to have an all-in-one component that handled all version as this could be updated centrally instead of the uk users having to write there own?

Penfold
Posted Nov 28, 2006 by Penfold
 

Comment

2 PayPal Direct Payment API Version

Is the API used the US version for paypal pro?

Penfold: Well I'm using the API they give you when you look for DirectPayment's SDK. I find it hard to believe they would change the API for another country. Can you provide a link (to my email) with the information you have that tells you their SDKs are different for US and UK?
Posted Nov 28, 2006 by Mariano Iglesias
 

Comment

3 Thanks

You have simplified it a lot :)
Posted Dec 31, 1969 by Abhimanyu Grover
 

Question

4 UK version

So... does anyone know if this will now work for the UK also?
Posted Dec 31, 1969 by Dallas Ms
 

Comment

5 Thank You

Thank you very much for providing this.
I'll be trying it out today. It will most definitely save me many, many hours.
Posted Jul 13, 2007 by Antonie Potgieter
 

Question

6 API Question

Controller Class:

<?php 
$this
->Paypal->setEnvironment(CAKE_COMPONENT_PAYPAL_ENVIRONMENT_SANDBOX);
$this->Paypal->setUser('ApiUser');
$this->Paypal->setPassword('ApiPassword');
$this->Paypal->setCertificate('sandbox.paypal.com.pem');
$this->Paypal->setOrder($order);
?>


Hi, where should i put the pem file? technically it is located under {cake}/app/vendors/PEAR/PayPal/cert but the code errors up the says sandbox.paypal.com.pem cannot be located.

Please, badly need help to implement your API.
Posted Aug 4, 2007 by john diegor
 

Comment

7 API Question

I am still testing but when I put 'sandbox.paypal.com.pem' in {cake}/app/webroot/ I stopped getting that error. Hope this helps!
Posted Aug 5, 2007 by Landon
 

Comment

8 important

this is the way to solve the certificate problem :

- Remove setCertificate and replace it with
$this->Paypal->setSignature('signature provided by paypal');.
and people it worked!!
Posted Oct 27, 2007 by joren
 

Question

9 Cake 1.2 integration for the Paypal component

Has someone created a similar component to work with Paypal API on Cake 1.2? I tried this component on 1.2 but it doesn't seem to work right. Errors here and there.
Posted Oct 28, 2007 by Tengku Zahasman
 

Comment

10 Cake 1.2 integration for the Paypal component

@Tengku: what errors are you seeing? "Erros here and there" is by no means a help for me to fix them, is it? BTW I'm using it on a 1.2 application and have not seen anything bad happening, so PLEASE report the errors you are getting.
Posted Oct 28, 2007 by Mariano Iglesias
 

Comment

11 Fatal Error

I am trying to use the component but I am getting the following example when trying to run the example.
Warning (2): require_once(PayPal.php) [function.require-once]: failed to open stream: No such file or directory [APP/controllers/components/paypal.php, line 12]
Posted Mar 27, 2008 by Donald Walters
 

Comment

12 Complicated Solutions

Hi!
I try to implement the whole tutorials but it seems...it's quit difficult for me to understand with specially it doesn't give any instruction where to go. Where to put the a certain code. Specially the form and it's components.

Can u or anyone could help me how to make a step by step work in cooking paypal payment methods? Sorry but i'm just a newbie,...could anyone help me???

thnks
Posted May 8, 2008 by geri