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

181 lines
4.8 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-03-31 20:03:25 +00:00
use Sakura\ActiveUser;
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()
{
2016-07-30 13:48:09 +00:00
parent::__construct();
2016-03-27 21:18:57 +00:00
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-07-26 17:29:53 +00:00
$price = config('premium.price_per_month');
$amountLimit = config('premium.max_months_at_once');
2016-03-27 21:18:57 +00:00
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
// 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()
2016-03-31 20:03:25 +00:00
|| ActiveUser::$user->permission(Site::DEACTIVATED)
|| !ActiveUser::$user->permission(Site::OBTAIN_PREMIUM)) {
2016-03-27 21:18:57 +00:00
$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
2016-07-26 17:29:53 +00:00
$amountLimit = config('premium.max_months_at_once');
2016-03-27 21:18:57 +00:00
// 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');
}
2016-07-26 17:29:53 +00:00
$pricePerMonth = config('premium.price_per_month');
2016-03-27 21:18:57 +00:00
$total = number_format($pricePerMonth * $months, 2, '.', '');
2016-07-26 17:29:53 +00:00
$siteName = config('general.name');
2016-03-27 21:18:57 +00:00
$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()
{
$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}");
}
2016-03-31 20:03:25 +00:00
ActiveUser::$user->addPremium(self::PERIOD_PER_PAYMENT * $months);
2016-03-27 21:18:57 +00:00
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
}
}