Add any SDK to Laravel 5.x - php

I'm trying to add this SDK in my Laravel 5.4 application. I downloaded and installed it in app/resources/assets and guessing that I can use it in controllers as:
require_once '[ruta/payu-php-sdk]/lib/PayU.php';
The SDK have some Eviromental variables and Keys.
What do you think is the better approach for this type of SDK in laravel?

I'd suggest using Omnipay, it's a well supported library the has integrations for many different gateways, including PayU. This library is framework agnostic, so you won't need to change your code should you need to change payment gateways.
You can use composer to add these libraries:
"require": {
"omnipay/omnipay": "~2.0",
"omnipay/payu": "~2.0"
}
This way you don't have to keep track of requires.
Edit: To add any type of library that doesn't use composer already, You could manually tell composer to autoload file(s).
For your case, there should be a section in your composer.json called autoload, here's what you could add:
...
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"ruta/payu-php-sdk/lib/PayU.php"
]
},
...
This should make the SDK available throughout the application.

Related

Create Stripe plan on laravel 5

I'm using Laravel 5.1, and I'm thinking of a way to create a plan without going to my Stripe account.
I used:
composer require stripe/stripe-php 2.*
Then I did this in my controller:
use \Stripe\Plan;
Plan::create(array(
"amount" => 2000,
"interval" => "monthly",
"name" => "Amazing Gold Plan",
"currency" => "usd",
"id" => "gold")
);
And got the following error:
Class 'Stripe\Plan' not found
Am I doing something wrong? Thanks in advance if you know what to do.
The problem it seems like your Strip class is not being loaded, I review the documentation and the library is a PHP library not a Laravel package
You could load it on the Autoload section on the composer.json but if you are already using Laravel I really recommend you to use Laravel Cashier and simply follow the steps of the documentation
https://laravel.com/docs/5.4/billing
It should be something like this... I recommend you to check the composer documentation
"autoload": {
"psr-4": {
"App\\": "app/",
"Stripe\\": "vendor/your/stripe/path",
},
}

Reflection Exception Class does not exist

