, (*1)
Sage Pay for Craft Commerce
This plugin provides a Sage Pay integration for Craft Commerce., (*2)
Tip: This plugin uses protocol version 4.00
of the API for SagePay/Opayo., (*3)
Requirements
This plugin requires either:, (*4)
- 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., (*5)
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., (*6)
With Composer
Open your terminal and run the following commands:, (*7)
# 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”., (*8)
Tip: The gateway settings can be set to environment variables. See Environmental Configuration in the Craft docs to learn more about that., (*9)
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., (*10)
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:, (*11)
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);
}
});