SagePay payment gateway plugin for Craft Commerce 2
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

SagePay payment gateway plugin for Craft Commerce 2

This plugin provides SagePay integrations for Craft Commerce.

It provides both SagePay Direct and SagePay Server gateways.


This plugin requires Craft Commerce 2.0.0-alpha.5 or later.


To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
  2. Then tell Composer to load the plugin:

     composer require craftcms/commerce-sagepay
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for SagePay.


To add a SagePay payment gateway, go to Commerce → Settings → Gateways, create a new gateway, and set the gateway type to either “SagePay Direct” or “SagePay Server”.

Disabling CSRF for webhooks.

You must disable CSRF protection for the incoming requests, assuming it is enabled for the site (default for Craft since 3.0).

A clean example for how to go about this can be found here.

Using the legacy basket format

SagePay has two formats for submit basket data — Basket and BasketXML. The Basket is a legacy format, but is the only way to integrate with Sage 50 Accounts.

To use the legacy format, simply turn on the appropriate setting in the gateway settings. To complete your integration with Sage 50 Accounts you can use the following event:

use \craft\commerce\omnipay\base\Gateway as BaseGateway;

Event::on(BaseGateway::class, BaseGatewa::EVENT_AFTER_CREATE_ITEM_BAG, function(ItemBagEvent $itemBagEvent) {
    $orderLineItems = $itemBagEvent->order->getLineItems();

     * @var $item Item
    foreach ($itemBagEvent->items as $key => $item) {

        if (!isset($orderLineItems[$key])) {

        $orderLineItem  = $orderLineItems[$key];

        // Make sure that the description and price are the same as we are relying upon the order
        // of the Order Items and The OmniPay Item Bag to be the same
        if ($orderLineItem->getDescription() != $item->getDescription()) {

        if ($orderLineItem->price != $item->getPrice()) {

        $sku = $orderLineItem->getSku();

        // Place the SKU within [] as the Product Record for the Sage 50 Accounts Integration
        $description = '[' . $sku . ']' . $item->getDescription();