I am having trouble with a Laravel application I am developing using Vagrant.
I have created a few custom classes to help me query an external API, but keep receiving a ReflectionException with the message 'Class ExtAPI does not exist'. These classes are under "app\Lib\ExtAPI"
I am using the Service Container to get the class.
Here is the composer.json:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\":"app/Models/*",
"App\\Interfaces\\":"app/Interfaces/*",
"App\\Repositories\\":"app/Repositories/*",
"App\\Lib\\":"app/Lib/*"
}
},
I don't understand what I am doing wrong because this setup was working previously. Since running my setup on a different host, this is not working anymore.
Any help would be much appreciated.

Autoload PHPExcel 1.0 in Symfony 2.6

We added PHPExcel to composer by adding the following
To repositories:
{
"type": "package",
"package": {
"name": "PHPOffice/PHPExcel",
"version": "1.9",
"source": {
"url": "https://github.com/PHPOffice/PHPExcel.git",
"type": "git",
"reference": "1.9"
},
"autoload": {
"psr-0": {
"PHPExcel": "src/"
}
}
}
To require:
"PHPOffice/PHPExcel": "1.9.*"
In our code:
use PHPExcel\IOFactory;
...
$file = $request->get('file');
$inputFileType = IOFactory::identify($file);
The error we get is:
Attempted to load class "IOFactory" from namespace "PHPExcel".
Did you forget a "use" statement for another namespace?
The namespace looks right (https://github.com/PHPOffice/PHPExcel/blob/1.9/src/PhpSpreadsheet/IOFactory.php).
Use of the 1.9 branch is not recommended. It isn't completely converted yet to use namespaces, and is subject to significant code change. Nor is it backward compatible with the official 1.8 branch, and the changes are not yet documented, and also subject to further major changes as we modify the code to take advantage of the newer features of PHP.
The official release branch is still 1.8
Just because the 1.8 branch exists on github, doesn't mean that it's working code. I store it there so that it's available for shared development, and as a security (rather than keeping it all on my development laptop) in case I get run over by a bus tomorrow.
I have not used 1.9. Glad to see that they are moving to namespaces. That said, you might be better off sticking with 1.8 just for stability.
In any event, 1.9 relies on psr-4. Try adding this to your composer.json file:
"autoload": {
"psr-4": {
"PHPExcel\\": "src/PhpSpreadsheet"
}
}
Then rebuilding the composer generated autoload.php file.

How to add adLDAP as library to Laravel 4

How i can add adLDAP library to LARAVEL 4 ?
http://adldap.sourceforge.net/
That library does not offer a composer.json file yet, you you have to create the necessary information yourself.
I have created a sample file that successfully downloaded something that looks good, but I haven't used the code.
{
"require": {
"adldap/adldap": "4.0.4"
},
"repositories" : [
{
"type": "package",
"package": {
"name" : "adldap/adldap",
"version": "4.0.4",
"dist": {
"url": "http://sourceforge.net/projects/adldap/files/adLDAP/adLDAP_4.0.4/adLDAP_4.0.4r2.zip/download",
"type": "zip"
},
"source": {
"url":"https://svn.code.sf.net/p/adldap/code/",
"type": "svn",
"reference": "tags/v4.0.4/"
},
"autoload": {
"classmap": ["src/"]
}
}
}
]
}
You'd have to create one repositories entry per version you are about to use (one entry is enough if you don't plan do something fancy). I created the entry for the most recent version 4.0.4 - if there is an update, you have to change the version tags everywhere.
The require entry should be added to whatever you are already using.
The distribution URL is a rough guess by following the download link to the ZIP file offered on Sourceforge, bypassing any ad-filled download page. It might stop working unexpectedly. If you delete the whole dist section, you will checkout from the original SVN repository instead, which might be slower than downloading and unpacking a ZIP file.
After that, you are all set with the Composer part. The remaining thing is to include Composer autoloading in the Laravel bootstrapping (you might already have that done), and then enjoy the LDAP classes.
Anyone coming across this in the future, adLDAP has a composer file now and can easily be autoloaded.
"require": {
"adldap/adldap": "4.0.*"
},
Then load the library in your controller:
$ldap = new \adLDAP\adLDAP($config);

Using a non-laravel package on Laravel 4

Is it possible to include a package that was not specifically designed for L4 in the framework?
If so, how is it done? I know I need to add the package to my composer.json which adds it to the vendor folder, but can I register it somehow in the providers array? are there any other steps necessary?
I would like to use the Google checkout package originally designed for Yii
Using third party composer packages with Laravel 4
When developers create composer packages, they should map the auto-loading using PSR-0 or PSR-4 standards. If this is not the case there can be issues loading the package in your Laravel application. The PSR-0 standard is:
{
"autoload": {
"psr-0": { "Acme": "src/" }
}
}
And the PSR-4 standard:
{
"autoload": {
"psr-4": { "Acme\\": "src/" }
}
}
Basically the above is a standard for telling composer where to look for name-spaced files. If you are not using your own namespaces you dont have to configure anything else.
SCENARIO 1
PSR-0 standard following packages (with autoload classmap) in Laravel
This is a simple one, and for example i will use the facebook php sdk, that can be found:
https://packagist.org/packages/facebook/php-sdk
Step 1:
Include the package in your composer.json file.
"require": {
"laravel/framework": "4.0.*",
"facebook/php-sdk": "dev-master"
}
Step 2:
run: composer update
Step 3:
Because the facebook package uses a class map its working out of the box, you can start using the package instantly. (The code example below comes straight from a normal view. Please keep your logic out from views in your production app.)
$facebook = new Facebook(array(
'appId' => 'secret',
'secret' => 'secret'
));
var_dump($facebook); // It works!
SCENARIO 2
For this example i will use a wrapper from the instagram php api. Here there need to be made some tweaks to get the package loaded. Lets give it a try!
The package can be found here:
https://packagist.org/packages/fishmarket/instaphp
Step 1:
Add to composer .json
"require": {
"laravel/framework": "4.0.*",
"fishmarket/instaphp": "dev-master"
}
Then you can update normally (composer update)
Next try to use the package like you did with the facebook package. Again, this is just code in a view.
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$api = Instaphp::Instance(null, $instagramconfig);
var_dump($api); // Epic fail!
If you try the above example you will get this error:
FatalErrorException: Error: Class 'Instaphp' not found in ...
So we need to fix this issue. To do this we can examine the instagram composer.json, that has its autoload diffrent than the facebook php sdk had.
"autoload": {
"psr-0": { "Instaphp": "." }
}
Compared to the facebook composer.json:
"autoload": {
"classmap": ["src"]
}
(Composer handles different kinds of autoloading, from files and class-maps to PSR. Take a look at your vendor/composer/ folder to see how its done.)
Now we will have to load the class, manually. Its easy, just add this (top of your controller, model or view):
use Instaphp\Instaphp;
composer dump-autoload, and it works!
step2 (optional)
Another method is (if you dont want to use the "use" statement, you can simply tell composer to look for the files straight from your code. Just change the Instance like so:
// reference the name-spaced class straight in the code
$api = Instaphp\Instaphp::Instance(null, $instagramconfig);
var_dump($api); // It works
However I suggest using the usestatement to make it clear to other developers (and your future self) what (external) classes/packages are used in the program.
SCENARIO 3
Here we use the Laravels built in IOC container to register service providers. Please note that some packages might not be suitable for this method. I will use the same Instagram package as in scenario 2.
Quick and dirty
If you don't care about design patterns and service providers you can bind a class like this:
App::bind('Instaphp', function($app)
{
return new Instaphp\Instaphp;
});
And you resolve it like this.
App::make('Instaphp');
Quick and dirty end
If you're working on a bigger project, and you make use of interfaces you should probably abstract the bindings further.
Step 1:
Create a folder inside your app folder, for example a 'providers' folder.
app/providers
Make sure Laravel auto-loads that folder, you can pass in some additional info to composer.json, like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/providers" // this was added
]
},
Now create a File inside the new folder called Instagram.php and place this inside:
<?php
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('Instaphp', function()
{
return new Instaphp\Instaphp;
});
}
}
Now run composer dump-autoload again, and you can use the package. Note that the instagram package has a final private function __construct(), this means you cannot use that package outside the original class without changing the construct method to public. I'm not saying this is a good practice, and i suggest to use the scenario 2, in the case of the instagram package.
Anyway, after this you can use the package like this:
$instagramInstance = App::make('Instaphp');
$instagramconfig = array(
'client_id' => 'secret',
'client_secret'=> 'secret',
'access_token' => 'secret'
);
$instagram = new $instagramInstance();
$userfeed = $instagram->Users->feed($instagramconfig);
var_dump($userfeed); // It works!
Add "tvr/googlecheckout": "dev-master" this to your composer.json.
Run composer install, then you can use the IoC container. Some code examples can be found in the official docs for Laravel 4: http://four.laravel.com/docs/ioc#basic-usage

Categories