Search code examples
phpopayo

Total transaction amount with Sagepay official PHP Integration Kit


Using the official Sagepay PHP integration kit for Protocol 3.0 there doesn't appear to be a way to actually set the total transaction amount. The total value of the transaction is calculated according to the items within the basket added using the SagepayBasket->addItem(SagepayItem $item) method.

Basic setup would be something like this:

$config = SagepaySettings::getInstance($configs,true);
$api = SagepayApiFactory::create('SERVER', $config);
$basket = new SagepayBasket();

You can add items:

$item = new SagepayItem();
$item->setDescription($oproduct['oc_item_title']);
$item->setUnitTaxAmount($oproduct['oc_item_vat']);
$item->setQuantity($oproduct['oc_qty']);
$item->setUnitNetAmount(($oproduct['oc_net_item_price']));
$basket->addItem($item);

You can also add shipping info:

$basket->setDeliveryNetAmount($order['shipping_cost_net']);
$basket->setDeliveryTaxAmount($order['shipping_cost_vat']);

However, a basket total is also dependent on discounts. You can add discounts to the basket using:

$discounts = [
    'discount' => [
        'fixed'         =>  $order['voucher_discount'],
        'description'   =>  'Voucher Discount'
    ]
];
$basket->setDiscounts($discounts);

However, this data is ignored in the total calculation. This means the amount passed to Sagepay is incorrect.

This can be seen looking at basket.php which lacks a call to the $basket->getDiscounts method:

public function getAmount()
{
    $amount = $this->getDeliveryGrossAmount();
    foreach ($this->_items as $item)
    {
        $amount += $item->getTotalGrossAmount();
    }
    return $amount;
}

When you make a call to Sagepay with this data they correctly identify that the line totals do not add up correctly to the Amount sent by sending this response:

StatusDetail=3195 : The line item amount details do not add up correctly

The only option here to make this work is to modify their library which seems wrong but there is no other option I can see. I've posted here as they list SO as their "support forum" so hopefully either someone from Sagepay will see this or else another developer who has used their code might point out where i'm going wrong.


Solution

  • In short - don't use this kit. It's not intended for production according to Sagepay and, as such, there's no intention to catch these kinds of issues.