-
Notifications
You must be signed in to change notification settings - Fork 55
/
paypal_ipn.php
198 lines (188 loc) · 6.56 KB
/
paypal_ipn.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
<?php
/**
* Created by PhpStorm.
* User: Ricardo
* Date: 25/03/2018
* Time: 00:05
*/
use phpDocumentor\Reflection\Types\Boolean;
/**
* Login to client 11.50 made by Muuleek
*/
require 'config/config.php';
/**
* comment to show E_NOTICE [undefinied variable etc.], comment if you want make script and see all errors
*/
error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE);
/**
* true = show sent queries and SQL queries status/status code/error message
*/
define('DEBUG_DATABASE', false);
define('INITIALIZED', true);
if (!defined('ONLY_PAGE')) {
define('ONLY_PAGE', true);
}
/**
* check if site is disabled/requires installation
*/
include_once('./system/load.loadCheck.php');
/**
* fix user data, load config, enable class auto loader
*/
include_once('./system/load.init.php');
/**
* DATABASE
*/
include_once('./system/load.database.php');
if (DEBUG_DATABASE) {
Website::getDBHandle()->setPrintQueries(true);
}
/**
* EndDatabase
*/
/** REQUEST PAYPAL-IPN CLASS */
require('paypal/PaypalIPN.php');
require_once "classes/account.php";
/**
* @param string $tid
* @return bool
*/
$issetTransactionOnDatabase = function ($tid) use ($SQL) {
$query = $SQL->prepare("SELECT * FROM paypal_transactions WHERE txn_id = :tid");
$query->execute(['tid' => $tid]);
if ($query->rowCount() > 0) {
return true;
} else {
return false;
}
};
/**
* @return bool
*/
$doubleStatus = function () use ($SQL) {
$q = $SQL->prepare("SELECT value FROM server_config WHERE config = 'double'");
$q->execute([]);
$q = $q->fetchAll();
if ($q[0]['value'] == "active") {
return true;
} else {
return false;
}
};
$transactionCompleted = function ($tid) use ($SQL) {
$query = $SQL->prepare("SELECT * FROM paypal_transactions WHERE txn_id = '$tid' and payment_status = 'Completed'");
$query->execute([]);
if ($query->rowCount() > 0) {
return true;
} else {
return false;
}
};
/**
* @param $payment_status
* @param $txn_id
*/
$updatepaypal = function ($payment_status, $txn_id) use ($SQL) {
$update = $SQL->prepare("UPDATE paypal_transactions SET payment_status = :payment_status WHERE txn_id = :txn_id");
$update->execute(['payment_status' => $payment_status, 'txn_id' => $txn_id]);
};
/**
* @param $payment_status
* @param $payer_email
* @param $payer_id
* @param $item_number1
* @param $mc_gross
* @param $mc_currency
* @param $txn_id
*/
$insertpaypal = function ($payment_status, $payer_email, $payer_id, $item_number1, $mc_gross, $mc_currency, $txn_id) use ($SQL) {
$date_now = date('Y-m-d H:i:s');
$insert = $SQL->prepare("INSERT INTO paypal_transactions (payment_status, date, payer_email, payer_id, item_number1, mc_gross, mc_currency, txn_id) VALUES (:payment_status, :date_now , :payer_email, :payer_id, :item_number1, :mc_gross, :mc_currency, :txn_id)");
$insert->execute(['payment_status' => $payment_status, 'date_now' => $date_now, 'payer_email' => $payer_email, 'payer_id' => $payer_id, 'item_number1' => $item_number1, 'mc_gross' => $mc_gross, 'mc_currency' => $mc_currency, 'txn_id' => $txn_id]);
};
$log_post = function () {
$handle = fopen('paypal.log', 'a');
fwrite($handle, "-------------------------\r\n");
foreach ($_POST as $key => $value) {
fwrite($handle, $key . "=>" . $value . "\r\n");
}
fwrite($handle, "-------------------------\r\n");
fclose($handle);
};
$log_post();
/** $payment_status = "Completed";$payer_email = "meucomprador@gmail.com";$payer_id = "5SZY8K2LZ8H8L";$item_number1 = "item_number1";$mc_gross = 98.10;$mc_currency = "BRL";$txn_id = "4GC74590A5738524S";if($issetTransactionOnDatabase($txn_id)){$updatepaypal($payment_status,$txn_id);}else{$insertpaypal($payment_status,$payer_email,$payer_id,$item_number1,$mc_gross,$mc_currency,$txn_id);}*/
/** @var PaypalIPN $ipn */
$ipn = new PaypalIPN();
if ($config['paypal']['env'] == "sandbox") {
$ipn->useSandbox();
}
$ipn->usePHPCerts();
date_default_timezone_set("America/Sao_Paulo");
try {
/** @var boolean $verified */
$verified = $ipn->verifyIPN();
if ($verified) {
$payment_status = $_POST['payment_status'];
$payer_id = $_POST['payer_id'];
$payer_email = $_POST['payer_email'];
$item_number1 = $_POST['item_number1'];
$mc_currency = $_POST['mc_currency'];
$tid = $_POST['txn_id'];
$ex = explode('-', $item_number1);
$acc_name = $ex[0];
$product_id = $ex[1];
$date = new DateTime();
$now = $date->format('[d-m-Y H:i:s] ');
$price = (array_keys($config['donate']['offers'][intval($product_id)])[0] / 100);
$coinCount = array_values($config['donate']['offers'][$product_id])[0];
$acc = new Account();
$acc->loadByName($acc_name);
if (!$transactionCompleted($tid)) {
if ($payment_status == "Completed") {
if ($issetTransactionOnDatabase($tid)) {
$updatepaypal($payment_status, $tid);
} else {
$insertpaypal($payment_status, $payer_email, $payer_id, $item_number1, $price, $mc_currency, $tid);
}
$handle = fopen("paypal.log", "a");
$coins_old = $acc->getPremiumPoints();
$acc->setPremiumPoints($acc->getPremiumPoints() + ($doubleStatus() ? $coinCount * 1 : $coinCount));
$acc->save();
$coins_new = $acc->getPremiumPoints();
fwrite($handle, $now . ":> status:" . $payment_status . ";accname:" . $acc_name . ";pid:" . $product_id . ";qnt:" . $coinCount . ";price:" . $price . ";saldo_anterior:" . $coins_old . ";novo_saldo:" . $coins_new . ";tid:" . $tid . "\r\n");
fclose($handle);
$date_now = date('Y-m-d H:i:s');
$transaction_code = $_POST['txn_id'];
$pay_method = "Paypal";
$name = $acc->getName();
include_once "send_payment_voucher.php";
// Reply with an empty 200 response to indicate to paypal the IPN was received correctly
header("HTTP/1.1 200 OK");
} else {
if ($issetTransactionOnDatabase($tid)) {
$updatepaypal($payment_status, $tid);
} else {
$insertpaypal($payment_status, $payer_email, $payer_id, $item_number1, $price, $mc_currency, $tid);
}
$handle = fopen("paypal.log", "a");
fwrite($handle, $now . ":> status:" . $payment_status . ";accname:" . $acc_name . ";pid:" . $product_id . ";qnt:" . $coinCount . ";price:" . $price . "\r\n");
fclose($handle);
header("HTTP/1.1 200 OK");
}
}
} else {
$handle = fopen("paypal.log", "a");
$date = new DateTime();
//$now = $date->format('d/m/Y H:i:s');
fwrite($handle, $now . ":>verify_error;from:" . $_SERVER['REMOTE_ADDR'] . "\r\n");
fclose($handle);
header("Location: " . $config['base_url']);
exit();
}
} catch (Exception $e) {
$handle = fopen("paypal.log", "a");
$date = new DateTime();
$now = $date->format('d/m/Y H:i:s');
fwrite($handle, $now . ":> " . $e->getMessage() . ";from:" . $_SERVER['REMOTE_ADDR'] . "\r\n");
fclose($handle);
}