240 lines
7.9 KiB
PHP
240 lines
7.9 KiB
PHP
<?php
|
|
/**
|
|
* app/controllers/member.php
|
|
*
|
|
* This is the members controller.
|
|
*
|
|
* @version 3.0
|
|
* @author Joey Kimsey <Joey@thetempusproject.com>
|
|
* @link https://TheTempusProject.com
|
|
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
|
*/
|
|
namespace TheTempusProject\Controllers;
|
|
|
|
use TheTempusProject\Houdini\Classes\Template;
|
|
use TheTempusProject\Houdini\Classes\Views;
|
|
use TheTempusProject\Houdini\Classes\Issues;
|
|
use TheTempusProject\Classes\Controller;
|
|
use TheTempusProject\TheTempusProject as App;
|
|
use TheTempusProject\Hermes\Functions\Redirect;
|
|
use TheTempusProject\Bedrock\Functions\Session;
|
|
use TheTempusProject\Models\MembershipCustomers;
|
|
use TheTempusProject\Models\MembershipProducts;
|
|
use TheTempusProject\Bedrock\Classes\Config;
|
|
use TheTempusProject\Bedrock\Functions\Input;
|
|
use Stripe\Checkout\Session as StripeSession;
|
|
use TheTempusProject\Hermes\Functions\Route as Routes;
|
|
use TheTempusProject\Houdini\Classes\Navigation;
|
|
use TheTempusProject\Models\Memberships;
|
|
use TheTempusProject\Houdini\Classes\Components;
|
|
|
|
class Member extends Controller {
|
|
public static $customers;
|
|
public static $products;
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
Template::noIndex();
|
|
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
|
self::$customers = new MembershipCustomers;
|
|
self::$products = new MembershipProducts;
|
|
}
|
|
|
|
public function index() {
|
|
self::$title = 'Members Area';
|
|
if ( !App::$isMember ) {
|
|
Session::flash( 'error', 'You do not have permission to view this page.' );
|
|
return Redirect::home();
|
|
}
|
|
Views::view( 'members.members' );
|
|
}
|
|
|
|
public function managepayment( $id = null ) {
|
|
|
|
|
|
|
|
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
|
$stripe = new \Stripe\StripeClient( $api_key );
|
|
|
|
$customer = self::$customers->findOrCreate( App::$activeUser->ID );
|
|
if ( empty( $customer ) ) {
|
|
Session::flash( 'error', 'no customer.' );
|
|
return Redirect::to( 'member/manage' );
|
|
}
|
|
|
|
$session = $stripe->billingPortal->sessions->create([
|
|
'customer' => $customer,
|
|
'return_url' => Routes::getAddress() . 'member/manage',
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
header('Location: ' . $session->url);
|
|
exit;
|
|
}
|
|
|
|
public function cancelconfirm( $id = null ) {
|
|
$memberships = new Memberships;
|
|
$result = $memberships->cancel( $id );
|
|
// dv( $result );
|
|
if ( ! empty( $result ) ) {
|
|
Session::flash( 'success', 'Your Membership has been paused.' );
|
|
Redirect::to( 'member/manage' );
|
|
} else {
|
|
Session::flash( 'error', 'There was an error canceling your membership' );
|
|
Redirect::to( 'member/manage' );
|
|
}
|
|
}
|
|
|
|
public function pauseconfirm( $id = null ) {
|
|
$memberships = new Memberships;
|
|
$result = $memberships->cancel( $id );
|
|
// dv( $result );
|
|
if ( ! empty( $result ) ) {
|
|
Session::flash( 'success', 'Your Membership has been paused.' );
|
|
Redirect::to( 'member/manage' );
|
|
} else {
|
|
Session::flash( 'error', 'There was an error canceling your membership' );
|
|
Redirect::to( 'member/manage' );
|
|
}
|
|
}
|
|
|
|
public function pause( $id = null ) {
|
|
self::$title = 'pause Membership';
|
|
Components::set( 'pauseid', $id );
|
|
Views::view( 'members.pause' );
|
|
}
|
|
|
|
public function resume( $id = null ) {
|
|
self::$title = 'resume Membership';
|
|
Views::view( 'members.resume' );
|
|
}
|
|
|
|
public function cancel( $id = null ) {
|
|
self::$title = 'Cancel Membership';
|
|
Components::set( 'cancelid', $id );
|
|
Views::view( 'members.cancel' );
|
|
}
|
|
|
|
public function manage( $id = null ) {
|
|
self::$title = 'Manage Membership';
|
|
|
|
$menu = Views::simpleView( 'nav.usercp', App::$userCPlinks );
|
|
Navigation::activePageSelect( $menu, null, true, true );
|
|
|
|
$memberships = new Memberships;
|
|
$userMemberships = $memberships->getUserSubs();
|
|
|
|
Views::view( 'members.manage', $userMemberships );
|
|
}
|
|
|
|
public function upgrade( $id = null ) {
|
|
self::$title = 'Upgrade Membership';
|
|
Views::view( 'members.upgrade' );
|
|
}
|
|
|
|
public function join( $id = null ) {
|
|
self::$title = 'Join {SIITENAME}!';
|
|
$product = self::$products->findById( $id );
|
|
if ( empty( $product ) ) {
|
|
Session::flash( 'success', 'We aren\'t currently accepting new members, please check back soon!' );
|
|
return Redirect::home();
|
|
}
|
|
Views::view( 'members.landing1', $product );
|
|
}
|
|
|
|
public function signup( $id = null ) {
|
|
self::$title = 'Sign-up for {SIITENAME}!';
|
|
$product = self::$products->findById( $id );
|
|
if ( empty( $product ) ) {
|
|
Session::flash( 'success', 'We aren\'t currently accepting new members, please check back soon!' );
|
|
return Redirect::home();
|
|
}
|
|
Views::view( 'members.landing2', $product );
|
|
}
|
|
|
|
public function getyearly( $id = null ) {
|
|
if ( empty( $id ) ) {
|
|
Issues::add( 'error', 'no id' );
|
|
return $this->index();
|
|
}
|
|
$product = self::$products->findById( $id );
|
|
if ( empty( $product ) ) {
|
|
Issues::add( 'error', 'no product' );
|
|
return $this->index();
|
|
}
|
|
$customer = self::$customers->findOrCreate( App::$activeUser->ID );
|
|
if ( empty( $customer ) ) {
|
|
Issues::add( 'error', 'no customer' );
|
|
return $this->index();
|
|
}
|
|
|
|
self::$title = 'Purchase';
|
|
$price = $product->stripe_price_yearly;
|
|
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
|
$stripe = new \Stripe\StripeClient( $api_key );
|
|
$session = $stripe->checkout->sessions->create([
|
|
'payment_method_types' => ['card'],
|
|
'customer' => $customer,
|
|
'line_items' => [[
|
|
'price' => $price,
|
|
'quantity' => 1,
|
|
]],
|
|
'mode' => 'subscription',
|
|
'success_url' => Routes::getAddress() . 'member/paymentcomplete?session_id={CHECKOUT_SESSION_ID}',
|
|
'cancel_url' => Routes::getAddress() . 'member/paymentcanceled',
|
|
]);
|
|
header('Location: ' . $session->url);
|
|
exit;
|
|
}
|
|
|
|
public function getmonthly( $id = null ) {
|
|
if ( empty( $id ) ) {
|
|
Issues::add( 'error', 'no id' );
|
|
return $this->index();
|
|
}
|
|
$product = self::$products->findById( $id );
|
|
if ( empty( $product ) ) {
|
|
Issues::add( 'error', 'no product' );
|
|
return $this->index();
|
|
}
|
|
$customer = self::$customers->findOrCreate( App::$activeUser->ID );
|
|
if ( empty( $customer ) ) {
|
|
Issues::add( 'error', 'no customer' );
|
|
return $this->index();
|
|
}
|
|
|
|
self::$title = 'Purchase';
|
|
$price = $product->stripe_price_monthly;
|
|
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
|
$stripe = new \Stripe\StripeClient( $api_key );
|
|
$session = $stripe->checkout->sessions->create([
|
|
'payment_method_types' => ['card'],
|
|
'customer' => $customer,
|
|
'line_items' => [[
|
|
'price' => $price,
|
|
'quantity' => 1,
|
|
]],
|
|
'mode' => 'subscription',
|
|
'success_url' => Routes::getAddress() . 'member/paymentcomplete?session_id={CHECKOUT_SESSION_ID}',
|
|
'cancel_url' => Routes::getAddress() . 'member/paymentcanceled',
|
|
]);
|
|
header('Location: ' . $session->url);
|
|
exit;
|
|
}
|
|
|
|
public function paymentcanceled() {
|
|
self::$title = '(almost) Members Area';
|
|
Views::view( 'members.paymentcanceled' );
|
|
}
|
|
|
|
public function paymentcomplete() {
|
|
self::$title = '(almost) Members Area';
|
|
Views::view( 'members.paymentcomplete' );
|
|
}
|
|
} |