PayPal Direct Payment API Component
2 : Usage Examples
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:
- First call to expressCheckout() after which user gets redirected to PayPal
- 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;
}
}
}
Comments
Comment
1 PayPal Direct Payment API Version
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
Comment
2 PayPal Direct Payment API Version
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?
Comment
3 Thanks
Question
4 UK version
Comment
5 Thank You
I'll be trying it out today. It will most definitely save me many, many hours.
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.
Comment
7 API Question
Comment
8 important
- Remove setCertificate and replace it with
$this->Paypal->setSignature('signature provided by paypal');.
and people it worked!!
Question
9 Cake 1.2 integration for the Paypal component
Comment
10 Cake 1.2 integration for the Paypal component
Comment
11 Fatal Error
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]Comment
12 Complicated Solutions
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