Skip to content

Commit c64bb5a

Browse files
committed
Added support for lumen
1 parent 58d9916 commit c64bb5a

File tree

2 files changed

+132
-57
lines changed

2 files changed

+132
-57
lines changed
Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,131 @@
11
<?php namespace Ignited\LaravelOmnipay;
22

3-
class LaravelOmnipayServiceProvider extends BaseServiceProvider {
3+
use Closure;
4+
use Omnipay\Common\GatewayFactory;
5+
use Omnipay\Common\Helper;
6+
use Omnipay\Common\CreditCard;
47

5-
public function boot()
8+
class LaravelOmnipayManager {
9+
/**
10+
* The application instance.
11+
*
12+
* @var \Illuminate\Foundation\Application
13+
*/
14+
protected $app;
15+
16+
/**
17+
* Omnipay Factory Instance
18+
* @var \Omnipay\Common\GatewayFactory
19+
*/
20+
protected $factory;
21+
22+
/**
23+
* The current gateway to use
24+
* @var string
25+
*/
26+
protected $gateway;
27+
28+
/**
29+
* The array of resolved queue connections.
30+
*
31+
* @var array
32+
*/
33+
protected $gateways = [];
34+
35+
/**
36+
* Create a new omnipay manager instance.
37+
*
38+
* @param \Illuminate\Foundation\Application $app
39+
* @param $factory
40+
*/
41+
public function __construct($app, $factory)
42+
{
43+
$this->app = $app;
44+
$this->factory = $factory;
45+
}
46+
47+
/**
48+
* Get an instance of the specified gateway
49+
* @param index of config array to use
50+
* @return Omnipay\Common\AbstractGateway
51+
*/
52+
public function gateway($name = null)
53+
{
54+
$name = $name ?: $this->getGateway();
55+
56+
if ( ! isset($this->gateways[$name]))
57+
{
58+
$this->gateways[$name] = $this->resolve($name);
59+
}
60+
61+
return $this->gateways[$name];
62+
}
63+
64+
protected function resolve($name)
65+
{
66+
$config = $this->getConfig($name);
67+
68+
if(is_null($config))
69+
{
70+
throw new \UnexpectedValueException("Gateway [$name] is not defined.");
71+
}
72+
73+
$gateway = $this->factory->create($config['driver']);
74+
75+
$class = trim(Helper::getGatewayClassName($config['driver']), "\\");
76+
77+
$reflection = new \ReflectionClass($class);
78+
79+
foreach($config['options'] as $optionName=>$value)
80+
{
81+
$method = 'set' . ucfirst($optionName);
82+
83+
if ($reflection->hasMethod($method)) {
84+
$gateway->{$method}($value);
85+
}
86+
}
87+
88+
return $gateway;
89+
}
90+
91+
public function creditCard($cardInput)
92+
{
93+
return new CreditCard($cardInput);
94+
}
95+
96+
protected function getDefault()
697
{
7-
// Publish config
8-
$configPath = __DIR__ . '/../../config/config.php';
9-
$this->publishes([$configPath => config_path('laravel-omnipay.php')], 'config');
98+
return $this->app['config']['laravel-omnipay.default'];
1099
}
11100

101+
protected function getConfig($name)
102+
{
103+
return $this->app['config']["laravel-omnipay.gateways.{$name}"];
104+
}
105+
106+
public function getGateway()
107+
{
108+
if(!isset($this->gateway))
109+
{
110+
$this->gateway = $this->getDefault();
111+
}
112+
return $this->gateway;
113+
}
114+
115+
public function setGateway($name)
116+
{
117+
$this->gateway = $name;
118+
}
119+
120+
public function __call($method, $parameters)
121+
{
122+
$callable = [$this->gateway(), $method];
123+
124+
if(method_exists($this->gateway(), $method))
125+
{
126+
return call_user_func_array($callable, $parameters);
127+
}
128+
129+
throw new \BadMethodCallException("Method [$method] is not supported by the gateway [$this->gateway].");
130+
}
12131
}
Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,12 @@
11
<?php namespace Ignited\LaravelOmnipay;
22

3-
use Illuminate\Support\ServiceProvider;
4-
use Omnipay\Common\GatewayFactory;
5-
6-
class LaravelOmnipayServiceProvider extends ServiceProvider {
7-
8-
/**
9-
* Indicates if loading of the provider is deferred.
10-
*
11-
* @var bool
12-
*/
13-
protected $defer = false;
14-
15-
public function boot()
16-
{
17-
// Publish config
18-
$configPath = __DIR__ . '/../../config/config.php';
19-
$this->publishes([$configPath => config_path('laravel-omnipay.php')], 'config');
20-
}
21-
22-
/**
23-
* Register the service provider.
24-
*
25-
* @return void
26-
*/
27-
public function register()
28-
{
29-
$this->registerManager();
30-
}
31-
32-
/**
33-
* Register the Omnipay manager
34-
*/
35-
public function registerManager()
36-
{
37-
$this->app['omnipay'] = $this->app->share(function($app)
38-
{
39-
$factory = new GatewayFactory;
40-
$manager = new LaravelOmnipayManager($app, $factory);
41-
42-
return $manager;
43-
});
44-
}
45-
46-
/**
47-
* Get the services provided by the provider.
48-
*
49-
* @return array
50-
*/
51-
public function provides()
52-
{
53-
return ['omnipay'];
54-
}
3+
class LaravelOmnipayServiceProvider extends BaseServiceProvider {
4+
5+
public function boot()
6+
{
7+
// Publish config
8+
$configPath = __DIR__ . '/../../config/config.php';
9+
$this->publishes([$configPath => config_path('laravel-omnipay.php')], 'config');
10+
}
5511

5612
}

0 commit comments

Comments
 (0)