javaopayo

How to pass Discount details along with BasketXML?


I am integrating sagepay 3.0 in my System.

Where I want to pass Discounts along with BasketXML as suggested in documentation 48th page.

But there is no such method in Basket class to pass discounts.

I am using sagepay-api-1.2.2.0.jar library for my integration.

    Basket basket = new Basket();
    // ... set common params to basket
    // basket.setDiscounts(discounts); // not exists!

    BasketXmlFormatter xmlFormatter = new BasketXmlFormatter();
    String basketXml = xmlFormatter.toXml(iFormPayment, basket);

Solution

  • As there is no such method to set Discounts in basket. We need to append XML string manually.

        String discountXML = "<discounts>";
        String discountTag = "<discount>"; // can be muliple
        discountTag += "<fixed>" + discountPrice + "</fixed>"; // REQUIRED
        discountTag += "<description>10% Discount Applied</description>";
        discountTag += "</discount>";
        discountXML += discountTag;
        discountXML += "</discounts>";
    
        BasketXmlFormatter xmlFormatter = new BasketXmlFormatter();
        String basketXml = xmlFormatter.toXml(iFormPayment, basket);
        basketXml = basketXml.replace("</basket>", discountXML + "</basket>");