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/libraries/Controllers/PremiumController.php

186 lines
4.9 KiB
PHP
Raw Normal View History

2016-02-13 13:36:21 +00:00
<?php
/**
* Holds the premium pages controllers.
2016-03-08 23:07:58 +00:00
*
2016-02-13 13:36:21 +00:00
* @package Sakura
*/
namespace Sakura\Controllers;
2016-02-27 17:28:45 +00:00
use Exception;
2016-02-13 13:36:21 +00:00
use Sakura\Config;
use Sakura\Payments;
use Sakura\Perms\Site;
2016-03-27 21:18:57 +00:00
use Sakura\Router;
use Sakura\Template;
2016-02-13 13:36:21 +00:00
/**
* Premium pages controller.
2016-03-08 23:07:58 +00:00
*
2016-02-13 13:36:21 +00:00
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
2016-02-27 16:46:16 +00:00
class PremiumController extends Controller
2016-02-13 13:36:21 +00:00
{
2016-03-27 21:18:57 +00:00
/**
* The amount of premium a user received per period.
*/
const PERIOD_PER_PAYMENT = 2628000;
/**
* Constructor.
*/
public function __construct()
{
Payments::init();
}
/**
* Returns the premium purchase index.
*
* @return mixed
*/
2016-02-14 22:46:07 +00:00
public function index()
2016-02-13 13:36:21 +00:00
{
2016-03-27 21:18:57 +00:00
global $currentUser;
$price = Config::get('premium_price_per_month');
$amountLimit = Config::get('premium_amount_max');
Template::vars(compact('price', 'amountLimit'));
2016-02-13 13:36:21 +00:00
2016-03-28 14:47:43 +00:00
return Template::render('premium/index');
2016-02-13 13:36:21 +00:00
}
2016-03-27 21:18:57 +00:00
/**
* Handles a purchase request.
*
* @return mixed
*/
public function purchase()
2016-02-13 13:36:21 +00:00
{
2016-03-27 21:18:57 +00:00
global $currentUser;
// Get values from post
$session = isset($_POST['session']) ? $_POST['session'] : '';
$months = isset($_POST['months']) ? $_POST['months'] : 0;
// Check if the session is valid
if ($session !== session_id()
|| $currentUser->permission(Site::DEACTIVATED)
|| !$currentUser->permission(Site::OBTAIN_PREMIUM)) {
$message = "You are not allowed to get premium!";
$redirect = Router::route('premium.index');
Template::vars(compact('message', 'redirect'));
return Template::render('global/information');
}
// Fetch the limit
$amountLimit = Config::get('premium_amount_max');
// Check months
if ($months < 1
|| $months > $amountLimit) {
$message = "An incorrect amount of months was specified, stop messing with the source.";
$redirect = Router::route('premium.index');
Template::vars(compact('message', 'redirect'));
return Template::render('global/information');
}
$pricePerMonth = Config::get('premium_price_per_month');
$total = number_format($pricePerMonth * $months, 2, '.', '');
$siteName = Config::get('sitename');
$multiMonths = $months !== 1 ? 's' : '';
$siteUrl = 'http'
. (isset($_SERVER['HTTPS']) ? 's' : '')
. "://{$_SERVER['SERVER_NAME']}"
. ($_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '');
$handlerRoute = Router::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
);
2016-02-13 13:36:21 +00:00
2016-03-27 21:18:57 +00:00
// Attempt to create a transaction
if (!$transaction) {
$message = "Something went wrong while preparing the transaction.";
$redirect = Router::route('premium.index');
Template::vars(compact('message', 'redirect'));
return Template::render('global/information');
}
// Store the amount of months in the global session array
$_SESSION['premiumMonths'] = (int) $months;
return header("Location: {$transaction}");
}
/**
* Handles the data returned by PayPal.
*
* @return mixed
*/
public function handle()
{
global $currentUser;
$success = isset($_GET['success']);
$payment = isset($_GET['paymentId']) ? $_GET['paymentId'] : null;
$payer = isset($_GET['PayerID']) ? $_GET['PayerID'] : null;
$months = isset($_SESSION['premiumMonths']) ? $_SESSION['premiumMonths'] : null;
$successRoute = Router::route('premium.complete');
$failRoute = Router::route('premium.index') . "?fail=true";
if (!$success
|| !$payment
|| !$payer
|| !$months) {
return header("Location: {$failRoute}");
}
// Attempt to complete the transaction
try {
$finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']);
} catch (Exception $e) {
$finalise = false;
}
if (!$finalise) {
return header("Location: {$failRoute}");
}
$pricePerMonth = Config::get('premium_price_per_month');
$currentUser->addPremium(self::PERIOD_PER_PAYMENT * $months);
return header("Location: {$successRoute}");
}
/**
* Presents the user with a thank you <3.
*
* @return mixed
*/
public function complete()
{
2016-03-28 14:47:43 +00:00
return Template::render('premium/complete');
2016-02-13 13:36:21 +00:00
}
}