This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/PremiumController.php
flashwave 4b3aeb6149 remove verified
can't even remember what it was supposed to do anyway
2016-12-07 15:38:57 +01:00

160 lines
4 KiB
PHP

<?php
/**
* Holds the premium pages controllers.
* @package Sakura
*/
namespace Sakura\Controllers;
use Exception;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use Sakura\Config;
use Sakura\CurrentSession;
use Sakura\Payments;
/**
* Premium pages controller.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class PremiumController extends Controller
{
/**
* The amount of premium a user received per period.
*/
private const PERIOD_PER_PAYMENT = 2628000;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
Payments::init();
}
/**
* Returns the premium purchase index.
* @return string
*/
public function index(): string
{
$price = config('premium.price_per_month');
$amountLimit = config('premium.max_months_at_once');
return view('premium/index', compact('price', 'amountLimit'));
}
/**
* Handles a purchase request.
* @return string
*/
public function purchase(): string
{
// Get values from post
$months = $_POST['months'] ?? 0;
// Check if the session is valid
if (!session_check() || !CurrentSession::$user->activated || CurrentSession::$user->restricted) {
throw new HttpMethodNotAllowedException;
}
// Fetch the limit
$amountLimit = config('premium.max_months_at_once');
// Check months
if ($months < 1
|| $months > $amountLimit) {
return redirect(route('premium.error'));
}
$pricePerMonth = config('premium.price_per_month');
$total = number_format($pricePerMonth * $months, 2, '.', '');
$siteName = config('general.name');
$multiMonths = $months !== 1 ? 's' : '';
$siteUrl = 'http'
. (isset($_SERVER['HTTPS']) ? 's' : '')
. "://{$_SERVER['SERVER_NAME']}"
. ($_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '');
$handlerRoute = route('premium.handle');
$itemName = "{$siteName} Premium - {$months} month{$multiMonths}";
$transactionName = "{$siteName} premium purchase";
$handlerUrl = "{$siteUrl}{$handlerRoute}";
// Create the transaction
$transaction = Payments::createTransaction(
$total,
$itemName,
$transactionName,
$handlerUrl
);
// Attempt to create a transaction
if (!$transaction) {
return redirect(route('premium.error'));
}
// Store the amount of months in the global session array
$_SESSION['premiumMonths'] = (int) $months;
return redirect($transaction);
}
/**
* Handles the data returned by PayPal.
* @return string
*/
public function handle(): string
{
$success = isset($_GET['success']);
$payment = $_GET['paymentId'] ?? null;
$payer = $_GET['PayerID'] ?? null;
$months = $_SESSION['premiumMonths'] ?? null;
$successRoute = route('premium.complete');
$failRoute = route('premium.error');
if (!$success
|| !$payment
|| !$payer
|| !$months) {
return redirect($failRoute);
}
// Attempt to complete the transaction
try {
$finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']);
} catch (Exception $e) {
$finalise = false;
}
if (!$finalise) {
return redirect($failRoute);
}
CurrentSession::$user->addPremium(self::PERIOD_PER_PAYMENT * $months);
return redirect($successRoute);
}
/**
* Presents the user with a thank you <3.
* @return string
*/
public function complete(): string
{
return view('premium/complete');
}
/**
* Errors.
* @return string
*/
public function error(): string
{
return view('premium/error');
}
}