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.
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. |
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.
Arguments ​
$transaction
(craft\commerce\models\Transaction) – The transaction
canRefundTransaction()
​
Returns true if a specific transaction can be refunded.
Arguments ​
$transaction
(craft\commerce\models\Transaction) – The transaction
createTransaction()
​
Create a transaction either from an order or a parent transaction. At least one must be present.
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 ​
- craft\commerce\errors\TransactionException
if neither$order
or$parentTransaction
is specified. - craft\commerce\errors\CurrencyException
- yii\base\InvalidConfigException
deleteTransaction()
​
DEPRECATED
Deprecated in 4.0. Use deleteTransactionById() instead.
Delete a transaction.
Arguments ​
$transaction
(craft\commerce\models\Transaction) – The transaction to delete
Throws ​
deleteTransactionById()
​
Delete a transaction by id.
Arguments ​
$id
(integer) – The transaction ID
Throws ​
eagerLoadTransactionsForOrders()
​
- Since
- 3.2.0
Arguments ​
$orders
(array, craft\commerce\elements\Order[])
Returns ​
craft\commerce\elements\Order[]
getAllTopLevelTransactionsByOrderId()
​
Arguments ​
$orderId
(integer) – The order's ID
Returns ​
getAllTransactionsByOrderId()
​
Returns all transactions for an order, per the order's ID.
Arguments ​
$orderId
(integer) – The order's ID
Returns ​
craft\commerce\models\Transaction[]
getChildrenByTransactionId()
​
Get all children transactions, per a parent transaction's ID.
Arguments ​
$transactionId
(integer) – The parent transaction's ID
getTransactionByHash()
​
Get a transaction by its hash.
Arguments ​
$hash
(string) – The hash of transaction
getTransactionById()
​
Get a transaction by its ID.
Arguments ​
$id
(integer) – The ID of transaction
getTransactionByReference()
​
Get a transaction by its reference.
Arguments ​
$reference
(string) – The transaction reference
Returns ​
craft\commerce\models\Transaction, null
getTransactionByReferenceAndStatus()
​
Get a transaction by its reference and status.
Arguments ​
isTransactionSuccessful()
​
Returns true if a transaction or a direct child of the transaction is successful.
Arguments ​
$transaction
refundableAmountForTransaction()
​
Return the refundable amount for a transaction.
Arguments ​
$transaction
saveTransaction()
​
Save a transaction.
Arguments ​
$model
(craft\commerce\models\Transaction) – The transaction model$runValidation
(boolean) – Should we validate this transaction before saving.
Throws ​
- Throwable
- craft\commerce\errors\TransactionException
if an attempt is made to modify an existing transaction - craft\commerce\errors\OrderStatusException
- craft\errors\ElementNotFoundException
- yii\base\Exception
Events ​
EVENT_AFTER_CREATE_TRANSACTION ​
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 ​
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
// ...
}
);