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.
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. |
| 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.
Arguments ​
$transaction(craft\commerce\models\Transaction) – The transaction to capture.
Throws ​
- craft\commerce\errors\TransactionException
if something went wrong when saving the transaction
completePayment() ​
Process return from off-site payment.
Arguments ​
$transaction(craft\commerce\models\Transaction)$customError(string, null)
Returns ​
Throws ​
- craft\commerce\errors\CurrencyException
- yii\base\ExitException
- yii\base\InvalidConfigException
\Twig\Error\LoaderError\Twig\Error\RuntimeError\Twig\Error\SyntaxError- Throwable
- craft\commerce\errors\TransactionException
- craft\commerce\errors\OrderStatusException
- craft\errors\ElementNotFoundException
- yii\base\Exception
processPayment() ​
Process a payment.
Arguments ​
$order(craft\commerce\elements\Order) – The order for which the payment is.$form(craft\commerce\models\payments\BasePaymentForm) – The payment form.$redirect(string, null) – A string parameter by reference that will contain the redirect URL, if any$transaction(craft\commerce\models\Transaction, null) – The transaction$redirectData(array, null) – The additional data the gateway might need to redirect the user to the payment page. This is useful for ajax payment responses.
Returns ​
void
Throws ​
- yii\base\InvalidConfigException
- craft\commerce\errors\PaymentException
if the payment was unsuccessful - craft\commerce\errors\TransactionException
- craft\commerce\errors\CurrencyException
refundTransaction() ​
Refund a transaction.
Arguments ​
$transaction(craft\commerce\models\Transaction) – The transaction to refund.$amount(float, null) – The amount to refund or null for full amount.$note(string) – The administrators note on the refund
Throws ​
- craft\commerce\errors\RefundException
if something went wrong during the refund.
Events ​
EVENT_AFTER_CAPTURE_TRANSACTION ​
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 ​
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 ​
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 ​
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 ​
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 ​
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 ​
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
// ...
}
);