Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

@@ -12,37 +12,72 @@
namespace IPS\nexus\modules\front\clients;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateInterval;
use DomainException;
use Exception;
use IPS\core\AdminNotification;
use IPS\DateTime;
use IPS\Db;
use IPS\Dispatcher\Controller;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Number;
use IPS\Helpers\Form\Radio;
use IPS\Http\Url;
use IPS\Member;
use IPS\MFA\MFAHandler;
use IPS\nexus\Customer;
use IPS\nexus\Customer\AlternativeContact;
use IPS\nexus\extensions\nexus\Item\AccountCreditIncrease;
use IPS\nexus\Gateway;
use IPS\nexus\Invoice;
use IPS\nexus\Money;
use IPS\nexus\Payout;
use IPS\Output;
use IPS\Patterns\ActiveRecordIterator;
use IPS\Request;
use IPS\Session;
use IPS\Settings;
use IPS\Theme;
use OutOfRangeException;
use function count;
use function defined;
use function in_array;
use function intval;
use function is_countable;
use function is_null;
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;
}
/**
* Account Credit
*/
class _credit extends \IPS\Dispatcher\Controller
class credit extends Controller
{
/**
* Execute
*
* @return void
*/
public function execute()
public function execute() : void
{
if ( !\IPS\Member::loggedIn()->member_id )
if ( !Member::loggedIn()->member_id )
{
\IPS\Output::i()->error( 'no_module_permission_guest', '2X239/1', 403, '' );
Output::i()->error( 'no_module_permission_guest', '2X239/1', 403, '' );
}
\IPS\Output::i()->cssFiles = array_merge( \IPS\Output::i()->cssFiles, \IPS\Theme::i()->css( 'clients.css', 'nexus' ) );
\IPS\Output::i()->breadcrumb[] = array( \IPS\Http\Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ), \IPS\Member::loggedIn()->language()->addToStack('client_credit') );
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('client_credit');
\IPS\Output::i()->sidebar['enabled'] = FALSE;
Output::i()->cssFiles = array_merge( Output::i()->cssFiles, Theme::i()->css( 'clients.css', 'nexus' ) );
Output::i()->breadcrumb[] = array( Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ), Member::loggedIn()->language()->addToStack('client_credit') );
Output::i()->title = Member::loggedIn()->language()->addToStack('client_credit');
Output::i()->sidebar['enabled'] = FALSE;
if ( $output = \IPS\MFA\MFAHandler::accessToArea( 'nexus', 'AccountCredit', \IPS\Http\Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ) ) )
if ( $output = MFAHandler::accessToArea( 'nexus', 'AccountCredit', Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ) ) )
{
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('clients')->credit( NULL, array(), NULL, FALSE, FALSE ) . $output;
Output::i()->output = Theme::i()->getTemplate('clients')->credit( NULL, array(), NULL, FALSE, FALSE ) . $output;
return;
}
@@ -54,38 +89,38 @@ class _credit extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function manage()
protected function manage() : void
{
/* Get Balance(s) */
$balance = array_filter( \IPS\nexus\Customer::loggedIn()->cm_credits, function( $val )
$balance = array_filter( Customer::loggedIn()->cm_credits, function( $val )
{
return $val->amount;
} );
/* Pending Withdrawls */
$perPage = 10;
$page = isset( \IPS\Request::i()->page ) ? \intval( \IPS\Request::i()->page ) : 1;
$page = isset( Request::i()->page ) ? intval( Request::i()->page ) : 1;
if( $page < 1 )
{
$page = 1;
}
$pastWithdrawalsSelect = \IPS\Db::i()->select( '*', 'nexus_payouts', array( 'po_member=?', \IPS\nexus\Customer::loggedIn()->member_id ), 'po_date DESC', array( ( $page - 1 ) * $perPage, $perPage ) );
$pastWithdrawalsPagination = \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->pagination(
\IPS\Http\Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ),
ceil( \IPS\Db::i()->select( 'COUNT(*)', 'nexus_payouts', array( 'po_member=?', \IPS\nexus\Customer::loggedIn()->member_id ) )->first() / $perPage ),
$pastWithdrawalsSelect = Db::i()->select( '*', 'nexus_payouts', array( 'po_member=?', Customer::loggedIn()->member_id ), 'po_date DESC', array( ( $page - 1 ) * $perPage, $perPage ) );
$pastWithdrawalsPagination = Theme::i()->getTemplate( 'global', 'core', 'global' )->pagination(
Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ),
ceil( Db::i()->select( 'COUNT(*)', 'nexus_payouts', array( 'po_member=?', Customer::loggedIn()->member_id ) )->first() / $perPage ),
$page,
$perPage
);
$pastWithdrawals = new \IPS\Patterns\ActiveRecordIterator( $pastWithdrawalsSelect, 'IPS\nexus\Payout' );
$pastWithdrawals = new ActiveRecordIterator( $pastWithdrawalsSelect, 'IPS\nexus\Payout' );
$activePayouts = [];
if( !\IPS\Settings::i()->nexus_payout_unlimited_times )
if( !Settings::i()->nexus_payout_unlimited_times )
{
foreach ( $pastWithdrawals as $payout )
{
if ( $payout->status == \IPS\nexus\Payout::STATUS_PENDING )
if ( $payout->status == Payout::STATUS_PENDING )
{
$activePayouts[ $payout->currency ] = TRUE;
}
@@ -94,10 +129,10 @@ class _credit extends \IPS\Dispatcher\Controller
/* Can we withdraw? */
$canWithdraw = FALSE;
$withdrawOptions = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
if( \is_countable( $withdrawOptions ) AND \count( $withdrawOptions ) )
$withdrawOptions = json_decode( Settings::i()->nexus_payout, TRUE );
if( is_countable( $withdrawOptions ) AND count( $withdrawOptions ) )
{
foreach ( \IPS\nexus\Money::currencies() as $currency )
foreach ( Money::currencies() as $currency )
{
if ( isset( $balance[ $currency ] ) and $balance[ $currency ]->amount->isGreaterThanZero() and !isset( $activePayouts[ $currency ] ) )
{
@@ -108,22 +143,20 @@ class _credit extends \IPS\Dispatcher\Controller
/* Can we topup? */
$canTopup = FALSE;
if ( \IPS\Settings::i()->nexus_min_topup )
if ( Settings::i()->nexus_min_topup )
{
$minimumTopupAmounts = ( \IPS\Settings::i()->nexus_min_topup and \IPS\Settings::i()->nexus_min_topup !== '*' ) ? json_decode( \IPS\Settings::i()->nexus_min_topup, TRUE ) : NULL;
$maximumBalanceAmounts = ( \IPS\Settings::i()->nexus_max_credit and \IPS\Settings::i()->nexus_max_credit !== '*' ) ? json_decode( \IPS\Settings::i()->nexus_max_credit, TRUE ) : NULL;
$maximumBalanceAmounts = ( Settings::i()->nexus_max_credit and Settings::i()->nexus_max_credit !== '*' ) ? json_decode( Settings::i()->nexus_max_credit, TRUE ) : NULL;
$maximums = array();
foreach ( array_merge( array( \IPS\nexus\Customer::loggedIn() ), iterator_to_array( \IPS\nexus\Customer::loggedIn()->parentContacts( array('billing=1') ) ) ) as $account )
foreach ( array_merge( array( Customer::loggedIn() ), iterator_to_array( Customer::loggedIn()->parentContacts( array('billing=1') ) ) ) as $account )
{
if ( $account instanceof \IPS\nexus\Customer\AlternativeContact )
if ( $account instanceof AlternativeContact )
{
$account = $account->main_id;
}
foreach ( $account->cm_credits as $value )
{
if ( $maximumBalanceAmounts === NULL or $value->amount->compare( new \IPS\Math\Number( number_format( $maximumBalanceAmounts[ $value->currency ]['amount'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $value->currency ), '.', '' ) ) ) === -1 )
if ( $maximumBalanceAmounts === NULL or $value->amount->compare( new \IPS\Math\Number( number_format( $maximumBalanceAmounts[ $value->currency ]['amount'], Money::numberOfDecimalsForCurrency( $value->currency ), '.', '' ) ) ) === -1 )
{
$canTopup = TRUE;
}
@@ -132,7 +165,7 @@ class _credit extends \IPS\Dispatcher\Controller
}
/* Display */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate('clients')->credit( $balance, $pastWithdrawals, $pastWithdrawalsPagination, $canWithdraw, $canTopup );
Output::i()->output = Theme::i()->getTemplate('clients')->credit( $balance, $pastWithdrawals, $pastWithdrawalsPagination, $canWithdraw, $canTopup );
}
/**
@@ -140,47 +173,47 @@ class _credit extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function withdraw()
protected function withdraw() : void
{
$activeWithdrawals = \IPS\Settings::i()->nexus_payout_unlimited_times ? [] : iterator_to_array( \IPS\Db::i()->select( 'po_currency', 'nexus_payouts', array( 'po_status=? AND po_member=?', \IPS\nexus\Payout::STATUS_PENDING, \IPS\nexus\Customer::loggedIn()->member_id ) ) );
$activeWithdrawals = Settings::i()->nexus_payout_unlimited_times ? [] : iterator_to_array( Db::i()->select( 'po_currency', 'nexus_payouts', array( 'po_status=? AND po_member=?', Payout::STATUS_PENDING, Customer::loggedIn()->member_id ) ) );
$withdrawForm = NULL;
$withdrawOptions = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
$withdrawOptions = json_decode( Settings::i()->nexus_payout, TRUE );
if ( isset( $withdrawOptions['Stripe'] ) )
{
unset( $withdrawOptions['Stripe'] );
}
if ( \is_countable( $withdrawOptions ) AND \count( $withdrawOptions ) )
if ( is_countable( $withdrawOptions ) AND count( $withdrawOptions ) )
{
$withdrawForm = new \IPS\Helpers\Form( 'withdraw', 'withdraw_credit' );
$withdrawForm = new Form( 'withdraw', 'withdraw_credit' );
$minimumWithdrawalsAmounts = ( \IPS\Settings::i()->nexus_payout_min and \IPS\Settings::i()->nexus_payout_min !== '*' ) ? json_decode( \IPS\Settings::i()->nexus_payout_min, TRUE ) : NULL;
$maximumWithdrawalsAmounts = ( \IPS\Settings::i()->nexus_payout_max and \IPS\Settings::i()->nexus_payout_max !== '*' ) ? json_decode( \IPS\Settings::i()->nexus_payout_max, TRUE ) : NULL;
$minimumWithdrawalsAmounts = ( Settings::i()->nexus_payout_min and Settings::i()->nexus_payout_min !== '*' ) ? json_decode( Settings::i()->nexus_payout_min, TRUE ) : NULL;
$maximumWithdrawalsAmounts = ( Settings::i()->nexus_payout_max and Settings::i()->nexus_payout_max !== '*' ) ? json_decode( Settings::i()->nexus_payout_max, TRUE ) : NULL;
$balance = array_filter( \IPS\nexus\Customer::loggedIn()->cm_credits, function( $val )
$balance = array_filter( Customer::loggedIn()->cm_credits, function( $val )
{
return $val->amount;
} );
$currencyOptions = array();
if ( \count( $balance ) > 1 )
if ( count( $balance ) > 1 )
{
$currencyToggles = array();
foreach ( \IPS\nexus\Money::currencies() as $currency )
foreach ( Money::currencies() as $currency )
{
if ( isset( $balance[ $currency ] ) AND !\in_array( $currency, $activeWithdrawals ) )
if ( isset( $balance[ $currency ] ) AND !in_array( $currency, $activeWithdrawals ) )
{
$currencyOptions[ $currency ] = $currency;
$currencyToggles[ $currency ] = array( 'withdraw_amount_' . $currency );
}
}
$withdrawForm->add( new \IPS\Helpers\Form\Radio( 'withdraw_currency', NULL, TRUE, array( 'options' => $currencyOptions, 'toggles' => $currencyToggles ) ) );
$withdrawForm->add( new Radio( 'withdraw_currency', NULL, TRUE, array( 'options' => $currencyOptions, 'toggles' => $currencyToggles ) ) );
}
$canWithdraw = FALSE;
foreach ( \IPS\nexus\Money::currencies() as $currency )
foreach ( Money::currencies() as $currency )
{
if ( !isset( $balance[ $currency ] ) OR ( !\IPS\Settings::i()->nexus_payout_unlimited_times AND \in_array( $currency, $activeWithdrawals ) ) )
if ( !isset( $balance[ $currency ] ) OR ( !Settings::i()->nexus_payout_unlimited_times AND in_array( $currency, $activeWithdrawals ) ) )
{
continue;
}
@@ -197,13 +230,13 @@ class _credit extends \IPS\Dispatcher\Controller
$upperLimit = $maximumWithdrawalsAmounts[ $currency ];
/* Determine our date cutoff */
$periodRestrictions = json_decode( \IPS\Settings::i()->nexus_payout_max_period, TRUE );
$periodRestrictions = json_decode( Settings::i()->nexus_payout_max_period, TRUE );
$interval = 'P' . (int) $periodRestrictions[0] . ( mb_strtoupper( mb_substr( $periodRestrictions[1], 0, 1 ) ) );
$date = \IPS\DateTime::create()->sub( new \DateInterval( $interval ) )->getTimestamp();
$date = DateTime::create()->sub( new DateInterval( $interval ) )->getTimestamp();
/* Get our (non-cancelled) payout requests since the date cutoff */
foreach( \IPS\Db::i()->select( '*', 'nexus_payouts', array( 'po_member=? AND po_date>? AND po_status NOT IN(?) AND po_currency=?', \IPS\nexus\Customer::loggedIn()->member_id, $date, 'canc', $currency ) ) as $request )
foreach( Db::i()->select( '*', 'nexus_payouts', array( 'po_member=? AND po_date>? AND po_status NOT IN(?) AND po_currency=?', Customer::loggedIn()->member_id, $date, 'canc', $currency ) ) as $request )
{
$upperLimit -= $request['po_amount'];
}
@@ -217,17 +250,17 @@ class _credit extends \IPS\Dispatcher\Controller
if( $max <= 0 )
{
\IPS\Member::loggedIn()->language()->words[ 'withdraw_amount_' . $currency . '_desc' ] = \IPS\Member::loggedIn()->language()->addToStack( 'max_withdrawl_exceeded' );
Member::loggedIn()->language()->words[ 'withdraw_amount_' . $currency . '_desc' ] = Member::loggedIn()->language()->addToStack( 'max_withdrawl_exceeded' );
}
$field = new \IPS\Helpers\Form\Number( 'withdraw_amount_' . $currency, (string) $max, \count( $currencyOptions ) ? NULL : TRUE, array( 'min' => $min, 'max' => (string) $max, 'decimals' => \IPS\nexus\Money::numberOfDecimalsForCurrency( $currency ), 'disabled' => ( $max <= 0 ) ), \count( $currencyOptions ) ? NULL : function( $val ) use ( $currency )
$field = new Number( 'withdraw_amount_' . $currency, (string) $max, count( $currencyOptions ) ? NULL : TRUE, array( 'min' => $min, 'max' => (string) $max, 'decimals' => Money::numberOfDecimalsForCurrency( $currency ), 'disabled' => ( $max <= 0 ) ), count( $currencyOptions ) ? NULL : function( $val ) use ( $currency )
{
if ( !$val and \IPS\Request::i()->withdraw_currency === $currency )
if ( !$val and Request::i()->withdraw_currency === $currency )
{
throw new \DomainException('form_required');
throw new DomainException('form_required');
}
}, NULL, $currency, 'withdraw_amount_' . $currency );
$field->label = \IPS\Member::loggedIn()->language()->addToStack('withdraw_amount');
$field->label = Member::loggedIn()->language()->addToStack('withdraw_amount');
$withdrawForm->add( $field );
$canWithdraw = TRUE;
@@ -242,7 +275,9 @@ class _credit extends \IPS\Dispatcher\Controller
{
$options[ $k ] = $k === 'Manual' ? $settings['name'] : 'withdraw__' . $k;
$toggles[ $k ] = array();
$class = \IPS\nexus\Gateway::payoutGateways()[ $k ];
/* @var Payout $class */
$class = Gateway::payoutGateways()[ $k ];
foreach ( $class::form() as $field )
{
if ( !$field->htmlId )
@@ -254,9 +289,9 @@ class _credit extends \IPS\Dispatcher\Controller
}
}
if ( \is_countable( $withdrawOptions ) AND \count( $withdrawOptions ) > 1 )
if ( is_countable( $withdrawOptions ) AND count( $withdrawOptions ) > 1 )
{
$withdrawForm->add( new \IPS\Helpers\Form\Radio( 'withdraw_method', NULL, TRUE, array( 'options' => $options, 'toggles' => $toggles ) ) );
$withdrawForm->add( new Radio( 'withdraw_method', NULL, TRUE, array( 'options' => $options, 'toggles' => $toggles ) ) );
}
foreach ( $fields as $field )
@@ -266,55 +301,55 @@ class _credit extends \IPS\Dispatcher\Controller
if ( $values = $withdrawForm->values() )
{
$currencies = array_keys( $balance );
$currency = \count( $currencyOptions ) ? $values['withdraw_currency'] : array_pop( $currencies );
$currency = count( $currencyOptions ) ? $values['withdraw_currency'] : array_pop( $currencies );
$withdrawMethods = array_keys( $options );
if ( \count( $withdrawMethods ) === 1 )
if ( count( $withdrawMethods ) === 1 )
{
$key = array_pop( $withdrawMethods );
$class = \IPS\nexus\Gateway::payoutGateways()[ $key ];
$class = Gateway::payoutGateways()[ $key ];
}
else
{
$class = \IPS\nexus\Gateway::payoutGateways()[ $values['withdraw_method'] ];
$class = Gateway::payoutGateways()[ $values['withdraw_method'] ];
}
$payout = new $class;
$payout->amount = new \IPS\nexus\Money( $values[ 'withdraw_amount_' . $currency ], $currency );
$payout->member = \IPS\nexus\Customer::loggedIn();
$payout->amount = new Money( $values[ 'withdraw_amount_' . $currency ], $currency );
$payout->member = Customer::loggedIn();
try
{
$payout->data = $payout->getData( $values );
}
catch ( \DomainException $e )
catch ( DomainException $e )
{
\IPS\Output::i()->error( $e->getMessage(), '1X239/2', 403, '' );
Output::i()->error( $e->getMessage(), '1X239/2', 403, '' );
}
$payout->ip = \IPS\Request::i()->ipAddress();
$payout->ip = Request::i()->ipAddress();
$payout->save();
$credits = \IPS\nexus\Customer::loggedIn()->cm_credits;
$credits[ $currency ]->amount = $credits[ $currency ]->amount->subtract( new \IPS\Math\Number( number_format( $values[ 'withdraw_amount_' . $currency ], \IPS\nexus\Money::numberOfDecimalsForCurrency( $currency ), '.', '' ) ) );
\IPS\nexus\Customer::loggedIn()->cm_credits = $credits;
\IPS\nexus\Customer::loggedIn()->save();
$credits = Customer::loggedIn()->cm_credits;
$credits[ $currency ]->amount = $credits[ $currency ]->amount->subtract( new \IPS\Math\Number( number_format( $values[ 'withdraw_amount_' . $currency ], Money::numberOfDecimalsForCurrency( $currency ), '.', '' ) ) );
Customer::loggedIn()->cm_credits = $credits;
Customer::loggedIn()->save();
if ( !\IPS\Settings::i()->nexus_payout_approve and !$payout::$requiresApproval )
if ( !Settings::i()->nexus_payout_approve and !$payout::$requiresApproval )
{
try
{
$payout->process();
\IPS\nexus\Customer::loggedIn()->log( 'payout', array( 'type' => 'autoprocess', 'amount' => $values[ 'withdraw_amount_' . $currency ], 'currency' => $currency, 'payout_id' => $payout->id ) );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=clients&controller=credit&withdraw=success', 'front', 'clientscredit' ) );
Customer::loggedIn()->log( 'payout', array( 'type' => 'autoprocess', 'amount' => $values[ 'withdraw_amount_' . $currency ], 'currency' => $currency, 'payout_id' => $payout->id ) );
Output::i()->redirect( Url::internal( 'app=nexus&module=clients&controller=credit&withdraw=success', 'front', 'clientscredit' ) );
}
catch ( \Exception $e ) {}
catch ( Exception ) {}
}
else
{
\IPS\core\AdminNotification::send( 'nexus', 'Withdrawal', NULL, TRUE, $payout );
AdminNotification::send( 'nexus', 'Withdrawal', NULL, TRUE, $payout );
}
\IPS\nexus\Customer::loggedIn()->log( 'payout', array( 'type' => 'request', 'amount' => $values[ 'withdraw_amount_' . $currency ], 'currency' => $currency, 'payout_id' => $payout->id ) );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=clients&controller=credit&withdraw=pending', 'front', 'clientscredit' ) );
Customer::loggedIn()->log( 'payout', array( 'type' => 'request', 'amount' => $values[ 'withdraw_amount_' . $currency ], 'currency' => $currency, 'payout_id' => $payout->id ) );
Output::i()->redirect( Url::internal( 'app=nexus&module=clients&controller=credit&withdraw=pending', 'front', 'clientscredit' ) );
}
}
else
@@ -325,10 +360,10 @@ class _credit extends \IPS\Dispatcher\Controller
if ( !$withdrawForm )
{
\IPS\Output::i()->error( 'no_module_permission', '2X239/3', 403, '' );
Output::i()->error( 'no_module_permission', '2X239/3', 403, '' );
}
\IPS\Output::i()->output = $withdrawForm->customTemplate( array( \IPS\Theme::i()->getTemplate( 'forms', 'core' ), 'popupTemplate' ) );
Output::i()->output = $withdrawForm->customTemplate( array( Theme::i()->getTemplate( 'forms', 'core' ), 'popupTemplate' ) );
}
/**
@@ -336,54 +371,55 @@ class _credit extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function topup()
protected function topup() : void
{
$topupForm = NULL;
if ( \IPS\Settings::i()->nexus_min_topup )
if ( Settings::i()->nexus_min_topup )
{
$minimumTopupAmounts = ( \IPS\Settings::i()->nexus_min_topup and \IPS\Settings::i()->nexus_min_topup !== '*' ) ? json_decode( \IPS\Settings::i()->nexus_min_topup, TRUE ) : NULL;
$maximumBalanceAmounts = ( \IPS\Settings::i()->nexus_max_credit and \IPS\Settings::i()->nexus_max_credit !== '*' ) ? json_decode( \IPS\Settings::i()->nexus_max_credit, TRUE ) : NULL;
$minimumTopupAmounts = ( Settings::i()->nexus_min_topup and Settings::i()->nexus_min_topup !== '*' ) ? json_decode( Settings::i()->nexus_min_topup, TRUE ) : NULL;
$maximumBalanceAmounts = ( Settings::i()->nexus_max_credit and Settings::i()->nexus_max_credit !== '*' ) ? json_decode( Settings::i()->nexus_max_credit, TRUE ) : NULL;
$maximums = array();
foreach ( array_merge( array( \IPS\nexus\Customer::loggedIn() ), iterator_to_array( \IPS\nexus\Customer::loggedIn()->parentContacts( array('billing=1') ) ) ) as $account )
foreach ( array_merge( array( Customer::loggedIn() ), iterator_to_array( Customer::loggedIn()->parentContacts( array('billing=1') ) ) ) as $account )
{
if ( $account instanceof \IPS\nexus\Customer\AlternativeContact )
if ( $account instanceof AlternativeContact )
{
$account = $account->main_id;
}
foreach ( $account->cm_credits as $value )
{
$max = $maximumBalanceAmounts ? ( ( new \IPS\Math\Number( number_format( $maximumBalanceAmounts[ $value->currency ]['amount'], \IPS\nexus\Money::numberOfDecimalsForCurrency( $value->currency ), '.', '' ) ) )->subtract( $value->amount ) ) : NULL;
if ( \is_null( $max ) or $max->isGreaterThanZero() )
$max = $maximumBalanceAmounts ? ( ( new \IPS\Math\Number( number_format( $maximumBalanceAmounts[ $value->currency ]['amount'], Money::numberOfDecimalsForCurrency( $value->currency ), '.', '' ) ) )->subtract( $value->amount ) ) : NULL;
if ( is_null( $max ) or $max->isGreaterThanZero() )
{
$maximums[ $account->member_id ][ $value->currency ] = \is_null( $max ) ? NULL : (string) $max;
$maximums[ $account->member_id ][ $value->currency ] = is_null( $max ) ? NULL : (string) $max;
}
}
}
if ( \count( $maximums ) )
if ( count( $maximums ) )
{
$topupForm = new \IPS\Helpers\Form( 'topup', 'checkout' );
$topupForm = new Form( 'topup', 'checkout' );
$accountOptions = array();
$accountOptionToggles = array();
foreach ( $maximums as $accountId => $data )
{
$currency = NULL;
if ( \count( $data ) === 1 )
if ( count( $data ) === 1 )
{
$currencies = array_keys( $data );
$currency = array_pop( $currencies );
}
$accountOptions[ $accountId ] = $accountId === \IPS\nexus\Customer::loggedIn()->id ? \IPS\nexus\Customer::loggedIn()->language()->addToStack( 'my_account', FALSE, array( 'sprintf' => array( \IPS\nexus\Customer::loggedIn()->cm_name ) ) ) : \IPS\nexus\Customer::load( $accountId )->cm_name;
$accountOptionToggles[ $accountId ] = ( \count( $data ) > 1 ) ? array( 'topup_currency_' . $accountId ) : array( 'topup_amount_' . $accountId . '_' . $currency );
$accountOptions[ $accountId ] = $accountId === Customer::loggedIn()->id ? Customer::loggedIn()->language()->addToStack( 'my_account', FALSE, array( 'sprintf' => array( Customer::loggedIn()->cm_name ) ) ) : Customer::load( $accountId )->cm_name;
$accountOptionToggles[ $accountId ] = ( count( $data ) > 1 ) ? array( 'topup_currency_' . $accountId ) : array( 'topup_amount_' . $accountId . '_' . $currency );
}
$topupForm->add( new \IPS\Helpers\Form\Radio( 'topup_account', NULL, TRUE, array( 'options' => $accountOptions, 'parse' => 'normal', 'toggles' => $accountOptionToggles ) ) );
$topupForm->add( new Radio( 'topup_account', NULL, TRUE, array( 'options' => $accountOptions, 'parse' => 'normal', 'toggles' => $accountOptionToggles ) ) );
foreach ( $maximums as $accountId => $data )
{
if ( \count( $data ) > 1 )
if ( count( $data ) > 1 )
{
$currencyToggles = array();
foreach ( $data as $currency => $maximum )
@@ -391,8 +427,8 @@ class _credit extends \IPS\Dispatcher\Controller
$currencyToggles[ $currency ] = array( 'topup_amount_' . $accountId . '_' . $currency );
}
$field = new \IPS\Helpers\Form\Radio( 'topup_currency_' . $accountId, NULL, NULL, array( 'options' => array_combine( array_keys( $data ), array_keys( $data ) ), 'toggles' => $currencyToggles ), NULL, NULL, NULL, 'topup_currency_' . $accountId );
$field->label = \IPS\Member::loggedIn()->language()->addToStack( 'topup_currency' );
$field = new Radio( 'topup_currency_' . $accountId, NULL, NULL, array( 'options' => array_combine( array_keys( $data ), array_keys( $data ) ), 'toggles' => $currencyToggles ), NULL, NULL, NULL, 'topup_currency_' . $accountId );
$field->label = Member::loggedIn()->language()->addToStack( 'topup_currency' );
$topupForm->add( $field );
}
@@ -400,29 +436,29 @@ class _credit extends \IPS\Dispatcher\Controller
{
$min = $minimumTopupAmounts ? ( $minimumTopupAmounts[ $currency ]['amount'] ) : 0.01;
$field = new \IPS\Helpers\Form\Number( 'topup_amount_' . $accountId . '_' . $currency, NULL, NULL, array( 'decimals' => \IPS\nexus\Money::numberOfDecimalsForCurrency( $currency ) ), function( $val ) use ( $currency, $accountOptions, $accountId, $data, $min, $maximum )
$field = new Number( 'topup_amount_' . $accountId . '_' . $currency, NULL, NULL, array( 'decimals' => Money::numberOfDecimalsForCurrency( $currency ) ), function( $val ) use ( $currency, $accountOptions, $accountId, $data, $min, $maximum )
{
if ( \count( $accountOptions ) === 1 or \IPS\Request::i()->topup_account == $accountId )
if ( count( $accountOptions ) === 1 or Request::i()->topup_account == $accountId )
{
$key = "topup_currency_{$accountId}";
if ( \count( $data ) === 1 or \IPS\Request::i()->$key == $currency )
if ( count( $data ) === 1 or Request::i()->$key == $currency )
{
if ( !$val )
{
throw new \DomainException('form_required');
throw new DomainException('form_required');
}
elseif ( !\is_null( $min ) and $val < $min )
elseif ( !is_null( $min ) and $val < $min )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_number_min', FALSE, array( 'sprintf' => array( $min ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack( 'form_number_min', FALSE, array( 'sprintf' => array( $min ) ) ) );
}
elseif ( $maximum and $val > $maximum )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_number_max', FALSE, array( 'sprintf' => array( $maximum ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack( 'form_number_max', FALSE, array( 'sprintf' => array( $maximum ) ) ) );
}
}
}
}, NULL, $currency, 'topup_amount_' . $accountId . '_' . $currency );
$field->label = \IPS\Member::loggedIn()->language()->addToStack('topup_amount');
$field->label = Member::loggedIn()->language()->addToStack('topup_amount');
$topupForm->add( $field );
}
}
@@ -430,7 +466,7 @@ class _credit extends \IPS\Dispatcher\Controller
if ( $values = $topupForm->values() )
{
$account = isset( $values['topup_account'] ) ? \IPS\nexus\Customer::load( $values['topup_account'] ) : \IPS\nexus\Customer::loggedIn();
$account = isset( $values['topup_account'] ) ? Customer::load( $values['topup_account'] ) : Customer::loggedIn();
if ( isset( $values[ 'topup_currency_' . $account->member_id ] ) )
{
$currency = $values[ 'topup_currency_' . $account->member_id ];
@@ -441,22 +477,22 @@ class _credit extends \IPS\Dispatcher\Controller
$currency = array_pop( $currencies );
}
$invoice = new \IPS\nexus\Invoice;
$invoice = new Invoice;
$invoice->member = $account;
$invoice->currency = $currency;
$invoice->return_uri = 'app=nexus&module=clients&controller=credit';
$invoice->addItem( new \IPS\nexus\extensions\nexus\Item\AccountCreditIncrease( \IPS\nexus\Customer::loggedIn()->language()->get('account_credit'), new \IPS\nexus\Money( $values["topup_amount_{$account->member_id}_{$currency}"], $currency ) ) );
\IPS\Output::i()->redirect( $invoice->checkoutUrl() );
$invoice->addItem( new AccountCreditIncrease( Customer::loggedIn()->language()->get('account_credit'), new Money( $values["topup_amount_{$account->member_id}_{$currency}"], $currency ) ) );
Output::i()->redirect( $invoice->checkoutUrl() );
}
}
}
if ( !$topupForm )
{
\IPS\Output::i()->error( 'no_module_permission', '2X239/4', 403, '' );
Output::i()->error( 'no_module_permission', '2X239/4', 403, '' );
}
\IPS\Output::i()->output = $topupForm->customTemplate( array( \IPS\Theme::i()->getTemplate( 'forms', 'core' ), 'popupTemplate' ) );
Output::i()->output = $topupForm->customTemplate( array( Theme::i()->getTemplate( 'forms', 'core' ), 'popupTemplate' ) );
}
/**
@@ -464,14 +500,14 @@ class _credit extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function cancel()
protected function cancel() : void
{
\IPS\Session::i()->csrfCheck();
Session::i()->csrfCheck();
try
{
$payout = \IPS\nexus\Payout::load( \IPS\Request::i()->id );
if ( $payout->member->member_id === \IPS\nexus\Customer::loggedIn()->member_id and $payout->status === $payout::STATUS_PENDING )
$payout = Payout::load( Request::i()->id );
if ( $payout->member->member_id === Customer::loggedIn()->member_id and $payout->status === $payout::STATUS_PENDING )
{
$payout->status = $payout::STATUS_CANCELED;
$payout->save();
@@ -481,11 +517,11 @@ class _credit extends \IPS\Dispatcher\Controller
$payout->member->cm_credits = $credits;
$payout->member->save();
\IPS\nexus\Customer::loggedIn()->log( 'payout', array( 'type' => 'cancel', 'amount' => $payout->amount->amount, 'currency' => $payout->amount->currency, 'payout_id' => $payout->id ) );
Customer::loggedIn()->log( 'payout', array( 'type' => 'cancel', 'amount' => $payout->amount->amount, 'currency' => $payout->amount->currency, 'payout_id' => $payout->id ) );
}
}
catch ( \OutOfRangeException $e ) { }
catch ( OutOfRangeException ) { }
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ) );
Output::i()->redirect( Url::internal( 'app=nexus&module=clients&controller=credit', 'front', 'clientscredit' ) );
}
}