Transactions

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Transactions » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Transaction service.

View source

# Public Properties

Property Description
behaviors yii\base\Behavior – List of behaviors attached to this component

# Public Methods

Method Description
__call() Calls the named method which is not a class method.
__clone() This method is called after the object is created by cloning an existing one.
__construct() Constructor.
__get() Returns the value of a component property.
__isset() Checks if a property is set, i.e. defined and not null.
__set() Sets the value of a component property.
__unset() Sets a component property to be null.
attachBehavior() Attaches a behavior to this component.
attachBehaviors() Attaches a list of behaviors to the component.
behaviors() Returns a list of behaviors that this component should behave as.
canCaptureTransaction() Returns true if a specific transaction can be refunded.
canGetProperty() Returns a value indicating whether a property can be read.
canRefundTransaction() Returns true if a specific transaction can be refunded.
canSetProperty() Returns a value indicating whether a property can be set.
className() Returns the fully qualified name of this class.
createTransaction() Create a transaction either from an order or a parent transaction. At least one must be present.
deleteTransaction() Delete a transaction.
detachBehavior() Detaches a behavior from the component.
detachBehaviors() Detaches all behaviors from the component.
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component.
getAllTopLevelTransactionsByOrderId()
getAllTransactionsByOrderId() Returns all transactions for an order, per the order's ID.
getBehavior() Returns the named behavior object.
getBehaviors() Returns all behaviors attached to this component.
getChildrenByTransactionId() Get all children transactions, per a parent transaction's ID.
getTransactionByHash() Get a transaction by its hash.
getTransactionById() Get a transaction by its ID.
getTransactionByReferenceAndStatus() Get a transaction by its reference and status.
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event.
hasMethod() Returns a value indicating whether a method is defined.
hasProperty() Returns a value indicating whether a property is defined for this component.
init() Initializes the object.
isTransactionSuccessful() Returns true if a transaction or a direct child of the transaction is successful.
off() Detaches an existing event handler from this component.
on() Attaches an event handler to an event.
refundableAmountForTransaction() Return the refundable amount for a transaction.
saveTransaction() Save a transaction.
trigger() Triggers an event.

# canCaptureTransaction()

Returns true if a specific transaction can be refunded.

View source

Arguments

Returns

boolean

# canRefundTransaction()

Returns true if a specific transaction can be refunded.

View source

Arguments

Returns

boolean

# createTransaction()

Create a transaction either from an order or a parent transaction. At least one must be present.

View source

Arguments

  • $order (craft\commerce\elements\Order) – Order that the transaction is a part of. Ignored, if $parentTransaction is specified.
  • $parentTransaction (craft\commerce\models\Transaction) – Parent transaction, if this transaction is a child. Required, if $order is not specified.
  • $typeOverride (string) – The type of transaction. If set, this overrides the type of the parent transaction, or sets the type when no parentTransaction is passed.

Returns

craft\commerce\models\Transaction

Throws

# deleteTransaction()

Delete a transaction.

View source

Arguments

Returns

boolean

Throws

# getAllTopLevelTransactionsByOrderId()

View source

Arguments

Returns

array, craft\commerce\models\Transaction[]

# getAllTransactionsByOrderId()

Returns all transactions for an order, per the order's ID.

View source

Arguments

Returns

craft\commerce\models\Transaction[]

# getChildrenByTransactionId()

Get all children transactions, per a parent transaction's ID.

View source

Arguments

  • $transactionId (integer) – The parent transaction's ID

Returns

array

# getTransactionByHash()

Get a transaction by its hash.

View source

Arguments

  • $hash (string) – The hash of transaction

Returns

craft\commerce\models\Transaction, null

# getTransactionById()

Get a transaction by its ID.

View source

Arguments

  • $id (integer) – The ID of transaction

Returns

craft\commerce\models\Transaction, null

# getTransactionByReferenceAndStatus()

Get a transaction by its reference and status.

View source

Arguments

  • $reference (string) – The transaction reference
  • $status (string) – The transaction status

Returns

craft\commerce\models\Transaction, null

# isTransactionSuccessful()

Returns true if a transaction or a direct child of the transaction is successful.

View source

Arguments

Returns

boolean

# refundableAmountForTransaction()

Return the refundable amount for a transaction.

View source

Arguments

Returns

float

# saveTransaction()

Save a transaction.

View source

Arguments

Returns

boolean

Throws

# Events

# EVENT_AFTER_CREATE_TRANSACTION

Type
craft\commerce\events\TransactionEvent

The event that is triggered after a transaction has been created.

use craft\commerce\events\TransactionEvent;
use craft\commerce\services\Transactions;
use craft\commerce\models\Transaction;
use yii\base\Event;

Event::on(
    Transactions::class,
    Transactions::EVENT_AFTER_CREATE_TRANSACTION,
    function(TransactionEvent $event) {
        // @var Transaction $transaction
        $transaction = $event->transaction;

        // Run custom logic depending on the transaction type
        // ...
    }
);

# EVENT_AFTER_SAVE_TRANSACTION

Type
craft\commerce\events\TransactionEvent

The event that is triggered after a transaction has been saved.

use craft\commerce\events\TransactionEvent;
use craft\commerce\services\Transactions;
use craft\commerce\models\Transaction;
use yii\base\Event;

Event::on(
    Transactions::class,
    Transactions::EVENT_AFTER_SAVE_TRANSACTION,
    function(TransactionEvent $event) {
        // @var Transaction $transaction
        $transaction = $event->transaction;
        
        // Run custom logic for failed transactions
        // ...
    }
);