Skip to content

Commit

Permalink
Merge pull request #219 from MythicalLTD/216-add-funds-page-with-plug…
Browse files Browse the repository at this point in the history
…in-support

Add stripe & paypal gateway!
  • Loading branch information
NaysKutzu authored Feb 9, 2025
2 parents 83716cd + d43cd8f commit 4296e55
Show file tree
Hide file tree
Showing 42 changed files with 1,717 additions and 188 deletions.
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Build Frontend",
"type": "node-terminal",
"command": "php mythicalclient frontend:build",
"request": "launch"

},
{
"name": "Watch Backend",
"type": "node-terminal",
"command": "php mythicalclient backend:watch",
"request": "launch"
},
{
"name": "Lint Backend",
"type": "node-terminal",
"command": "php mythicalclient backend:lint",
"request": "launch"
}
]
}
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"cSpell.words": [
"Adminannouncements",
"fastcgi",
"filesize",
"HWID",
"letsencrypt",
"lucide",
"modyfi",
"mythicalclient",
"mythicaldash",
"PHPHWID",
"Predis",
"qrcode",
"recommand",
"Regen",
"sendfile",
"shortdescription",
"SOURCECODE",
"supportbuddy",
Expand All @@ -19,7 +25,6 @@
"json.maxItemsComputed": 10000,
"editor.largeFileOptimizations": false,
"editor.renderWhitespace": "all",
"files.maxMemoryForLargeFilesMB": 16384,
"editor.useTabStops": true,
"editor.tabFocusMode": false,
"workbench.sideBar.location": "left",
Expand Down
11 changes: 0 additions & 11 deletions backend/app/Api/Admin/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\Chat\User\UserActivities;

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* # MythicalSystems License v2.0
*
* ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman
*
* Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services.
*/

$router->get('/api/admin', function (): void {
App::init();
$appInstance = App::getInstance(true);
Expand Down
9 changes: 9 additions & 0 deletions backend/app/Api/System/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
ConfigInterface::COMPANY_ZIP => $config->getSetting(ConfigInterface::COMPANY_ZIP, '12345'),
ConfigInterface::COMPANY_COUNTRY => $config->getSetting(ConfigInterface::COMPANY_COUNTRY, 'MythicalCountry'),
ConfigInterface::COMPANY_VAT => $config->getSetting(ConfigInterface::COMPANY_VAT, '1234567890'),

ConfigInterface::ENABLE_PAYPAL => $config->getSetting(ConfigInterface::ENABLE_PAYPAL, 'false'),
ConfigInterface::ENABLE_STRIPE => $config->getSetting(ConfigInterface::ENABLE_STRIPE, 'false'),

ConfigInterface::PAYPAL_CLIENT_ID => $config->getSetting(ConfigInterface::PAYPAL_CLIENT_ID, ''),
ConfigInterface::PAYPAL_IS_SANDBOX => $config->getSetting(ConfigInterface::PAYPAL_IS_SANDBOX, 'false'),

ConfigInterface::STRIPE_PUBLISHABLE_KEY => $config->getSetting(ConfigInterface::STRIPE_PUBLISHABLE_KEY, ''),
ConfigInterface::STRIPE_WEBHOOK_ID => $config->getSetting(ConfigInterface::STRIPE_WEBHOOK_ID, ''),
];

App::OK('Sure here are the settings you were looking for', ['settings' => $settings]);
Expand Down
7 changes: 6 additions & 1 deletion backend/app/Api/User/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@
$password = $_POST['password'];

$login = User::login($login, $password);
setcookie('user_token', $login, time() + 3600, '/');
if (APP_DEBUG) {
// Set the cookie to expire in 1 year if the app is in debug mode
setcookie('user_token', $login, time() + 3600 * 31 * 360, '/');

} else {
setcookie('user_token', $login, time() + 3600, '/');
}
if ($login == 'false') {
$appInstance->BadRequest('Invalid login credentials', ['error_code' => 'INVALID_CREDENTIALS']);
} else {
Expand Down
24 changes: 24 additions & 0 deletions backend/app/Api/User/Services/AddFunds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* # MythicalSystems License v2.0
*
* ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman
*
* Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services.
*/

use MythicalClient\App;
use MythicalClient\Chat\User\Session;

$router->add('/api/user/addfunds', function () {
global $pluginManager;
App::init();
$appInstance = App::getInstance(true);
$appInstance->allowOnlyPOST();
$session = new Session($appInstance);

});
20 changes: 10 additions & 10 deletions backend/app/Api/User/Services/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@
return;
}
$appInstance->OK('Order created', [
'metadata' => [
'order' => $order,
'invoice' => $invoice,
'service' => $service,
'requirements' => $requirements,
'config' => $ordersConfig,
'providerName' => $provider,
]
]);
'metadata' => [
'order' => $order,
'invoice' => $invoice,
'service' => $service,
'requirements' => $requirements,
'config' => $ordersConfig,
'providerName' => $provider,
],
]);

} else {
$appInstance->InternalServerError('Order processing error', [
'error' => 'ERR_INTERNAL_SERVER_ERROR',
Expand Down
79 changes: 41 additions & 38 deletions backend/app/Api/User/Services/OrderInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,44 @@
});

$router->post('/api/user/invoice/(.*)/pay', function ($id) {
App::init();
$appInstance = App::getInstance(true);
$session = new Session($appInstance);

$invoice = OrdersInvoices::getInvoice($id);

if (!$invoice) {
$appInstance->NotFound('Invoice not found', []);
return;
}

$orderInfo = Orders::getOrder($invoice['order']);
$serviceInfo = Services::getService($orderInfo['service']);
$price = (int) $serviceInfo['price'];

// Payment logic here
if ((int) $session->getInfo(UserColumns::CREDITS,false) < $price) {
$appInstance->Forbidden('Insufficient credits', [
'error' => 'ERR_INSUFFICIENT_CREDITS',
]);
return;
}
if ($invoice['status'] === 'paid') {
$appInstance->Forbidden('Invoice already paid', [
'error' => 'ERR_INVOICE_ALREADY_PAID',
]);
return;
}
$newBalance = (int) $session->getInfo(UserColumns::CREDITS,false) - $price;
$session->setInfo(UserColumns::CREDITS, $newBalance,false);
OrdersInvoices::updateStatus($id, "paid");
Orders::updateStatus($invoice['order'], "processed");
Orders::updateDaysLeft($invoice['order'], 30);

$appInstance->OK('Invoice status', [
'price' => $price,
]);
});
App::init();
$appInstance = App::getInstance(true);
$session = new Session($appInstance);

$invoice = OrdersInvoices::getInvoice($id);

if (!$invoice) {
$appInstance->NotFound('Invoice not found', []);

return;
}

$orderInfo = Orders::getOrder($invoice['order']);
$serviceInfo = Services::getService($orderInfo['service']);
$price = (int) $serviceInfo['price'];

// Payment logic here
if ((int) $session->getInfo(UserColumns::CREDITS, false) < $price) {
$appInstance->Forbidden('Insufficient credits', [
'error' => 'ERR_INSUFFICIENT_CREDITS',
]);

return;
}
if ($invoice['status'] === 'paid') {
$appInstance->Forbidden('Invoice already paid', [
'error' => 'ERR_INVOICE_ALREADY_PAID',
]);

return;
}
$newBalance = (int) $session->getInfo(UserColumns::CREDITS, false) - $price;
$session->setInfo(UserColumns::CREDITS, $newBalance, false);
OrdersInvoices::updateStatus($id, 'paid');
Orders::updateStatus($invoice['order'], 'processed');
Orders::updateDaysLeft($invoice['order'], 30);

$appInstance->OK('Invoice status', [
'price' => $price,
]);
});
66 changes: 66 additions & 0 deletions backend/app/Api/WebHooks/PayPal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* # MythicalSystems License v2.0
*
* ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman
*
* Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services.
*/

