Version 4.1.19

This commit is contained in:
Neo committed 2025-12-19 05:38:56 -08:00
1 parent 28bd025b35
commit 2bd5025018
2674 files changed
+233518 -77766

No files matched your search

@@ -2,9 +2,9 @@
/**
* @brief Transaction Model
* @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
* @package IPS Community Suite
* @subpackage Nexus
* @since 10 Feb 2014
* @version SVN_VERSION_NUMBER
@@ -30,8 +30,9 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
const STATUS_HELD = 'hold'; // Transaction is being held for approval
const STATUS_REVIEW = 'revw'; // Transaction, after being held for approval, has been flagged for review by staff
const STATUS_REFUSED = 'fail'; // Transaction was refused
const STATUS_REFUNDED = 'rfnd'; // Transaction has been refunded in fulll
const STATUS_REFUNDED = 'rfnd'; // Transaction has been refunded in full
const STATUS_PART_REFUNDED = 'prfd'; // Transaction has been partially refunded
const STATUS_GATEWAY_PENDING= 'gwpd'; // The gateway is processing the transaction
/**
* @brief Multiton Store
@@ -94,8 +95,6 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
*/
public static function table( $where = array(), \IPS\Http\Url $url, $ref = 't' )
{
$where[] = array( 't_status<>?', static::STATUS_PENDING );
/* Create the table */
$table = new \IPS\Helpers\Table\Db( 'nexus_transactions', $url, $where );
$table->sortBy = $table->sortBy ?: 't_date';
@@ -185,7 +184,14 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
*/
public function get_member()
{
return \IPS\nexus\Customer::load( $this->_data['member'] );
try
{
return \IPS\nexus\Customer::load( $this->_data['member'] );
}
catch( \OutOfRangeException $e )
{
return NULL;
}
}
/**
@@ -202,11 +208,19 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
/**
* Get invoice
*
* @return \IPS\nexus\Invoice
* @return \IPS\nexus\Invoice|NULL
*/
public function get_invoice()
{
return \IPS\nexus\Invoice::load( $this->_data['invoice'] );
/* If an invoice is deleted, then the transaction will remain present, which then can result in uncaught exception errors. */
try
{
return \IPS\nexus\Invoice::load( $this->_data['invoice'] );
}
catch( \OutOfRangeException $e )
{
return NULL;
}
}
/**
@@ -261,7 +275,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
public function get_amount()
{
$amount = new \IPS\nexus\Money( $this->_data['amount'], $this->_data['currency'] );
return $amount->round(); // A transaction has to be a valid monetary amount, eg not $1.005
return $amount;
}
/**
@@ -272,7 +286,6 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
*/
public function set_amount( \IPS\nexus\Money $amount )
{
$amount = $amount->round(); // A transaction has to be a valid monetary amount, eg not $1.005
$this->_data['amount'] = $amount->amount;
$this->_data['currency'] = $amount->currency;
}
@@ -318,7 +331,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
{
$this->_data['extra'] = json_encode( $extra );
}
/**
* Get MaxMind data
*
@@ -411,13 +424,33 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
}
/**
* Check fraud rules and capture
* Get billing agreement
*
* @return \IPS\nexus\Customer\BillingAgreement|NULL
*/
public function get_billing_agreement()
{
return $this->_data['billing_agreement'] ? \IPS\nexus\Customer\BillingAgreement::load( $this->_data['billing_agreement'] ) : NULL;
}
/**
* Set billing agreement
*
* @param \IPS\nexus\Customer\BillingAgreement|NULL $billingAgreement The billing agreement
* @return void
*/
public function set_billing_agreement( \IPS\nexus\Customer\BillingAgreement $billingAgreement = NULL )
{
$this->_data['billing_agreement'] = $billingAgreement === NULL ? NULL : $billingAgreement->id;
}
/**
* Run Anti-Fraud Checks and return status for transaction
*
* @param \IPS\nexus\Fraud\MaxMind\Request|NULL $maxMind *If* MaxMind is enabled, the request object will be passed here so gateway can additional data before request is made
* @return void
* @throws \LogicException
* @return string
*/
public function checkFraudRulesAndCapture( $maxMind=NULL )
public function runFraudCheck( $maxMind=NULL )
{
/* Run MaxMind */
if ( \IPS\Settings::i()->maxmind_key )
@@ -428,26 +461,27 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$maxMind->setTransaction( $this );
}
$this->fraud = $maxMind->request();
/* If MaxMind fails, stop here */
if ( $this->fraud->err and \IPS\Settings::i()->maxmind_error == 'hold' )
try
{
$this->status = static::STATUS_HELD;
$extra = $this->extra;
$extra['history'][] = array( 's' => static::STATUS_HELD );
$this->extra = $extra;
$this->save();
$maxMindResponse = $maxMind->request();
$this->member->log( 'transaction', array(
'type' => 'paid',
'status' => $this->status,
'id' => $this->id,
'invoice_id' => $this->invoice->id,
'invoice_title' => $this->invoice->title,
) );
$this->fraud = $maxMindResponse;
$this->save();
return;
/* If MaxMind fails, stop here */
if ( $this->fraud->error() and \IPS\Settings::i()->maxmind_error == 'hold' )
{
return static::STATUS_HELD;
}
}
catch ( \Exception $e )
{
\IPS\Log::log( $e, 'maxmind_error' );
if ( \IPS\Settings::i()->maxmind_error == 'hold' )
{
return static::STATUS_HELD;
}
}
}
@@ -457,28 +491,86 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
if ( $rule->matches( $this ) )
{
$this->fraud_blocked = $rule;
$this->save();
return $rule->action;
}
}
if ( $this->fraud_blocked and $this->fraud_blocked->action !== static::STATUS_PAID )
/* Still here? No fraud rule matches so we can proceed with the transaction */
return static::STATUS_PAID;
}
/**
* Check fraud rules and capture
*
* @param \IPS\nexus\Fraud\MaxMind\Request|NULL $maxMind *If* MaxMind is enabled, the request object will be passed here so gateway can additional data before request is made
* @return void
* @throws \LogicException
*/
public function checkFraudRulesAndCapture( $maxMind=NULL )
{
/* Check fraud rules */
$fraudResult = $this->runFraudCheck( $maxMind );
if ( $fraudResult )
{
$this->status = $this->fraud_blocked->action;
$extra = $this->extra;
$extra['history'][] = array( 's' => $this->fraud_blocked->action );
$this->extra = $extra;
$this->save();
$this->executeFraudAction( $fraudResult, TRUE );
}
/* If we're not being fraud blocked, we can capture and approve */
if ( $fraudResult === static::STATUS_PAID )
{
$this->captureAndApprove();
}
}
/**
* Perform fraud rule action
*
* @param string $fraudResult Status as returned by runFraudCheck()
* @param bool $isApproved Has the payment already been approved? If so and the fraus rule wants to refuse, we will void
* @return void
* @throws \LogicException
*/
public function executeFraudAction( $fraudResult, $isApproved=TRUE )
{
/* If the fraud rule wants to hold or refuse... */
if ( $fraudResult !== static::STATUS_PAID )
{
/* If it wants to refuse, void the payment */
if ( $isApproved and $fraudResult === static::STATUS_REFUSED )
{
$this->method->void( $this );
}
/* Set the status */
$this->status = $fraudResult;
$extra = $this->extra;
$extra['history'][] = array( 's' => $fraudResult );
$this->extra = $extra;
/* Log */
$this->member->log( 'transaction', array(
'type' => 'paid',
'status' => $this->status,
'status' => $fraudResult,
'id' => $this->id,
'invoice_id' => $this->invoice->id,
'invoice_title' => $this->invoice->title,
) );
return;
}
/* Save */
$this->save();
}
/**
* Capture and approve
*
* @return void
* @throws \LogicException
*/
public function captureAndApprove()
{
$this->capture();
$this->member->log( 'transaction', array(
@@ -528,12 +620,41 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$this->save();
/* Mark the invoice paid if necessary */
if ( $this->invoice->amountToPay()->amount <= 0 )
if ( !$this->invoice->amountToPay()->amount->isGreaterThanZero() )
{
$this->invoice->markPaid();
}
}
/**
* Void
*
* @return void
* @throws \Exception
*/
public function void()
{
/* Void it */
$this->method->void( $this );
/* Update transaction */
$extra = $this->extra;
$extra['history'][] = array( 's' => \IPS\nexus\Transaction::STATUS_REFUSED, 'on' => time(), 'by' => \IPS\Member::loggedIn()->member_id );
$this->extra = $extra;
$this->status = \IPS\nexus\Transaction::STATUS_REFUSED;
$this->save();
/* Log it */
if ( $this->member )
{
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => \IPS\nexus\Transaction::STATUS_REFUSED,
'id' => $this->id
) );
}
}
/**
* Refund
*
@@ -545,7 +666,14 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
public function refund( $refundMethod='gateway', $amount=NULL )
{
/* What's the amount? */
if ( !$amount or $amount == $this->amount->amount )
if ( $amount )
{
if ( !( $amount instanceof \IPS\Math\Number ) )
{
$amount = new \IPS\Math\Number( number_format( $amount, \IPS\nexus\Money::numberOfDecimalsForCurrency( $this->amount->currency ), '.', '' ) );
}
}
if ( !$amount or $this->amount->amount->compare( $amount ) === 0 )
{
$amount = NULL;
}
@@ -559,7 +687,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
elseif ( $refundMethod === 'credit' )
{
$credits = $this->member->cm_credits;
$credits[ $this->amount->currency ]->amount += ( $amount ?: $this->amount->amount );
$credits[ $this->amount->currency ]->amount = $credits[ $this->amount->currency ]->amount->add( $amount ?: $this->amount->amount );
$this->member->cm_credits = $credits;
$this->member->save();
}
@@ -571,11 +699,14 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$this->status = static::STATUS_REFUSED;
$extra['history'][] = array( 's' => static::STATUS_REFUSED, 'by' => \IPS\Member::loggedIn()->member_id, 'on' => time() );
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => static::STATUS_REFUSED,
'id' => $this->id
) );
if ( $this->member )
{
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => static::STATUS_REFUSED,
'id' => $this->id
) );
}
}
else
{
@@ -584,16 +715,19 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$this->status = static::STATUS_REFUNDED;
$extra['history'][] = array( 's' => static::STATUS_REFUNDED, 'by' => \IPS\Member::loggedIn()->member_id, 'on' => time(), 'to' => $refundMethod, 'ref' => $refundReference );
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => static::STATUS_REFUNDED,
'id' => $this->id,
'refund' => $refundMethod
) );
if ( $this->member )
{
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => static::STATUS_REFUNDED,
'id' => $this->id,
'refund' => $refundMethod
) );
}
}
else
{
$this->partial_refund->amount += $amount;
$this->partial_refund->amount = $this->partial_refund->amount->add( $amount );
$extra['history'][] = array( 's' => static::STATUS_PART_REFUNDED, 'by' => \IPS\Member::loggedIn()->member_id, 'on' => time(), 'to' => $refundMethod, 'amount' => $amount, 'ref' => $refundReference );
if ( $amount >= $this->amount->amount )
{
@@ -605,14 +739,17 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$this->status = static::STATUS_PART_REFUNDED;
}
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => static::STATUS_PART_REFUNDED,
'id' => $this->id,
'refund' => $refundMethod,
'amount' => $amount,
'currency' => $this->currency
) );
if ( $this->member )
{
$this->member->log( 'transaction', array(
'type' => 'status',
'status' => static::STATUS_PART_REFUNDED,
'id' => $this->id,
'refund' => $refundMethod,
'amount' => $amount,
'currency' => $this->currency
) );
}
}
}
$this->extra = $extra;
@@ -653,13 +790,18 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$key = 'transactionRefunded';
$emailKey = 'payment_refunded';
break;
case static::STATUS_GATEWAY_PENDING:
$key = 'transactionGatewayPending';
$emailKey = 'payment_gateway_pending';
break;
default:
throw new \RuntimeException;
break;
}
\IPS\Email::buildFromTemplate( 'nexus', $key, array( $this, $this->invoice, $this->invoice->summary() ) )
\IPS\Email::buildFromTemplate( 'nexus', $key, array( $this, $this->invoice, $this->invoice->summary() ), \IPS\Email::TYPE_TRANSACTIONAL )
->send(
$this->invoice->member,
array_map(
@@ -687,7 +829,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
{
if( $this->_url === NULL )
{
$this->_url = \IPS\Http\Url::internal( "app=nexus&module=payments&controller=checkout&do=transaction&id={$this->invoice->id}&t={$this->id}", 'front', 'nexus_checkout', \IPS\Settings::i()->nexus_https );
$this->_url = \IPS\Http\Url::internal( "app=nexus&module=checkout&controller=checkout&do=transaction&id={$this->invoice->id}&t={$this->id}", 'front', 'nexus_checkout', \IPS\Settings::i()->nexus_https );
}
return $this->_url;
@@ -715,7 +857,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
$return = array();
/* Approve button */
if ( in_array( $this->status, array( static::STATUS_PENDING, static::STATUS_WAITING, static::STATUS_HELD, static::STATUS_REVIEW ) ) and \IPS\Member::loggedIn()->hasAcpRestriction( 'nexus', 'payments', 'transactions_edit' ) )
if ( in_array( $this->status, array( static::STATUS_PENDING, static::STATUS_WAITING, static::STATUS_HELD, static::STATUS_REVIEW, static::STATUS_GATEWAY_PENDING ) ) and \IPS\Member::loggedIn()->hasAcpRestriction( 'nexus', 'payments', 'transactions_edit' ) )
{
$return['approve'] = array(
'title' => $this->auth ? 'transaction_capture' : 'transaction_approve',
@@ -736,7 +878,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
}
/* Void button */
if ( ( $this->status === static::STATUS_PENDING or ( $this->auth and in_array( $this->status, array( static::STATUS_HELD, static::STATUS_REVIEW ) ) ) ) and \IPS\Member::loggedIn()->hasAcpRestriction( 'nexus', 'payments', 'transactions_edit' ) )
if ( ( in_array( $this->status, array( static::STATUS_PENDING, static::STATUS_GATEWAY_PENDING ) ) or ( $this->auth and in_array( $this->status, array( static::STATUS_HELD, static::STATUS_REVIEW ) ) ) ) and \IPS\Member::loggedIn()->hasAcpRestriction( 'nexus', 'payments', 'transactions_edit' ) )
{
$return['void'] = array(
'title' => 'transaction_void',
@@ -798,7 +940,7 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
}
else
{
if ( !in_array( $this->status, array( static::STATUS_PENDING, static::STATUS_WAITING ) ) )
if ( !in_array( $this->status, array( static::STATUS_PENDING, static::STATUS_WAITING, static::STATUS_GATEWAY_PENDING ) ) )
{
$return[] = array(
's' => $this->status,
@@ -810,4 +952,34 @@ class _Transaction extends \IPS\Patterns\ActiveRecord
return $return;
}
/**
* Get output for API
*
* @return array
* @apiresponse int id ID number
* @apiresponse string status Status: 'okay' = Paid; 'pend' = Pending, waiting for gateway; 'wait' = Pending, manual approval required; 'hold' = Held for manual approval; 'revw' = Flagged for review; 'fail' = Failed; 'rfnd' = Refunded; 'prfd' = Partially refunded
* @apiresponse int invoiceId Invoice ID Number
* @apiresponse \IPS\nexus\Money amount Amount
* @apiresponse \IPS\nexus\Money refundAmount If partially refunded, the amount that has been refunded
* @apiresponse \IPS\nexus\Gateway gateway The gateway
* @apiresponse string gatewayId Any ID number provided by the gateway to identify the transaction on their end
* @apiresponse datetime date Date
* @apiresponse \IPS\nexus\Customer customer Customer
*/
public function apiOutput()
{
return array(
'id' => $this->id,
'status' => $this->status,
'invoiceId' => $this->invoice->id,
'amount' => $this->amount->apiOutput(),
'refundAmount' => $this->partial_refund ? $this->partial_refund->apiOutput() : null,
'gateway' => $this->method->apiOutput(),
'gatewayId' => $this->gw_id,
'date' => $this->date->rfc3339(),
'customer' => $this->member->apiOutput()
);
}
}