1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* Created by Damián Imrich / Haze Studio.
|
4
|
|
|
* Date: 22.11.2016
|
5
|
|
|
* Time: 14:54
|
6
|
|
|
*/
|
7
|
|
|
|
8
|
|
|
namespace HazeStudio\LaravelGoPaySDK;
|
9
|
|
|
|
10
|
|
|
use GoPay;
|
11
|
|
|
use HazeStudio\LaravelGoPaySDK\Events\PaymentCreated;
|
12
|
|
|
|
13
|
|
|
class GoPaySDK
|
14
|
|
|
{
|
15
|
|
|
protected $gopay;
|
16
|
|
|
|
17
|
|
|
protected $config = [];
|
18
|
|
|
protected $services = [];
|
19
|
|
|
protected $needReInit = false;
|
20
|
|
|
|
21
|
|
|
protected $logsBefore = [];
|
22
|
|
|
private $logClosure;
|
23
|
|
|
|
24
|
|
|
public function __construct()
|
25
|
|
|
{
|
26
|
|
|
$this->config = [
|
27
|
|
|
'goid' => config('gopay.goid'),
|
28
|
|
|
'clientId' => config('gopay.clientId'),
|
29
|
|
|
'clientSecret' => config('gopay.clientSecret'),
|
30
|
|
|
'isProductionMode' => !filter_var(getenv('APP_DEBUG'), FILTER_VALIDATE_BOOLEAN),
|
31
|
|
|
'timeout' => config('gopay.timeout')
|
32
|
|
|
];
|
33
|
|
|
|
34
|
|
|
$fallback = config('app.fallback_locale');
|
35
|
|
|
|
36
|
|
|
if(isset(config('gopay.languages')[\App::getLocale()])){
|
37
|
|
|
$language = config('gopay.languages')[\App::getLocale()];
|
38
|
|
|
} else {
|
39
|
|
|
$language = config('gopay.languages')[$fallback];
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
View Code Duplication |
if(defined($langConst = 'GoPay\Definition\Language::'.$language)) {
|
|
|
|
|
43
|
|
|
$this->config['language'] = constant($langConst);
|
44
|
|
|
} else {
|
45
|
|
|
$this->config['language'] = GoPay\Definition\Language::ENGLISH;
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
if(defined($scopeConst = 'GoPay\Definition\TokenScope::'.config('gopay.defaultScope'))){
|
49
|
|
|
$this->config['scope'] = constant($scopeConst);
|
50
|
|
|
} else {
|
51
|
|
|
$this->config['scope'] = GoPay\Definition\TokenScope::CREATE_PAYMENT;
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
$this->services['cache'] = new LaravelTokenCache();
|
55
|
|
|
$this->services['logger'] = new Logger();
|
56
|
|
|
|
57
|
|
|
$this->initGoPay();
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
protected function initGoPay()
|
61
|
|
|
{
|
62
|
|
|
$this->gopay = GoPay\Api::payments($this->config, $this->services);
|
63
|
|
|
if($this->needReInit)
|
64
|
|
|
$this->needReInit = false;
|
65
|
|
|
return $this->gopay;
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
public function __call($name, $arguments)
|
69
|
|
|
{
|
70
|
|
|
if(method_exists($this, $name))
|
71
|
|
|
{
|
72
|
|
|
return $this->{$name}(...$arguments);
|
73
|
|
|
} else if(method_exists($this->gopay, $name)){
|
74
|
|
|
if($this->needReInit){
|
75
|
|
|
$gp = $this->initGoPay();
|
76
|
|
|
} else {
|
77
|
|
|
$gp = $this->gopay;
|
78
|
|
|
}
|
79
|
|
|
$methodResult = $gp->{$name}(...$arguments);
|
80
|
|
|
|
81
|
|
|
switch ($name){
|
82
|
|
|
case 'createPayment':
|
83
|
|
|
event(new PaymentCreated($methodResult));
|
84
|
|
|
break;
|
85
|
|
|
default:
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
return $methodResult;
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
return null;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
public function scope($scope)
|
95
|
|
|
{
|
96
|
|
View Code Duplication |
if(defined($scopeConst = 'GoPay\Definition\TokenScope::'.$scope))
|
|
|
|
|
97
|
|
|
{
|
98
|
|
|
$this->config['scope'] = constant($scopeConst);
|
99
|
|
|
} else {
|
100
|
|
|
$this->config['scope'] = $scope;
|
101
|
|
|
}
|
102
|
|
|
$this->needReInit = true;
|
103
|
|
|
return $this;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
public function lang($lang)
|
107
|
|
|
{
|
108
|
|
|
if(defined($langConst = 'GoPay\Definition\Language::'.$lang))
|
109
|
|
|
{
|
110
|
|
|
$this->config['language'] = constant($langConst);
|
111
|
|
|
} else if(isset(config('gopay.languages')[$lang]) && defined($langConst = 'GoPay\Definition\Language::'.config('gopay.languages')[$lang])) {
|
112
|
|
|
$this->config['language'] = constant($langConst);
|
113
|
|
|
} else {
|
114
|
|
|
$this->config['language'] = $lang;
|
115
|
|
|
}
|
116
|
|
|
$this->needReInit = true;
|
117
|
|
|
return $this;
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
public function logHttpCommunication($request, $response)
|
121
|
|
|
{
|
122
|
|
|
if($this->logClosure == null) {
|
123
|
|
|
|
124
|
|
|
$this->logsBefore[] = [$request, $response];
|
125
|
|
|
}else{
|
126
|
|
|
call_user_func($this->logClosure, $request, $response);
|
127
|
|
|
}
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
public function log($closure)
|
131
|
|
|
{
|
132
|
|
|
$this->logClosure = $closure;
|
133
|
|
|
|
134
|
|
|
foreach($this->logsBefore as $log)
|
135
|
|
|
{
|
136
|
|
|
call_user_func_array($this->logClosure, $log);
|
137
|
|
|
}
|
138
|
|
|
$this->logsBefore = [];
|
139
|
|
|
return $this;
|
140
|
|
|
}
|
141
|
|
|
}
|
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.