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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
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.
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

Signature

public boolean canCaptureTransaction ( craft\commerce\models\Transaction $transaction )

canRefundTransaction()

Returns true if a specific transaction can be refunded.

View source

Arguments

Returns

boolean

Signature

public boolean canRefundTransaction ( craft\commerce\models\Transaction $transaction )

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

Signature

public craft\commerce\models\Transaction createTransaction ( craft\commerce\elements\Order $order = null, craft\commerce\models\Transaction $parentTransaction = null, $typeOverride = null )

deleteTransaction()

Delete a transaction.

View source

Arguments

Returns

boolean

Signature

public boolean deleteTransaction ( craft\commerce\models\Transaction $transaction )

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[]

Signature

public craft\commerce\models\Transaction[] getAllTransactionsByOrderId ( \craft\commerce\services\int $orderId )

getChildrenByTransactionId()

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

View source

Arguments

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

Returns

array

Signature

public array getChildrenByTransactionId ( \craft\commerce\services\int $transactionId )

getTransactionByHash()

Get a transaction by its hash.

View source

Arguments

  • $hash (string) – The hash of transaction

Returns

craft\commerce\models\Transaction, null

Signature

public craft\commerce\models\Transaction, null getTransactionByHash ( \craft\commerce\services\string $hash )

getTransactionById()

Get a transaction by its ID.

View source

Arguments

  • $id (integer) – The ID of transaction

Returns

craft\commerce\models\Transaction, null

Signature

public craft\commerce\models\Transaction, null getTransactionById ( \craft\commerce\services\int $id )

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

Signature

public craft\commerce\models\Transaction, null getTransactionByReferenceAndStatus ( \craft\commerce\services\string $reference, \craft\commerce\services\string $status )

isTransactionSuccessful()

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

View source

Arguments

Returns

boolean

Signature

public boolean isTransactionSuccessful ( craft\commerce\models\Transaction $transaction )

refundableAmountForTransaction()

Return the refundable amount for a transaction.

View source

Arguments

Returns

float

Signature

public float refundableAmountForTransaction ( craft\commerce\models\Transaction $transaction )

saveTransaction()

Save a transaction.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean saveTransaction ( craft\commerce\models\Transaction $model, \craft\commerce\services\bool $runValidation = true )

Events

EVENT_AFTER_CREATE_TRANSACTION

Type
craft\commerce\events\TransactionEvent

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

Plugins can get notified after a transaction has been created.

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

Event::on(Transactions::class, Transactions::EVENT_AFTER_CREATE_TRANSACTION, function(TransactionEvent $e) {
    // Do something - perhaps run our 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.

Plugins can get notified after a transaction has been saved.

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

Event::on(Transactions::class, Transactions::EVENT_AFTER_SAVE_TRANSACTION, function(TransactionEvent $e) {
    // Do something - perhaps run our custom logic for failed transactions
});