-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillingReferrals.php
More file actions
211 lines (177 loc) · 6.78 KB
/
Copy pathBillingReferrals.php
File metadata and controls
211 lines (177 loc) · 6.78 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
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
<?php
/*
* This file is part of FeatherPanel.
*
* Copyright (C) 2025 MythicalSystems Studios
* Copyright (C) 2025 FeatherPanel Contributors
* Copyright (C) 2025 Cassian Gherman (aka NaysKutzu)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See the LICENSE file or <https://www.gnu.org/licenses/>.
*/
namespace App\Addons\billingreferrals;
use App\App;
use App\Plugins\AppPlugin;
use App\Plugins\Events\Events\AuthEvent;
use App\Addons\billingcore\Helpers\CreditsHelper;
use App\Addons\billingreferrals\Chat\ReferralCode;
use App\Addons\billingreferrals\Chat\ReferralUsage;
use App\Addons\billingreferrals\Helpers\ReferralHelper;
class BillingReferrals implements AppPlugin
{
public static function processEvents(\App\Plugins\PluginEvents $event): void
{
// Listen for successful user registration
$event->on(AuthEvent::onAuthRegisterSuccess(), function (array $data) {
self::processNewUserRegistration($data);
});
// Listen for user creation (admin created users)
$event->on(\App\Plugins\Events\Events\UserEvent::onUserCreated(), function (array $data) {
self::processNewUserRegistration($data['user_data'] ?? $data);
});
}
public static function pluginInstall(): void
{
// Plugin installation logic
// Tables will be created via migrations
}
public static function pluginUpdate(?string $oldVersion, ?string $newVersion): void
{
// Plugin update logic
}
public static function pluginUninstall(): void
{
// Plugin uninstallation logic
// Clean up is handled by the migration system
}
/**
* Process a new user registration to check for referral codes.
*/
private static function processNewUserRegistration(array $userData): void
{
// Check if referral system is enabled
if (!ReferralHelper::isEnabled()) {
return;
}
$userId = $userData['id'] ?? null;
if (!$userId) {
return;
}
// Check if there's a referral code in the session/cookie
$referralCode = self::getReferralCodeFromSession();
if (empty($referralCode)) {
return;
}
// Get the referral code from database
$code = ReferralCode::getByCode($referralCode);
if (!$code) {
return;
}
// Check if code is valid (not expired, not maxed out)
if (!ReferralCode::isValid($code)) {
App::getInstance(true)->getLogger()->warning('Referral code no longer valid: ' . $referralCode);
return;
}
// Don't allow self-referral
if ((int) $code['user_id'] === (int) $userId) {
App::getInstance(true)->getLogger()->warning('User tried to use their own referral code: ' . $userId);
return;
}
// Check if this user has already been referred by someone else
if (ReferralUsage::hasUserBeenReferred($userId)) {
App::getInstance(true)->getLogger()->warning('User already referred by someone else: ' . $userId);
return;
}
// Use transaction to ensure atomicity
$pdo = \App\Chat\Database::getPdoConnection();
try {
$pdo->beginTransaction();
// Double-check code validity with lock
$stmt = $pdo->prepare(
'SELECT * FROM featherpanel_billingreferrals_codes WHERE id = :id FOR UPDATE'
);
$stmt->execute(['id' => $code['id']]);
$lockedCode = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$lockedCode || !ReferralCode::isValid($lockedCode)) {
$pdo->rollBack();
return;
}
// Record the referral usage
$usageRecorded = ReferralUsage::recordUsage((int) $code['id'], $userId, $pdo);
if (!$usageRecorded) {
$pdo->rollBack();
return;
}
// Increment code uses
$incremented = ReferralCode::incrementUses((int) $code['id'], $pdo);
if (!$incremented) {
$pdo->rollBack();
return;
}
// Award credits to the referrer
$referrerCredits = ReferralHelper::getReferrerCredits();
if ($referrerCredits > 0) {
$referrerId = (int) $code['user_id'];
$added = CreditsHelper::addUserCredits($referrerId, $referrerCredits);
if (!$added) {
App::getInstance(true)->getLogger()->error(
'Failed to award referrer credits for user ' . $referrerId . ' and referral ' . $code['id']
);
}
}
// Award credits to the new user (referee)
$refereeCredits = ReferralHelper::getRefereeCredits();
if ($refereeCredits > 0) {
$added = CreditsHelper::addUserCredits($userId, $refereeCredits);
if (!$added) {
App::getInstance(true)->getLogger()->error(
'Failed to award referee credits for user ' . $userId
);
}
}
$pdo->commit();
// Clear the referral code from session
self::clearReferralCodeFromSession();
App::getInstance(true)->getLogger()->info(
'Referral processed successfully. Code: ' . $referralCode . ', New user: ' . $userId .
', Referrer: ' . $code['user_id']
);
} catch (\Exception $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
App::getInstance(true)->getLogger()->error('Failed to process referral: ' . $e->getMessage());
}
}
/**
* Get referral code from session/cookie.
*/
private static function getReferralCodeFromSession(): ?string
{
// Check for referral code in cookie (set when user visits referral link)
if (isset($_COOKIE['billingreferrals_code'])) {
return $_COOKIE['billingreferrals_code'];
}
return null;
}
/**
* Clear referral code from session/cookie.
*/
private static function clearReferralCodeFromSession(): void
{
// Clear the cookie
if (isset($_COOKIE['billingreferrals_code'])) {
setcookie('billingreferrals_code', '', [
'expires' => time() - 3600,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax',
]);
}
}
}