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,50 +12,74 @@
|
||||
namespace IPS\nexus\Invoice\Item;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DateInterval;
|
||||
use Exception;
|
||||
use IPS\Application;
|
||||
use IPS\DateTime;
|
||||
use IPS\File;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Coupon;
|
||||
use IPS\nexus\Customer;
|
||||
use IPS\nexus\Invoice;
|
||||
use IPS\nexus\Invoice\Item;
|
||||
use IPS\nexus\Money;
|
||||
use IPS\nexus\Purchase;
|
||||
use IPS\nexus\Purchase\RenewalTerm;
|
||||
use IPS\nexus\Transaction;
|
||||
use OutOfRangeException;
|
||||
use function defined;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice Item Class for Renewals
|
||||
*/
|
||||
class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
class Renewal extends Item
|
||||
{
|
||||
/**
|
||||
* @brief Act (new/charge)
|
||||
*/
|
||||
public static $act = 'renewal';
|
||||
public static string $act = 'renewal';
|
||||
|
||||
/**
|
||||
* @brief Icon
|
||||
*/
|
||||
public static $icon = 'refresh';
|
||||
public static string $icon = 'arrows-rotate';
|
||||
|
||||
/**
|
||||
* @brief Title
|
||||
*/
|
||||
public static $title = 'renewal';
|
||||
public static string $title = 'renewal';
|
||||
|
||||
/**
|
||||
* @brief Requires login to purchase?
|
||||
*/
|
||||
public static $requiresAccount = TRUE;
|
||||
public static bool $requiresAccount = TRUE;
|
||||
|
||||
/**
|
||||
* @brief New expiry date (NULL will cause automatic calculation)
|
||||
*/
|
||||
public $expire = NULL;
|
||||
public mixed $expire = NULL;
|
||||
|
||||
/**
|
||||
* @brief Expiry date (NULL will cause automatic calculation)
|
||||
*/
|
||||
public mixed $expireDate = NULL;
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* @param \IPS\nexus\Purchase $purchase The purchase to renew
|
||||
* @param int $cycles The number of cycles to renew for
|
||||
* @return void
|
||||
* @param Purchase $purchase The purchase to renew
|
||||
* @param int $cycles The number of cycles to renew for
|
||||
* @return static
|
||||
*/
|
||||
public static function create( \IPS\nexus\Purchase $purchase, $cycles = 1 )
|
||||
public static function create( Purchase $purchase, int $cycles = 1 ): static
|
||||
{
|
||||
$obj = new static( sprintf( $purchase->member->language()->get('renew_title'), $purchase->name ), $purchase->renewals->cost );
|
||||
$obj->tax = $purchase->renewals->tax;
|
||||
@@ -78,23 +102,75 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to override the static property, if necessary
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canUseAccountCredit() : bool
|
||||
{
|
||||
/* Account credits can only be used for renewals if the original item
|
||||
is allowed to use credit */
|
||||
if( $extension = $this->getPurchaseExtension() )
|
||||
{
|
||||
return $extension->canUseAccountCredit();
|
||||
}
|
||||
|
||||
return static::$canUseAccountCredit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to override the static property, if necessary
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canUseCoupons() : bool
|
||||
{
|
||||
/* Coupons can only be used on renewals for packages that allow coupons */
|
||||
if( $extension = $this->getPurchaseExtension() )
|
||||
{
|
||||
return $extension->canUseCoupons();
|
||||
}
|
||||
|
||||
return static::$canUseCoupons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the coupon can be applied to this item
|
||||
*
|
||||
* @param array|string $data
|
||||
* @param Invoice $invoice
|
||||
* @param Customer $customer
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid( array|string $data, Invoice $invoice, Customer $customer ) : bool
|
||||
{
|
||||
/* Coupons can only be used on renewals for packages that allow coupons */
|
||||
if( $extension = $this->getPurchaseExtension() )
|
||||
{
|
||||
return $extension->isValid( $data, $invoice, $customer );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* On Paid
|
||||
*
|
||||
* @param \IPS\nexus\Invoice $invoice The invoice
|
||||
* @return void
|
||||
* @param Invoice $invoice The invoice
|
||||
* @return void
|
||||
*/
|
||||
public function onPaid( \IPS\nexus\Invoice $invoice )
|
||||
public function onPaid( Invoice $invoice ): void
|
||||
{
|
||||
$purchase = \IPS\nexus\Purchase::load( $this->id );
|
||||
$purchase = Purchase::load( $this->id );
|
||||
|
||||
if ( $purchase->renewals and $interval = $purchase->renewals->interval and !$purchase->cancelled and $expire = $purchase->expire )
|
||||
{
|
||||
$_expire = clone $expire;
|
||||
if ( $_expire->add( new \DateInterval( 'PT' . $purchase->grace_period . 'S' ) )->getTimestamp() < time() )
|
||||
if ( $_expire->add( new DateInterval( 'PT' . $purchase->grace_period . 'S' ) )->getTimestamp() < time() )
|
||||
{
|
||||
$expire = new \IPS\DateTime;
|
||||
$expire = new DateTime;
|
||||
}
|
||||
for ( $i=0; $i<$this->quantity; $i++ )
|
||||
{
|
||||
@@ -105,7 +181,7 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
$purchase->invoice_pending = NULL;
|
||||
|
||||
$billingAgreement = NULL;
|
||||
foreach ( $invoice->transactions( array( \IPS\nexus\Transaction::STATUS_PAID, \IPS\nexus\Transaction::STATUS_PART_REFUNDED ), array( array( 't_billing_agreement IS NOT NULL' ) ) ) as $transaction )
|
||||
foreach ( $invoice->transactions( array( Transaction::STATUS_PAID, Transaction::STATUS_PART_REFUNDED ), array( array( 't_billing_agreement IS NOT NULL' ) ) ) as $transaction )
|
||||
{
|
||||
$billingAgreement = $transaction->billing_agreement;
|
||||
}
|
||||
@@ -115,7 +191,7 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
}
|
||||
|
||||
$purchase->save();
|
||||
$purchase->onRenew( $this->quantity );
|
||||
$purchase->onRenew($this->quantity);
|
||||
|
||||
$purchase->member->log( 'purchase', array( 'type' => 'renew', 'id' => $purchase->id, 'name' => $purchase->name, 'invoice_id' => $invoice->id, 'invoice_title' => $invoice->title ) );
|
||||
}
|
||||
@@ -124,18 +200,18 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
/**
|
||||
* On Unpaid description
|
||||
*
|
||||
* @param \IPS\nexus\Invoice $invoice The invoice
|
||||
* @param Invoice $invoice The invoice
|
||||
* @return array
|
||||
*/
|
||||
public function onUnpaidDescription( \IPS\nexus\Invoice $invoice )
|
||||
public function onUnpaidDescription( Invoice $invoice ): array
|
||||
{
|
||||
$return = parent::onUnpaidDescription( $invoice );
|
||||
|
||||
try
|
||||
{
|
||||
$purchase = \IPS\nexus\Purchase::load( $this->id );
|
||||
$purchase = Purchase::load( $this->id );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
return $return;
|
||||
}
|
||||
@@ -147,7 +223,7 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
$expire->sub( $interval );
|
||||
}
|
||||
|
||||
$return[] = \IPS\Member::loggedIn()->language()->addToStack( 'renewal_unpaid', FALSE, array( 'sprintf' => array( $purchase->name, $purchase->id, $expire->localeDate() ) ) );
|
||||
$return[] = Member::loggedIn()->language()->addToStack( 'renewal_unpaid', FALSE, array( 'sprintf' => array( $purchase->name, $purchase->id, $expire->localeDate() ) ) );
|
||||
}
|
||||
|
||||
return $return;
|
||||
@@ -156,17 +232,17 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
/**
|
||||
* On Unpaid
|
||||
*
|
||||
* @param \IPS\nexus\Invoice $invoice The invoice
|
||||
* @param Invoice $invoice The invoice
|
||||
* @param string $status Status
|
||||
* @return void
|
||||
* @return void
|
||||
*/
|
||||
public function onUnpaid( \IPS\nexus\Invoice $invoice, $status )
|
||||
public function onUnpaid( Invoice $invoice, string $status ): void
|
||||
{
|
||||
try
|
||||
{
|
||||
$purchase = \IPS\nexus\Purchase::load( $this->id );
|
||||
$purchase = Purchase::load( $this->id );
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -188,15 +264,15 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
/**
|
||||
* Client Area URL
|
||||
*
|
||||
* @return |IPS\Http\Url|NULL
|
||||
* @return Url|string|null
|
||||
*/
|
||||
public function url()
|
||||
function url(): Url|string|null
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\nexus\Purchase::load( $this->id )->url();
|
||||
return Purchase::load( $this->id )->url();
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -205,15 +281,15 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
/**
|
||||
* ACP URL
|
||||
*
|
||||
* @return |IPS\Http\Url|NULL
|
||||
* @return Url|null
|
||||
*/
|
||||
public function acpUrl()
|
||||
public function acpUrl(): Url|null
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\nexus\Purchase::load( $this->id )->acpUrl();
|
||||
return Purchase::load( $this->id )->acpUrl();
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -222,15 +298,15 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
/**
|
||||
* Image
|
||||
*
|
||||
* @return |IPS\File|NULL
|
||||
* @return File|null
|
||||
*/
|
||||
public function image()
|
||||
public function image(): File|null
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\nexus\Purchase::load( $this->id )->image();
|
||||
return Purchase::load( $this->id )->image();
|
||||
}
|
||||
catch ( \OutOfRangeException $e )
|
||||
catch ( OutOfRangeException )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -239,17 +315,44 @@ class _Renewal extends \IPS\nexus\Invoice\Item
|
||||
/**
|
||||
* Utility method to get the purchase. Useful if we are unsure if the purchase exists, or has been removed due to a refund of the original transaction.
|
||||
*
|
||||
* @return \IPS\nexus\Purchase|NULL
|
||||
* @return Purchase|NULL
|
||||
*/
|
||||
public function getPurchase()
|
||||
public function getPurchase() : Purchase|null
|
||||
{
|
||||
try
|
||||
{
|
||||
return \IPS\nexus\Purchase::load( $this->id );
|
||||
return Purchase::load( $this->id );
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
public function getPurchaseExtension() : Item|null
|
||||
{
|
||||
if( $purchase = $this->getPurchase() )
|
||||
{
|
||||
/* Copy the code to load the extension. It's a protected method and I don't really want to
|
||||
expose it */
|
||||
try
|
||||
{
|
||||
foreach ( Application::load( $purchase->app )->extensions( 'nexus', 'Item', FALSE ) as $ext )
|
||||
{
|
||||
if ( $ext::$type == $purchase->type )
|
||||
{
|
||||
$price = new Money( "0", $purchase->member->defaultCurrency() );
|
||||
if( $purchase->renewals instanceof RenewalTerm )
|
||||
{
|
||||
$price = $purchase->renewals->cost;
|
||||
}
|
||||
return new $ext( $purchase->name, $price );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( Exception ){}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user