use MythicalClient\App;
use MythicalClient\Chat\User\Session;
use MythicalClient\Chat\columns\UserColumns;

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* # MythicalSystems License v2.0
*
* ## Copyright (c) 2021–2025 MythicalSystems and Cassian Gherman
*
* Breaking any of the following rules will result in a permanent ban from the MythicalSystems community and all of its services.
*/

// Route handlers
$router->post('/api/webhooks/paypal', function () {
$paypal = new MythicalClient\Gateways\PayPal();
$paypal->handleIPN();
});

$router->get('/api/paypal/process', function () {
App::init();
$app = App::getInstance(true);
$session = new Session($app);

try {
if (!isset($_GET['coins']) || empty($_GET['coins'])) {
throw new InvalidArgumentException('Missing coins parameter');
}

$paypal = new MythicalClient\Gateways\PayPal();
$redirectUrl = $paypal->createPayment(
(float) $_GET['coins'],
$session->getInfo(UserColumns::UUID, false)
);

header("Location: $redirectUrl");
exit;
} catch (Throwable $e) {
$app->getLogger()->error('PayPal process error: ' . $e->getMessage());
header('Location: /?error=payment_failed&message=' . $e->getMessage());
exit;
}
});

$router->add('/api/paypal/finish', function () {
App::init();
$app = App::getInstance(true);

header('Location: /dashboard');
exit;
});
Loading

0 comments on commit 4296e55

Please sign in to comment.