-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaymentModel.php
241 lines (186 loc) · 6.57 KB
/
PaymentModel.php
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* @package ImpressPages
*/
namespace Plugin\Paysera;
class PaymentModel
{
const MODE_PRODUCTION = 'Production';
const MODE_TEST = 'Test';
const MODE_SKIP = 'Skip';
protected static $instance;
protected function __construct()
{
if ($this->projectId() == '' || $this->password() == '') {
throw new \Ip\Exception('Please enter project ID and password in Paysera plugin configuration.');
}
}
protected function __clone()
{
}
/**
* Get singleton instance
* @return PaymentModel
*/
public static function instance()
{
if (!self::$instance) {
self::$instance = new PaymentModel();
}
return self::$instance;
}
public function processCallback()
{
require_once('WebToPay.php');
$response = array();
try {
$response = WebToPay::validateAndParseData(ipRequest()->getQuery(), $this->projectId(), $this->password());
if ($response['test'] != PaymentModel::isTestMode()) {
ipLog()->error('Paysera.ipn: test mode parameter don\'t match', $response);
return false;
}
if ($response['type'] !== 'macro') {
ipLog()->error('Paysera.ipn: Only macro payment callbacks are accepted', $response);
return false;
}
$paymentId = $response['orderid'];
$amount = $response['amount'];
$currency = $response['currency'];
$payment = Model::getPayment($paymentId);
if ($payment['isPaid']) {
return $response;
}
if ($payment['price'] != $amount) {
ipLog()->error('Paysera.ipn: Price don\'t match.', array_merge($response, array('expectedPrice' => $payment['price'], 'recieved' => $amount)));
return false;
}
if ($payment['currency'] != $currency) {
ipLog()->error('Paysera.ipn: currencies don\'t match.', array_merge($response, array('expectedCurrency' => $payment['currency'], 'recieved' => $currency)));
return false;
}
if ($response['status'] == 1) {
$this->markAsPaid($paymentId);
} else {
ipLog()->info('Paysera.ipn: non-success payment status', $response);
}
return $response;
} catch (\Exception $e) {
ipLog()->error('Paysera.ipn: ' . get_class($e) . ' ' . $e->getMessage(), $response);
}
}
public function processSMS()
{
require_once('WebToPay.php');
$response = array();
try {
$response = WebToPay::validateAndParseData(ipRequest()->getQuery(), $this->projectId(), $this->password());
if ($response['test'] != PaymentModel::isTestMode()) {
ipLog()->error('Paysera.sms: test mode parameter doesn\'t match', $response);
return false;
}
if ($response['projectid'] != $this->projectId()) {
ipLog()->error('Paysera.sms: project ID doesn\'t match', $response);
return false;
}
return $response;
} catch (\Exception $e) {
ipLog()->error('Paysera.sms: ' . get_class($e) . ' ' . $e->getMessage(), $response);
}
}
public function markAsPaid($paymentId, $dbData = array(), $eventData = array())
{
$payment = Model::getPayment($paymentId);
$dbData['isPaid'] = 1;
Model::update($paymentId, $dbData);
$info = array(
'id' => $payment['orderId'],
'paymentId' => $payment['id'],
'paymentMethod' => 'Paysera',
'title' => $payment['title'],
'userId' => $payment['userId']
);
$info = array_merge($info, $eventData);
ipEvent('ipPaymentReceived', $info);
}
public function getPayseraForm($paymentId)
{
require_once('WebToPay.php');
$payment = Model::getPayment($paymentId);
if (!$payment) {
throw new \Ip\Exception("Can't find order id. " . $paymentId);
}
$options = array(
'projectid' => $this->projectId(),
'sign_password' => $this->password(),
'orderid' => $paymentId,
'amount' => $payment['price'],
'currency' => $payment['currency'],
'accepturl' => ipRouteUrl('Paysera_return'),
'callbackurl' => ipRouteUrl('Paysera_ipn'),
'test' => $this->isTestMode() ? "1" : "0",
'cancelurl' => ipConfig()->baseUrl(),
'parameters' => $payment['securityCode'],
'p_email' => isset($payment['payer_email']) ? $payment['payer_email'] : null,
'payment' => isset($payment['payment']) ? $payment['payment'] : null,
'paytext' => isset($payment['title']) ? $payment['title'] . ' ([site_name] / [order_nr])' : null
);
$request = WebToPay::redirectToPayment($options);
var_dump($request);exit;
}
/**
*
* Returns $data encoded in UTF8. Very useful before json_encode as it fails if some strings are not utf8 encoded
* @param mixed $dat array or string
* @return array
*/
private function checkEncoding($dat)
{
if (is_string($dat)) {
if (mb_check_encoding($dat, 'UTF-8')) {
return $dat;
} else {
return utf8_encode($dat);
}
}
if (is_array($dat)) {
$answer = array();
foreach ($dat as $i => $d) {
$answer[$i] = $this->checkEncoding($d);
}
return $answer;
}
return $dat;
}
public function projectId()
{
return ipGetOption('Paysera.projectId');
}
public function password()
{
return ipGetOption('Paysera.password');
}
public function isTestMode()
{
return ipGetOption('Paysera.mode') == self::MODE_TEST;
}
public function isSkipMode()
{
return ipGetOption('Paysera.mode') == self::MODE_SKIP;
}
public function isProductionMode()
{
return ipGetOption('Paysera.mode') == self::MODE_PRODUCTION;
}
public function secretWord()
{
return ipGetOption('Paysera.secretWord');
}
public function correctConfiguration()
{
if ($this->getActive() && $this->getSid()) {
return true;
} else {
return false;
}
}
}