forked from darkmihai/Warcry-CMS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipn_6tm9y3vxegbilkfpm0jf.php
221 lines (198 loc) · 5.81 KB
/
ipn_6tm9y3vxegbilkfpm0jf.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
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
<?php
include_once 'engine/initialize.php';
//Load the most important module
$CORE->load_CoreModule('accounts.finances');
//Setup the finances class
$finance = new AccountFinances();
define('SECRET', ''); // YOUR SECRET KEY
define('CREDIT_TYPE_CHARGEBACK', 2);
//Whitelisted IP addresses
$ipsWhitelist = array(
);
//Get them variables
$userId = isset($_GET['uid']) ? (int)$_GET['uid'] : NULL;
$credits = isset($_GET['currency']) ? (int)$_GET['currency'] : NULL;
$type = isset($_GET['type']) ? (int)$_GET['type'] : NULL;
$refId = isset($_GET['ref']) ? $_GET['ref'] : NULL;
$signature = isset($_GET['sig']) ? $_GET['sig'] : NULL;
//Assume failured
$result = false;
//A little fuction
function calculatePingbackSignature($params, $secret)
{
$str = '';
foreach ($params as $k=>$v)
{
$str .= "$k=$v";
}
$str .= $secret;
return md5($str);
}
//Check if them variables are set
if (!empty($userId) && !empty($credits) && isset($type) && !empty($refId) && !empty($signature))
{
//Let's generate the signature
$signatureParams = array(
'uid' => $userId,
'currency' => $credits,
'type' => $type,
'ref' => $refId
);
$signatureCalculated = calculatePingbackSignature($signatureParams, SECRET);
//check if IP is in whitelist and if signature matches
if (in_array($_SERVER['REMOTE_ADDR'], $ipsWhitelist) && ($signature == $signatureCalculated))
{
//Success
$result = true;
//Log this transaction
$CORE->load_CoreModule('transaction.logging');
//Setup the log class
$Logs = new TransactionLogging();
//Save the variables
$Logs->SetVariables($_GET);
//Set the account id
$finance->SetAccount($userId);
//Set the currency to gold
$finance->SetCurrency(CURRENCY_GOLD);
//Check if it's deduction, Paymentwall send amount value with "-"
if ($type == CREDIT_TYPE_CHARGEBACK)
{
//remove the minus
$credits = (int)trim($credits, '-');
}
//Set the amount we are Giving/Taking
$finance->SetAmount($credits);
if ($type == CREDIT_TYPE_CHARGEBACK)
{
// Deduct credits from user
// This is optional, but we recommend this type of crediting to be implemented as well
// Note that currency amount sent for chargeback is negative, e.g. -5, so be caferul about the sign
// Don’t deduct negative number, otherwise user will get credits instead of losing them
//Resolve the deduction reason by id
switch ($_GET['reason'])
{
case 1:
$reason = 'Chargeback';
$reasonUser = 'Payment chargeback';
break;
case 2:
$reason = 'Credit Card fraud Ban user';
$reasonUser = 'Credit Card fraud';
break;
case 3:
$reason = 'Order fraud Ban user';
$reasonUser = 'Order fraud';
break;
case 4:
$reason = 'Bad data entry';
$reasonUser = 'Bad data entry';
break;
case 5:
$reason = 'Fake / proxy user';
$reasonUser = 'Fake / proxy user';
break;
case 6:
$reason = 'Rejected by advertiser';
$reasonUser = 'Rejected by advertiser';
break;
case 7:
$reason = 'Duplicate conversions';
$reasonUser = 'Duplicate conversions';
break;
case 8:
$reason = 'Goodwill credit taken back';
$reasonUser = 'Goodwill credit taken back';
break;
case 9:
$reason = 'Cancelled order';
$reasonUser = 'Cancelled order';
break;
case 10:
$reason = 'Partially reversed transaction';
$reasonUser = 'Partially reversed transaction';
break;
default:
$reason = 'Unknown code ' . (int)$_GET['reason'];
$reasonUser = 'Uuknown reason';
break;
}
//append message to the log
$Logs->append("The transaction is deduction type, reason: \"".$reason."\". ");
//Take the coins from the user
$Deduct = $finance->Charge('Deduction reason: ' . $reasonUser . '.', CA_SOURCE_TYPE_DEDUCTION);
//Check if the deduction was successfull
if ($Deduct === true)
{
//Deduction success
$Logs->SetLogType(TRANSACTION_LOG_TYPE_NORMAL);
//append message to the log
$Logs->append("The deduction was successfull.");
}
else
{
//Deduction failed
$Logs->SetLogType(TRANSACTION_LOG_TYPE_URGENT);
//append message to the log
$Logs->append("The deduction of coins failed, error returned: ".$Deduct.". ");
}
unset($reason, $reasonUser, $Deduct);
}
else
{
// Give credits to user
//resolve the transaction type
switch ($type)
{
case 0:
$TransactionType = 'Credit is given.';
$CA_SourceType = CA_SOURCE_TYPE_PURCHASE;
$CA_SourceString = 'Purchased Gold Coins';
break;
case 1:
$TransactionType = 'Credit is given as a customer service.';
$CA_SourceType = CA_SOURCE_TYPE_REWARD;
$CA_SourceString = 'Earned Gold Coins';
break;
default:
$TransactionType = 'Uknown type ' . $type;
$CA_SourceType = CA_SOURCE_TYPE_NONE;
$CA_SourceString = 'Received gold coins from unknown source';
break;
}
//append message to the log
$Logs->append("The transaction is reward type, type: \"".$TransactionType."\". ");
//Give coins to the user
$Reward = $finance->Reward($CA_SourceString, $CA_SourceType);
//check if the reward was successful
if ($Reward)
{
//Reward success
$Logs->SetLogType(TRANSACTION_LOG_TYPE_NORMAL);
//append message to the log
$Logs->append("The rewarding was successfull. ");
}
else
{
//Reward failed
$Logs->SetLogType(TRANSACTION_LOG_TYPE_URGENT);
//append message to the log
$Logs->append("The rewarding with coins failed, error returned: ".$Reward.". ");
}
unset($TransactionType, $CA_SourceType, $CA_SourceString, $Reward);
}
unset($finance);
//save the log
$Logs->save();
}
}
//The request was OK
if ($result)
{
echo 'OK';
exit;
}
else
{
header('HTTP/1.0 404 not found');
exit;
}