-
Notifications
You must be signed in to change notification settings - Fork 11
/
transactions.inc
224 lines (204 loc) · 4.78 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
<?php
/*
* This file contains transaction operations.
*/
/**
* Class YAMoneyTransaction
*/
class YAMoneyTransaction extends stdClass {
const STATUS_IN_PROCESS = 'in_process';
const STATUS_PROCESSED = 'processed';
const STATUS_PAYED = 'payed';
const STATUS_FAILED = 'failed';
/**
* @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 function __construct() {
$this->status = YAMoneyTransaction::STATUS_IN_PROCESS;
$this->created = time();
$this->data = array();
}
}
/**
* @param int $ymid Transaction id.
* @return YAMoneyTransaction
*/
function yamoney_transaction_load($ymid) {
$row = $transaction = db_select('yamoney_transaction', 'yt')
->fields('yt')
->condition('yt.ymid', $ymid)
->execute()
->fetch();
if (!$row) {
return NULL;
}
$transaction = new YAMoneyTransaction();
foreach ($row as $key => $value) {
$transaction->{$key} = $value;
}
$transaction->data = unserialize($transaction->data);
return $transaction;
}
/**
* @param YAMoneyTransaction $transaction
* @return bool
*/
function yamoney_transaction_save(YAMoneyTransaction $transaction) {
$fields = (array) $transaction;
$fields['data'] = serialize($transaction->data);
if ($transaction->ymid) {
return !!db_update('yamoney_transaction')
->fields($fields)
->condition('ymid', $transaction->ymid)
->execute();
}
else {
unset($fields['ymid']);
$id = db_insert('yamoney_transaction')
->fields($fields)
->execute();
$transaction->ymid = $id;
return !!$id;
}
}
/**
* Simply set transaction status.
* @param $ymid int Transaction id.
* @param $status string Transaction status.
* @return bool
*/
function yamoney_update_transaction_status($ymid, $status) {
return !!db_update('yamoney_transaction')
->fields(array('status' => $status))
->condition('ymid', $ymid)
->execute();
}
/**
* Delete user payment transaction
* @param int $ymid
* @return bool
*/
function yamoney_transaction_delete($ymid) {
return !!db_delete('yamoney_transaction')
->condition('ymid', $ymid)
->execute();
}
/**
* Render all transactions page
* @return mixed
*/
function yamoney_transactions_page() {
$transactions = db_select('yamoney_transaction', 'yt')
->fields('yt')
->extend('PagerDefault') // this adds the pager
->limit(20);
$transactions = $transactions->execute()->fetchAll();
$table = yamoney_create_transaction_table($transactions);
$output = drupal_render($table);
drupal_set_title(t('Transactions'));
return $output;
}
/**
* Render user transactions
* @param stdClass $user
* @return bool|string
*/
function yamoney_transactions_user_page($user) {
$transactions = db_select('yamoney_transaction', 'yt')
->fields('yt')
->condition('uid', $user->uid)
->extend('PagerDefault') // this adds the pager
->limit(20);
$transactions = $transactions->execute()->fetchAll();
$table = yamoney_create_transaction_table($transactions);
$output = drupal_render($table);
drupal_set_title(t('My transactions'));
return $output;
}
/**
* @param array $table_rows
* @return null|string
*/
function yamoney_create_transaction_table($table_rows) {
$rows = array();
if (count($table_rows) > 0) {
foreach ($table_rows as $item) {
$rows[] = yamoney_transaction_build_payment_row((array) $item);
}
$header = array(t('Id'), t('Name'), t('Amount'), t('Date/Time'), t('Status'), t('E-mail'), t('Order ID'));
$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 yamoney_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;
}