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

160 lines
4 KiB
PHP
Raw Permalink Normal View History

2016-02-13 13:36:21 +00:00
<?php
/**
* Holds the premium pages controllers.
* @package Sakura
*/
namespace Sakura\Controllers;
2016-02-27 17:28:45 +00:00
use Exception;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
2016-02-13 13:36:21 +00:00
use Sakura\Config;
2016-08-07 14:10:27 +00:00
use Sakura\CurrentSession;
2016-02-13 13:36:21 +00:00
use Sakura\Payments;
/**
* Premium pages controller.
* @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.
*/
2016-12-04 16:33:52 +00:00
private const PERIOD_PER_PAYMENT = 2628000;
2016-03-27 21:18:57 +00:00
/**
* 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.
2016-08-05 02:35:37 +00:00
* @return string
2016-03-27 21:18:57 +00:00
*/
2016-12-04 16:33:52 +00:00
public function index(): string
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');
return view('premium/index', compact('price', 'amountLimit'));
2016-02-13 13:36:21 +00:00
}
2016-03-27 21:18:57 +00:00
/**
* Handles a purchase request.
2016-08-05 02:35:37 +00:00
* @return string
2016-03-27 21:18:57 +00:00
*/
2016-12-04 16:33:52 +00:00
public function purchase(): string
2016-02-13 13:36:21 +00:00
{
2016-03-27 21:18:57 +00:00
// Get values from post
2016-12-04 16:33:52 +00:00
$months = $_POST['months'] ?? 0;
2016-03-27 21:18:57 +00:00
// Check if the session is valid
if (!session_check() || !CurrentSession::$user->activated || CurrentSession::$user->restricted) {
throw new HttpMethodNotAllowedException;
2016-03-27 21:18:57 +00:00
}
// 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) {
2016-12-04 16:33:52 +00:00
return redirect(route('premium.error'));
2016-03-27 21:18:57 +00:00
}
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 = route('premium.handle');
2016-03-27 21:18:57 +00:00
$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) {
2016-12-04 16:33:52 +00:00
return redirect(route('premium.error'));
2016-03-27 21:18:57 +00:00
}
// Store the amount of months in the global session array
$_SESSION['premiumMonths'] = (int) $months;
2016-12-04 16:33:52 +00:00
return redirect($transaction);
2016-03-27 21:18:57 +00:00
}
/**
* Handles the data returned by PayPal.
2016-08-05 02:35:37 +00:00
* @return string
2016-03-27 21:18:57 +00:00
*/
2016-12-04 16:33:52 +00:00
public function handle(): string
2016-03-27 21:18:57 +00:00
{
$success = isset($_GET['success']);
2016-12-04 16:33:52 +00:00
$payment = $_GET['paymentId'] ?? null;
$payer = $_GET['PayerID'] ?? null;
$months = $_SESSION['premiumMonths'] ?? null;
2016-03-27 21:18:57 +00:00
$successRoute = route('premium.complete');
$failRoute = route('premium.error');
2016-03-27 21:18:57 +00:00
if (!$success
|| !$payment
|| !$payer
|| !$months) {
2016-12-04 16:33:52 +00:00
return redirect($failRoute);
2016-03-27 21:18:57 +00:00
}
// Attempt to complete the transaction
try {
$finalise = Payments::completeTransaction($_GET['paymentId'], $_GET['PayerID']);
} catch (Exception $e) {
$finalise = false;
}
if (!$finalise) {
2016-12-04 16:33:52 +00:00
return redirect($failRoute);
2016-03-27 21:18:57 +00:00
}
2016-08-07 14:10:27 +00:00
CurrentSession::$user->addPremium(self::PERIOD_PER_PAYMENT * $months);
2016-03-27 21:18:57 +00:00
2016-12-04 16:33:52 +00:00
return redirect($successRoute);
2016-03-27 21:18:57 +00:00
}
/**
* Presents the user with a thank you <3.
2016-08-05 02:35:37 +00:00
* @return string
2016-03-27 21:18:57 +00:00
*/
2016-12-04 16:33:52 +00:00
public function complete(): string
2016-03-27 21:18:57 +00:00
{
return view('premium/complete');
}
/**
* Errors.
* @return string
*/
2016-12-04 16:33:52 +00:00
public function error(): string
{
return view('premium/error');
2016-02-13 13:36:21 +00:00
}
}