all atb changes
This commit is contained in:
50
app/plugins/members/controllers/admin/invoices.php
Normal file
50
app/plugins/members/controllers/admin/invoices.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/controllers/admin/members/blog.php
|
||||
*
|
||||
* This is the Membership Invoices admin controller.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Admin;
|
||||
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Classes\AdminController;
|
||||
use TheTempusProject\Models\MembershipRecords as MemberModel;
|
||||
|
||||
class Invoices extends AdminController {
|
||||
public static $memberships;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$title = 'Admin - Memberships';
|
||||
self::$memberships = new MemberModel;
|
||||
$view = Navigation::activePageSelect( 'nav.admin', '/admin/member' );
|
||||
Components::set( 'ADMINNAV', $view );
|
||||
}
|
||||
|
||||
public function index( $data = null ) {
|
||||
Views::view( 'members.admin.list', self::$memberships->list() );
|
||||
}
|
||||
|
||||
public function create( $data = null ) {
|
||||
}
|
||||
|
||||
public function edit( $data = null ) {
|
||||
}
|
||||
|
||||
public function view( $data = null ) {
|
||||
}
|
||||
|
||||
public function delete( $data = null ) {
|
||||
}
|
||||
|
||||
public function preview( $data = null ) {
|
||||
}
|
||||
}
|
75
app/plugins/members/controllers/admin/members.php
Normal file
75
app/plugins/members/controllers/admin/members.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/blog/controllers/admin/blog.php
|
||||
*
|
||||
* This is the Blog admin controller.
|
||||
*
|
||||
* @package TP Blog
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Admin;
|
||||
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Classes\AdminController;
|
||||
use TheTempusProject\Plugins\Members as MemberModel;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Hermes\Functions\Route as Routes;
|
||||
use TheTempusProject\Hermes\Functions\Redirect;
|
||||
|
||||
class Members extends AdminController {
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$view = Navigation::activePageSelect( 'nav.admin', '/admin/member' );
|
||||
Components::set( 'ADMINNAV', $view );
|
||||
}
|
||||
|
||||
public function index( $data = null ) {
|
||||
self::$title = 'Admin - Membership Scripts';
|
||||
Views::view( 'members.admin.scripts' );
|
||||
}
|
||||
|
||||
public function orphans( $data = null, $id = null ) {
|
||||
self::$title = 'Admin - Orphaned PRoducts';
|
||||
if ( $data = 'abandon' && ! empty( $id ) ) {
|
||||
MemberModel::orphanAbandon( $id );
|
||||
Redirect::to('admin/members/orphans');
|
||||
}
|
||||
$orphans = MemberModel::findOrphans();
|
||||
Views::view( 'members.admin.orphans', $orphans );
|
||||
}
|
||||
|
||||
public function webhooks( $data = null, $id = null ) {
|
||||
self::$title = 'Admin - Membership Webhooks';
|
||||
|
||||
if ( $data = 'delete' && ! empty( $id ) ) {
|
||||
MemberModel::webhookRemove( $id );
|
||||
Redirect::to('admin/members/webhooks');
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$webhooks = MemberModel::webhookList();
|
||||
foreach ($webhooks->data as $key => $webhook) {
|
||||
$hook = new \stdClass;
|
||||
$hook->id = $webhook->id;
|
||||
$hook->enabled_events = implode( ', <br>', $webhook->enabled_events );
|
||||
$hook->status = $webhook->status;
|
||||
$hook->url = $webhook->url;
|
||||
$data[] = $hook;
|
||||
}
|
||||
Components::set( 'urltouse', Routes::getAddress() );
|
||||
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'members.admin.webhooks', $data );
|
||||
}
|
||||
MemberModel::webhookSetup();
|
||||
Issues::add( 'success', 'Webhooks Generated' );
|
||||
Issues::add( 'error', 'Now, LEAVE!' );
|
||||
Redirect::to('admin/members/webhooks');
|
||||
}
|
||||
}
|
97
app/plugins/members/controllers/admin/products.php
Normal file
97
app/plugins/members/controllers/admin/products.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/controllers/admin/members/products.php
|
||||
*
|
||||
* This is the Membership Products admin controller.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Admin;
|
||||
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Classes\AdminController;
|
||||
use TheTempusProject\Models\MembershipProducts;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
|
||||
class Products extends AdminController {
|
||||
public static $products;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$title = 'Admin - Membership Products';
|
||||
self::$products = new MembershipProducts;
|
||||
$view = Navigation::activePageSelect( 'nav.admin', '/admin/products' );
|
||||
Components::set( 'ADMINNAV', $view );
|
||||
}
|
||||
|
||||
public function index( $data = null ) {
|
||||
Views::view( 'members.admin.products.list', self::$products->list() );
|
||||
}
|
||||
|
||||
public function create( $data = null ) {
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'members.admin.products.create' );
|
||||
}
|
||||
if ( !Forms::check( 'newMembershipProduct' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
|
||||
return $this->index();
|
||||
}
|
||||
$result = self::$products->create( Input::post( 'name' ), Input::post( 'description' ), Input::post( 'monthly_price' ), Input::post( 'yearly_price' ) );
|
||||
if ( $result ) {
|
||||
Issues::add( 'success', 'Your product has been created.' );
|
||||
return $this->index();
|
||||
} else {
|
||||
Issues::add( 'error', [ 'There was an unknown error submitting your data.' => Check::userErrors() ] );
|
||||
return $this->index();
|
||||
}
|
||||
}
|
||||
|
||||
public function edit( $id = null ) {
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'members.admin.products.edit', self::$products->findById( $id ) );
|
||||
}
|
||||
if ( !Forms::check( 'editMembershipProduct' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
|
||||
return $this->index();
|
||||
}
|
||||
|
||||
if ( self::$products->updatePost( $id, Input::post( 'name' ), Input::post( 'description' ), Input::post( 'monthly_price' ), Input::post( 'yearly_price' ) ) === true ) {
|
||||
Issues::add( 'success', 'Your product Updated.' );
|
||||
return $this->index();
|
||||
}
|
||||
Issues::add( 'error', 'There was an error with your request.' );
|
||||
$this->index();
|
||||
}
|
||||
|
||||
public function view( $id = null ) {
|
||||
$data = self::$products->findById( $id );
|
||||
if ( $data !== false ) {
|
||||
return Views::view( 'members.admin.products.view', $data );
|
||||
}
|
||||
Issues::add( 'error', 'Product not found.' );
|
||||
$this->index();
|
||||
}
|
||||
|
||||
public function delete( $data = null ) {
|
||||
if ( $data == null ) {
|
||||
if ( Input::exists( 'MP_' ) ) {
|
||||
$data = Input::post( 'MP_' );
|
||||
}
|
||||
}
|
||||
if ( !self::$products->delete( (array) $data ) ) {
|
||||
Issues::add( 'error', 'There was an error with your request.' );
|
||||
} else {
|
||||
Issues::add( 'success', 'Post has been deleted' );
|
||||
}
|
||||
$this->index();
|
||||
}
|
||||
}
|
56
app/plugins/members/controllers/admin/records.php
Normal file
56
app/plugins/members/controllers/admin/records.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/controllers/admin/members/records.php
|
||||
*
|
||||
* This is the Membership Records admin controller.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Admin;
|
||||
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Classes\AdminController;
|
||||
use TheTempusProject\Models\Memberships as MemberModel;
|
||||
|
||||
class Records extends AdminController {
|
||||
public static $memberships;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$title = 'Admin - Memberships';
|
||||
self::$memberships = new MemberModel;
|
||||
$view = Navigation::activePageSelect( 'nav.admin', '/admin/member' );
|
||||
Components::set( 'ADMINNAV', $view );
|
||||
}
|
||||
|
||||
public function index( $data = null ) {
|
||||
Views::view( 'members.admin.memberships.list', self::$memberships->list() );
|
||||
}
|
||||
|
||||
public function create( $data = null ) {
|
||||
}
|
||||
|
||||
public function edit( $data = null ) {
|
||||
}
|
||||
|
||||
public function view( $id = null ) {
|
||||
$data = self::$memberships->findById( $id );
|
||||
if ( $data !== false ) {
|
||||
return Views::view( 'members.admin.memberships.view', $data );
|
||||
}
|
||||
Issues::add( 'error', 'Membership not found.' );
|
||||
$this->index();
|
||||
}
|
||||
|
||||
public function delete( $data = null ) {
|
||||
}
|
||||
|
||||
public function preview( $data = null ) {
|
||||
}
|
||||
}
|
188
app/plugins/members/controllers/api/stripe.php
Normal file
188
app/plugins/members/controllers/api/stripe.php
Normal file
@ -0,0 +1,188 @@
|
||||
<?php
|
||||
/**
|
||||
* app/controllers/api/users.php
|
||||
*
|
||||
* This is the users' api 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\Api;
|
||||
|
||||
use Stripe\StripeClient;
|
||||
use Stripe\Event;
|
||||
use TheTempusProject\Models\User;
|
||||
use TheTempusProject\Classes\ApiController;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Models\MembershipCustomers;
|
||||
use TheTempusProject\Models\Memberships;
|
||||
|
||||
class Stripe extends ApiController {
|
||||
public static $stripe;
|
||||
public static $customers;
|
||||
public static $memberships;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct( false );
|
||||
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
||||
self::$stripe = new StripeClient($api_key);
|
||||
}
|
||||
|
||||
public function webhook() {
|
||||
try {
|
||||
$payload = @file_get_contents('php://input');
|
||||
$payload = json_decode( $payload, true );
|
||||
if ( ! is_array( $payload ) ) {
|
||||
throw new \Exception("Error Processing Request", 1);
|
||||
}
|
||||
$event = null;
|
||||
$event = Event::constructFrom( $payload );
|
||||
|
||||
$eventData = $event->data->object;
|
||||
|
||||
// $event->type gives the event type obv
|
||||
switch ($event->type) {
|
||||
// case 'invoice.paid':
|
||||
// Debug::error( 'processing: ' . $event->type );
|
||||
// "id": "in_1QSDcJGsigymNdIJo0Z1a20K",
|
||||
// "customer": "cus_RKtOtR7X7CwPRU",
|
||||
// "charge": "ch_3QSDcJGsigymNdIJ0Smb7Rmx",
|
||||
// "subscription": "sub_1QSDcJGsigymNdIJWGw7Zrv9",
|
||||
// break;
|
||||
|
||||
// case 'charge.succeeded':
|
||||
// Debug::error( 'processing: ' . $event->type );
|
||||
// "id": "ch_3QSDcJGsigymNdIJ0Smb7Rmx",
|
||||
// "invoice": "in_1QSDcJGsigymNdIJo0Z1a20K",
|
||||
// "status": "succeeded",
|
||||
// break;
|
||||
|
||||
case 'customer.subscription.updated':
|
||||
case 'customer.subscription.paused':
|
||||
case 'customer.subscription.resumed':
|
||||
case 'customer.subscription.deleted':
|
||||
Debug::error( 'processing: ' . $event->type );
|
||||
|
||||
self::$memberships = new Memberships;
|
||||
$membership_id = self::$memberships->findBySubscriptionID( $eventData->id );
|
||||
if ( empty( $membership_id ) ) {
|
||||
Debug::error( 'membership not found' );
|
||||
|
||||
self::$customers = new MembershipCustomers;
|
||||
$customer = self::$customers->findByCustomerID( $eventData->customer );
|
||||
if ( empty( $customer ) ) {
|
||||
Debug::error( 'customer not found' );
|
||||
Debug::v( $eventData->customer );
|
||||
break;
|
||||
}
|
||||
|
||||
$result = self::$memberships->create(
|
||||
$eventData->customer,
|
||||
$eventData->id,
|
||||
$eventData->plan->id,
|
||||
$eventData->current_period_start,
|
||||
$eventData->current_period_end,
|
||||
$eventData->status,
|
||||
$customer->local_user,
|
||||
'frequency'
|
||||
);
|
||||
} else {
|
||||
$result = self::$memberships->update( $membership_id->ID, $eventData->current_period_start, $eventData->current_period_end, $eventData->status );
|
||||
}
|
||||
|
||||
if ( empty( $result ) ) {
|
||||
Debug::error( 'membership not updated' );
|
||||
Debug::v( $result );
|
||||
}
|
||||
break;
|
||||
case 'customer.subscription.created':
|
||||
Debug::error( 'processing: ' . $event->type );
|
||||
|
||||
self::$memberships = new Memberships;
|
||||
$membership_id = self::$memberships->findBySubscriptionID( $eventData->id );
|
||||
if ( ! empty( $membership_id ) ) {
|
||||
Debug::error( 'subscription already created' );
|
||||
break;
|
||||
}
|
||||
self::$customers = new MembershipCustomers;
|
||||
$customer = self::$customers->findByCustomerID( $eventData->customer );
|
||||
if ( empty( $customer ) ) {
|
||||
Debug::error( 'customer not found' );
|
||||
Debug::v( $eventData->customer );
|
||||
break;
|
||||
}
|
||||
Debug::error( 'processing: ' . $event->type );
|
||||
|
||||
$result = self::$memberships->create(
|
||||
$eventData->customer,
|
||||
$eventData->id,
|
||||
$eventData->plan->id,
|
||||
$eventData->current_period_start,
|
||||
$eventData->current_period_end,
|
||||
$eventData->status,
|
||||
$customer->local_user,
|
||||
'frequency'
|
||||
);
|
||||
Debug::error( 'processing: ' . $event->type );
|
||||
|
||||
if ( empty( $result ) ) {
|
||||
Debug::error( 'membership not made' );
|
||||
Debug::v( $result );
|
||||
}
|
||||
Debug::error( 'done processing: ' . var_export($result,true) );
|
||||
break;
|
||||
// case 'invoice.created':
|
||||
// Debug::error( 'processing: ' . $event->type );
|
||||
// happens when the payment first starts
|
||||
|
||||
// "id": "in_1QSDcJGsigymNdIJo0Z1a20K",
|
||||
// "customer": "cus_RKtOtR7X7CwPRU",
|
||||
// "status": "open",
|
||||
// "total": 888,
|
||||
// break;
|
||||
// case 'checkout.session.completed':
|
||||
// Debug::error( 'processing: ' . $event->type );
|
||||
// new customer has completed first checkout
|
||||
// add thier record or update iit
|
||||
|
||||
// "invoice": "in_1QSDcJGsigymNdIJo0Z1a20K",
|
||||
// "mode": "subscription",
|
||||
// "status": "complete",
|
||||
// "customer": "cus_RKtOtR7X7CwPRU",
|
||||
// break;
|
||||
// case 'payment_intent.succeeded':
|
||||
// Debug::error( 'processing: ' . $event->type );
|
||||
// $paymentIntent = $event->data->object;
|
||||
// break;
|
||||
// case 'payment_method.attached':
|
||||
// Debug::error( 'processing: ' . $event->type );
|
||||
// $paymentMethod = $event->data->object;
|
||||
// break;
|
||||
default:
|
||||
Debug::error( 'Skipped Event:' . $event->type );
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
$responseType = 'success';
|
||||
$response = true;
|
||||
} catch(\UnexpectedValueException $e) {
|
||||
Debug::error( 'UnexpectedValueException' );
|
||||
Debug::v( $e );
|
||||
http_response_code(400);
|
||||
$responseType = 'error';
|
||||
$response = 'UnexpectedValueException';
|
||||
} catch(\Exception $e) {
|
||||
Debug::error( 'Exception' );
|
||||
Debug::error( $e );
|
||||
http_response_code(400);
|
||||
$responseType = 'error';
|
||||
$response = 'Exception';
|
||||
}
|
||||
Views::view( 'api.response', ['response' => json_encode( [ $responseType => $response ], true )]);
|
||||
}
|
||||
}
|
339
app/plugins/members/controllers/member.php
Normal file
339
app/plugins/members/controllers/member.php
Normal file
@ -0,0 +1,339 @@
|
||||
<?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;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
use TheTempusProject\Bedrock\Functions\Hash;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use Stripe\StripeClient;
|
||||
|
||||
class Member extends Controller {
|
||||
public static $customers;
|
||||
public static $products;
|
||||
public static $stripe;
|
||||
private static $loaded = false;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
if ( ! self::$loaded ) {
|
||||
Template::noIndex();
|
||||
self::$customers = new MembershipCustomers;
|
||||
self::$products = new MembershipProducts;
|
||||
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
||||
if ( $api_key == 'sk_xxxxxxxxxxxxxxx' || empty($api_key) ) {
|
||||
Debug::error( "Memberships:__construct No Stripe Key found" );
|
||||
} else {
|
||||
self::$stripe = new StripeClient( $api_key );
|
||||
}
|
||||
self::$loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function index() {
|
||||
$this->confirmAuth();
|
||||
self::$title = 'Members Area';
|
||||
Views::view( 'members.members' );
|
||||
}
|
||||
|
||||
public function managepayment() {
|
||||
$this->confirmAuth();
|
||||
$customer = self::$customers->findByUserID( App::$activeUser->ID );
|
||||
if ( empty( $customer ) ) {
|
||||
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' );
|
||||
}
|
||||
try {
|
||||
$session = self::$stripe->billingPortal->sessions->create([
|
||||
'customer' => $customer->stripe_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 ) {
|
||||
$this->confirmAuth( $id );
|
||||
$memberships = new Memberships;
|
||||
$result = $memberships->cancel( $id );
|
||||
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 ) {
|
||||
$this->confirmAuth( $id );
|
||||
$memberships = new Memberships;
|
||||
$result = $memberships->cancel( $id );
|
||||
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 ) {
|
||||
$this->confirmAuth( $id );
|
||||
self::$title = 'pause Membership';
|
||||
Components::set( 'pauseid', $id );
|
||||
Views::view( 'members.pause' );
|
||||
}
|
||||
|
||||
public function resume( $id = null ) {
|
||||
$this->confirmAuth( $id );
|
||||
self::$title = 'resume Membership';
|
||||
Views::view( 'members.resume' );
|
||||
}
|
||||
|
||||
public function cancel( $id = null ) {
|
||||
$this->confirmAuth( $id );
|
||||
self::$title = 'Cancel Membership';
|
||||
Components::set( 'cancelid', $id );
|
||||
Views::view( 'members.cancel' );
|
||||
}
|
||||
|
||||
public function manage() {
|
||||
if ( ! App::$isLoggedIn ) {
|
||||
Session::flash( 'error', 'You do not have permission to access this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
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() {
|
||||
if ( ! App::$isLoggedIn ) {
|
||||
Session::flash( 'error', 'You do not have permission to access this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
// need to check if the plan CAN be upgraded
|
||||
self::$title = 'Upgrade Membership';
|
||||
Views::view( 'members.upgrade' );
|
||||
}
|
||||
|
||||
public function join( $plan = 'monthly' ) {
|
||||
if ( ! App::$isLoggedIn ) {
|
||||
Session::flash( 'error', 'You do not have permission to access this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
$plan = strtolower( $plan );
|
||||
if ( ! in_array( $plan, ['monthly','yearly'] ) ) {
|
||||
Session::flash( 'error', 'Unknown plan' );
|
||||
return Redirect::to( 'home/index' );
|
||||
}
|
||||
|
||||
self::$title = 'Join {SITENAME}!';
|
||||
$stripePrice = $this->findPrice( $plan );
|
||||
|
||||
$product = self::$products->findByPriceID( $stripePrice );
|
||||
if ( empty( $product ) ) {
|
||||
Session::flash( 'success', 'We aren\'t currently accepting new members, please check back soon!' );
|
||||
return Redirect::home();
|
||||
}
|
||||
Views::view( 'members.join', $product );
|
||||
}
|
||||
|
||||
public function checkout( $plan = 'monthly' ) {
|
||||
if ( ! App::$isLoggedIn ) {
|
||||
Session::flash( 'error', 'You do not have permission to access this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
$customer = self::$customers->findOrCreate( App::$activeUser->ID );
|
||||
if ( empty( $customer ) ) {
|
||||
Issues::add( 'error', 'no customer' );
|
||||
return $this->index();
|
||||
}
|
||||
|
||||
$plan = strtolower( $plan );
|
||||
if ( ! in_array( $plan, ['monthly','yearly','upgrade'] ) ) {
|
||||
Session::flash( 'error', 'Unknown plan' );
|
||||
return Redirect::to( 'home/index' );
|
||||
}
|
||||
if ( $plan === 'upgrade' ) {
|
||||
$plan = 'yearly';
|
||||
$successUrl = Routes::getAddress() . 'member/payment/upgrade?session_id={CHECKOUT_SESSION_ID}';
|
||||
} else {
|
||||
$successUrl = Routes::getAddress() . 'member/payment/complete?session_id={CHECKOUT_SESSION_ID}';
|
||||
}
|
||||
|
||||
$stripePrice = $this->findPrice( $plan );
|
||||
|
||||
$session = self::$stripe->checkout->sessions->create([
|
||||
'payment_method_types' => ['card'],
|
||||
'customer' => $customer,
|
||||
'line_items' => [[
|
||||
'price' => $stripePrice,
|
||||
'quantity' => 1,
|
||||
]],
|
||||
'mode' => 'subscription',
|
||||
'allow_promotion_codes' => true,
|
||||
'success_url' => $successUrl,
|
||||
'cancel_url' => Routes::getAddress() . 'member/payment/cancel',
|
||||
]);
|
||||
header('Location: ' . $session->url);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function payment( $type = '' ) {
|
||||
$type = strtolower( $type );
|
||||
if ( ! in_array( $type, ['cancel','complete','upgrade'] ) ) {
|
||||
Session::flash( 'error', 'Unknown Payment' );
|
||||
return Redirect::to( 'home/index' );
|
||||
}
|
||||
|
||||
if ( $type == 'cancel' ) {
|
||||
self::$title = '(almost) Members Area';
|
||||
return Views::view( 'members.paymentcanceled' );
|
||||
}
|
||||
|
||||
if ( $type == 'upgrade' ) {
|
||||
self::$title = 'Better Members Area';
|
||||
return Views::view( 'members.upgradeCompleted' );
|
||||
}
|
||||
|
||||
self::$title = '(almost) Members Area';
|
||||
Views::view( 'members.paymentcomplete' );
|
||||
}
|
||||
|
||||
// This combines a registration with a checkout
|
||||
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' );
|
||||
}
|
||||
|
||||
$stripePrice = $this->findPrice( $plan );
|
||||
|
||||
$pretty = 'prettyPrice' . ucfirst( $plan );
|
||||
$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, you can <a href="/member/join">subscribe here</a>, or <a href="/member/upgrade">upgrade here</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' );
|
||||
}
|
||||
$session = self::$stripe->checkout->sessions->create([
|
||||
'payment_method_types' => ['card'],
|
||||
'customer' => $customer,
|
||||
'line_items' => [[
|
||||
'price' => $stripePrice,
|
||||
'quantity' => 1,
|
||||
]],
|
||||
'mode' => 'subscription',
|
||||
'allow_promotion_codes' => true,
|
||||
'success_url' => Routes::getAddress() . 'member/payment/complete?session_id={CHECKOUT_SESSION_ID}',
|
||||
'cancel_url' => Routes::getAddress() . 'member/payment/cancel',
|
||||
]);
|
||||
header('Location: ' . $session->url);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function findPrice( $plan ) {
|
||||
$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;
|
||||
$stripePrice = $product->$index;
|
||||
return $stripePrice;
|
||||
}
|
||||
|
||||
private function confirmAuth() {
|
||||
if ( ! App::$isLoggedIn || ! App::$isMember ) {
|
||||
Session::flash( 'error', 'You do not have permission to access this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user