1
1
<?php namespace Ignited \LaravelOmnipay ;
2
2
3
- class LaravelOmnipayServiceProvider extends BaseServiceProvider {
3
+ use Closure ;
4
+ use Omnipay \Common \GatewayFactory ;
5
+ use Omnipay \Common \Helper ;
6
+ use Omnipay \Common \CreditCard ;
4
7
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 ()
6
97
{
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 ' ];
10
99
}
11
100
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
+ }
12
131
}
0 commit comments