Skip to content

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 ​

PropertyDescription
behaviorsyii\base\Behavior – List of behaviors attached to this component.

Public Methods ​

MethodDescription
__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.
deleteTransactionById()Delete a transaction by id.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
eagerLoadTransactionsForOrders()
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.
getTransactionByReference()Get a transaction by its reference.
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 ​

canRefundTransaction() ​

Returns true if a specific transaction can be refunded.

View source

Arguments ​

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

Throws ​

deleteTransaction() ​

DEPRECATED

Deprecated in 4.0. Use deleteTransactionById() instead.

Delete a transaction.

View source

Arguments ​

Throws ​

deleteTransactionById() ​

Delete a transaction by id.

View source

Arguments ​

  • $id (integer) – The transaction ID

Throws ​

eagerLoadTransactionsForOrders() ​

Since
3.2.0

View source

Arguments ​

Returns ​

craft\commerce\elements\Order[]

getAllTopLevelTransactionsByOrderId() ​

View source

Arguments ​

  • $orderId (integer) – The order's ID

Returns ​

array

getAllTransactionsByOrderId() ​

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

View source

Arguments ​

  • $orderId (integer) – The order's ID

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

getTransactionByHash() ​

Get a transaction by its hash.

View source

Arguments ​

  • $hash (string) – The hash of transaction

getTransactionById() ​

Get a transaction by its ID.

View source

Arguments ​

  • $id (integer) – The ID of transaction

getTransactionByReference() ​

Get a transaction by its reference.

View source

Arguments ​

  • $reference (string) – The transaction reference

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

isTransactionSuccessful() ​

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

View source

Arguments ​

  • $transaction

refundableAmountForTransaction() ​

Return the refundable amount for a transaction.

View source

Arguments ​

  • $transaction

saveTransaction() ​

Save a transaction.

View source

Arguments ​

Throws ​

Events ​

EVENT_AFTER_CREATE_TRANSACTION ​

Type
craft\commerce\events\TransactionEvent

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

php
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.

php
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
        // ...
    }
);