This commit is contained in:
Joey Kimsey
2024-12-11 07:49:48 -05:00
parent 3bc838ce24
commit a1c849a626
21 changed files with 690 additions and 392 deletions

View File

@ -27,6 +27,9 @@ 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;
@ -61,10 +64,17 @@ class Member extends Controller {
Session::flash( 'error', 'You do not have any active payment methods. You can subscribe by going <a href="/member/join/1">here</a>' );
return Redirect::to( 'member/manage' );
}
$session = $stripe->billingPortal->sessions->create([
'customer' => $customer,
'return_url' => Routes::getAddress() . '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' );
}
@ -146,16 +156,6 @@ class Member extends Controller {
}
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 ) ) {
@ -236,4 +236,82 @@ class Member extends Controller {
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 <a href="#">Upgrading</a>?' );
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;
}
}