Payments

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Payments » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
2.0

Payments service.

View source (opens new window)

# Public Properties

Property Description
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of a component property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets the value of a component property.
__unset() (opens new window) Sets a component property to be null.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
captureTransaction() Capture a transaction.
className() (opens new window) Returns the fully qualified name of this class.
completePayment() Process return from off-site payment.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined for this component.
init() (opens new window) Initializes the object.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
processPayment() Process a payment.
refundTransaction() Refund a transaction.
trigger() (opens new window) Triggers an event.

# captureTransaction()

Capture a transaction.

View source (opens new window)

Arguments

Throws

# completePayment()

Process return from off-site payment.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

Throws

# processPayment()

Process a payment.

View source (opens new window)

Arguments

Returns

void

Throws

# refundTransaction()

Refund a transaction.

View source (opens new window)

Arguments

Throws

# Events

# EVENT_AFTER_CAPTURE_TRANSACTION

Type
craft\commerce\events\TransactionEvent

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

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.

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.

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.

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.

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.

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.

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