1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelPayum; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Payum\Core\Payum; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Payum\Core\Model\Token; |
9
|
|
|
use Payum\Core\Model\Payout; |
10
|
|
|
use Payum\Core\PayumBuilder; |
11
|
|
|
use Payum\Core\Model\Payment; |
12
|
|
|
use Illuminate\Routing\Router; |
13
|
|
|
use Payum\Core\Model\ArrayObject; |
14
|
|
|
use Payum\Core\CoreGatewayFactory; |
15
|
|
|
use Illuminate\Filesystem\Filesystem; |
16
|
|
|
use Illuminate\Support\ServiceProvider; |
17
|
|
|
use Payum\Core\GatewayFactoryInterface; |
18
|
|
|
use Payum\Core\Storage\StorageInterface; |
19
|
|
|
use Payum\Core\Storage\FilesystemStorage; |
20
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
21
|
|
|
use Payum\Core\Registry\StorageRegistryInterface; |
22
|
|
|
use Recca0120\LaravelPayum\Security\TokenFactory; |
23
|
|
|
use Recca0120\LaravelPayum\Storage\EloquentStorage; |
24
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory; |
25
|
|
|
use Recca0120\LaravelPayum\Action\GetHttpRequestAction; |
26
|
|
|
use Recca0120\LaravelPayum\Action\RenderTemplateAction; |
27
|
|
|
use Recca0120\LaravelPayum\Model\Token as EloquentToken; |
28
|
|
|
use Recca0120\LaravelPayum\Action\ObtainCreditCardAction; |
29
|
|
|
use Payum\Core\Bridge\Symfony\Security\HttpRequestVerifier; |
30
|
|
|
use Recca0120\LaravelPayum\Model\Payment as EloquentPayment; |
31
|
|
|
use Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter; |
32
|
|
|
use Recca0120\LaravelPayum\Extension\UpdatePaymentStatusExtension; |
33
|
|
|
|
34
|
|
|
class LaravelPayumServiceProvider extends ServiceProvider |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* This namespace is applied to your controller routes. |
38
|
|
|
* |
39
|
|
|
* In addition, it is set as the URL generator's root namespace. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $namespace = 'Recca0120\LaravelPayum\Http\Controllers'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* boot. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Routing\Router $router |
49
|
|
|
* @param \Illuminate\Contracts\View\Factory $viewFactory |
50
|
|
|
*/ |
51
|
1 |
|
public function boot(Router $router, ViewFactory $viewFactory) |
52
|
|
|
{ |
53
|
1 |
|
$viewFactory->addNamespace('payum', __DIR__.'/../resources/views'); |
54
|
1 |
|
$this->handleRoutes($router, $this->app['config']['payum']); |
55
|
1 |
|
if ($this->app->runningInConsole() === true) { |
56
|
1 |
|
$this->handlePublishes(); |
57
|
|
|
} |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register the service provider. |
62
|
|
|
*/ |
63
|
1 |
|
public function register() |
64
|
|
|
{ |
65
|
1 |
|
$this->mergeConfigFrom(__DIR__.'/../config/payum.php', 'payum'); |
66
|
|
|
|
67
|
|
|
$this->app->singleton('payum.builder', function ($app) { |
68
|
1 |
|
$config = $app['config']['payum']; |
69
|
|
|
|
70
|
1 |
|
$routeAlias = Arr::get($config, 'route.as'); |
71
|
1 |
|
$builder = (new PayumBuilder()) |
72
|
|
|
->setTokenFactory(function (StorageInterface $tokenStorage, StorageRegistryInterface $registry) use ($app) { |
73
|
1 |
|
return new TokenFactory($tokenStorage, $registry, $app->make(UrlGenerator::class)); |
74
|
1 |
|
}) |
75
|
|
|
->setHttpRequestVerifier(function (StorageInterface $tokenStorage) { |
76
|
1 |
|
return new HttpRequestVerifier($tokenStorage); |
77
|
1 |
|
}) |
78
|
|
|
->setCoreGatewayFactory(function ($defaultConfig) { |
79
|
1 |
|
return new CoreGatewayFactory($defaultConfig); |
80
|
1 |
|
}) |
81
|
1 |
|
->setCoreGatewayFactoryConfig([ |
82
|
1 |
|
'payum.action.get_http_request' => $app->make(GetHttpRequestAction::class), |
83
|
1 |
|
'payum.action.obtain_credit_card' => $app->make(ObtainCreditCardAction::class), |
84
|
1 |
|
'payum.action.render_template' => $app->make(RenderTemplateAction::class), |
85
|
1 |
|
'payum.converter.reply_to_http_response' => $app->make(ReplyToSymfonyResponseConverter::class), |
86
|
1 |
|
'payum.extension.update_payment_status' => $app->make(UpdatePaymentStatusExtension::class), |
87
|
|
|
]) |
88
|
1 |
|
->setGenericTokenFactoryPaths([ |
|
|
|
|
89
|
1 |
|
'authorize' => $routeAlias.'authorize', |
90
|
1 |
|
'capture' => $routeAlias.'capture', |
91
|
1 |
|
'notify' => $routeAlias.'notify', |
92
|
1 |
|
'payout' => $routeAlias.'payout', |
93
|
1 |
|
'refund' => $routeAlias.'refund', |
94
|
1 |
|
'cancel' => $routeAlias.'cancel', |
95
|
1 |
|
'sync' => $routeAlias.'sync', |
96
|
1 |
|
'done' => $routeAlias.'done', |
97
|
|
|
]); |
98
|
|
|
|
99
|
1 |
|
$this->setStorage($builder, $app->make(Filesystem::class), $config); |
100
|
1 |
|
$this->setGatewayConfigs($builder, $config['gateway_configs']); |
101
|
|
|
|
102
|
1 |
|
return $builder; |
103
|
1 |
|
}); |
104
|
|
|
|
105
|
|
|
$this->app->singleton(Payum::class, function ($app) { |
106
|
1 |
|
return $app['payum.builder']->getPayum(); |
107
|
1 |
|
}); |
108
|
|
|
|
109
|
1 |
|
$this->app->alias(Payum::class, 'payum'); |
110
|
|
|
|
111
|
|
|
$this->app->singleton(PayumManager::class, function ($app) { |
112
|
1 |
|
return new PayumManager($app); |
113
|
1 |
|
}); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* provides. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function provides() |
122
|
|
|
{ |
123
|
|
|
return ['payum.builder', 'payum']; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* setStorage. |
128
|
|
|
* |
129
|
|
|
* @param \Payum\Core\PayumBuilder $builder |
130
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
131
|
|
|
* @param array $config |
132
|
|
|
*/ |
133
|
1 |
|
protected function setStorage(PayumBuilder $builder, Filesystem $files, $config) |
134
|
|
|
{ |
135
|
1 |
|
if ($config['storage']['token'] === 'files') { |
136
|
|
|
$storagePath = $config['path']; |
137
|
|
|
if ($files->isDirectory($storagePath) === false) { |
138
|
|
|
$files->makeDirectory($storagePath, 0777, true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $builder |
142
|
|
|
->setTokenStorage(new FilesystemStorage($storagePath, Token::class, 'hash')) |
143
|
|
|
->addStorage(Payment::class, new FilesystemStorage($storagePath, Payment::class, 'number')) |
144
|
|
|
->addStorage(ArrayObject::class, new FilesystemStorage($storagePath, ArrayObject::class)) |
145
|
|
|
->addStorage(Payout::class, new FilesystemStorage($storagePath, Payout::class)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $builder |
149
|
1 |
|
->setTokenStorage(new EloquentStorage(EloquentToken::class)) |
150
|
1 |
|
->addStorage(Payment::class, new EloquentStorage(EloquentPayment::class)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* setGatewayConfigs. |
155
|
|
|
* |
156
|
|
|
* @param \Payum\Core\PayumBuilder $builder |
157
|
|
|
* @param array $gatewayConfigs |
158
|
|
|
*/ |
159
|
1 |
|
protected function setGatewayConfigs(PayumBuilder $builder, $gatewayConfigs) |
160
|
|
|
{ |
161
|
1 |
|
foreach ($gatewayConfigs as $name => $config) { |
162
|
1 |
|
$this->setGateway($builder, $name, $config); |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
return $builder; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* setGateway. |
170
|
|
|
* |
171
|
|
|
* @param \Payum\Core\PayumBuilder $builder |
172
|
|
|
* @param string $name |
173
|
|
|
* @param array $config |
174
|
|
|
*/ |
175
|
1 |
|
protected function setGateway(PayumBuilder $builder, $name, $config) |
176
|
|
|
{ |
177
|
1 |
|
$factory = $config['factory']; |
178
|
1 |
|
if (($factory instanceof Closure) === false && class_exists($factory) === true) { |
179
|
|
|
$factory = function ($config, GatewayFactoryInterface $coreGatewayFactory) use ($factory) { |
180
|
1 |
|
return new $factory($config, $coreGatewayFactory); |
181
|
1 |
|
}; |
182
|
1 |
|
$builder->addGatewayFactory($name, $factory); |
183
|
1 |
|
$config['factory'] = $name; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
return $builder->addGateway($name, $config); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* register routes. |
191
|
|
|
* |
192
|
|
|
* @param \Illuminate\Routing\Router $router |
193
|
|
|
* @param array $config |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
1 |
|
protected function handleRoutes(Router $router, $config = []) |
197
|
|
|
{ |
198
|
1 |
|
if ($this->app->routesAreCached() === false) { |
199
|
1 |
|
$router->group(array_merge([ |
200
|
1 |
|
'prefix' => 'payum', |
201
|
1 |
|
'as' => 'payum.', |
202
|
1 |
|
'namespace' => $this->namespace, |
203
|
|
|
'middleware' => ['web'], |
204
|
1 |
|
], Arr::get($config, 'route', [])), function (Router $router) { |
205
|
|
|
require __DIR__.'/Http/routes.php'; |
206
|
1 |
|
}); |
207
|
|
|
} |
208
|
|
|
|
209
|
1 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* handle publishes. |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
1 |
|
protected function handlePublishes() |
218
|
|
|
{ |
219
|
1 |
|
$this->publishes([ |
220
|
1 |
|
__DIR__.'/../config/payum.php' => $this->app->configPath().'/payum.php', |
221
|
1 |
|
], 'config'); |
222
|
|
|
|
223
|
1 |
|
$this->publishes([ |
224
|
1 |
|
__DIR__.'/../resources/views' => $this->app->basePath().'/resources/views/vendor/payum/', |
225
|
1 |
|
], 'views'); |
226
|
|
|
|
227
|
1 |
|
$this->publishes([ |
228
|
1 |
|
__DIR__.'/../database/migrations' => $this->app->databasePath().'/migrations/', |
229
|
1 |
|
], 'public'); |
230
|
|
|
|
231
|
1 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: