Skip to content

Payments ​

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

Payments 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.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
captureTransaction()Capture a transaction.
className()Returns the fully qualified name of this class.
completePayment()Process return from off-site payment.
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.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
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.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
processPayment()Process a payment.
refundTransaction()Refund a transaction.
trigger()Triggers an event.

captureTransaction() ​

Capture a transaction.

View source

Arguments ​

Throws ​

completePayment() ​

Process return from off-site payment.

View source

Arguments ​

Returns ​

boolean

Throws ​

processPayment() ​

Process a payment.

View source

Arguments ​

Returns ​

void

Throws ​

refundTransaction() ​

Refund a transaction.

View source

Arguments ​

Throws ​

Events ​

EVENT_AFTER_CAPTURE_TRANSACTION ​

Type
craft\commerce\events\TransactionEvent

The event that is triggered after a payment transaction is captured.

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

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

        // Notify the warehouse we're ready to ship
        // ...
    }
);

EVENT_AFTER_COMPLETE_PAYMENT ​

Type
craft\commerce\events\TransactionEvent

The event that is triggered when a complete-payment request is made. After this event, the customer will be redirected offsite or be redirected to the order success returnUrl.

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

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

        // Check whether it was an authorize transaction
        // and make sure that warehouse team is on top of it
        // ...
    }
);

EVENT_AFTER_PROCESS_PAYMENT ​

Type
craft\commerce\events\ProcessPaymentEvent

The event that is triggered after a payment is processed.

php
use craft\commerce\events\ProcessPaymentEvent;
use craft\commerce\services\Payments;
use craft\commerce\elements\Order;
use craft\commerce\models\payments\BasePaymentForm;
use craft\commerce\models\Transaction;
use craft\commerce\base\RequestResponseInterface;
use yii\base\Event;

Event::on(
    Payments::class,
    Payments::EVENT_AFTER_PROCESS_PAYMENT,
    function(ProcessPaymentEvent $event) {
        // @var Order $order
        $order = $event->order;
        // @var BasePaymentForm $form
        $form = $event->form;
        // @var Transaction $transaction
        $transaction = $event->transaction;
        // @var RequestResponseInterface $response
        $response = $event->response;

        // Let the accounting department know an order transaction went through
        // ...
    }
);

EVENT_AFTER_REFUND_TRANSACTION ​

Type
craft\commerce\events\TransactionEvent

The event that is triggered after a transaction is refunded.

php
use craft\commerce\events\RefundTransactionEvent;
use craft\commerce\services\Payments;
use yii\base\Event;

Event::on(
    Payments::class,
    Payments::EVENT_AFTER_REFUND_TRANSACTION,
    function(RefundTransactionEvent $event) {
        // @var float $amount
        $amount = $event->amount;

        // Do something else if the refund amount’s >50% of the transaction
        // ...
    }
);

EVENT_BEFORE_CAPTURE_TRANSACTION ​

Type
craft\commerce\events\TransactionEvent

The event that is triggered before a payment transaction is captured.

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

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

        // Check that shipment’s ready before capturing
        // ...
    }
);

EVENT_BEFORE_PROCESS_PAYMENT ​

Type
craft\commerce\events\ProcessPaymentEvent

The event that is triggered before a payment is processed.

You may set the isValid property to false on the event to prevent the payment from being processed.

php
use craft\commerce\events\ProcessPaymentEvent;
use craft\commerce\services\Payments;
use craft\commerce\elements\Order;
use craft\commerce\models\payments\BasePaymentForm;
use craft\commerce\models\Transaction;
use craft\commerce\base\RequestResponseInterface;
use yii\base\Event;

Event::on(
    Payments::class,
    Payments::EVENT_BEFORE_PROCESS_PAYMENT,
    function(ProcessPaymentEvent $event) {
        // @var Order $order
        $order = $event->order;
        // @var BasePaymentForm $form
        $form = $event->form;
        // @var Transaction $transaction
        $transaction = $event->transaction;
        // @var RequestResponseInterface $response
        $response = $event->response;

        // Check some business rules to see whether the transaction is allowed
        // ...
    }
);

EVENT_BEFORE_REFUND_TRANSACTION ​

Type
craft\commerce\events\TransactionEvent

The event that is triggered before a transaction is refunded.

php
use craft\commerce\events\RefundTransactionEvent;
use craft\commerce\services\Payments;
use yii\base\Event;

Event::on(
    Payments::class,
    Payments::EVENT_BEFORE_REFUND_TRANSACTION,
    function(RefundTransactionEvent $event) {
        // @var float $amount
        $amount = $event->amount;

        // Do something else if the refund amount’s >50% of the transaction
        // ...
    }
);