Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -12,7 +12,7 @@
|
||||
namespace IPS\nexus\tasks;
|
||||
|
||||
/* 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;
|
||||
@@ -36,16 +36,19 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
/* What's out cutoff? */
|
||||
$renewalDate = \IPS\DateTime::create();
|
||||
if( \IPS\Settings::i()->cm_invoice_generate )
|
||||
{
|
||||
$renewalDate->add( new \DateInterval( 'P' . \IPS\Settings::i()->cm_invoice_generate . 'D' ) );
|
||||
}
|
||||
$renewalDate->add( new \DateInterval( 'PT' . \IPS\Settings::i()->cm_invoice_generate . 'H' ) );
|
||||
}
|
||||
|
||||
/* Get purchases grouped by member and currency */
|
||||
$select = \IPS\Db::i()->select( '*', 'nexus_purchases', array(
|
||||
"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 AND ( ps_grouped_renewals='' OR ps_grouped_renewals IS NULL )",
|
||||
"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 AND ( ps_grouped_renewals='' OR ps_grouped_renewals IS NULL )",
|
||||
$renewalDate->getTimestamp()
|
||||
), 'ps_member', 50 );
|
||||
$log = \IPS\Db::_replaceBinds( $select->query, $select->binds ) . "\n" . \count( $select ) . " matches\n\n";
|
||||
|
||||
$groupedPurchases = array();
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
|
||||
@@ -68,14 +71,17 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
{
|
||||
$member = \IPS\nexus\Customer::load( $memberId );
|
||||
foreach ( $currencies as $currency => $purchases )
|
||||
{
|
||||
{
|
||||
$log .= "Member {$memberId}, {$currency}: " . \count( $purchases ) . " purchase(s) to be renewed: " . implode( ', ', array_keys( $purchases ) ) . ". ";
|
||||
|
||||
/* Create Invoice */
|
||||
$invoice = new \IPS\nexus\Invoice;
|
||||
$invoice->system = TRUE;
|
||||
$invoice->currency = $currency;
|
||||
$invoice->member = $member;
|
||||
$invoice->billaddress = $member->primaryBillingAddress();
|
||||
$items = array();
|
||||
|
||||
|
||||
foreach ( $purchases as $purchase )
|
||||
{
|
||||
/* Check the renewal is valid */
|
||||
@@ -86,6 +92,7 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
}
|
||||
|
||||
/* Remove renewals for this purchase */
|
||||
$log .= "Purchase {$purchase->id} cannot be renewed. ";
|
||||
$purchase->renewals = NULL;
|
||||
$purchase->member->log( 'purchase', array( 'type' => 'info', 'id' => $purchase->id, 'name' => $purchase->name, 'info' => 'remove_renewals' ) );
|
||||
$purchase->can_reactivate = TRUE;
|
||||
@@ -93,7 +100,7 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
}
|
||||
|
||||
/* Continue to next invoice if no items left */
|
||||
if( !count( $items ) )
|
||||
if( !\count( $items ) )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -103,129 +110,146 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
{
|
||||
$invoice->addItem( \IPS\nexus\Invoice\Item\Renewal::create( $item ) );
|
||||
}
|
||||
|
||||
$invoice->save();
|
||||
|
||||
/* Nothing to pay? */
|
||||
if ( $invoice->amountToPay()->amount->isZero() )
|
||||
$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 */
|
||||
if ( $invoice->billaddress )
|
||||
{
|
||||
$extra = $invoice->status_extra;
|
||||
$extra['type'] = 'zero';
|
||||
$invoice->status_extra = $extra;
|
||||
$invoice->markPaid();
|
||||
}
|
||||
|
||||
/* Charge what we can to account credit */
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
$credits = $member->cm_credits;
|
||||
if ( isset( $credits[ $currency ] ) )
|
||||
/* Nothing to pay? */
|
||||
if ( $invoice->amountToPay()->amount->isZero() )
|
||||
{
|
||||
$credit = $credits[$currency]->amount;
|
||||
if( $credit->isGreaterThanZero() )
|
||||
{
|
||||
if ( $credit->compare( $invoice->total->amount ) === 1 )
|
||||
{
|
||||
$take = $invoice->total->amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
$take = $credit;
|
||||
}
|
||||
|
||||
$transaction = new \IPS\nexus\Transaction;
|
||||
$transaction->member = $member;
|
||||
$transaction->invoice = $invoice;
|
||||
$transaction->amount = new \IPS\nexus\Money( $take, $currency );
|
||||
$transaction->extra = array('automatic' => TRUE);
|
||||
$transaction->save();
|
||||
$transaction->approve();
|
||||
|
||||
$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 );
|
||||
|
||||
$credits[$currency]->amount = $credits[$currency]->amount->subtract( $take );
|
||||
$member->cm_credits = $credits;
|
||||
$member->save();
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
}
|
||||
$log .= "Nothing to pay!";
|
||||
|
||||
$extra = $invoice->status_extra;
|
||||
$extra['type'] = 'zero';
|
||||
$invoice->status_extra = $extra;
|
||||
$invoice->markPaid();
|
||||
}
|
||||
}
|
||||
/* Charge to card */
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) ), 'IPS\nexus\Customer\CreditCard' ) as $card )
|
||||
|
||||
/* Charge what we can to account credit */
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
try
|
||||
$credits = $member->cm_credits;
|
||||
if ( isset( $credits[ $currency ] ) )
|
||||
{
|
||||
$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
|
||||
$credit = $credits[$currency]->amount;
|
||||
if( $credit->isGreaterThanZero() )
|
||||
{
|
||||
$transaction->auth = $gateway->auth( $transaction, array(
|
||||
( $gateway->id . '_card' ) => $card
|
||||
) );
|
||||
$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 );
|
||||
|
||||
$log .= "{$credit} account credit available... ";
|
||||
|
||||
if ( $credit->compare( $invoice->total->amount ) === 1 )
|
||||
{
|
||||
$take = $invoice->total->amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
$take = $credit;
|
||||
}
|
||||
|
||||
$transaction = new \IPS\nexus\Transaction;
|
||||
$transaction->member = $member;
|
||||
$transaction->invoice = $invoice;
|
||||
$transaction->amount = new \IPS\nexus\Money( $take, $currency );
|
||||
$transaction->extra = array('automatic' => TRUE);
|
||||
$transaction->save();
|
||||
$transaction->approve();
|
||||
|
||||
break;
|
||||
}
|
||||
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();
|
||||
|
||||
$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,
|
||||
$log .= "Transaction {$transaction->id} generated... ";
|
||||
|
||||
$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 );
|
||||
|
||||
$credits[$currency]->amount = $credits[$currency]->amount->subtract( $take );
|
||||
$member->cm_credits = $credits;
|
||||
$member->save();
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
}
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
}
|
||||
// error with card, move on
|
||||
catch ( \Exception $e ){}
|
||||
}
|
||||
/* Charge to card */
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) ), 'IPS\nexus\Customer\CreditCard' ) as $card )
|
||||
{
|
||||
$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
|
||||
{
|
||||
$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;
|
||||
}
|
||||
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 ){}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Update the purchase */
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
{
|
||||
foreach ( $purchases as $purchase )
|
||||
{
|
||||
$purchase->invoice_pending = $invoice;
|
||||
@@ -235,8 +259,11 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
|
||||
/* Send notification */
|
||||
$invoice->sendNotification();
|
||||
$log .= "Final status: {$invoice->status}\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $log;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user