forked from affka/yii2-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGatewayModule.php
More file actions
215 lines (186 loc) · 5.31 KB
/
GatewayModule.php
File metadata and controls
215 lines (186 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace gateway;
use gateway\exceptions\GatewayException;
use gateway\exceptions\NotFoundGatewayException;
use gateway\gateways\Base;
use gateway\models\Order;
use yii\base\Module;
use yii\db\ActiveRecord;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\log\Logger;
use yii\web\Application;
class GatewayModule extends Module
{
/**
* @var string
*/
public $controllerNamespace = 'gateway\controllers';
/**
* @var array
*/
public $gateways = [];
/**
* @var string
*/
public $logFilePath = '@runtime/gateway.log';
/**
* @var string|array|callable|null
*/
public $siteUrl = null;
/**
* @var string|array|callable
*/
public $successUrl = ['/gateway/gateway/success'];
/**
* @var string|array|callable
*/
public $failureUrl = ['/gateway/gateway/failure'];
/**
* @var array Note: processed by gateway, so format is unified: no string, no callback
*/
public $callbackUrl = ['/gateway/gateway/callback'];
/**
* @var Order
*/
public $orderClassName;
/**
* @var Order
*/
public $transactionClassName;
/**
* @var Order
*/
public $callbackLogEntryClassName;
/**
* @return \gateway\GatewayModule
*/
public static function getInstance()
{
return \Yii::$app->getModule('gateway');
}
public function init()
{
parent::init();
if (\Yii::$app instanceof Application) {
$this->siteUrl = $this->siteUrl ? Url::to($this->siteUrl, true) : \Yii::$app->homeUrl;
$this->successUrl = Url::to($this->successUrl, true);
$this->failureUrl = Url::to($this->failureUrl, true);
} else {
$this->successUrl = is_array($this->successUrl) ? $this->successUrl[0] : $this->successUrl;
$this->failureUrl = is_array($this->failureUrl) ? $this->failureUrl[0] : $this->failureUrl;
}
}
/**
* @param string|callable|array|null $url
* @param Order $order
* @param Base $gateway
* @return string
*/
protected function expandUrl($url, $order, $gateway)
{
if ($url === null) {
return Url::home(true);
}
if (is_callable($url)) {
return call_user_func($url, $order, $gateway);
}
return Url::to($url, true);
}
/**
* @param Order $order
* @param Base $gateway
* @return string
*/
public function getEffectiveSiteUrl($order, $gateway)
{
return $this->expandUrl($this->siteUrl, $order, $gateway);
}
/**
* @param Order $order
* @param Base $gateway
* @return string
*/
public function getEffectiveSuccessUrl($order, $gateway)
{
return $this->expandUrl($this->successUrl, $order, $gateway);
}
/**
* @param Order $order
* @param Base $gateway
* @return string
*/
public function getEffectiveFailureUrl($order, $gateway)
{
return $this->expandUrl($this->failureUrl, $order, $gateway);
}
public function hasGateway($name)
{
return isset($this->gateways[$name]);
}
/**
* @param string $name
* @return Base
* @throws NotFoundGatewayException
* @throws \yii\base\InvalidConfigException
*/
public function getGateway($name)
{
if (!isset($this->gateways[$name])) {
throw new NotFoundGatewayException();
}
if (is_array($this->gateways[$name])) {
$this->gateways[$name] = \Yii::createObject(['name' => $name] + $this->gateways[$name]);
}
return $this->gateways[$name];
}
/**
* @param string $message
* @param integer $level
* @param integer $transactionId
* @param array $stateData
* @throws \gateway\exceptions\GatewayException
*/
public function log($message, $level = Logger::LEVEL_INFO, $transactionId = null, $stateData = [])
{
$message .= "\n" .
"Transaction: " . $transactionId . "\n" .
"State: " . print_r($stateData, true) . "\n\n" .
"------------------------------------------\n\n";
\Yii::getLogger()->log($message, $level, 'gateway');
}
/**
* Отправляет POST запрос на указанный адрес
* @param string $url
* @param array $params
* @param array $headers
* @return string
*/
public function httpSend($url, $params = [], $headers = [])
{
$headers = array_merge([
'Content-Type' => 'application/x-www-form-urlencoded',
], $headers);
$headersString = '';
foreach ($headers as $key => $value) {
$headersString .= trim($key) . ": " . trim($value) . "\n";
}
return file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $headersString . "\n",
'content' => is_array($params) ? http_build_query($params) : $params,
),
)));
}
/**
* @param ActiveRecord $model
* @throws GatewayException
*/
public static function saveOrPanic($model)
{
if (!$model->save()) {
throw new GatewayException('Unexpected ' . $model->className() . ' behavior');
}
}
}