-
Notifications
You must be signed in to change notification settings - Fork 3
/
transactions.inc
397 lines (363 loc) · 11.7 KB
/
transactions.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/*
* This file contains transaction operations.
*/
/**
* Class YooMoneyApiTransaction
*/
class YooMoneyApiTransaction extends stdClass {
const STATUS_IN_PROCESS = 'in_process';
const STATUS_PROCESSED = 'processed';
const STATUS_PAYED = 'payed';
const STATUS_FAILED = 'failed';
const STATUS_COMPLETED = 'completed';
/**
* @var int Transaction id.
*/
public $ymid;
/**
* @var int Transaction user id.
*/
public $uid;
/**
* @var int Transaction amount.
*/
public $amount;
/**
* @var int Unix timestamp when the transaction was created.
*/
public $created;
/**
* @var string Transaction status.
*/
public $status;
/**
* @var string Transaction user email.
*/
public $mail;
/**
* @var int Order id.
*/
public $order_id;
/**
* @var array Array of additional transaction information.
*/
public $data;
public $payment_id;
/**
*
*/
public function __construct() {
$this->status = YooMoneyApiTransaction::STATUS_IN_PROCESS;
$this->created = time();
$this->data = array();
$this->payment_id = '';
}
}
/**
* @param int $ymid Transaction id.
* @return YooMoneyApiTransaction
*/
function yoomoney_api_transaction_load($ymid) {
$row = $transaction = db_select('yoomoney_api_transaction', 'yt')
->fields('yt')
->condition('yt.ymid', $ymid)
->execute()
->fetch();
if (!$row) {
return NULL;
}
$transaction = new YooMoneyApiTransaction();
foreach ($row as $key => $value) {
$transaction->{$key} = $value;
}
$transaction->data = unserialize($transaction->data);
return $transaction;
}
/**
* @param int $order_id Order id.
* @return YooMoneyApiTransaction
*/
function yoomoney_api_transaction_load_by_order($order_id) {
$row = $transaction = db_select('yoomoney_api_transaction', 'yt')
->fields('yt')
->condition('yt.order_id', $order_id)
->execute()
->fetch();
if (!$row) {
return NULL;
}
$transaction = new YooMoneyApiTransaction();
foreach ($row as $key => $value) {
$transaction->{$key} = $value;
}
$transaction->data = unserialize($transaction->data);
return $transaction;
}
/**
* @param string $payment_id
* @return YooMoneyApiTransaction
*/
function yoomoney_api_transaction_load_by_payment($payment_id) {
$row = $transaction = db_select('yoomoney_api_transaction', 'yt')
->fields('yt')
->condition('yt.payment_id',$payment_id)
->execute()
->fetch();
if (!$row) {
return NULL;
}
$transaction = new YooMoneyApiTransaction();
foreach ($row as $key => $value) {
$transaction->{$key} = $value;
}
$transaction->data = unserialize($transaction->data);
return $transaction;
}
/**
* @param YooMoneyApiTransaction $transaction
* @return bool
*/
function yoomoney_api_transaction_save(YooMoneyApiTransaction $transaction) {
$fields = (array) $transaction;
$fields['data'] = serialize($transaction->data);
if ($transaction->ymid) {
return !!db_update('yoomoney_api_transaction')
->fields($fields)
->condition('ymid', $transaction->ymid)
->execute();
}
else {
unset($fields['ymid']);
$id = db_insert('yoomoney_api_transaction')
->fields($fields)
->execute();
$transaction->ymid = $id;
return !!$id;
}
}
/**
* Simply set transaction payment_id.
* @param $ymid string Transaction id
* @param $payment_id string Payment id
* @return bool
*/
function yoomoney_api_update_transaction_payment_id($ymid, $payment_id) {
return !!db_update('yoomoney_api_transaction')
->fields(array('payment_id' => $payment_id))
->condition('ymid', $ymid)
->execute();
}
/**
* Simply set transaction status.
* @param $payment_id string Transaction id in Kassa
* @param $status string Transaction status.
* @return bool
*/
function yoomoney_api_update_transaction_status($payment_id, $status) {
return !!db_update('yoomoney_api_transaction')
->fields(array('status' => $status))
->condition('payment_id', $payment_id)
->execute();
}
/**
* Simply set transaction status.
* @param $payment_id string Transaction id in Kassa
* @param $amount
* @return bool
*/
function yoomoney_api_update_transaction_amount($payment_id, $amount) {
return !!db_update('yoomoney_api_transaction')
->fields(array('amount' => $amount))
->condition('payment_id', $payment_id)
->execute();
}
/**
* Delete user payment transaction
* @param int $ymid
* @return bool
*/
function yoomoney_api_transaction_delete($ymid) {
return !!db_delete('yoomoney_api_transaction')
->condition('ymid', $ymid)
->execute();
}
/**
* Render all transactions page
* @return mixed
*/
function yoomoney_api_transactions_page()
{
$transactions = db_select('yoomoney_api_transaction', 'yt')
->fields('yt')
->extend('PagerDefault')// this adds the pager
->orderBy('ymid', 'DESC')
->limit(20);
$transactions = $transactions->execute()->fetchAll();
$table = yoomoney_api_create_transaction_table($transactions);
$output = drupal_render($table);
drupal_set_title(t('Transactions'));
drupal_add_js('
function yoomoney_api_capture_payment_request() {
document.getElementsByTagName("body")[0].style.cursor = "wait";
let paymentId = this.dataset.paymentId;
let r = new XMLHttpRequest();
let el = document.createElement("span");
el.innerText = this.text;
this.parentElement.insertBefore(el, this);
this.style.display = "none";
r.open("POST", "'.url('admin/yoomoney_api/actions').'", true);
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
r.onreadystatechange = function () {
if (r.readyState !== 4) {
return;
}
document.getElementsByTagName("body")[0].style.cursor = "default";
if(r.status === 200 && r.responseText === "OK") {
alert("'.t('Вы подтвердили платёж в ЮKassa.').'");
} else {
alert("'.t('Платёж не подтвердился. Попробуйте ещё раз.').'");
}
location.reload();
};
r.send("action=capturePayment&paymentId=" + this.dataset.paymentId
+ "&module=" + this.dataset.module);
}
function yoomoney_api_cancel_payment_request() {
document.getElementsByTagName("body")[0].style.cursor = "wait";
let paymentId = this.dataset.paymentId;
let r = new XMLHttpRequest();
let el = document.createElement("span");
el.innerText = this.text;
this.parentElement.insertBefore(el, this);
this.style.display = "none";
r.open("POST", "'.url('admin/yoomoney_api/actions').'", true);
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
r.onreadystatechange = function () {
if (r.readyState !== 4) {
return;
}
document.getElementsByTagName("body")[0].style.cursor = "default";
if(r.status === 200 && r.responseText === "OK") {
alert("'.t('Вы отменили платёж в ЮKassa. Деньги вернутся клиенту.').'");
} else {
alert("'.t('Платёж не отменился. Попробуйте ещё раз.').'");
}
location.reload();
};
r.send("action=cancelPayment&paymentId=" + this.dataset.paymentId
+ "&module=" + this.dataset.module);
}
document.addEventListener("DOMContentLoaded", function() {
[].forEach.call(document.getElementsByClassName("yoomoney_api_capture_request_link"), function(el) {
el.addEventListener("click", yoomoney_api_capture_payment_request);
});
[].forEach.call(document.getElementsByClassName("yoomoney_api_cancel_request_link"), function(el) {
el.addEventListener("click", yoomoney_api_cancel_payment_request);
});
});', 'inline');
return $output;
}
/**
* Render user transactions
* @param stdClass $user
* @return bool|string
*/
function yoomoney_api_transactions_user_page($user) {
$transactions = db_select('yoomoney_api_transaction', 'yt')
->fields('yt')
->condition('uid', $user->uid)
->extend('PagerDefault') // this adds the pager
->orderBy('ymid', 'DESC')
->limit(20);
$transactions = $transactions->execute()->fetchAll();
$table = yoomoney_api_create_transaction_table($transactions);
$output = drupal_render($table);
drupal_set_title(t('My transactions'));
return $output;
}
/**
* @param array $table_rows
* @return array
*/
function yoomoney_api_create_transaction_table($table_rows) {
$rows = array();
if (count($table_rows) > 0) {
foreach ($table_rows as $item) {
$row = yoomoney_api_transaction_build_payment_row((array)$item);
$orderId = $row['order_id'];
$actions = '';
$module = '';
if (module_exists('commerce')
&& commerce_order_load($row['order_id'])
&& yooCommerceApiPaymentTransactioLoad($row['payment_id'])
) {
$module = 'commerce';
$row['order_id'] = l($row['order_id'], 'admin/commerce/orders/'.$orderId);
$actions .= l(t('View'), 'admin/commerce/orders/'.$orderId).' ';
} elseif (module_exists('uc_payment')
&& uc_order_load($row['order_id'])
) {
$module = 'uc_payment';
$row['order_id'] = l($row['order_id'], 'admin/store/orders/'.$orderId);
$actions .= l(t('View'), 'admin/store/orders/'.$orderId).' ';
}
if ($row['status'] === \YooKassa\Model\PaymentStatus::WAITING_FOR_CAPTURE) {
if ($module) {
if ($module === 'commerce') {
$actions .= l(t('Edit'), 'admin/commerce/orders/'.$orderId.'/edit');
} elseif ($module === 'uc_payment') {
$actions .= l(t('Edit'), 'admin/store/orders/'.$orderId.'/edit');
}
$actions .= ' <a href="javascript:void(0);" class="yoomoney_api_capture_request_link" data-payment-id="'.$row['payment_id'].'" data-module="'.$module.'">'.t('Confirm').'</a> ';
$actions .= '<a href="javascript:void(0);" class="yoomoney_api_cancel_request_link" data-payment-id="'.$row['payment_id'].'" data-module="'.$module.'">'.t('Cancel').'</a>';
}
}
$row[] = $actions;
$rows[] = $row;
}
$header = array(t('Id'), t('Name'), t('Amount'), t('Date/Time'), t('Status'), t('E-mail'), t('Order ID'), t('Payment ID'), t('Actions'));
$table['table_rows'] = array(
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
);
$table['pager'] = array(
'#theme' => 'pager',
'#weight' => 5,
);
}
else {
$table = array(
'#type' => 'markup',
'#markup' => t('There no transactions.'),
);
}
return $table;
}
/**
* @param array $row
* @return array
*/
function yoomoney_api_transaction_build_payment_row($row) {
unset($row['data']);
foreach ($row as $key => $item) {
if ($key == 'uid') {
if ($item) {
$user = user_load($item);
$item = $user->name;
}
else {
$item = 'guest';
}
}
if ($key == 'created') {
$item = format_date($item, 'custom', 'd-m-Y H:i:s');
}
if ($key == 'amount') {
$item = $item . ' RUR';
}
$row[$key] = $item;
}
return $row;
}