Version 4.1.19
This commit is contained in:
1 parent
28bd025b35
commit
2bd5025018
2674 files changed
+233518
-77766
No files matched your search
@@ -2,10 +2,10 @@
|
||||
/**
|
||||
* @brief Generate Renewal Invoices Task
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @subpackage nexus
|
||||
* @package IPS Community Suite
|
||||
* @subpackage Nexus
|
||||
* @since 01 Apr 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -44,14 +44,24 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
}
|
||||
/* 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<?',
|
||||
"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 );
|
||||
|
||||
$groupedPurchases = array();
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( $select, 'IPS\nexus\Purchase' ) as $purchase )
|
||||
{
|
||||
$groupedPurchases[ $purchase->member->member_id ][ $purchase->renewal_currency ][ $purchase->id ] = $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 )
|
||||
{
|
||||
/* Set the purchase inactive so we don't try again. */
|
||||
$purchase->active = 0;
|
||||
$purchase->save();
|
||||
}
|
||||
}
|
||||
|
||||
/* Loop */
|
||||
@@ -72,7 +82,7 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
$invoice->save();
|
||||
|
||||
/* Nothing to pay? */
|
||||
if ( $invoice->amountToPay()->amount === (float) 0 )
|
||||
if ( $invoice->amountToPay()->amount->isZero() )
|
||||
{
|
||||
$extra = $invoice->status_extra;
|
||||
$extra['type'] = 'zero';
|
||||
@@ -84,45 +94,57 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
$credits = $member->cm_credits;
|
||||
$credit = $credits[$currency]->amount;
|
||||
if( $credit > 0 )
|
||||
if ( isset( $credits[ $currency ] ) )
|
||||
{
|
||||
$take = $credit > $invoice->total->amount ? $invoice->total->amount : $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 -= $take;
|
||||
$member->cm_credits = $credits;
|
||||
$member->save();
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Charge to card */
|
||||
if ( $invoice->status !== $invoice::STATUS_PAID )
|
||||
{
|
||||
try
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) ), 'IPS\nexus\Customer\CreditCard' ) as $card )
|
||||
{
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'nexus_customer_cards', array( 'card_member=?', $member->member_id ) ), 'IPS\nexus\Customer\CreditCard' ) as $card )
|
||||
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;
|
||||
@@ -130,14 +152,14 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
$transaction->amount = $amountToPay;
|
||||
$transaction->currency = $currency;
|
||||
$transaction->extra = array( 'automatic' => TRUE );
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
$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,
|
||||
@@ -146,17 +168,34 @@ class _generateRenewalInvoices extends \IPS\Task
|
||||
'invoice_title' => $invoice->title,
|
||||
'automatic' => TRUE,
|
||||
), FALSE );
|
||||
|
||||
|
||||
$transaction->approve();
|
||||
|
||||
break;
|
||||
}
|
||||
catch ( \Exception $e ){}
|
||||
|
||||
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,
|
||||
), FALSE );
|
||||
}
|
||||
|
||||
$invoice->status = $transaction->invoice->status;
|
||||
}
|
||||
// credit card doesn't exist anymore , move on
|
||||
catch ( \OutOfRangeException $e ){}
|
||||
}
|
||||
// credit card doesn't exist anymore , move on
|
||||
catch ( \OutOfRangeException $e ){}
|
||||
|
||||
}
|
||||
|
||||
/* Update the purchase */
|
||||
|
||||
Reference in new issue
Block a user