Skip to content

Commit

Permalink
Merge pull request #67 from coatesap/old-basket
Browse files Browse the repository at this point in the history
Support old-style basket for Sage integration
  • Loading branch information
delatbabel committed Aug 24, 2016
2 parents eed074c + b9434fc commit b1a6aa6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ The following gateways are provided by this package:
For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
repository.

### Basket format

Sagepay currently supports two different formats for sending cart/item information to them:
- [BasketXML](http://www.sagepay.co.uk/support/12/36/protocol-3-00-basket-xml)
- [Basket](http://www.sagepay.co.uk/support/error-codes/3021-invalid-basket-format-invalid)

These are incompatible with each other, and cannot be both sent in the same transaction. *BasketXML* is the most modern format, and is the default used by this driver. *Basket* is an older format which may be deprecated one day, but is also the only format currently supported by some of the Sage accounting products (Line 50, etc) which can pull transaction data directly from Sagepay. Therefore for users who require this type of integration, an optional parameter `useOldBasketFormat` with a value of `true` can be passed in the driver's `initialize()` method.

## Support

If you are having general issues with Omnipay, we suggest posting on
Expand Down
12 changes: 12 additions & 0 deletions src/DirectGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public function setVendor($value)
{
return $this->setParameter('vendor', $value);
}

public function setUseOldBasketFormat($value)
{
$value = (bool)$value;

return $this->setParameter('useOldBasketFormat', $value);
}

public function getUseOldBasketFormat()
{
return $this->getParameter('useOldBasketFormat');
}

// Access to the HTTP client for debugging.
// NOTE: this is likely to be removed or replaced with something
Expand Down
52 changes: 52 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public function getService()
{
return $this->action;
}

public function setUseOldBasketFormat($value)
{
$value = (bool)$value;

return $this->setParameter('useOldBasketFormat', $value);
}

public function getUseOldBasketFormat()
{
return $this->getParameter('useOldBasketFormat');
}

public function getAccountType()
{
Expand Down Expand Up @@ -233,4 +245,44 @@ protected function getItemData()

return $result;
}

/**
* Generate Basket string in the old format
* This is called if "useOldBasketFormat" is set to true in the gateway config
* @return string Basket field in format of:
* 1:Item:2:10.00:0.00:10.00:20.00
* [number of lines]:[item name]:[quantity]:[unit cost]:[item tax]:[item total]:[line total]
*/
protected function getItemDataNonXML()
{

$result = '';
$items = $this->getItems();
$count = 0;

foreach ($items as $basketItem) {
$lineTotal = ($basketItem->getQuantity() * $basketItem->getPrice());

$description = $this->filterItemName($basketItem->getName());
$description = str_replace(':', ' ', $description); // Make sure there aren't any colons in the name
// Perhaps : should be replaced with '-' or other symbol?
$result .= ':' . $description . // Item name
':' . $basketItem->getQuantity() . // Quantity
':' . number_format($basketItem->getPrice(), 2, '.', '') . // Unit cost (without tax)
':0.00' . // Item tax
':' . number_format($basketItem->getPrice(), 2, '.', '') . // Item total
':' . number_format($lineTotal, 2, '.', ''); // Line total
// As the getItemData() puts 0.00 into tax, same was done here

$count++;

}

// Prepend number of lines to the result string
$result = $count . $result;

return $result;

}
}
13 changes: 10 additions & 3 deletions src/Message/DirectAuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ protected function getBaseAuthorizeData()
$data['DeliveryPhone'] = $card->getShippingPhone();
$data['CustomerEMail'] = $card->getEmail();

$basketXML = $this->getItemData();
if (!empty($basketXML)) {
$data['BasketXML'] = $basketXML;
if ($this->getUseOldBasketFormat()) {
$basket = $this->getItemDataNonXML();
if (!empty($basket)) {
$data['Basket'] = $basket;
}
} else {
$basketXML = $this->getItemData();
if (!empty($basketXML)) {
$data['BasketXML'] = $basketXML;
}
}

return $data;
Expand Down

0 comments on commit b1a6aa6

Please sign in to comment.