-
Notifications
You must be signed in to change notification settings - Fork 3
/
sites.php
151 lines (125 loc) · 3.6 KB
/
sites.php
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
<?php
chdir('../');
require_once 'includes/main/WebUI.php';
require_once 'include/Webservices/Revise.php';
require_once 'include/Webservices/Create.php';
require_once 'include/Webservices/Delete.php';
require_once 'modules/Users/Users.php';
require_once 'modules/SPPayments/SPPayments.php';
$current_user = Users::getActiveAdminUser();
$adb = PearDatabase::getInstance();
file_put_contents(
'vt.log',
sprintf("%s / %s / %s:%s %s\n",
date('Ymd H:i:s'),
$_SERVER['REMOTE_ADDR'],
array_pop(explode('/',__FILE__)),
__LINE__,
var_export($_GET,1)
),
FILE_APPEND
);
$data = processIncome($_GET);
if (is_bool($data)){
header('Content-Type: application/json');
echo json_encode(['status'=>'Err']);
exit();
}
if ($data['status'] == 'Y') {
//wsUpdate($data);
wsCreatePayment($data);
} else {
wsZeroPayments($data['id']);
}
/*
* order - cf_845
* status Y | N
*/
header('Content-Type: application/json');
echo json_encode(['status'=>'Ok']);
function processIncome($data)
{
$keys = ['order', 'status', 'amount'];
foreach ($keys as $field){
if (!(array_key_exists($field, $data)
&& !empty($data[$field])
&& ($data['amount'] != '0.00'))
){
return false;
}
}
$leadid = findLeadBySiteId($data['order']);
return empty($leadid)? false : [
'id' => $leadid,
'cf_1051' => $data['amount'],
'status' => $data['status']
];
}
function findLeadBySiteId($id)
{
$db = PearDatabase::getInstance();
$db->database->SetFetchMode(2);
$sql = "SELECT leadid FROM vtiger_leadscf
LEFT JOIN vtiger_crmentity vc ON leadid = crmid
WHERE cf_845 = ?
AND deleted = 0
ORDER BY leadid DESC LIMIT 1";
$leadResult = $db->pquery($sql, [$id]);
$nr = $db->num_rows($leadResult);
if ($nr == 0) return false;
return $leadResult->FetchRow()['leadid'];
}
function wsUpdate($data)
{
if (empty($data)) return false;
global $current_user;
try {
$wsid = vtws_getWebserviceEntityId('Leads', $data['id']);
$data['id'] = $wsid;
vtws_revise($data, $current_user);
return true;
} catch (WebServiceException $ex) {
return $ex->getMessage();
}
}
function wsZeroPayments($leadid)
{
$db = PearDatabase::getInstance();
$db->database->SetFetchMode(2);
$sql = "SELECT payid FROM sp_payments
LEFT JOIN vtiger_crmentity vc ON payid = crmid
WHERE payer = ?
AND deleted = 0
ORDER BY payid DESC";
$paymentsResult = $db->pquery($sql, [$leadid]);
$nr = $db->num_rows($paymentsResult);
if ($nr == 0) return false;
while ($row = $db->fetch_array($paymentsResult)){
wsDeletePayment($row['payid']);
}
return $nr;
}
function wsCreatePayment($data = [])
{
if (empty($data)) return false;
global $current_user;
$paymentsMod = 'SPPayments';
$wsid = vtws_getWebserviceEntityId('Leads', $data['id']);
$payment['payer'] = $wsid;
$payment['pay_date'] = date('Y-m-d');
$payment['amount'] = $data['cf_1051'];
$payment['cf_648'] = 'CloudPayments';
$payment['spstatus'] = 'Executed';
$payment['assigned_user_id'] = $current_user->id;
return vtws_create($paymentsMod, $payment, $current_user);
}
function wsDeletePayment($id)
{
global $current_user;
try {
$wsid = vtws_getWebserviceEntityId('SPPayments', $id);
vtws_delete($wsid, $current_user);
} catch (WebServiceException $ex) {
echo $ex->getMessage();
}
}