Version 4.5.0

This commit is contained in:
Neo committed 2025-12-19 05:51:58 -08:00
1 parent 1a0c7fd3c2
commit f79dcf067a
3791 files changed
+248032 -76372

No files matched your search

@@ -12,7 +12,7 @@
namespace IPS\nexus\modules\front\subscriptions;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
@@ -23,6 +23,26 @@ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
*/
class _subscriptions extends \IPS\Dispatcher\Controller
{
/**
* Execute
*
* @return void
*/
public function execute()
{
/* Work out currency */
if ( isset( \IPS\Request::i()->currency ) and \in_array( \IPS\Request::i()->currency, \IPS\nexus\Money::currencies() ) )
{
if ( isset( \IPS\Request::i()->csrfKey ) and \IPS\Login::compareHashes( (string) \IPS\Session::i()->csrfKey, (string) \IPS\Request::i()->csrfKey ) )
{
$_SESSION['cart'] = array();
\IPS\Request::i()->setCookie( 'currency', \IPS\Request::i()->currency );
}
}
parent::execute();
}
/**
* Show the subscription packages
*
@@ -34,10 +54,24 @@ class _subscriptions extends \IPS\Dispatcher\Controller
{
\IPS\Output::i()->error( 'nexus_no_subs', '2X379/1', 404, '' );
}
/* Create the table */
$table = new \IPS\nexus\Subscription\Table( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
$table->activeSubscription = \IPS\nexus\Subscription::loadByMember( \IPS\Member::loggedIn(), FALSE );
if ( isset( \IPS\Request::i()->purchased ) and $table->activeSubscription )
{
try
{
$invoice = \IPS\nexus\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');
}
catch( \Exception $e ) { }
}
\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' ) );
@@ -65,6 +99,13 @@ class _subscriptions extends \IPS\Dispatcher\Controller
{
\IPS\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, '' );
}
try
{
@@ -108,82 +149,9 @@ class _subscriptions extends \IPS\Dispatcher\Controller
$purchase->member->log( 'subscription', array( 'type' => 'change', 'id' => $purchase->id, 'old' => $purchase->name, 'name' => $newPackage->titleForLog(), 'system' => FALSE ) );
\IPS\Output::i()->redirect( $purchase->url() );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
}
/**
* Cancels a subscription doens't it? Yes it does.
*
* @return void
*/
protected function cancel()
{
/* CSRF Check */
\IPS\Session::i()->csrfCheck();
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
$subscription = \IPS\nexus\Subscription::loadActiveByMember( \IPS\Member::loggedIn() );
/* Build Form */
$form = new \IPS\Helpers\Form;
$form->class = 'ipsForm_vertical';
if ( $subscription->renews )
{
$form->add( new \IPS\Helpers\Form\Radio( 'nexus_sub_cancel_type', 0, TRUE, array(
'options' => array( 0 => 'nexus_sub_cancel_type_renewal', 1 => 'nexus_sub_cancel_type_immediate' )
), NULL, NULL, NULL, 'nexus_sub_cancel_type' ) );
}
else
{
$form->add( new \IPS\Helpers\Form\YesNo( 'nexus_sub_cancel_now', 0, TRUE, array(), NULL, NULL, NULL, 'nexus_sub_cancel_now' ) );
}
if( $subscription->_expire )
{
\IPS\Member::loggedIn()->language()->words['nexus_sub_cancel_type_renewal_desc'] = \IPS\Member::loggedIn()->language()->addToStack( 'nexus_sub_cancel_type_renewal__desc', NULL, array( 'sprintf' => array( $subscription->_expire->dayAndMonth() . ' ' . $subscription->_expire->format('Y') ) ) );
}
if ( $values = $form->values() )
{
if ( ( isset( $values['nexus_sub_cancel_now'] ) and $values['nexus_sub_cancel_now'] ) or ( isset( $values['nexus_sub_cancel_type'] ) and $values['nexus_sub_cancel_type'] == 1 ) )
{
/* Cancel purchase */
foreach ( \IPS\nexus\extensions\nexus\Item\Subscription::getPurchases( \IPS\nexus\Customer::loggedIn(), $package->id ) as $purchase )
{
$purchase->cancelled = TRUE;
$purchase->member->log( 'purchase', array( 'type' => 'cancel', 'id' => $purchase->id, 'name' => $purchase->name ) );
$purchase->can_reactivate = FALSE;
$purchase->save();
}
/* Cancel the subscription */
$package->cancelMember( \IPS\nexus\Customer::loggedIn() );
}
else if ( isset( $values['nexus_sub_cancel_type'] ) and $values['nexus_sub_cancel_type'] == 0 )
{
/* Just stop the renewal bud */
$subscription->renews = 0;
$subscription->save();
/* Cancel renewals */
foreach ( \IPS\nexus\extensions\nexus\Item\Subscription::getPurchases( \IPS\nexus\Customer::loggedIn(), $package->id ) as $purchase )
{
$purchase->renewals = NULL;
$purchase->save();
}
$purchase->member->log( 'subscription', array( 'type' => 'cancelrenewals', 'id' => $package->id ) );
}
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
}
/* Display */
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('nexus_subs_cancel');
\IPS\Output::i()->output = \IPS\Request::i()->isAjax() ? $form->customTemplate( array( call_user_func_array( array( \IPS\Theme::i(), 'getTemplate' ), array( 'forms', 'core' ) ), 'popupTemplate' ) ) : $form;
}
/**
* Purchase
*
@@ -195,12 +163,19 @@ class _subscriptions extends \IPS\Dispatcher\Controller
\IPS\Session::i()->csrfCheck();
/* Already purchased a subscription? */
if ( \IPS\nexus\Subscription::loadActiveByMember( \IPS\nexus\Customer::loggedIn() ) )
if ( \IPS\nexus\Subscription::loadByMember( \IPS\nexus\Customer::loggedIn(), FALSE ) )
{
\IPS\Output::i()->error( 'nexus_subs_already_got_package', '2X379/6', 403, '' );
}
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
/* Is the subscription purchasable ? */
if ( !$package->enabled )
{
\IPS\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 );
@@ -210,24 +185,85 @@ class _subscriptions extends \IPS\Dispatcher\Controller
$item->tax = \IPS\nexus\Tax::load( $package->tax );
}
catch ( \OutOfRangeException $e ) { }
if ( $package->gateways !== '*' )
{
$item->paymentMethodIds = explode( ',', $package->gateways );
}
$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'] ) );
}
/* Generate the invoice */
$invoice = new \IPS\nexus\Invoice;
$invoice->currency = $price->currency;
$invoice->member = \IPS\nexus\Customer::loggedIn();
$invoice->addItem( $item );
$invoice->return_uri = "app=nexus&module=subscriptions&controller=subscriptions";
$invoice->return_uri = "app=nexus&module=subscriptions&controller=subscriptions&purchased=1";
$invoice->save();
/* Take them to it */
\IPS\Output::i()->redirect( $invoice->checkoutUrl() );
}
/**
* Reactivate
*
* @return void
*/
protected function reactivate()
{
/* CSRF Check */
\IPS\Session::i()->csrfCheck();
/* Get subscription and purchase */
try
{
$package = \IPS\nexus\Subscription\Package::load( \IPS\Request::i()->id );
}
catch( \OutOfRangeException $e )
{
\IPS\Output::i()->error( 'nexus_no_subs_package', '2X379/2', 404, '' );
}
try
{
$subscription = \IPS\nexus\Subscription::loadByMemberAndPackage( \IPS\Member::loggedIn(), $package, FALSE );
}
catch( \OutOfRangeException $e )
{
\IPS\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 ) as $row )
{
if ( $row->can_reactivate )
{
$purchase = $row;
break;
}
}
if ( $purchase === NULL )
{
\IPS\Output::i()->error( 'nexus_sub_no_purchase', '2X379/9', 404, '' );
}
/* Set renewal terms */
$currency = $purchase->original_invoice->currency;
$purchase->renewals = $package->renewalTerm( $currency );
$purchase->cancelled = FALSE;
$purchase->save();
$purchase->member->log( 'purchase', array( 'type' => 'info', 'id' => $purchase->id, 'name' => $purchase->name, 'info' => 'change_renewals', 'to' => array( 'cost' => $purchase->renewals->cost->amount, 'currency' => $purchase->renewals->cost->currency, 'term' => $purchase->renewals->getTerm() ) ) );
/* Either send to renewal invoice or just back to subscriptions list */
if ( !$purchase->active and $purchase->canRenewUntil() !== FALSE )
{
\IPS\Output::i()->redirect( $purchase->url()->setQueryString( 'do', 'renew' )->csrf() );
}
else
{
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=subscriptions&controller=subscriptions', 'front', 'nexus_subscriptions' ) );
}
}
}