diff --git a/cp/app/Controllers/FinancialsController.php b/cp/app/Controllers/FinancialsController.php index f2837f5..564bb6e 100644 --- a/cp/app/Controllers/FinancialsController.php +++ b/cp/app/Controllers/FinancialsController.php @@ -26,20 +26,6 @@ class FinancialsController extends Controller public function deposit(Request $request, Response $response) { if ($_SESSION["auth_roles"] != 0) { - if ($request->getMethod() === 'POST') { - // Retrieve POST data - $data = $request->getParsedBody(); - $db = $this->container->get('db'); - $balance = $db->selectRow('SELECT name, email, accountBalance, creditLimit FROM registrar WHERE id = ?', - [ $_SESSION["auth_registrar_id"] ] - ); - echo "Payment here"; - - return view($response,'admin/financials/deposit-registrar.twig', [ - 'balance' => $balance - ]); - } - $db = $this->container->get('db'); $balance = $db->selectRow('SELECT name, accountBalance, creditLimit FROM registrar WHERE id = ?', [ $_SESSION["auth_registrar_id"] ] @@ -127,4 +113,142 @@ class FinancialsController extends Controller 'registrars' => $registrars ]); } + + public function createPayment(Request $request, Response $response) + { + $postData = $request->getParsedBody(); + $amount = $postData['amount']; // Make sure to validate and sanitize this amount + + // Set Stripe's secret key + \Stripe\Stripe::setApiKey(envi('STRIPE_SECRET_KEY')); + + // Convert amount to cents (Stripe expects the amount in the smallest currency unit) + $amountInCents = $amount * 100; + + // Create Stripe Checkout session + $checkout_session = \Stripe\Checkout\Session::create([ + 'payment_method_types' => ['card', 'paypal'], + 'line_items' => [[ + 'price_data' => [ + 'currency' => $_SESSION['_currency'], + 'product_data' => [ + 'name' => 'Registrar Balance Deposit', + ], + 'unit_amount' => $amountInCents, + ], + 'quantity' => 1, + ]], + 'mode' => 'payment', + 'success_url' => envi('APP_URL').'/payment-success?session_id={CHECKOUT_SESSION_ID}', + 'cancel_url' => envi('APP_URL').'/payment-cancel', + ]); + + // Return session ID to the frontend + $response->getBody()->write(json_encode(['id' => $checkout_session->id])); + return $response->withHeader('Content-Type', 'application/json'); + } + + public function success(Request $request, Response $response) + { + $session_id = $request->getQueryParams()['session_id'] ?? null; + $db = $this->container->get('db'); + + if ($session_id) { + \Stripe\Stripe::setApiKey(envi('STRIPE_SECRET_KEY')); + + try { + $session = \Stripe\Checkout\Session::retrieve($session_id); + $amountPaid = $session->amount_total; // Amount paid, in cents + $amount = $amountPaid / 100; + $amountPaidFormatted = number_format($amount, 2, '.', ''); + $paymentIntentId = $session->payment_intent; + + $isPositiveNumberWithTwoDecimals = filter_var($amount, FILTER_VALIDATE_FLOAT) !== false && preg_match('/^\d+(\.\d{1,2})?$/', $amount); + + if ($isPositiveNumberWithTwoDecimals) { + $db->beginTransaction(); + + try { + $currentDateTime = new \DateTime(); + $date = $currentDateTime->format('Y-m-d H:i:s.v'); + $db->insert( + 'statement', + [ + 'registrar_id' => $_SESSION['auth_registrar_id'], + 'date' => $date, + 'command' => 'create', + 'domain_name' => 'deposit', + 'length_in_months' => 0, + 'from' => $date, + 'to' => $date, + 'amount' => $amount + ] + ); + + $db->insert( + 'payment_history', + [ + 'registrar_id' => $_SESSION['auth_registrar_id'], + 'date' => $date, + 'description' => 'Registrar Balance Deposit via Stripe ('.$paymentIntentId.')', + 'amount' => $amount + ] + ); + + $db->exec( + 'UPDATE registrar SET accountBalance = (accountBalance + ?) WHERE id = ?', + [ + $amount, + $_SESSION['auth_registrar_id'], + ] + ); + + $db->commit(); + } catch (Exception $e) { + $db->rollBack(); + $balance = $db->selectRow('SELECT name, accountBalance, creditLimit FROM registrar WHERE id = ?', + [ $_SESSION["auth_registrar_id"] ] + ); + + return view($response, 'admin/financials/deposit-registrar.twig', [ + 'error' => $e->getMessage(), + 'balance' => $balance + ]); + } + + $balance = $db->selectRow('SELECT name, accountBalance, creditLimit FROM registrar WHERE id = ?', + [ $_SESSION["auth_registrar_id"] ] + ); + + return view($response, 'admin/financials/deposit-registrar.twig', [ + 'deposit' => $amount, + 'balance' => $balance + ]); + } else { + $balance = $db->selectRow('SELECT name, accountBalance, creditLimit FROM registrar WHERE id = ?', + [ $_SESSION["auth_registrar_id"] ] + ); + + return view($response, 'admin/financials/deposit-registrar.twig', [ + 'error' => 'Invalid entry: Deposit amount must be positive. Please enter a valid amount.', + 'balance' => $balance + ]); + } + } catch (\Exception $e) { + $balance = $db->selectRow('SELECT name, accountBalance, creditLimit FROM registrar WHERE id = ?', + [ $_SESSION["auth_registrar_id"] ] + ); + + return view($response, 'admin/financials/deposit-registrar.twig', [ + 'error' => 'We encountered an issue while processing your payment. Please check your payment details and try again.', + 'balance' => $balance + ]); + } + } + } + + public function cancel(Request $request, Response $response) + { + return view($response,'admin/financials/cancel.twig'); + } } \ No newline at end of file diff --git a/cp/composer.json b/cp/composer.json index da26059..e8cd907 100644 --- a/cp/composer.json +++ b/cp/composer.json @@ -1,8 +1,8 @@ { "name": "pinga/pinga-panel", - "description": "Pinga Framework Boilerplate", + "description": "Namingo Registry Control Panel", "type": "project", - "keywords": ["slim", "slim 4", "skeleton", "authentication", "template", "orm","pinga"], + "keywords": ["slim", "slim 4", "domain", "registry", "panel"], "homepage": "https://github.com/getpinga/pinga-panel", "license": "MIT", "authors": [ @@ -34,11 +34,12 @@ "mevdschee/php-crud-api": "^2.14", "gettext/gettext": "^5.7", "punic/punic": "^3.8", - "league/iso3166": "^4.3" + "league/iso3166": "^4.3", + "stripe/stripe-php": "^13.3" }, "autoload": { - "psr-4": { - "App\\": "app/" - } + "psr-4": { + "App\\": "app/" + } } } diff --git a/cp/config/app.php b/cp/config/app.php index bd42e12..271b222 100644 --- a/cp/config/app.php +++ b/cp/config/app.php @@ -72,4 +72,7 @@ return [ 'api_key' => $_ENV['MAIL_API_KEY'] ?? 'test-api-key', 'api_provider' => $_ENV['MAIL_API_PROVIDER'] ?? 'sendgrid', ], + 'payment' => [ + 'stripe' => $_ENV['STRIPE_SECRET_KEY'] ?? 'stripe-secret-key', + ], ]; \ No newline at end of file diff --git a/cp/env-sample b/cp/env-sample index f148a40..56a0ee9 100644 --- a/cp/env-sample +++ b/cp/env-sample @@ -1,6 +1,6 @@ APP_NAME='CP' APP_ENV=public -APP_URL=http://localhost +APP_URL=https://cp.example.com APP_DOMAIN=example.com DB_DRIVER=mysql @@ -22,3 +22,5 @@ MAIL_FROM_ADDRESS='example@domain.com' MAIL_FROM_NAME='Example' MAIL_API_KEY='test-api-key' MAIL_API_PROVIDER='sendgrid' + +STRIPE_SECRET_KEY='stripe-secret-key' \ No newline at end of file diff --git a/cp/resources/views/admin/financials/cancel.twig b/cp/resources/views/admin/financials/cancel.twig new file mode 100644 index 0000000..fd3fbd6 --- /dev/null +++ b/cp/resources/views/admin/financials/cancel.twig @@ -0,0 +1,51 @@ +{% extends "layouts/app.twig" %} + +{% block title %}{{ __('Deposit Payment Unsuccessful') }}{% endblock %} + +{% block content %} +
We've noticed that your deposit payment process was not completed. It appears that the payment was either cancelled or failed during the transaction. If this was an error, or if you have any questions, please don't hesitate to contact us. We're here to help ensure your transaction is smooth and secure.
+Ready to try again? When you're set to proceed with your deposit, simply return to the Deposit Page to initiate a new payment. We value your partnership and are committed to assisting you every step of the way.
+