forked from dawngerpony/phpIPN
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotify.php
More file actions
187 lines (161 loc) · 7.52 KB
/
notify.php
File metadata and controls
187 lines (161 loc) · 7.52 KB
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
<?php
/************************************************************************
* This file is part of phpIPN. *
* *
* phpIPN is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* phpIPN is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with phpIPN. If not, see <http://www.gnu.org/licenses/>. *
************************************************************************/
// include all necessary files
require_once("include/includes.php");
define('DB_MANAGER_CLASS_NAME', 'DBManager');
$ticketTypes = Config::$ticketTypes;
function kaput($msg) {
Logger::error("KAPUT: $msg");
$mail = SingletonFactory::getInstance()->getSingleton('MailManager');
$mail->sendErrorMailToAdmins($msg);
die("ERROR: $msg");
}
Logger::notice("Processing new payment notification...");
//Logger::debug("POST: " . print_r($_POST, true));
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
// build the request
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
//Logger::debug("req = $req");
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$isSandbox = (isset($_POST['test_ipn']) && ($_POST['test_ipn'] == 1));
$paypalUrl = $isSandbox ? Config::$paypalUrlBeta : Config::$paypalUrl;
Logger::debug("isSandbox = $isSandbox, paypal URL = $paypalUrl");
$fp = fsockopen($paypalUrl, 80, $errno, $errstr, 30);
if(!$fp) {
kaput("HTTP error! Couldn't open socket to $paypalUrl");
// HTTP ERROR
}
fputs ($fp, $header . $req);
while (false === feof($fp)) {
$res = fgets($fp, 1024);
if(0 != strcmp($res, "VERIFIED")) {
// Do nothing
} else {
Logger::info("Transaction verified! res = $res");
// check that txn_id has not been previously processed
if(false === isset($_POST['txn_id']) || empty($_POST['txn_id'])) {
kaput("No transaction ID was found in the request. You need one of them.");
}
$txn_id = $_POST['txn_id'];
if(true === isDuplicateTxnId($txn_id, $db)) {
kaput("Duplicate transaction id: $txn_id");
} else {
//Logger::debug("Transaction ID $txn_id isn't duplicate");
}
// check that receiver_email is your Primary PayPal email
$receiverEmail = isset($_POST['receiver_email']) ? $_POST['receiver_email'] : '';
$correctEmail = $isSandbox ? Config::$receiverEmailTest : Config::$receiverEmail;
if($receiverEmail != $correctEmail) {
kaput("Receiver_email from IPN: $receiverEmail, should be $correctEmail");
} else {
//Logger::debug("E-mails are correct");
}
Logger::debug("Parameters all valid: txn_id[$txn_id], receiverEmail[$receiverEmail]");
// process payment
if(true === Config::DB_ENABLED) {
//Logger::debug("Creating new transaction");
try {
$txn = new Transaction($_POST);
} catch(Exception $e) {
kaput($e->getMessage());
}
//Logger::debug("Adding transaction to database");
// get an instance of the database from the singleton factory
$db = SingletonFactory::getInstance()->getSingleton(DB_MANAGER_CLASS_NAME);
try {
$db->addTransaction($txn);
} catch(Exception $e) {
kaput("Error adding payment with txn_id=$txn_id to database: " . $e->getMessage());
}
$payment_status = $txn->getField('payment_status');
if(true === Config::MAIL_ENABLED) {
$templateFound = false;
foreach($ticketTypes as $oneType) {
$params = array();
// @TODO Update these with dynamic quantities
$payer_email = $txn->getField('payer_email');
$to = "$payer_email, tickets@planetangel.net";
$params['pa_ticket_id'] = $txn->getPaTicketId();
$params['first_name'] = $txn->getField('first_name');
$params['last_name'] = $txn->getField('last_name');
$prepayItem = $txn->getPrepayItem($oneType);
if(true === is_null($prepayItem)) {
//Logger::debug("No match for $oneType");
continue;
}
$templateFound = true;
$params['quantity'] = $prepayItem['quantity'];
$params['item_name'] = $prepayItem['item_name'];
$mail = SingletonFactory::getInstance()->getSingleton('MailManager');
if("Completed" === $payment_status) {
try {
$status = $mail->sendConfirmationMailToUser($to, $params, $oneType);
} catch(Exception $e) {
kaput($e->getMessage());
}
Logger::notice("Sent confirmation mail (type=$oneType) successfully to $to");
}
$params['payment_status'] = $payment_status;
try {
$mail->sendTransactionMailToAdmins($params);
} catch(Exception $e) {
kaput($e->getMessage());
}
Logger::debug("Sent notification mail to admins successfully, payment_status=$payment_status");
}
if(false === $templateFound) {
Logger::error("Unable to find e-mail template");
}
} else {
Logger::debug("Mail disabled, not sending mail");
}
} else {
Logger::debug("DB disabled, not adding transaction");
}
}
if(strcmp ($res, "INVALID") == 0) {
$txn_id = isset($_POST['txn_id']) ? $_POST['txn_id'] : 'none supplied';
$msg = "Invalid transaction received (response from Paypal: $res)! Transaction ID: $txn_id";
Logger::warn($msg);
echo $msg;
// log for manual investigation
// send e-mail to P&A
}
}
fclose ($fp);
/**
* Checks the database for an existing transaction ID.
* @return true if it's duplicate, false if not.
*/
function isDuplicateTxnId($txn_id) {
$db = SingletonFactory::getInstance()->getSingleton(DB_MANAGER_CLASS_NAME);
$dupFound = $db->checkDuplicateRow('txn_id', $txn_id);
if($dupFound == Constants::DUPLICATE_FOUND) {
Logger::error("Duplicate transaction id: $txn_id!");
return true;
} else {
return false;
}
}