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;
|
2016-09-10 15:05:54 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @return string
|
2016-03-27 21:18:57 +00:00
|
|
|
*/
|
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-09-10 15:05:54 +00:00
|
|
|
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
|
|
|
*/
|
|
|
|
public function purchase()
|
2016-02-13 13:36:21 +00:00
|
|
|
{
|
2016-03-27 21:18:57 +00:00
|
|
|
// Get values from post
|
|
|
|
$months = isset($_POST['months']) ? $_POST['months'] : 0;
|
|
|
|
|
|
|
|
// Check if the session is valid
|
2016-08-07 14:10:27 +00:00
|
|
|
if (!session_check()
|
2016-11-01 21:14:02 +00:00
|
|
|
|| !CurrentSession::$user->activated
|
|
|
|
|| !CurrentSession::$user->verified
|
|
|
|
|| CurrentSession::$user->restricted) {
|
2016-09-10 15:05:54 +00:00
|
|
|
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-09-13 22:05:03 +00:00
|
|
|
redirect(route('premium.error'));
|
2016-09-10 15:05:54 +00:00
|
|
|
return;
|
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']}" : '');
|
2016-09-10 15:05:54 +00:00
|
|
|
$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-09-13 22:05:03 +00:00
|
|
|
redirect(route('premium.error'));
|
2016-09-10 15:05:54 +00:00
|
|
|
return;
|
2016-03-27 21:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the amount of months in the global session array
|
|
|
|
$_SESSION['premiumMonths'] = (int) $months;
|
|
|
|
|
2016-09-13 22:05:03 +00:00
|
|
|
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
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2016-09-10 15:05:54 +00:00
|
|
|
$successRoute = route('premium.complete');
|
|
|
|
$failRoute = route('premium.error');
|
2016-03-27 21:18:57 +00:00
|
|
|
|
|
|
|
if (!$success
|
|
|
|
|| !$payment
|
|
|
|
|| !$payer
|
|
|
|
|| !$months) {
|
2016-09-13 22:05:03 +00:00
|
|
|
redirect($failRoute);
|
|
|
|
return;
|
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-09-13 22:05:03 +00:00
|
|
|
redirect($failRoute);
|
|
|
|
return;
|
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-09-13 22:05:03 +00:00
|
|
|
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
|
|
|
*/
|
|
|
|
public function complete()
|
|
|
|
{
|
2016-09-10 15:05:54 +00:00
|
|
|
return view('premium/complete');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Errors.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function error()
|
|
|
|
{
|
|
|
|
return view('premium/error');
|
2016-02-13 13:36:21 +00:00
|
|
|
}
|
|
|
|
}
|