Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate Paypal PHP sdk with Cakephp 3.x

To use Paypal PHP SDK is quite simple but when i try to use this SDK in cakephp 3.x then it generates errors. I changed namespace name in "Paypal SDK project" to bring all files under one name space.

My cakephp project namespace name is namespace App

but namespace in Paypal SDK is namespace PayPal I just changed it to "namespace App" and put all files in PayPal folder and put that folder in "src" folder in cakephp project. but PayPal does not work using this technique.

Can you please advise me how to use this SDK in CakePHP or where to put files are best. I know there are some other techniques to make payment via paypal without SDK but i want to use SDK.

Can you please guide me little bit how to integrate PayPal PHP SDK in CakePHP.

SDK is provided here https://github.com/paypal/PayPal-PHP-SDK some payment samples are here http://paypal.github.io/PayPal-PHP-SDK/sample/

like image 256
learner Avatar asked Feb 02 '17 04:02

learner


1 Answers

1. Use composer for installation which will manages the dependencies:

Go to your project directory (Eg:E:\xampp\htdocs\cakephp) in command promot and type following:

composer require "paypal/rest-api-sdk-php:*"

This will install latest version of paypal sdk into the vendor folder,you can go into vendor and check that.

2. Configure your environment:

Make any function for configuration of paypal in any controller you would like:

public function configuration() {
   $apiContext = new \PayPal\Rest\ApiContext(
     new \PayPal\Auth\OAuthTokenCredential(
     'YOUR APPLICATION CLIENT ID',  // you will get information about client id and secret once you have created test account in paypal sandbox  
     'YOUR APPLICATION CLIENT SECRET'  
    )
  );
 }

As you are using cakephp framework you don't need to write following line into your function as suggested in papal documentation:

// Autoload SDK package for composer based installations
 require 'vendor/autoload.php';

This is because you have already done that with autoload.php file within your vendor folder.

3. Using paypal classes:

You need to use paypal classes/namespace within your controller in this way:

namespace App\Controller; // your controller have this already

use App\Controller\AppController; // your controller have this already

use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;

4. Completely follow this quick start guide which will be pretty straight forward now:

Paypal quick start guide.

For making sandbox test accounts:(Paypal developer guide)

like image 91
Manohar Khadka Avatar answered Sep 20 '22 18:09

Manohar Khadka