I am trying to write a Web API controller in .NET Core to receive Worldpay notifications.
I have done an AddMvc with the XmlSerializerInputFormatter and it is happily reading a "tweaked" version of their payload.
services.AddMvc(config =>
{
config.InputFormatters.Add(new XmlSerializerInputFormatter(config));
});
My test controller
[HttpPost]
[Route("SendDocument")]
[Consumes("application/xml")]
public ActionResult SendDocument([FromBody] paymentService payment)
{
return Ok();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//Worldpay//DTD Worldpay PaymentService v1//EN"
"http://dtd.worldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="Your_merchant_code">
<notify>
<orderStatusEvent orderCode="ExampleOrder1">
<payment>
<paymentMethod>ECMC-SSL</paymentMethod>
<amount value="2400" currencyCode="EUR" exponent="2" debitCreditIndicator="credit"/>
<lastEvent>AUTHORISED</lastEvent>
<AuthorisationId id="622206"/>
<balance accountType="IN_PROCESS_AUTHORISED">
<amount value="2400" currencyCode="EUR" exponent="2" debitCreditIndicator="credit"/>
</balance>
<cardNumber>5255********2490</cardNumber>
<riskScore value="0"/>
</payment>
</orderStatusEvent>
</notify>
</paymentService>
However, they include a DOCTYPE tag at the start and if I add this back into the payload it fails. Without DOCTYPE, it works perfectly
I have done lots of searching and seen info about Ignoring DTD etc but I have no idea how I can do this with the InputFormatter.
How can I go about getting the InputFormatter to ignore this DTD DOCTYPE tag in the file?
After a bit of messing about I got something working which may be of use to others
So my ConfigureServices contains
I created a Raw Formatter which passes through the XML as a string having removed any lines starting <!DOCTYPE
In my controller I use it like this. I takes the RAW XML string, deserializes it into the paymentService Classes I wrote and then returns it in JSON so you can see it worked in POSTMAN
My paymentService classes are these, provided for completeness
I hope this saves somebody some time. WorldPay are useless at providing info and to get to this point has taken hours over weeks.