Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -12,40 +12,65 @@
|
||||
namespace IPS\nexus\modules\front\subscriptions;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DateInterval;
|
||||
use Exception;
|
||||
use IPS\core\Facebook\Pixel;
|
||||
use IPS\Dispatcher\Controller;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Login;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Customer;
|
||||
use IPS\nexus\extensions\nexus\Item\Subscription;
|
||||
use IPS\nexus\Invoice;
|
||||
use IPS\nexus\Money;
|
||||
use IPS\nexus\Purchase;
|
||||
use IPS\nexus\Subscription\Package;
|
||||
use IPS\nexus\Subscription\Table;
|
||||
use IPS\nexus\Tax;
|
||||
use IPS\Output;
|
||||
use IPS\Request;
|
||||
use IPS\Session;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* subscriptions
|
||||
*/
|
||||
class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
class subscriptions extends Controller
|
||||
{
|
||||
/**
|
||||
* Execute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function execute()
|
||||
public function execute() : void
|
||||
{
|
||||
/* Work out currency */
|
||||
if ( isset( \IPS\Request::i()->currency ) and \in_array( \IPS\Request::i()->currency, \IPS\nexus\Money::currencies() ) )
|
||||
if ( isset( Request::i()->currency ) and in_array( Request::i()->currency, Money::currencies() ) )
|
||||
{
|
||||
if ( isset( \IPS\Request::i()->csrfKey ) and \IPS\Login::compareHashes( (string) \IPS\Session::i()->csrfKey, (string) \IPS\Request::i()->csrfKey ) )
|
||||
if ( isset( Request::i()->csrfKey ) and Login::compareHashes( (string) Session::i()->csrfKey, (string) Request::i()->csrfKey ) )
|
||||
{
|
||||
$_SESSION['cart'] = array();
|
||||
\IPS\Request::i()->setCookie( 'currency', \IPS\Request::i()->currency );
|
||||
Request::i()->setCookie( 'currency', Request::i()->currency );
|
||||
|
||||
$url = \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' );
|
||||
$url = Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' );
|
||||
|
||||
if( isset( \IPS\Request::i()->register ) )
|
||||
if( isset( Request::i()->register ) )
|
||||
{
|
||||
$url = $url->setQueryString( 'register', (int) \IPS\Request::i()->register );
|
||||
$url = $url->setQueryString( 'register', (int) Request::i()->register );
|
||||
}
|
||||
|
||||
\IPS\Output::i()->redirect( $url );
|
||||
Output::i()->redirect( $url );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,46 +82,45 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function manage()
|
||||
protected function manage() : void
|
||||
{
|
||||
if ( ! \IPS\Settings::i()->nexus_subs_enabled )
|
||||
if ( ! Settings::i()->nexus_subs_enabled )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_no_subs', '2X379/1', 404, '' );
|
||||
Output::i()->error( 'nexus_no_subs', '2X379/1', 404, '' );
|
||||
}
|
||||
|
||||
/* Send no-cache headers for this page, required for guest sign-ups */
|
||||
\IPS\Output::setCacheTime( false );
|
||||
Output::i()->pageCaching = FALSE;
|
||||
|
||||
/* Create the table */
|
||||
$table = new \IPS\nexus\Subscription\Table( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
|
||||
$current = \IPS\nexus\Subscription::loadByMember( \IPS\Member::loggedIn(), FALSE );
|
||||
$table = new Table( Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
|
||||
$current = \IPS\nexus\Subscription::loadByMember( Member::loggedIn(), FALSE );
|
||||
|
||||
if ( $current )
|
||||
{
|
||||
$table->activeSubscription = $current;
|
||||
}
|
||||
|
||||
if ( isset( \IPS\Request::i()->purchased ) and $table->activeSubscription )
|
||||
if ( isset( Request::i()->purchased ) and $table->activeSubscription )
|
||||
{
|
||||
try
|
||||
{
|
||||
$invoice = \IPS\nexus\Invoice::load( $table->activeSubscription->invoice_id );
|
||||
$invoice = Invoice::load( $table->activeSubscription->invoice_id );
|
||||
|
||||
/* Fire the pixel event */
|
||||
\IPS\core\Facebook\Pixel::i()->Purchase = array( 'value' => $invoice->total->amount, 'currency' => $invoice->total->currency );
|
||||
\IPS\Output::i()->inlineMessage = \IPS\Member::loggedIn()->language()->addToStack( 'nexus_subs_paid_flash_msg');
|
||||
Pixel::i()->Purchase = array( 'value' => $invoice->total->amount, 'currency' => $invoice->total->currency );
|
||||
Output::i()->inlineMessage = Member::loggedIn()->language()->addToStack( 'nexus_subs_paid_flash_msg');
|
||||
}
|
||||
catch( \Exception $e ) { }
|
||||
catch( Exception ) { }
|
||||
}
|
||||
|
||||
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'front_subscriptions.js', 'nexus', 'front' ) );
|
||||
\IPS\Output::i()->cssFiles = array_merge( \IPS\Output::i()->cssFiles, \IPS\Theme::i()->css( 'subscriptions.css', 'nexus' ) );
|
||||
Output::i()->jsFiles = array_merge( Output::i()->jsFiles, Output::i()->js( 'front_subscriptions.js', 'nexus', 'front' ) );
|
||||
|
||||
\IPS\Output::i()->breadcrumb['module'] = array( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ), \IPS\Member::loggedIn()->language()->addToStack('nexus_front_subscriptions') );
|
||||
Output::i()->breadcrumb['module'] = array( Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ), Member::loggedIn()->language()->addToStack('nexus_front_subscriptions') );
|
||||
|
||||
\IPS\Output::i()->linkTags['canonical'] = (string) \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' );
|
||||
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('nexus_front_subscriptions');
|
||||
\IPS\Output::i()->output = $table;
|
||||
Output::i()->linkTags['canonical'] = (string) Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' );
|
||||
Output::i()->title = Member::loggedIn()->language()->addToStack('nexus_front_subscriptions');
|
||||
Output::i()->output = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,40 +128,40 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
*
|
||||
* @return void just like life, it is meaningless and temporary so live in the moment, enjoy each day and eat chocolate unless you have an allergy in which case don't. See your GP before starting any new diet.
|
||||
*/
|
||||
protected function change()
|
||||
protected function change() : void
|
||||
{
|
||||
/* CSRF Check */
|
||||
\IPS\Session::i()->csrfCheck();
|
||||
Session::i()->csrfCheck();
|
||||
|
||||
try
|
||||
{
|
||||
$newPackage = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
|
||||
$newPackage = Package::load( Request::i()->id );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_no_subs_package', '2X379/2', 404, '' );
|
||||
Output::i()->error( 'nexus_no_subs_package', '2X379/2', 404, '' );
|
||||
}
|
||||
|
||||
/* Is the subscription purchasable ? */
|
||||
if ( !$newPackage->enabled )
|
||||
{
|
||||
\IPS\Output::i()->error( 'node_error', '2X379/7', 403, '' );
|
||||
Output::i()->error( 'node_error', '2X379/7', 403, '' );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$subscription = \IPS\nexus\Subscription::loadByMember( \IPS\Member::loggedIn(), FALSE );
|
||||
$subscription = \IPS\nexus\Subscription::loadByMember( Member::loggedIn(), FALSE );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_no_subs_subs', '2X379/3', 404, '' );
|
||||
Output::i()->error( 'nexus_no_subs_subs', '2X379/3', 404, '' );
|
||||
}
|
||||
|
||||
/* Fetch purchase */
|
||||
$purchase = NULL;
|
||||
if( $subscription )
|
||||
{
|
||||
foreach ( \IPS\nexus\extensions\nexus\Item\Subscription::getPurchases( \IPS\nexus\Customer::loggedIn(), $subscription->package->id, TRUE, TRUE ) as $row )
|
||||
foreach ( Subscription::getPurchases( Customer::loggedIn(), $subscription->package->id, TRUE, TRUE ) as $row )
|
||||
{
|
||||
if ( !$row->cancelled OR ( $row->cancelled AND $row->can_reactivate ) )
|
||||
{
|
||||
@@ -149,33 +173,34 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
|
||||
if ( $purchase === NULL )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_sub_no_purchase', '2X379/4', 404, '' );
|
||||
Output::i()->error( 'nexus_sub_no_purchase', '2X379/4', 404, '' );
|
||||
}
|
||||
|
||||
/* @var Purchase $purchase */
|
||||
/* We cannot process changes if an active Billing Agreement is in place */
|
||||
if( $purchase->billing_agreement and !$purchase->billing_agreement->canceled )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_sub_no_change_ba', '2X379/B', 404, '' );
|
||||
Output::i()->error( 'nexus_sub_no_change_ba', '2X379/B', 404, '' );
|
||||
}
|
||||
|
||||
/* Right, that's all the "I'll tamper with the URLs for a laugh" stuff out of the way... */
|
||||
$upgradeCost = $newPackage->costToUpgrade( $subscription->package, \IPS\nexus\Customer::loggedIn() );
|
||||
$upgradeCost = $newPackage->costToUpgrade( $subscription->package, Customer::loggedIn() );
|
||||
|
||||
if ( $upgradeCost === NULL )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_no_subs_nocost', '2X379/5', 404, '' );
|
||||
Output::i()->error( 'nexus_no_subs_nocost', '2X379/5', 404, '' );
|
||||
}
|
||||
|
||||
$invoice = $subscription->package->upgradeDowngrade( $purchase, $newPackage );
|
||||
|
||||
if ( $invoice )
|
||||
{
|
||||
\IPS\Output::i()->redirect( $invoice->checkoutUrl() );
|
||||
Output::i()->redirect( $invoice->checkoutUrl() );
|
||||
}
|
||||
|
||||
$purchase->member->log( 'subscription', array( 'type' => 'change', 'id' => $purchase->id, 'old' => $purchase->name, 'name' => $newPackage->titleForLog(), 'system' => FALSE ) );
|
||||
|
||||
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
|
||||
Output::i()->redirect( Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,37 +208,37 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function purchase()
|
||||
protected function purchase() : void
|
||||
{
|
||||
/* CSRF Check */
|
||||
\IPS\Session::i()->csrfCheck();
|
||||
Session::i()->csrfCheck();
|
||||
|
||||
/* Send no-cache headers for this page, required for guest sign-ups */
|
||||
\IPS\Output::setCacheTime( false );
|
||||
Output::i()->pageCaching = FALSE;
|
||||
|
||||
/* Already purchased a subscription */
|
||||
if ( $current = \IPS\nexus\Subscription::loadByMember( \IPS\nexus\Customer::loggedIn(), FALSE ) AND ( $current->purchase AND ( !$current->purchase->cancelled OR $current->purchase->can_reactivate ) ) )
|
||||
if ( $current = \IPS\nexus\Subscription::loadByMember( Customer::loggedIn(), FALSE ) AND ( $current->purchase AND ( !$current->purchase->cancelled OR $current->purchase->can_reactivate ) ) )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_subs_already_got_package', '2X379/6', 403, '' );
|
||||
Output::i()->error( 'nexus_subs_already_got_package', '2X379/6', 403, '' );
|
||||
}
|
||||
|
||||
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
|
||||
$package = Package::load( Request::i()->id );
|
||||
|
||||
/* Is the subscription purchasable ? */
|
||||
if ( !$package->enabled )
|
||||
{
|
||||
\IPS\Output::i()->error( 'node_error', '2X379/7', 403, '' );
|
||||
Output::i()->error( 'node_error', '2X379/7', 403, '' );
|
||||
}
|
||||
|
||||
$price = $package->price();
|
||||
|
||||
$item = new \IPS\nexus\extensions\nexus\Item\Subscription( \IPS\nexus\Customer::loggedIn()->language()->get( $package->_titleLanguageKey ), $price );
|
||||
$item = new Subscription( Customer::loggedIn()->language()->get( $package->_titleLanguageKey ), $price );
|
||||
$item->id = $package->id;
|
||||
try
|
||||
{
|
||||
$item->tax = \IPS\nexus\Tax::load( $package->tax );
|
||||
$item->tax = Tax::load( $package->tax );
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
catch ( OutOfRangeException ) { }
|
||||
if ( $package->gateways !== '*' )
|
||||
{
|
||||
$item->paymentMethodIds = explode( ',', $package->gateways );
|
||||
@@ -221,19 +246,19 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
$item->renewalTerm = $package->renewalTerm( $price->currency );
|
||||
if ( $package->price and $costs = json_decode( $package->price, TRUE ) and isset( $costs['cost'] ) )
|
||||
{
|
||||
$item->initialInterval = new \DateInterval( 'P' . $costs['term'] . mb_strtoupper( $costs['unit'] ) );
|
||||
$item->initialInterval = new DateInterval( 'P' . $costs['term'] . mb_strtoupper( $costs['unit'] ) );
|
||||
}
|
||||
|
||||
/* Generate the invoice */
|
||||
$invoice = new \IPS\nexus\Invoice;
|
||||
$invoice = new Invoice;
|
||||
$invoice->currency = $price->currency;
|
||||
$invoice->member = \IPS\nexus\Customer::loggedIn();
|
||||
$invoice->member = Customer::loggedIn();
|
||||
$invoice->addItem( $item );
|
||||
$invoice->return_uri = "app=nexus&module=subscriptions&controller=subscriptions&purchased=1";
|
||||
$invoice->save();
|
||||
|
||||
/* Take them to it */
|
||||
\IPS\Output::i()->redirect( $invoice->checkoutUrl() );
|
||||
Output::i()->redirect( $invoice->checkoutUrl() );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,30 +266,30 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function reactivate()
|
||||
protected function reactivate() : void
|
||||
{
|
||||
/* CSRF Check */
|
||||
\IPS\Session::i()->csrfCheck();
|
||||
Session::i()->csrfCheck();
|
||||
|
||||
/* Get subscription and purchase */
|
||||
try
|
||||
{
|
||||
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
|
||||
$package = Package::load( Request::i()->id );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_no_subs_package', '2X379/2', 404, '' );
|
||||
Output::i()->error( 'nexus_no_subs_package', '2X379/2', 404, '' );
|
||||
}
|
||||
try
|
||||
{
|
||||
$subscription = \IPS\nexus\Subscription::loadByMemberAndPackage( \IPS\Member::loggedIn(), $package, FALSE );
|
||||
$subscription = \IPS\nexus\Subscription::loadByMemberAndPackage( Member::loggedIn(), $package, FALSE );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_no_subs_subs', '2X379/8', 404, '' );
|
||||
Output::i()->error( 'nexus_no_subs_subs', '2X379/8', 404, '' );
|
||||
}
|
||||
$purchase = NULL;
|
||||
foreach ( \IPS\nexus\extensions\nexus\Item\Subscription::getPurchases( \IPS\nexus\Customer::loggedIn(), $subscription->package->id, TRUE, TRUE ) as $row )
|
||||
foreach ( Subscription::getPurchases( Customer::loggedIn(), $subscription->package->id, TRUE, TRUE ) as $row )
|
||||
{
|
||||
if ( $row->can_reactivate )
|
||||
{
|
||||
@@ -274,15 +299,16 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
}
|
||||
if ( $purchase === NULL )
|
||||
{
|
||||
\IPS\Output::i()->error( 'nexus_sub_no_purchase', '2X379/9', 404, '' );
|
||||
Output::i()->error( 'nexus_sub_no_purchase', '2X379/9', 404, '' );
|
||||
}
|
||||
|
||||
|
||||
/* @var Purchase $purchase */
|
||||
/* Set renewal terms */
|
||||
try
|
||||
{
|
||||
$currency = $purchase->original_invoice->currency;
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
catch ( Exception )
|
||||
{
|
||||
$currency = $purchase->member->defaultCurrency();
|
||||
}
|
||||
@@ -296,11 +322,11 @@ class _subscriptions extends \IPS\Dispatcher\Controller
|
||||
if ( !$purchase->active and $cycles = $purchase->canRenewUntil( NULL, TRUE ) AND $cycles !== FALSE )
|
||||
{
|
||||
$url = $cycles === 1 ? $purchase->url()->setQueryString( 'do', 'renew' )->csrf() : $purchase->url()->setQueryString( 'do', 'renew' );
|
||||
\IPS\Output::i()->redirect( $url );
|
||||
Output::i()->redirect( $url );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
|
||||
Output::i()->redirect( Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user