forked from servekon/yandex-money-cms-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamoney.module
454 lines (391 loc) · 14 KB
/
yamoney.module
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
define('YAMONEY_CHECK_RESULT_CODE_SUCCESS', 0);
define('YAMONEY_CHECK_RESULT_CODE_ERROR_MD5', 1);
define('YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM', 100);
define('YAMONEY_CHECK_RESULT_CODE_ERROR_REQUEST', 200);
define('YAMONEY_DEFAULT_PAYMENT_METHOD', 'PC');
/**
* Implements hook_init().
*/
function yamoney_init() {
module_load_include('inc', 'yamoney', 'transactions');
}
/**
* Implements hook_permission().
*/
function yamoney_permission() {
return array(
'access yandex money transaction' => array(
'title' => t('Access YaMoney transaction'),
'description' => t('Display the yandex money transaction.'),
),
'access own yandex money transaction' => array(
'title' => t('Access own YaMoney transaction'),
'description' => t('Display the yandex money transaction by own user.'),
),
'access yamoney settings' => array(
'title' => t('Access YaMoney API'),
'description' => t('Access to Yandex Money API variables.'),
),
);
}
/**
* Implements hook_menu().
*/
function yamoney_menu() {
$items = array();
// Secret transaction status url
$items['yamoney/check_order'] = array(
'page callback' => 'yamoney_check_order',
'access callback' => 'yamoney_check_server_ip',
'type' => MENU_CALLBACK,
);
$items['yamoney/check_payment'] = array(
'page callback' => 'yamoney_check_payment',
'access callback' => 'yamoney_check_server_ip',
'type' => MENU_CALLBACK,
);
// Config page
$items['admin/config/system/yamoney'] = array(
'title' => t('Yandex.Money Core'),
'description' => t('Yandex.Money base settings page.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('yamoney_admin_settings'),
'access arguments' => array('access yamoney settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'yamoney.admin.inc',
);
// Transactions page menu
$items['admin/reports/yamoney_transactions'] = array(
'title' => t('Yandex.Money transactions'),
'description' => t('Yandex.Money all transactions page.'),
'page callback' => 'yamoney_transactions_page',
'access arguments' => array('access yandex money transaction'),
'type' => MENU_NORMAL_ITEM,
'file' => 'transactions.inc',
);
// Account transaction table
$items['user/%user/yamoney_payments'] = array(
'title' => t('My transactions'),
'description' => t('Yandex money user transactions page.'),
'page callback' => 'yamoney_transactions_user_page',
'page arguments' => array(1),
'access arguments' => array('access own yandex money transaction'),
'type' => MENU_LOCAL_TASK,
'file' => 'transactions.inc',
);
// Transaction callbacks
$items['yamoney/complete'] = array(
'page callback' => 'yamoney_complete_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['yamoney/fail'] = array(
'page callback' => 'yamoney_fail_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['yamoney/temp'] = array(
'page callback' => 'yamoney_temp_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Returns text message for success transaction
* @return mixed
*/
function yamoney_complete_page() {
drupal_set_title(t('Congratulations!'));
module_invoke_all('yamoney_complete');
$success = variable_get('yamoney_success_text', array('value' => ''));
$page = array(
'#markup' => $success['value'],
);
drupal_alter('yamoney_complete_page', $page);
return $page;
}
/**
* Returns text message for failed transaction.
* @return mixed
*/
function yamoney_fail_page() {
drupal_set_title(t('Sorry!'));
module_invoke_all('yamoney_fail');
$fail = variable_get('yamoney_fail_text', array('value' => ''));
$page = array(
'#markup' => $fail['value'],
);
drupal_alter('yamoney_fail_page', $page);
return $page;
}
/**
* One callback to handle both Success and Fail pages.
*/
function yamoney_temp_page() {
if (isset($_GET['action']) && $_GET['action'] === 'PaymentSuccess') {
drupal_goto('yamoney/complete', array('query' => $_GET));
}
else {
drupal_goto('yamoney/fail', array('query' => $_GET));
}
}
/**
* Check Yandex.Money IP
* @return bool
*/
function yamoney_check_server_ip() {
$ip = ip_address();
$allowed_ips = variable_get('yamoney_ip', '0.0.0.0');
$allowed_ips = explode("\n", $allowed_ips);
foreach ($allowed_ips as $allowed_ip) {
$allowed_ip = trim($allowed_ip);
if (empty($allowed_ip)) {
continue;
}
if ($allowed_ip === '0.0.0.0' || $ip === $allowed_ip) {
return TRUE;
}
}
return FALSE;
}
/**
* @param array $params
* @return string
*/
function yamoney_create_md5($params = array()) {
if (!empty($params)) {
$output = '';
$output .= $params['action'];
$output .= ';' . $params['orderSumAmount'];
$output .= ';' . $params['orderSumCurrencyPaycash'];
$output .= ';' . $params['orderSumBankPaycash'];
$output .= ';' . $params['shopId'];
$output .= ';' . $params['invoiceId'];
$output .= ';' . $params['customerNumber'];
$md5 = md5($output . ';' . variable_get('yamoney_secret', ''));
return drupal_strtoupper($md5);
}
else {
return '';
}
}
/**
* Handle "check order" requests from Yandex.Money
*/
function yamoney_check_order() {
if (!isset($_POST['action']) || $_POST['action'] !== 'checkOrder') {
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_REQUEST, 'Invalid action. Expected action: checkOrder.');
}
if (!isset($_POST['md5']) || $_POST['md5'] !== yamoney_create_md5($_POST)) {
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_MD5);
}
if (!isset($_POST['transaction_id'])) {
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, 'Invalid transaction_id provided.');
}
$transaction = yamoney_transaction_load($_POST['transaction_id']);
if (!$transaction) {
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, 'Invalid transaction_id provided.');
}
if ($transaction->status !== YAMoneyTransaction::STATUS_IN_PROCESS) {
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM,
'Invalid transaction state: ' . $transaction->status . '. Expected: ' . YAMoneyTransaction::STATUS_IN_PROCESS . '.');
}
$order = array(
'success' => TRUE,
'transaction' => $transaction,
'request' => $_POST,
);
drupal_alter('yamoney_check_order', $order);
if (!$order['success']) {
$error = isset($order['error']) ? $order['error'] : 'Can not process transaction.';
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, $error);
}
if (yamoney_update_transaction_status($transaction->ymid, YAMoneyTransaction::STATUS_PROCESSED)) {
yamoney_check_send_result('checkOrderResponse');
}
else {
yamoney_check_send_result('checkOrderResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, 'Can not save transaction.');
}
}
/**
* Handle "check payment" requests from Yandex.Money
*/
function yamoney_check_payment() {
if (!isset($_POST['action']) || $_POST['action'] !== 'paymentAviso') {
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_REQUEST, 'Invalid action. Expected action: paymentAviso.');
}
if (!isset($_POST['md5']) || $_POST['md5'] !== yamoney_create_md5($_POST)) {
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_MD5);
}
if (!isset($_POST['transaction_id'])) {
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, 'Invalid transaction_id provided.');
}
$transaction = yamoney_transaction_load($_POST['transaction_id']);
if (!$transaction) {
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, 'Invalid transaction_id provided.');
}
if ($transaction->status !== YAMoneyTransaction::STATUS_PROCESSED && $transaction->status !== YAMoneyTransaction::STATUS_PAYED) {
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM,
'Invalid transaction state: ' . $transaction->status . '. Expected: ' . YAMoneyTransaction::STATUS_PROCESSED . '.');
}
$payment = array(
'success' => TRUE,
'transaction' => $transaction,
'request' => $_POST,
);
drupal_alter('yamoney_process_payment', $payment);
if (!$payment['success']) {
$error = isset($payment['error']) ? $payment['error'] : 'Can not process transaction.';
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, $error);
}
if ($transaction->status === YAMoneyTransaction::STATUS_PAYED || yamoney_update_transaction_status($transaction->ymid, YAMoneyTransaction::STATUS_PAYED)) {
yamoney_check_send_result('paymentAvisoResponse');
}
else {
yamoney_check_send_result('paymentAvisoResponse', YAMONEY_CHECK_RESULT_CODE_ERROR_CUSTOM, 'Can not save transaction.');
}
}
/**
* @param string $type
* @param int $code
* @param string $message
*/
function yamoney_check_send_result($type, $code = 0, $message = '') {
$attributes = array(
'performedDatetime' => $_POST['requestDatetime'],
'code' => $code,
'invoiceId' => $_POST['invoiceId'],
'shopId' => $_POST['shopId'],
);
if ($message) {
$attributes['message'] = $message;
}
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<' . $type . ' ' . drupal_attributes($attributes) . ' />';
drupal_exit();
}
/**
* @return array
*/
function yamoney_get_payment_methods() {
return array(
'PC' => t('Payment from a Yandex.Money e-wallet'),
'AC' => t('Payment by any bank card'),
'GP' => t('Payment in cash via retailers and payment kiosks'),
'MC' => t('Payment from a mobile phone balance'),
'WM' => t('Payment from a WebMoney e-wallet'),
'AB' => t('Payment via Alfa-Click'),
'SB' => t('Payment via Sberbank: payment by text messages or Sberbank Online'),
'MA' => t('Payment via MasterPass'),
'PB' => t('Payment via Promsvyazbank'),
);
}
/**
* @return array
*/
function yamoney_get_enabled_payment_methods() {
$all_payments = yamoney_get_payment_methods();
$enabled_payments = variable_get('yamoney_payment_method', array_keys($all_payments));
$payments = array();
foreach ($all_payments as $key => $label) {
if (isset($enabled_payments[$key]) && $enabled_payments[$key] === $key) {
$payments[$key] = $label;
}
}
return $payments;
}
/**
* @param YAMoneyTransaction $transaction
* @return array
*/
function yamoney_get_order_submission_params(YAMoneyTransaction $transaction) {
if (variable_get('yamoney_shop', 0)) {
// shop
$params = yamoney_get_shop_params($transaction);
}
else {
$params = yamoney_get_quick_params($transaction);
}
drupal_alter('yamoney_order_submission_params', $params);
return $params;
}
/**
* @param YAMoneyTransaction $transaction
* @return array
*/
function yamoney_get_shop_params(YAMoneyTransaction $transaction) {
$params = array();
// Идентификатор Контрагента
$params['shopId'] = variable_get('yamoney_shop_id', '');
// Идентификатор товара
//$params['shopArticleId'] = '';
// Номер витрины Контрагента
$params['scid'] = variable_get('yamoney_scid', '');
// Сумма заказа
$params['sum'] = $transaction->amount;
// Идентификатор плательщика. Номер оплачиваемого мобильного телефона, договора и т. п., специфично для Контрагента.
$params['customerNumber'] = $transaction->uid;
// Уникальный для данного shopId номер заказа в ИС Контрагента.
$params['orderNumber'] = $transaction->order_id;
// URL, на который должен быть осуществлен редирект в случае успеха перевода (urlencoded значение).
$params['shopSuccessURL'] = url('yamoney/complete', array('absolute' => TRUE));
// URL, на который должен быть осуществлен редирект в случае ошибки (urlencoded значение).
$params['shopFailURL'] = url('yamoney/fail', array('absolute' => TRUE));
// Детали способа совершения платежа.
$params['paymentType'] = variable_get('yamoney_default_payment_method', YAMONEY_DEFAULT_PAYMENT_METHOD);
// Provide CMS name
$params['cms_name'] = 'drupal';
// Internally used field
$params['order_id'] = $transaction->order_id;
// Internally used field
$params['transaction_id'] = $transaction->ymid;
drupal_alter('yamoney_shop_params', $params);
return $params;
}
/**
* @param YAMoneyTransaction $transaction
* @return array
*/
function yamoney_get_quick_params(YAMoneyTransaction $transaction) {
$params = array();
$params['receiver'] = variable_get('yamoney_receiver', '');
$params['formcomment'] = variable_get('yamoney_formcomment', '');
$params['short-dest'] = t('Payments for order No') . $transaction->order_id;
$params['writable-targets'] = FALSE;
$params['comment-needed'] = FALSE;
$params['label'] = $transaction->order_id;
$params['targets'] = t('Payments for order No') . $transaction->order_id;
$params['sum'] = $transaction->amount;
$params['quickpay-form'] = 'shop';
$params['paymentType'] = variable_get('yamoney_default_payment_method', YAMONEY_DEFAULT_PAYMENT_METHOD);
$params['cms_name'] = 'drupal';
drupal_alter('yamoney_quick_params', $params);
return $params;
}
/**
* @return string
*/
function yamoney_get_order_submission_url() {
$is_test = variable_get('yamoney_mode', 'test') === 'test';
if (variable_get('yamoney_shop', 0)) {
// shop
if ($is_test) {
return 'https://demomoney.yandex.ru/eshop.xml';
}
else {
return 'https://money.yandex.ru/eshop.xml';
}
}
else {
// quick
if ($is_test) {
return 'https://demomoney.yandex.ru/quickpay/confirm.xml';
}
else {
return 'https://money.yandex.ru/quickpay/confirm.xml';
}
}
}