Skip to content

Commit 7da97b2

Browse files
committed
Initial commit
0 parents  commit 7da97b2

File tree

8 files changed

+254
-0
lines changed

8 files changed

+254
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2014 Barry vd. Heuvel
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "barryvdh/laravel-omnipay",
3+
"description": "Omnipay Service Provider for Laravel",
4+
"keywords": ["laravel", "omnipay"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Barry vd. Heuvel",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.3.0",
14+
"illuminate/support": "~4.0",
15+
"omnipay/common": "~2.0"
16+
},
17+
"autoload": {
18+
"psr-0": {
19+
"Barryvdh\\Omnipay": "src/"
20+
}
21+
},
22+
"minimum-stability": "stable"
23+
}

readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Omnipay for Laravel 4
2+
3+
This is a package to integrate [Omnipay](https://github.com/omnipay/omnipay) with Laravel.
4+
You can use it to easily manage your configuration, and use the Facade to provide shortcuts to your gateway.
5+
6+
## Installation
7+
8+
Require this package in your composer.json and run composer update (or run `composer require barryvdh/laravel-omnipay:0.1.x` directly):
9+
10+
"barryvdh/laravel-omnipay": "0.1.*"
11+
12+
After updating composer, add the ServiceProvider to the providers array in app/config/app.php
13+
14+
'Barryvdh\Omnipay\ServiceProvider',
15+
16+
You need to publish the config for this package
17+
18+
$ php artisan config:publish barryvdh/laravel-omnipay
19+
20+
To use the Facade (`Omnipay::purchase()` instead of `App::make(`omnipay`)->purchase()`), add that to the facades array.
21+
22+
'Omnipay' => 'Barryvdh\Omnipay\Facade',
23+
24+
When calling the Omnipay facade/instance, it will create the default gateway, based on the configuration.
25+
You can change the default gateway by calling `Omnipay::setDefaultGateway('My\Gateway')`.
26+
You can get a different gateway by calling `Omnipay::gateway('My\Cass')`

src/Barryvdh/Omnipay/Facade.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace Barryvdh\Omnipay;
2+
3+
use Omnipay\Common\CreditCard;
4+
5+
class Facade extends \Illuminate\Support\Facades\Facade {
6+
7+
/**
8+
* @param array $parameters
9+
* @return CreditCard
10+
*/
11+
public static function creditCard($parameters = null)
12+
{
13+
return new CreditCard($parameters);
14+
}
15+
16+
/**
17+
* {@inheritDoc}
18+
*/
19+
protected static function getFacadeAccessor() { return 'omnipay.manager'; }
20+
21+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php namespace Barryvdh\Omnipay;
2+
3+
use Omnipay\Common\GatewayFactory;
4+
5+
class GatewayManager{
6+
7+
/**
8+
* The application instance.
9+
*
10+
* @var \Illuminate\Foundation\Application
11+
*/
12+
protected $app;
13+
14+
/**
15+
* The registered gateways
16+
*/
17+
protected $gateways;
18+
19+
/**
20+
* The default settings, applied to every gateway
21+
*/
22+
protected $defaults;
23+
24+
/**
25+
* Create a new Gateway manager instance.
26+
*
27+
* @param \Illuminate\Foundation\Application $app
28+
* @param \Omnipay\Common\GatewayFactory $factory
29+
* @param array
30+
*/
31+
public function __construct($app, GatewayFactory $factory, $defaults = array())
32+
{
33+
$this->app = $app;
34+
$this->factory = $factory;
35+
$this->defaults = $defaults;
36+
}
37+
38+
public function gateway($class = null)
39+
{
40+
$class = $class ?: $this->getDefaultGateway();
41+
42+
if(!isset($this->gateways[$class])){
43+
$gateway = $this->factory->create($class, $this->app['request']);
44+
$gateway->initialize($this->getConfig($class));
45+
$this->gateways[$class] = $gateway;
46+
}
47+
48+
return $this->gateways[$class];
49+
}
50+
51+
/**
52+
* Get the configuration, based on the config and the defaults.
53+
*/
54+
protected function getConfig($name)
55+
{
56+
return array_merge(
57+
$this->defaults,
58+
$this->app['config']->get('laravel-omnipay::gateways.'.$name, array())
59+
);
60+
}
61+
62+
/**
63+
* Get the default gateway name.
64+
*
65+
* @return string
66+
*/
67+
public function getDefaultGateway()
68+
{
69+
return $this->app['config']['laravel-omnipay::gateway'];
70+
}
71+
72+
/**
73+
* Set the default gateway name.
74+
*
75+
* @param string $name
76+
* @return void
77+
*/
78+
public function setDefaultGateway($name)
79+
{
80+
$this->app['config']['laravel-omnipay::gateway'] = $name;
81+
}
82+
83+
/**
84+
* Dynamically call the default driver instance.
85+
*
86+
* @param string $method
87+
* @param array $parameters
88+
* @return mixed
89+
*/
90+
public function __call($method, $parameters)
91+
{
92+
return call_user_func_array(array($this->gateway(), $method), $parameters);
93+
}
94+
95+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace Barryvdh\Omnipay;
2+
3+
use Omnipay\Common\GatewayFactory;
4+
5+
class ServiceProvider extends \Illuminate\Support\ServiceProvider {
6+
7+
/**
8+
* Indicates if loading of the provider is deferred.
9+
*
10+
* @var bool
11+
*/
12+
protected $defer = false;
13+
14+
/**
15+
* Bootstrap the application events.
16+
*
17+
* @return void
18+
*/
19+
public function boot()
20+
{
21+
$this->app['config']->package('barryvdh/laravel-omnipay', $this->guessPackagePath() . '/config');
22+
}
23+
24+
/**
25+
* Register the service provider.
26+
*
27+
* @return void
28+
*/
29+
public function register()
30+
{
31+
$this->app['omnipay.manager'] = $this->app->share(function ($app){
32+
$defaults = $app['config']->get('laravel-omnipay::defaults', array());
33+
return new GatewayManager($app, new GatewayFactory, $defaults);
34+
});
35+
}
36+
37+
/**
38+
* Get the services provided by the provider.
39+
*
40+
* @return array
41+
*/
42+
public function provides()
43+
{
44+
return array('omnipay.manager');
45+
}
46+
}

src/config/config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
return array(
4+
5+
/** The default gateway name */
6+
'gateway' => 'My\Gateway',
7+
8+
/** The default settings, applied to all gateways */
9+
'defaults' => array(
10+
'testMode' => false,
11+
),
12+
13+
/** Gateway specific parameters */
14+
'gateways' => array(
15+
'My\Gateway' => array(
16+
17+
),
18+
),
19+
20+
);

0 commit comments

Comments
 (0)