forked from aliukevicius/commerce_mokilizingas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MokiLizingas.inc
303 lines (254 loc) · 7.49 KB
/
MokiLizingas.inc
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
class MokiLizingas
{
/** @var string Mokilizingas authorization username */
protected $userName;
/** @var string Mokilizingas authorization password */
protected $userPassword;
/** @var bool Payment processing mode */
protected $testingMode;
/** @var string Redirect URL to mokilizingas */
protected $redirectUrl = 'https://secure.mokilizingas.lt/online/api/post';
/** @var string Mokilizingas authorization URL */
protected $authUrl = 'https://secure.mokilizingas.lt/online/api/auth';
/** @var string Mokilizingas Payment status check URL */
protected $checkUrl = 'https://secure.mokilizingas.lt/online/api/check';
public function __construct($userName, $password, $testingMode = false)
{
$this->userName = trim($userName);
$this->userPassword = trim($password);
$this->testingMode = $testingMode;
}
public function startSession(MokilizingasOrder $order, $returnUrl, $alertUrl)
{
$sessionId = null;
$orderData = $order->getOrderData();
if ($this->testingMode) {
$orderData['testing'] = 'Y';
}
$orderData['user_name'] = $this->userName;
$orderData['user_psw'] = $this->userPassword;
$orderData['return_url'] = $returnUrl;
$orderData['alert_url'] = $alertUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getAuthUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($orderData));
$result = curl_exec($ch);
$resultData = json_decode($result);
if (strlen($resultData->errorCode) > 0) {
// remove user and password to prevent accidental display in error message for user!
$orderData['user_name'] = '****';
$orderData['user_psw'] = '****';
throw new MokilizingasException($resultData->errorCode . ': ' . $resultData->errorMessage . "\n" . http_build_query($orderData));
}
return $resultData;
}
public function checkPayment($sessionId, $orderId)
{
$data = array(
'session_id' => $sessionId,
'order_id' => $orderId,
'user_name' => $this->userName,
'user_psw' => $this->userPassword
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getPaymentCheckUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$resultData = json_decode($result);
if ($resultData->result == 'ERROR') {
// remove user and password to prevent accidental display in error message for user!
$orderData['user_name'] = '****';
$orderData['user_psw'] = '****';
throw new MokilizingasException($resultData->result. ': ' . $resultData->resultMsg . "\n" . http_build_query($orderData));
}
return $resultData;
}
/**
* User redirect URL
*
* @return string
*/
public function getRedirectUrl()
{
return $this->redirectUrl;
}
/**
* Authorization URL
*
* @return string
*/
public function getAuthUrl()
{
return $this->authUrl;
}
/**
* Payment check URL
* @return string
*/
public function getPaymentCheckUrl()
{
return $this->checkUrl;
}
}
class MokilizingasOrder
{
protected $orderData;
public function __construct($orderId, $orderAmount)
{
$this->orderData['order_id'] = $orderId;
if (is_int($orderAmount) || is_float($orderAmount)) {
$this->orderData['order_amount'] = $orderAmount;
} else if (preg_match('/^([0-9]+)+([.,])+([0-9]{1,2})$/', $orderAmount) == 1 ) {
$tmp = round(str_replace(',', '.', $orderAmount), 2);
if ($tmp <= 0) {
throw new MokilizingasException("Order amount should be more than 0. " . $orderAmount);
}
$this->orderData['order_amount'] = $orderAmount;
} else {
throw new MokilizingasException("Order amount should be a decimal. No thousand separator! " . $orderAmount);
}
}
/**
* Get order data
*
* @return array
*/
public function getOrderData()
{
return $this->orderData;
}
/**
* Set order information. Can be any additional information.
*
* @param $orderInfo
*/
public function setOrderInfo($orderInfo)
{
$this->orderData['order_info'] = $orderInfo;
}
/**
* Set client first name
*
* @param $firstName
*/
public function setFirstName($firstName)
{
$this->orderData['first_name'] = $firstName;
}
/**
* Set client last name
*
* @param $lastName
*/
public function setLastName($lastName)
{
$this->orderData['last_name'] = $lastName;
}
/**
* Set client phone number
*
* @param $clientPhone
*/
public function setClientPhone($clientPhone)
{
$this->orderData['client_phone'] = $clientPhone;
}
/**
* Set client email
*
* @param $clientEmail
*/
public function setClientEmail($clientEmail)
{
$this->orderData['client_email'] = $clientEmail;
}
/**
* Set client address
*
* @param $clientAddress
*/
public function setClientAddress($clientAddress)
{
$this->orderData['client_address'] = $clientAddress;
}
/**
* Set clients personal identification code
*
* @param $persCode
*/
public function setPersCode($persCode)
{
$this->orderData['pers_code'] = $persCode;
}
/**
* Set persons identification document type
*
* Availabel types:
* PP - pasport
* AK - personal identification card
*
* @param $docType
*/
public function setDocType($docType)
{
if ($docType == 'PP' || $docType == 'AK') {
$this->orderData['doc_type'] = $docType;
} else {
throw new MokilizingasException("Personal identification document type can only be 'PP' for passport and
'AK' for personal identification card");
}
}
/**
* Set identification document number
*
* @param $docNo
*/
public function setDocNo($docNo)
{
$this->orderData['doc_no'] = $docNo;
}
/**
* Set vendor phone
*
* @param $vendorPhone
*/
public function setVendorPhone($vendorPhone)
{
$this->orderData['vendor_phone'] = $vendorPhone;
}
/**
* Set vendor email
*
* @param $vendorEmail
*/
public function setVendorEmail($vendorEmail)
{
$this->orderData['vendor_email'] = $vendorEmail;
}
/**
* Array in JSON format with shopping cart items
* FORMAT
* [
* {
* "name": "Item name", // string
* "category": "B", // string
* "price": "1123.56" // string
* "sale": false // boolean
* }
* ]
*
* @param $orderJson
*/
public function setOrderJson($orderJson)
{
$this->orderData['order_json'] = $orderJson;
}
}
class MokilizingasException extends Exception {}