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,39 @@
|
||||
namespace IPS\nexus\tasks;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DateInterval;
|
||||
use Exception;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Db\Select;
|
||||
use IPS\Math\Number;
|
||||
use IPS\nexus\Customer;
|
||||
use IPS\nexus\Gateway;
|
||||
use IPS\nexus\Invoice;
|
||||
use IPS\nexus\Invoice\Item\Renewal;
|
||||
use IPS\nexus\Money;
|
||||
use IPS\nexus\Tax;
|
||||
use IPS\nexus\Transaction;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use IPS\Settings;
|
||||
use IPS\Task;
|
||||
use OutOfRangeException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
use function is_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Renewal Invoices Task
|
||||
*/
|
||||
class _generateRenewalInvoices extends \IPS\Task
|
||||
class generateRenewalInvoices extends Task
|
||||
{
|
||||
/**
|
||||
* Execute
|
||||
@@ -31,25 +54,25 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
* If an error occurs which means the task could not finish running, throw an \IPS\Task\Exception - do not log an error as a normal log.
|
||||
* Tasks should execute within the time of a normal HTTP request.
|
||||
*
|
||||
* @return mixed Message to log or NULL
|
||||
* @throws \IPS\Task\Exception
|
||||
* @return string|null Message to log or NULL
|
||||
* @throws Task\Exception
|
||||
*/
|
||||
public function execute()
|
||||
public function execute() : string|null
|
||||
{
|
||||
/* Get purchases grouped by member and currency */
|
||||
$select = $this->_getSelectQuery();
|
||||
$log = \IPS\Db::_replaceBinds( $select->query, $select->binds ) . "\n" . \count( $select ) . " matches\n\n";
|
||||
$availableTaxes = \IPS\nexus\Tax::roots();
|
||||
$log = Db::_replaceBinds( $select->query, $select->binds ) . "\n" . count( $select ) . " matches\n\n";
|
||||
$availableTaxes = Tax::roots();
|
||||
|
||||
$groupedPurchases = array();
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
|
||||
foreach ( new ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
|
||||
{
|
||||
/* If the member does not exist, we should not lock the task */
|
||||
try
|
||||
{
|
||||
$groupedPurchases[ $purchase->member->member_id ][ $purchase->renewal_currency ][ $purchase->id ] = $purchase;
|
||||
}
|
||||
catch( \OutOfRangeException $e )
|
||||
catch( OutOfRangeException )
|
||||
{
|
||||
/* Set the purchase inactive so we don't try again. */
|
||||
$purchase->active = 0;
|
||||
@@ -60,13 +83,13 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
/* Loop */
|
||||
foreach ( $groupedPurchases as $memberId => $currencies )
|
||||
{
|
||||
$member = \IPS\nexus\Customer::load( $memberId );
|
||||
$member = Customer::load( $memberId );
|
||||
foreach ( $currencies as $currency => $purchases )
|
||||
{
|
||||
$log .= "Member {$memberId}, {$currency}: " . \count( $purchases ) . " purchase(s) to be renewed: " . implode( ', ', array_keys( $purchases ) ) . ". ";
|
||||
$log .= "Member {$memberId}, {$currency}: " . count( $purchases ) . " purchase(s) to be renewed: " . implode( ', ', array_keys( $purchases ) ) . ". ";
|
||||
|
||||
/* Create Invoice */
|
||||
$invoice = new \IPS\nexus\Invoice;
|
||||
$invoice = new Invoice;
|
||||
$invoice->system = TRUE;
|
||||
$invoice->currency = $currency;
|
||||
$invoice->member = $member;
|
||||
@@ -91,7 +114,7 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
}
|
||||
|
||||
/* Continue to next invoice if no items left */
|
||||
if( !\count( $items ) )
|
||||
if( !count( $items ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -99,14 +122,14 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
/* Add items to invoice */
|
||||
foreach( $items as $item )
|
||||
{
|
||||
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $item ) );
|
||||
$invoice->addItem( Renewal::create( $item ) );
|
||||
}
|
||||
$invoice->save();
|
||||
$log .= "Invoice {$invoice->id} generated... ";
|
||||
|
||||
/* Try to take payment automatically, but *only* if we have a billing address (i.e. the customer has a primary billing address set)
|
||||
otherwise we don't know how we're taxing this and the customer will need to manually come and pay it - we can skip this if tax has not been configured */
|
||||
if ( $invoice->billaddress OR \count( $availableTaxes ) === 0 )
|
||||
if ( $invoice->billaddress OR count( $availableTaxes ) === 0 )
|
||||
{
|
||||
/* Nothing to pay? */
|
||||
if ( $invoice->amountToPay()->amount->isZero() )
|
||||
@@ -130,14 +153,14 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
{
|
||||
$take = NULL;
|
||||
/* If credit is equal or larger than invoice value */
|
||||
if ( \in_array( $credit->compare( $invoice->total->amount ), [ 0, 1 ] ) )
|
||||
if ( in_array( $credit->compare( $invoice->total->amount ), [ 0, 1 ] ) )
|
||||
{
|
||||
$take = $invoice->total->amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Only use credit if amount remaining is greater than card gateway min amount */
|
||||
if( $invoice->total->amount->subtract( $credit ) > new \IPS\Math\Number( '0.50' ) )
|
||||
if( $invoice->total->amount->subtract( $credit ) > new Number( '0.50' ) )
|
||||
{
|
||||
$take = $credit;
|
||||
}
|
||||
@@ -147,10 +170,10 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
{
|
||||
$log .= "{$credit} account credit available... ";
|
||||
|
||||
$transaction = new \IPS\nexus\Transaction;
|
||||
$transaction = new Transaction;
|
||||
$transaction->member = $member;
|
||||
$transaction->invoice = $invoice;
|
||||
$transaction->amount = new \IPS\nexus\Money( $take, $currency );
|
||||
$transaction->amount = new Money( $take, $currency );
|
||||
$transaction->extra = array('automatic' => TRUE);
|
||||
$transaction->save();
|
||||
$transaction->approve();
|
||||
@@ -159,7 +182,7 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
|
||||
$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,
|
||||
@@ -183,85 +206,45 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
$allowedPaymentMethods = array();
|
||||
foreach( $invoice->items as $item )
|
||||
{
|
||||
if( \is_array( $item->paymentMethodIds ) and !\in_array( '*', $item->paymentMethodIds ) )
|
||||
if( is_array( $item->paymentMethodIds ) and !in_array( '*', $item->paymentMethodIds ) )
|
||||
{
|
||||
$allowedPaymentMethods = array_merge( $allowedPaymentMethods, $item->paymentMethodIds );
|
||||
}
|
||||
}
|
||||
|
||||
$cardWhere = array(
|
||||
array( 'card_member=?', $member->member_id )
|
||||
);
|
||||
if( \count( $allowedPaymentMethods ) )
|
||||
{
|
||||
$cardWhere[] = array( \IPS\Db::i()->in( 'card_method', $allowedPaymentMethods ) );
|
||||
}
|
||||
/* Check all available gateways */
|
||||
if( empty( $allowedPaymentMethods ) )
|
||||
{
|
||||
foreach( Gateway::roots() as $gateway )
|
||||
{
|
||||
$allowedPaymentMethods[] = $gateway->_id;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', $cardWhere ), 'IPS\nexus\Customer\CreditCard' ) as $card )
|
||||
/* Loop through each payment method and try to take payment */
|
||||
foreach( $allowedPaymentMethods as $paymentMethodId )
|
||||
{
|
||||
$log .= "Attempting card {$card->id}... ";
|
||||
|
||||
try
|
||||
{
|
||||
$cardDetails = $card->card; // We're just checking this doesn't throw an exception
|
||||
|
||||
$amountToPay = $invoice->amountToPay();
|
||||
$gateway = $card->method;
|
||||
|
||||
$transaction = new \IPS\nexus\Transaction;
|
||||
$transaction->member = $member;
|
||||
$transaction->invoice = $invoice;
|
||||
$transaction->method = $gateway;
|
||||
$transaction->amount = $amountToPay;
|
||||
$transaction->currency = $currency;
|
||||
$transaction->extra = array( 'automatic' => TRUE );
|
||||
|
||||
try
|
||||
$gateway = Gateway::load( $paymentMethodId );
|
||||
if( $gateway::SUPPORTS_AUTOPAY )
|
||||
{
|
||||
$transaction->auth = $gateway->auth( $transaction, array(
|
||||
( $gateway->id . '_card' ) => $card
|
||||
), NULL, array(), 'renewal' );
|
||||
$transaction->capture();
|
||||
|
||||
$transaction->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_PAID,
|
||||
'id' => $transaction->id,
|
||||
'invoice_id' => $invoice->id,
|
||||
'invoice_title' => $invoice->title,
|
||||
'automatic' => TRUE,
|
||||
), FALSE );
|
||||
|
||||
$transaction->approve();
|
||||
|
||||
$log .= "Transaction {$transaction->id} approved! ";
|
||||
|
||||
break;
|
||||
foreach( $gateway->autopay( $invoice ) as $transaction )
|
||||
{
|
||||
if( $transaction->status == Transaction::STATUS_REFUSED )
|
||||
{
|
||||
$log .= "Transaction {$transaction->id} failed. ";
|
||||
}
|
||||
else
|
||||
{
|
||||
$log .= "Transaction {$transaction->id} approved! ";
|
||||
}
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
$transaction->status = \IPS\nexus\Transaction::STATUS_REFUSED;
|
||||
$extra = $transaction->extra;
|
||||
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'noteRaw' => $e->getMessage() );
|
||||
$transaction->extra = $extra;
|
||||
$transaction->save();
|
||||
|
||||
$log .= "Transaction {$transaction->id} failed. ";
|
||||
|
||||
$transaction->member->log( 'transaction', array(
|
||||
'type' => 'paid',
|
||||
'status' => \IPS\nexus\Transaction::STATUS_REFUSED,
|
||||
'id' => $transaction->id,
|
||||
'invoice_id' => $invoice->id,
|
||||
'invoice_title' => $invoice->title,
|
||||
'automatic' => TRUE,
|
||||
), FALSE );
|
||||
}
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
}
|
||||
// error with card, move on
|
||||
catch ( \Exception $e ){}
|
||||
catch( OutOfRangeException ){}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -294,7 +277,7 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function cleanup()
|
||||
public function cleanup() : void
|
||||
{
|
||||
|
||||
}
|
||||
@@ -302,19 +285,19 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
/**
|
||||
* Get Purchases Query
|
||||
*
|
||||
* @return \IPS\Db\Select
|
||||
* @throws \Exception
|
||||
* @return Select
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _getSelectQuery(): \IPS\Db\Select
|
||||
protected function _getSelectQuery(): Select
|
||||
{
|
||||
/* What's out cutoff? */
|
||||
$renewalDate = \IPS\DateTime::create();
|
||||
if( \IPS\Settings::i()->cm_invoice_generate )
|
||||
$renewalDate = DateTime::create();
|
||||
if( Settings::i()->cm_invoice_generate )
|
||||
{
|
||||
$renewalDate->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_generate . 'H' ) );
|
||||
$renewalDate->add( new DateInterval( 'PT' . Settings::i()->cm_invoice_generate . 'H' ) );
|
||||
}
|
||||
|
||||
return \IPS\Db::i()->select( 'ps.*', [ 'nexus_purchases', 'ps' ],
|
||||
return Db::i()->select( 'ps.*', [ 'nexus_purchases', 'ps' ],
|
||||
[
|
||||
"ps_cancelled=0 AND ps_renewals>0 AND ps_invoice_pending=0 AND ps_active=1 AND ps_expire>0 AND ps_expire<? AND (ps_billing_agreement IS NULL OR ba.ba_canceled=1) AND ( ps_grouped_renewals='' OR ps_grouped_renewals IS NULL )",
|
||||
$renewalDate->getTimestamp()
|
||||
|
||||
Reference in new issue
Block a user