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
| <?php
use Tygh\Registry;
if (!defined('BOOTSTRAP')) { die('Access denied'); }
// define('__PATH','/var/www/vhosts/wonderwander-shop.com/httpdocs/app/payments/tinkoff/');
define('__PATH',dirname(__FILE__) . '/tinkoff/');
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' ' ."\r\n",FILE_APPEND);
/*
$order_id = '';
if(isset($_REQUEST['OrderId'])){
$order_id = (int) $_REQUEST['OrderId'];
}
if(isset($_REQUEST['Success'])){
$success = $_REQUEST['Success'];
}
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' success ' .$success ."\r\n",FILE_APPEND);
if($success){
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' redirect ' .$order_id ."\r\n",FILE_APPEND);
// $order_info = fn_get_order_info($order_id);
print_r($auth);
// echo " ";
// fn_order_placement_routines('route', $order_id);
// print_r(Tygh::$app['session']['cart']);
// fn_clear_cart($_SESSION['cart']);
// fn_clear_cart(Tygh::$app['session']['cart']);
// header('Location:' . 'https://wonderwander-shop.com/index.php?dispatch=checkout.complete&order_id='.$order_id);
die('...');
}
*/
require_once dirname(__FILE__) . '/tinkoff/TinkoffMerchantAPI.php';
require_once dirname(__FILE__) . '/tinkoff/Debug.php';
// file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' libs ' ."\r\n",FILE_APPEND);
if (!defined('BOOTSTRAP')) { die('Access denied'); }
// file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' BOOTSTRAP ' ."\r\n",FILE_APPEND);
if (defined('PAYMENT_NOTIFICATION')) {
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' PAYMENT_NOTIFICATION ' ."\r\n",FILE_APPEND);
if(isset($_REQUEST['OrderId'])){
$order_id = (int) $_REQUEST['OrderId'];
}
else{
$order_id = $_COOKIE['tinkoff_order_id'];
}
$order_info = fn_get_order_info($order_id);
$processor_data = $order_info['payment_method'];
if ($mode == 'notification') {
$_POST['Password'] = $processor_data['processor_params']['secret_key'];
ksort($_POST);
$sorted = $_POST;
$original_token = $sorted['Token'];
unset($sorted['Token']);
$values = implode('', array_values($sorted));
$token = hash('sha256', $values);
// //log
$log = $_POST;
$log['token'] = $token;
$log['original_token'] = $original_token;
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' ' . json_encode($log)."\r\n",FILE_APPEND);
if($token == $original_token){
if($_POST['Status'] == 'AUTHORIZED' && $order_info['status'] == 'P'){
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' OK1'."\r\n",FILE_APPEND);
die('OK');
}
switch ($_POST['Status']) {
case 'AUTHORIZED': $order_status = 'O'; break; /*Деньги на карте захолдированы. Корзина очищается.*/
case 'CONFIRMED': $order_status = 'P'; break; /*Платеж подтвержден.*/
case 'CANCELED': $order_status = 'I'; break; /*Платеж отменен*/
case 'REJECTED': $order_status = 'F'; break; /*Платеж отклонен.*/
case 'REVERSED': $order_status = 'D'; break; /*Платеж отменен*/
case 'REFUNDED': $order_status = 'I'; break; /*Произведен возврат денег клиенту*/
}
if(!isset($order_status)){
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' NOTOK1'."\r\n",FILE_APPEND);
die('NOTOK');
}
fn_change_order_status($order_id, $order_status);
if($order_status == 'P'){
$pp_response = array();
$pp_response['order_status'] = $order_status;
$pp_response['reason_text'] = 'Success payment';
// $pp_response['transaction_id'] = isset($_POST['paymentId']) ? $_POST['paymentId'] : '';
$pp_response['transaction_id'] = isset($_POST['PaymentId']) ? $_POST['PaymentId'] : '';
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' fn_finish_payment '. $pp_response['transaction_id']."\r\n",FILE_APPEND);
fn_finish_payment($order_id, $pp_response);
}
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' OK2'."\r\n",FILE_APPEND);
die('OK');
}
else{
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' NOTOK2'."\r\n",FILE_APPEND);
die('NOTOK');
}
}
elseif ($mode == 'success' || $mode == 'failed') {
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' fn_order_placement_routines1'."\r\n",FILE_APPEND);
fn_order_placement_routines('route', $order_id, false);
}
} else {
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' NO PAYMENT_NOTIFICATION' ."\r\n",FILE_APPEND);
$order_info = fn_get_order_info($order_id);
$arrFields = array(
'OrderId' => $order_info['order_id'],
'Amount' => $order_info['total'] * 100,
'DATA' => 'Email='.$order_info['email'],
);
$Tinkoff = new TinkoffMerchantAPI( $processor_data['processor_params']['merchant_id'], $processor_data['processor_params']['secret_key'], $processor_data['processor_params']['gateway'] );
$request = $Tinkoff->buildQuery('Init', $arrFields);
$request = json_decode($request);
setcookie('tinkoff_order_id', $order_id, 0, '/');
setcookie('tinkoff_redirect', (isset($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].'/index.php?dispatch=payment_notification.success&payment=tinkoff', 0, '/');
if(isset($request->PaymentURL)){
fn_change_order_status($order_id, 'O');
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' redirect '.$order_id . ' ' . $request->PaymentURL ."\r\n",FILE_APPEND);
header('Location: '. $request->PaymentURL);
}
else{
fn_change_order_status($order_id, 'F');
file_put_contents(__PATH.'log.txt', date('Y.m.d H:i:s') . ' fn_order_placement_routines2'."\r\n",FILE_APPEND);
fn_order_placement_routines('route', $order_id, false);
}
}
exit; |