A CakePHP plugin to interact with Paypal's "classic" and new REST APIs.
- CakePHP 2.x
- A PayPal Website Payments Pro account
[Manual]
- Download this: http://github.com/robmcvey/cakephp-paypal/zipball/master
- Unzip that download.
- Copy the resulting folder to
app/Plugin - Rename the folder you just copied to
Paypal
[GIT Submodule]
In your app directory type:
git submodule add -b master git://github.com/robmcvey/cakephp-paypal.git Plugin/Paypal
git submodule init
git submodule update[GIT Clone]
In your Plugin directory type:
git clone -b master git://github.com/robmcvey/cakephp-paypal.git PaypalMake sure the plugin is loaded in app/Config/bootstrap.php.
You can either load them one by one or all of them in a single call:
CakePlugin::loadAll(); // Loads all plugins at once
CakePlugin::load('Paypal'); // Loads a single plugin named Paypal
Create an instance of the class with your PayPal credentials. For testing purposes, ensure sandboxMode is set to true.
App::uses('Paypal', 'Paypal.Lib');
$this->Paypal = new Paypal(array(
'sandboxMode' => true,
'nvpUsername' => '{username}',
'nvpPassword' => '{password}',
'nvpSignature' => '{signature}'
));Create an order(s) in the following format. setExpressCheckout will return a string URL to redirect the customer to.
$order = array(
'description' => 'Your purchase with Acme clothes store',
'currency' => 'GBP',
'return' => 'https://www.my-amazing-clothes-store.com/review-paypal.php',
'cancel' => 'https://www.my-amazing-clothes-store.com/checkout.php',
'custom' => 'bingbong',
'items' => array(
0 => array(
'name' => 'Blue shoes',
'description' => 'A pair of really great blue shoes',
'tax' => 2.00,
'shipping' => 0.00,
'subtotal' => 8.00,
),
1 => array(
'name' => 'Red trousers',
'description' => 'Tight pair of red pants, look good with a hat.',
'tax' => 2.00,
'shipping' => 2.00,
'subtotal' => 6.00
),
)
);
$this->Paypal->setExpressCheckout($order);Once the customer has returned to your site (see return URL above) you can request their details with the token returned from the SetExpressCheckout method.
$this->Paypal->getExpressCheckoutDetails($token);Complete the transaction using the same order details. The $token and $payerId will be returned from the SetExpressCheckout method.
$order = array(
'description' => 'Your purchase with Acme clothes store',
'currency' => 'GBP',
'return' => 'https://www.my-amazing-clothes-store.com/review-paypal.php',
'cancel' => 'https://www.my-amazing-clothes-store.com/checkout.php',
'custom' => 'bingbong',
'items' => array(
0 => array(
'name' => 'Blue shoes',
'description' => 'A pair of really great blue shoes',
'tax' => 2.00,
'shipping' => 0.00,
'subtotal' => 8.00,
),
1 => array(
'name' => 'Red trousers',
'description' => 'Tight pair of red pants, look good with a hat.',
'tax' => 2.00,
'shipping' => 2.00,
'subtotal' => 6.00
),
)
);
$this->Paypal->doExpressCheckoutPayment($order, $token, $payerId);Charge a credit card. Ensure you are using SSL and following PCI compliance guidelines.
$payment = array(
'amount' => 30.00,
'card' => '4008 0687 0641 8697', // This is a sandbox CC
'expiry' => array(
'M' => '2',
'Y' => '2016',
),
'cvv' => '321',
);
$this->Paypal->doDirectPayment($payment);