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,16 +12,43 @@
|
||||
namespace IPS\nexus\Customer;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DomainException;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Customer;
|
||||
use IPS\nexus\Gateway;
|
||||
use IPS\nexus\Invoice;
|
||||
use IPS\nexus\Invoice\Item\Renewal;
|
||||
use IPS\nexus\Transaction;
|
||||
use IPS\Patterns\ActiveRecord;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use OutOfRangeException;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Billing Agreement Model
|
||||
*
|
||||
* @method status() : string
|
||||
* @method nextPaymentDate() : DateTime
|
||||
* @method doSuspend()
|
||||
* @method doReactivate()
|
||||
* @method doCancel()
|
||||
* @method latestUnclaimedTransaction() : Transaction
|
||||
* @method term() : RenewalTerm
|
||||
*/
|
||||
class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
class BillingAgreement extends ActiveRecord
|
||||
{
|
||||
/**
|
||||
* @brief Billing Agreement is active and will charge automatically
|
||||
@@ -41,28 +68,28 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* @brief Multiton Store
|
||||
*/
|
||||
protected static $multitons;
|
||||
protected static array $multitons;
|
||||
|
||||
/**
|
||||
* @brief Database Table
|
||||
*/
|
||||
public static $databaseTable = 'nexus_billing_agreements';
|
||||
public static ?string $databaseTable = 'nexus_billing_agreements';
|
||||
|
||||
/**
|
||||
* @brief Database Prefix
|
||||
*/
|
||||
public static $databasePrefix = 'ba_';
|
||||
public static string $databasePrefix = 'ba_';
|
||||
|
||||
/**
|
||||
* Construct ActiveRecord from database row
|
||||
*
|
||||
* @param array $data Row from database table
|
||||
* @param bool $updateMultitonStoreIfExists Replace current object in multiton store if it already exists there?
|
||||
* @return static
|
||||
* @param array $data Row from database table
|
||||
* @param bool $updateMultitonStoreIfExists Replace current object in multiton store if it already exists there?
|
||||
* @return static
|
||||
*/
|
||||
public static function constructFromData( $data, $updateMultitonStoreIfExists = TRUE )
|
||||
public static function constructFromData( array $data, bool $updateMultitonStoreIfExists = TRUE ): static
|
||||
{
|
||||
$gateway = \IPS\nexus\Gateway::load( $data['ba_method'] );
|
||||
$gateway = Gateway::load( $data['ba_method'] );
|
||||
$classname = 'IPS\nexus\Gateway\\' . $gateway->gateway . '\\BillingAgreement';
|
||||
|
||||
/* Initiate an object */
|
||||
@@ -74,7 +101,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
{
|
||||
if( static::$databasePrefix AND mb_strpos( $k, static::$databasePrefix ) === 0 )
|
||||
{
|
||||
$k = \substr( $k, \strlen( static::$databasePrefix ) );
|
||||
$k = substr( $k, strlen( static::$databasePrefix ) );
|
||||
}
|
||||
|
||||
$obj->_data[ $k ] = $v;
|
||||
@@ -94,17 +121,17 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Load and check permissions
|
||||
*
|
||||
* @param mixed $id ID to load
|
||||
* @return \IPS\Content\Item
|
||||
* @throws \OutOfRangeException
|
||||
* @param int $id ID to load
|
||||
* @return static
|
||||
* @throws OutOfRangeException
|
||||
*/
|
||||
public static function loadAndCheckPerms( $id )
|
||||
public static function loadAndCheckPerms( int $id ) : static
|
||||
{
|
||||
$obj = static::load( $id );
|
||||
|
||||
if ( !$obj->canView() )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
throw new OutOfRangeException;
|
||||
}
|
||||
|
||||
return $obj;
|
||||
@@ -113,12 +140,12 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Member can view?
|
||||
*
|
||||
* @param \IPS\Member|NULL $member The member to check for, or NULL for currently logged in member
|
||||
* @param Member|NULL $member The member to check for, or NULL for currently logged in member
|
||||
* @return bool
|
||||
*/
|
||||
public function canView( \IPS\Member $member = NULL )
|
||||
public function canView( Member $member = null ) : bool
|
||||
{
|
||||
$member = $member ?: \IPS\Member::loggedIn();
|
||||
$member = $member ?: Member::loggedIn();
|
||||
|
||||
return $this->member->member_id === $member->member_id or array_key_exists( $member->member_id, iterator_to_array( $this->member->alternativeContacts( array( 'billing=1' ) ) ) );
|
||||
}
|
||||
@@ -126,20 +153,20 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Get member
|
||||
*
|
||||
* @return \IPS\Member
|
||||
* @return Customer
|
||||
*/
|
||||
public function get_member()
|
||||
public function get_member() : Customer
|
||||
{
|
||||
return \IPS\nexus\Customer::load( $this->_data['member'] );
|
||||
return Customer::load( $this->_data['member'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set member
|
||||
*
|
||||
* @param \IPS\Member $member Member
|
||||
* @param Member $member Member
|
||||
* @return void
|
||||
*/
|
||||
public function set_member( \IPS\Member $member )
|
||||
public function set_member( Member $member ) : void
|
||||
{
|
||||
$this->_data['member'] = $member->member_id;
|
||||
}
|
||||
@@ -147,20 +174,20 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Get payment gateway
|
||||
*
|
||||
* @return \IPS\nexus\Gateway
|
||||
* @return Gateway
|
||||
*/
|
||||
public function get_method()
|
||||
public function get_method() : Gateway
|
||||
{
|
||||
return \IPS\nexus\Gateway::load( $this->_data['method'] );
|
||||
return Gateway::load( $this->_data['method'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set payment gateway
|
||||
*
|
||||
* @param \IPS\nexus\Gateway $gateway Payment gateway
|
||||
* @param Gateway $gateway Payment gateway
|
||||
* @return void
|
||||
*/
|
||||
public function set_method( \IPS\nexus\Gateway $gateway )
|
||||
public function set_method( Gateway $gateway ) : void
|
||||
{
|
||||
$this->_data['method'] = $gateway->id;
|
||||
}
|
||||
@@ -168,41 +195,41 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Get start date
|
||||
*
|
||||
* @return \IPS\DateTime
|
||||
* @return DateTime
|
||||
*/
|
||||
public function get_started()
|
||||
public function get_started() : DateTime
|
||||
{
|
||||
return \IPS\DateTime::ts( $this->_data['started'] );
|
||||
return DateTime::ts( $this->_data['started'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set next payment date
|
||||
*
|
||||
* @param \IPS\DateTime $date The invoice date
|
||||
* @param DateTime|null $date The invoice date
|
||||
* @return void
|
||||
*/
|
||||
public function set_next_cycle( \IPS\DateTime $date = NULL )
|
||||
public function set_next_cycle( ?DateTime $date = NULL ) : void
|
||||
{
|
||||
$this->_data['next_cycle'] = $date ? $date->getTimestamp() : NULL;
|
||||
$this->_data['next_cycle'] = $date?->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next payment date
|
||||
*
|
||||
* @return \IPS\DateTime
|
||||
* @return DateTime|null
|
||||
*/
|
||||
public function get_next_cycle()
|
||||
public function get_next_cycle() : DateTime|null
|
||||
{
|
||||
return $this->_data['next_cycle'] ? \IPS\DateTime::ts( $this->_data['next_cycle'] ) : NULL;
|
||||
return $this->_data['next_cycle'] ? DateTime::ts( $this->_data['next_cycle'] ) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set start date
|
||||
*
|
||||
* @param \IPS\DateTime $date The invoice date
|
||||
* @param DateTime $date The invoice date
|
||||
* @return void
|
||||
*/
|
||||
public function set_started( \IPS\DateTime $date )
|
||||
public function set_started( DateTime $date ) : void
|
||||
{
|
||||
$this->_data['started'] = $date->getTimestamp();
|
||||
}
|
||||
@@ -211,9 +238,9 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
* Suspend
|
||||
*
|
||||
* @return void
|
||||
* @throws \DomainException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function refresh()
|
||||
public function refresh() : void
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -222,7 +249,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
$this->checkForLatestTransaction();
|
||||
}
|
||||
}
|
||||
catch( \OutOfRangeException | \UnderflowException $e )
|
||||
catch( OutOfRangeException | UnderflowException )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -232,9 +259,9 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
* Suspend
|
||||
*
|
||||
* @return void
|
||||
* @throws \DomainException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function suspend()
|
||||
public function suspend() : void
|
||||
{
|
||||
$this->doSuspend();
|
||||
|
||||
@@ -247,9 +274,9 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
* Reactivate
|
||||
*
|
||||
* @return void
|
||||
* @throws \DomainException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function reactivate()
|
||||
public function reactivate() : void
|
||||
{
|
||||
$this->doReactivate();
|
||||
|
||||
@@ -262,9 +289,9 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
* Cancel
|
||||
*
|
||||
* @return void
|
||||
* @throws \DomainException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function cancel()
|
||||
public function cancel() : void
|
||||
{
|
||||
$this->doCancel();
|
||||
|
||||
@@ -277,21 +304,21 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Front-End URL
|
||||
*
|
||||
* @return \IPS\Http\Url
|
||||
* @return Url
|
||||
*/
|
||||
public function url()
|
||||
function url(): Url
|
||||
{
|
||||
return \IPS\Http\Url::internal( "app=nexus&module=clients&controller=billingagreements&do=view&id={$this->id}", 'front', 'clientsbillingagreement' );
|
||||
return Url::internal( "app=nexus&module=clients&controller=billingagreements&do=view&id={$this->id}", 'front', 'clientsbillingagreement' );
|
||||
}
|
||||
|
||||
/**
|
||||
* ACP URL
|
||||
*
|
||||
* @return \IPS\Http\Url
|
||||
* @return Url
|
||||
*/
|
||||
public function acpUrl()
|
||||
public function acpUrl() : Url
|
||||
{
|
||||
return \IPS\Http\Url::internal( "app=nexus&module=payments&controller=billingagreements&id={$this->id}", 'admin' );
|
||||
return Url::internal( "app=nexus&module=payments&controller=billingagreements&id={$this->id}", 'admin' );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -299,7 +326,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function checkForLatestTransaction()
|
||||
public function checkForLatestTransaction() : void
|
||||
{
|
||||
/* Fetch the latest unclaimed transaction */
|
||||
try
|
||||
@@ -307,11 +334,11 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
$transaction = $this->latestUnclaimedTransaction();
|
||||
}
|
||||
/* If there isn't one yet, it might just be that PayPal hasn't taken it yet. Let it try for up to 11 days before giving up, which is how long PayPal will try for if there's an issue. */
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException $e )
|
||||
{
|
||||
if ( $this->next_cycle->getTimestamp() > ( time() - ( 86400 * 11 ) ) )
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -323,7 +350,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
$this->next_cycle = $nextPaymentDate;
|
||||
$this->save();
|
||||
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
@@ -331,17 +358,17 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
}
|
||||
|
||||
/* Get purchases */
|
||||
$purchases = new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_purchases', array( 'ps_billing_agreement=?', $this->id ) ), 'IPS\nexus\Purchase' );
|
||||
$purchases = new ActiveRecordIterator( Db::i()->select( '*', 'nexus_purchases', array( 'ps_billing_agreement=?', $this->id ) ), 'IPS\nexus\Purchase' );
|
||||
|
||||
/* Generate an invoice */
|
||||
$invoice = new \IPS\nexus\Invoice;
|
||||
$invoice = new Invoice;
|
||||
$invoice->system = TRUE;
|
||||
$invoice->currency = $transaction->amount->currency;
|
||||
$invoice->member = $this->member;
|
||||
$invoice->billaddress = $this->member->primaryBillingAddress();
|
||||
foreach ( $purchases as $purchase )
|
||||
{
|
||||
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $purchase ) );
|
||||
$invoice->addItem( Renewal::create( $purchase ) );
|
||||
}
|
||||
$invoice->save();
|
||||
|
||||
@@ -354,7 +381,7 @@ class _BillingAgreement extends \IPS\Patterns\ActiveRecord
|
||||
/* Log */
|
||||
$invoice->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_PAID,
|
||||
'status' => Transaction::STATUS_PAID,
|
||||
'id' => $transaction->id,
|
||||
'invoice_id' => $invoice->id,
|
||||
'invoice_title' => $invoice->title,
|
||||
|
||||
Reference in new issue
Block a user