* @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;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Bedrock\Functions\Hash;
use TheTempusProject\Canary\Bin\Canary as Debug;
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', 'You do not have any active payment methods. You can subscribe by going here' );
return Redirect::to( 'member/manage' );
}
try {
$session = $stripe->billingPortal->sessions->create([
'customer' => $customer,
'return_url' => Routes::getAddress() . 'member/manage',
]);
} catch (\Stripe\Exception\InvalidRequestException $e) {
Debug::error('Membership -> ManagePayment - Stripe not configured correctly');
Debug::error( $e );
Session::flash( 'error', 'There was an issue redirecting you to Stripe, please try again.' );
return Redirect::to( '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 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' );
}
public function signup( $plan = 'monthly' ) {
$plan = strtolower( $plan );
if ( ! in_array( $plan, ['monthly','yearly'] ) ) {
Session::flash( 'error', 'Unknown plan' );
return Redirect::to( 'home/index' );
}
$product = self::$products->mainProduct();
if ( empty( $product ) ) {
Session::flash( 'error', 'Unknown product' );
return Redirect::to( 'home/index' );
}
$index = 'stripe_price_' . $plan;
$pretty = 'prettyPrice' . ucfirst( $plan );
$stripePrice = $product->$index;
$prettyPrice = $product->$pretty;
self::$title = 'Sign up for {SITENAME} ' . ucfirst( $plan );
Components::set( 'planName', ucfirst( $plan ) );
Components::set( 'prettyPrice', $prettyPrice );
Components::set( 'TERMS', Views::simpleView( 'terms' ) );
if ( App::$isLoggedIn ) {
Session::flash( 'notice', 'You are already logged in, were you looking for information on Upgrading?' );
return Redirect::to( 'home/index' );
}
if ( !Input::exists() ) {
return Views::view( 'members.register' );
}
if ( ! Forms::check( 'register' ) ) {
Issues::add( 'error', [ 'There was an error with your registration.' => Check::userErrors() ] );
return Views::view( 'members.register' );
}
self::$user->create( [
'username' => Input::post( 'username' ),
'password' => Hash::make( Input::post( 'password' ) ),
'email' => Input::post( 'email' ),
'terms' => 1,
] );
if ( !self::$user->logIn( Input::post( 'username' ), Input::post( 'password' ), Input::post( 'remember' ) ) ) {
Session::flash( 'error', 'Thank you for registering! Unfortunately, there was an issue logging you in, please log in and order again.' );
return Redirect::to( 'home/index' );
}
$user = self::$user->authorize( Input::post( 'username' ), Input::post( 'password' ) );
$customer = self::$customers->findOrCreate( $user->ID );
if ( empty( $customer ) ) {
Session::flash( 'error', 'Thank you for registering! Unfortunately, there was an issue communicating with Stripe and we can\'t collect payment right now.' );
return Redirect::to( 'home/index' );
}
$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' => $stripePrice,
'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;
}
}