forked from NB-Core/lotgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment.php
214 lines (195 loc) · 6.93 KB
/
payment.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
<?php
// mail ready
// addnews ready
// translator ready
ob_start();
set_error_handler("payment_error");
define("ALLOW_ANONYMOUS",true);
require_once("common.php");
require_once("lib/http.php");
tlschema("payment");
// Send an empty HTTP 200 OK response to acknowledge receipt of the notification
header('HTTP/1.1 200 OK');
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
$post = httpallpost();
reset($post);
foreach ($post as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// Set up the acknowledgement request headers
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n"; // HTTP POST request
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .="Host: www.paypal.com\r\n";
$header .="Connection: close\r\n\r\n";
// Open a socket for the acknowledgement request
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
//$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = httppost('item_name');
$item_number = httppost('item_number');
$payment_status = httppost('payment_status');
$payment_amount = httppost('mc_gross');
$payment_currency = httppost('mc_currency');
$txn_id = httppost('txn_id');
$receiver_email = httppost('business'); //formerly receiver_email, but with using multiple emails for paypal it's gross
$payer_email = httppost('payer_email');
$payment_fee = httppost('mc_fee');
$response='';
if (!$fp) {
// HTTP ERROR
payment_error(E_ERROR,"Unable to open socket to verify payment",__FILE__,__LINE__);
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
$response .= $res;
if (strcmp (trim($res), "VERIFIED") == 0) {
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
if ($payment_status=="Completed" || $payment_status == 'Refunded'){
if ($payment_status == 'Refunded'){
//sanitize the data to look like a completed transaction
$payment_amount = $mc_gross;
$payment_fee = 0;
$txn_type = 'refund';
}
$sql = "SELECT * FROM " . db_prefix("paylog") . " WHERE txnid='{$txn_id}'";
$result = db_query($sql);
if (db_num_rows($result)==1){
$emsg .= "Already logged this transaction ID ($txn_id)\n";
payment_error(E_ERROR,$emsg,__FILE__,__LINE__);
}
if (($receiver_email != "[email protected]") &&
($receiver_email != getsetting("paypalemail", ""))) {
$emsg = "This payment isn't to me(".getsetting("paypalemail", "").")! It's to $receiver_email.\n";
payment_error(E_WARNING,$emsg,__FILE__,__LINE__);
}
writelog($response);
}else{
modulehook("donation-error",$post);
payment_error(E_ERROR,"Payment Status isn't 'Completed' it's '$payment_status'",__FILE__,__LINE__);
}
}
else if (strcmp (trim($res), "INVALID") == 0) {
// log for manual investigation
payment_error(E_ERROR,"Payment Status is 'INVALID'!\n\nPOST data:`n".serialize($_POST),__FILE__,__LINE__);
}
}
fclose ($fp);
}
function writelog($response){
global $post;
global $item_name, $item_number, $payment_status, $payment_amount;
global $payment_currency, $txn_id, $receiver_email, $payer_email;
global $payment_fee,$txn_type;
$match = array();
preg_match("'([^:]*):([^/])*'",$item_number,$match);
if ($match[1]>""){
$sql = "SELECT acctid FROM " . db_prefix("accounts") . " WHERE login='{$match[1]}'";
$result = db_query($sql);
$row = db_fetch_assoc($result);
$acctid = $row['acctid'];
if ($acctid>0){
$donation = $payment_amount;
// if it's a reversal, it'll only post back to us the amount
// we received back, with out counting the fees, which we
// receive under a different transaction, but get no
// notification for.
if ($txn_type =="reversal") $donation -= $payment_fee;
$hookresult = modulehook("donation_adjustments",array("points"=>$donation*getsetting('dpointspercurrencyunit',100),"amount"=>$donation,"acctid"=>$acctid,"messages"=>array()));
//updated to make a setting here for each Dollar, Euro, Shekel
$hookresult['points'] = round($hookresult['points']);
$sql = "UPDATE " . db_prefix("accounts") . " SET donation = donation + '{$hookresult['points']}' WHERE acctid=$acctid";
$result = db_query($sql);
debuglog("Received donator points for donating -- Credited Automatically",false,$acctid,"donation",$hookresult['points'],false);
if (!is_array($hookresult['messages'])){
$hookresult['messages'] = array($hookresult['messages']);
}
foreach ($hookresult['messages'] as $id=>$message){
debuglog($message,false,$acctid,"donation",0,false);
}
if (db_affected_rows()>0) $processed = 1;
}
}
$sql = "
INSERT INTO " . db_prefix("paylog") . " (
info,
response,
txnid,
amount,
name,
acctid,
processed,
filed,
txfee,
processdate
)VALUES (
'".addslashes(serialize($post))."',
'".addslashes($response)."',
'$txn_id',
'$payment_amount',
'{$match[1]}',
".(int)$acctid.",
".(int)$processed.",
0,
'$payment_fee',
'".date("Y-m-d H:i:s")."'
)";
$result = db_query($sql);
if ($match[1]>"" && $acctid>0) modulehook("donation", array("id"=>$acctid, "amt"=>$donation*getsetting('dpointspercurrencyunit',100), "manual"=>false));
modulehook("donation-processed",$post);
$err = db_error();
if ($err) {
payment_error(E_ERROR,"SQL: $sql\nERR: $err", __FILE__,__LINE__);
}
}
function payment_error($errno, $errstr, $errfile, $errline){
global $payment_errors;
if (!is_int($errno) || (is_int($errno) && ($errno & error_reporting()))) {
$payment_errors.="Error $errno: $errstr in $errfile on $errline\n";
}
}
$adminEmail = getsetting("gameadminemail", "[email protected]");
if ($payment_errors>"") {
$subj = translate_mail("Payment Error",0);
// $payment_errors not translated
ob_start();
echo "<b>GET:</b><pre>";
reset($_GET);
var_dump($_GET);
echo "</pre><b>POST:</b><pre>";
reset($_POST);
var_dump($_POST);
echo "</pre><b>SERVER:</b><pre>";
reset($_SERVER);
var_dump($_SERVER);
echo "</pre>";
$contents = ob_get_contents();
ob_end_clean();
$payment_errors .= "<hr>".$contents;
mail($adminEmail,$subj,$payment_errors."<hr>","From: " . getsetting("gameadminemail", "[email protected]"));
}
$output = ob_get_contents();
if ($output > ""){
if ($adminEmail == "") $adminEmail = "[email protected]";
echo "<b>GET:</b><pre>";
reset($_GET);
var_dump($_GET);
echo "</pre><b>POST:</b><pre>";
reset($_POST);
var_dump($_POST);
echo "</pre><b>SERVER:</b><pre>";
reset($_SERVER);
var_dump($_SERVER);
echo "</pre>";
mail($adminEmail,"Serious LoGD Payment Problems on {$_SERVER['HTTP_HOST']}",ob_get_contents(),"Content-Type: text/html");
}
ob_end_clean();
?>