Skip to content

craftcms/commerce-sagepay

Repository files navigation

Sage Pay for Craft Commerce icon

Sage Pay for Craft Commerce

This plugin provides a Sage Pay integration for Craft Commerce.

Tip: This plugin uses protocol version 4.00 of the API for SagePay/Opayo.

Requirements

This plugin requires either:

  • Craft 4.0 and Craft Commerce 4.0 or later
  • Craft 5.0 and Craft Commerce 5.0 or later

Installation

You can install this plugin from the Plugin Store or with Composer.

From the Plugin Store

Go to the Plugin Store in your project’s Control Panel and search for Sage Pay for Craft Commerce”. Then click on the “Install” button in its modal window.

With Composer

Open your terminal and run the following commands:

# go to the project directory
cd /path/to/my-project.test

# tell Composer to load the plugin
composer require craftcms/commerce-sagepay

# tell Craft to install the plugin
./craft install/plugin commerce-sagepay

Setup

To add a Sage Pay payment gateway, go to Commerce → Settings → Gateways, create a new gateway, and set the gateway type to “Sage Pay Server”.

Tip: The gateway settings can be set to environment variables. See Environmental Configuration in the Craft docs to learn more about that.

Using the legacy basket format

Sage Pay 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])) {
            return;
        }

        $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()) {
            return;
        }

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

        $sku = $orderLineItem->getSku();

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