|
| 1 | +module Kernel.External.Insurance.IffcoTokio.Flow where |
| 2 | + |
| 3 | +import qualified Data.ByteString.Lazy as BSL |
| 4 | +import qualified Data.Text as T |
| 5 | +import qualified Data.Text.Encoding as TE |
| 6 | +import qualified Data.Text.Lazy as TL |
| 7 | +import EulerHS.Types (EulerClient, client) |
| 8 | +import Kernel.External.Encryption |
| 9 | +import Kernel.External.Insurance.IffcoTokio.Types |
| 10 | +import Kernel.Prelude |
| 11 | +import Kernel.Tools.Metrics.CoreMetrics (CoreMetrics) |
| 12 | +import Kernel.Types.Error (GenericError (..)) |
| 13 | +import Kernel.Utils.Common |
| 14 | +import qualified Network.HTTP.Media as M |
| 15 | +import Servant hiding (throwError) |
| 16 | +import Text.XML (def, parseText) |
| 17 | +import Text.XML.Cursor (content, fromDocument, laxElement, ($//), (&/)) |
| 18 | +import qualified Text.XML.Cursor as XC |
| 19 | + |
| 20 | +-- --------------------------------------------------------------------------- |
| 21 | +-- Servant content type for SOAP / XML |
| 22 | + |
| 23 | +data IffcoApplicationXML deriving (Typeable) |
| 24 | + |
| 25 | +instance Accept IffcoApplicationXML where |
| 26 | + contentType _ = "text" M.// "xml" |
| 27 | + |
| 28 | +-- Render: pass raw BSL bytes directly as the request body |
| 29 | +instance MimeRender IffcoApplicationXML BSL.ByteString where |
| 30 | + mimeRender _ bs = bs |
| 31 | + |
| 32 | +-- Unrender: decode UTF-8 bytes and parse the SOAP response envelope |
| 33 | +instance MimeUnrender IffcoApplicationXML HomeDeclarationResp where |
| 34 | + mimeUnrender _ bs = |
| 35 | + case TE.decodeUtf8' (BSL.toStrict bs) of |
| 36 | + Left err -> Left (show err) |
| 37 | + Right txt -> case parseHomeDeclarationResponse txt of |
| 38 | + Left e -> Left (T.unpack e) |
| 39 | + Right r -> Right r |
| 40 | + |
| 41 | +-- --------------------------------------------------------------------------- |
| 42 | +-- Servant API type |
| 43 | + |
| 44 | +-- The IFFCO Tokio endpoint takes fixed query params that identify the operation. |
| 45 | +-- SOAPAction value: "document/http://siebel.com/CustomUI:RegisterHomeDeclaration_New" |
| 46 | +-- (Servant QueryParam handles URL-encoding automatically.) |
| 47 | +type RegisterHomeDeclarationAPI = |
| 48 | + "eai_ws_enu" |
| 49 | + :> "start.swe" |
| 50 | + :> QueryParam "SWEExtSource" Text |
| 51 | + :> QueryParam "SWEExtCmd" Text |
| 52 | + :> QueryParam "SOAPAction" Text |
| 53 | + :> ReqBody '[IffcoApplicationXML] BSL.ByteString |
| 54 | + :> Post '[IffcoApplicationXML] HomeDeclarationResp |
| 55 | + |
| 56 | +registerHomeDeclarationClient :: |
| 57 | + Maybe Text -> |
| 58 | + Maybe Text -> |
| 59 | + Maybe Text -> |
| 60 | + BSL.ByteString -> |
| 61 | + EulerClient HomeDeclarationResp |
| 62 | +registerHomeDeclarationClient = client (Proxy :: Proxy RegisterHomeDeclarationAPI) |
| 63 | + |
| 64 | +-- --------------------------------------------------------------------------- |
| 65 | +-- Main call function |
| 66 | + |
| 67 | +-- | Make the SOAP call and throw on failure (standard throwing variant). |
| 68 | +registerHomeDeclaration :: |
| 69 | + ( HasCallStack, |
| 70 | + EncFlow m r, |
| 71 | + MonadFlow m, |
| 72 | + CoreMetrics m, |
| 73 | + HasRequestId r, |
| 74 | + MonadReader r m |
| 75 | + ) => |
| 76 | + IffcoTokioConfig -> |
| 77 | + HomeDeclarationReq -> |
| 78 | + m HomeDeclarationResp |
| 79 | +registerHomeDeclaration config req = |
| 80 | + registerHomeDeclarationEither config req |
| 81 | + >>= either (\err -> throwError $ InternalError $ "IFFCO Tokio API call failed: " <> err) return |
| 82 | + |
| 83 | +-- | Make the SOAP call and return 'Either' instead of throwing. |
| 84 | +-- Used by the async interface layer so that the callback always fires. |
| 85 | +registerHomeDeclarationEither :: |
| 86 | + ( HasCallStack, |
| 87 | + EncFlow m r, |
| 88 | + MonadFlow m, |
| 89 | + CoreMetrics m, |
| 90 | + HasRequestId r, |
| 91 | + MonadReader r m |
| 92 | + ) => |
| 93 | + IffcoTokioConfig -> |
| 94 | + HomeDeclarationReq -> |
| 95 | + m (Either Text HomeDeclarationResp) |
| 96 | +registerHomeDeclarationEither config req = do |
| 97 | + decryptedPassword <- decrypt config.password |
| 98 | + let soapBody = buildSoapEnvelope config.username decryptedPassword config req |
| 99 | + eulerClient = |
| 100 | + registerHomeDeclarationClient |
| 101 | + (Just "SecureWebService") |
| 102 | + (Just "Execute") |
| 103 | + -- SOAPAction value includes surrounding double-quotes as per SOAP spec |
| 104 | + (Just "\"document/http://siebel.com/CustomUI:RegisterHomeDeclaration_New\"") |
| 105 | + soapBody |
| 106 | + result <- callAPI config.url eulerClient "IFFCO-Tokio-register-home-declaration" (Proxy @RegisterHomeDeclarationAPI) |
| 107 | + return $ case result of |
| 108 | + Left err -> Left (T.pack (show err)) |
| 109 | + Right resp -> Right resp |
| 110 | + |
| 111 | +-- --------------------------------------------------------------------------- |
| 112 | +-- SOAP envelope builder |
| 113 | + |
| 114 | +buildSoapEnvelope :: Text -> Text -> IffcoTokioConfig -> HomeDeclarationReq -> BSL.ByteString |
| 115 | +buildSoapEnvelope username password cfg req = |
| 116 | + BSL.fromStrict . TE.encodeUtf8 $ |
| 117 | + T.concat |
| 118 | + [ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", |
| 119 | + "<soapenv:Envelope", |
| 120 | + " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"", |
| 121 | + " xmlns:cus=\"http://siebel.com/CustomUI\"", |
| 122 | + " xmlns:ins=\"http://www.siebel.com/xml/INSDeclarationsWebServiceRequestIO\">", |
| 123 | + "<soapenv:Header>", |
| 124 | + "<wsse:Security xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">", |
| 125 | + "<wsse:UsernameToken xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\">", |
| 126 | + "<wsse:Username>", |
| 127 | + xmlEscape username, |
| 128 | + "</wsse:Username>", |
| 129 | + "<wsse:Password Type=\"wsse:PasswordText\">", |
| 130 | + xmlEscape password, |
| 131 | + "</wsse:Password>", |
| 132 | + "</wsse:UsernameToken>", |
| 133 | + "</wsse:Security>", |
| 134 | + "</soapenv:Header>", |
| 135 | + "<soapenv:Body>", |
| 136 | + "<cus:RegisterHomeDeclaration_New_Input>", |
| 137 | + "<ins:INSDeclarationsWebServiceRequestIO>", |
| 138 | + "<ins:DeclarationDetails>", |
| 139 | + "<ins:MasterPolicyClient>", |
| 140 | + xmlEscape cfg.masterPolicyClient, |
| 141 | + "</ins:MasterPolicyClient>", |
| 142 | + "<ins:InsurancePlan>", |
| 143 | + xmlEscape cfg.insurancePlan, |
| 144 | + "</ins:InsurancePlan>", |
| 145 | + "<ins:InsuredAddress>", |
| 146 | + xmlEscape req.insuredAddress, |
| 147 | + "</ins:InsuredAddress>", |
| 148 | + "<ins:InsuredEmail>", |
| 149 | + xmlEscape req.insuredEmail, |
| 150 | + "</ins:InsuredEmail>", |
| 151 | + "<ins:InsuredMobile>", |
| 152 | + xmlEscape req.insuredMobile, |
| 153 | + "</ins:InsuredMobile>", |
| 154 | + "<ins:InsuredName>", |
| 155 | + xmlEscape req.insuredName, |
| 156 | + "</ins:InsuredName>", |
| 157 | + "<ins:InvoiceDate>", |
| 158 | + xmlEscape req.invoiceDate, |
| 159 | + "</ins:InvoiceDate>", |
| 160 | + "<ins:InvoiceRequestNumber>", |
| 161 | + xmlEscape req.invoiceRequestNumber, |
| 162 | + "</ins:InvoiceRequestNumber>", |
| 163 | + "<ins:EWCommencesOn>", |
| 164 | + maybe "" xmlEscape req.ewCommencesOn, |
| 165 | + "</ins:EWCommencesOn>", |
| 166 | + "</ins:DeclarationDetails>", |
| 167 | + "</ins:INSDeclarationsWebServiceRequestIO>", |
| 168 | + "</cus:RegisterHomeDeclaration_New_Input>", |
| 169 | + "</soapenv:Body>", |
| 170 | + "</soapenv:Envelope>" |
| 171 | + ] |
| 172 | + |
| 173 | +-- Escape special XML characters in text node values to prevent injection |
| 174 | +xmlEscape :: Text -> Text |
| 175 | +xmlEscape = |
| 176 | + T.replace "&" "&" |
| 177 | + . T.replace "<" "<" |
| 178 | + . T.replace ">" ">" |
| 179 | + . T.replace "\"" """ |
| 180 | + . T.replace "'" "'" |
| 181 | + |
| 182 | +-- SOAP response XML parser (uses xml-conduit cursor navigation) |
| 183 | + |
| 184 | +lookupSoapField :: XC.Cursor -> Text -> Maybe Text |
| 185 | +lookupSoapField cursor localName = |
| 186 | + case cursor $// laxElement localName &/ content of |
| 187 | + [] -> Nothing |
| 188 | + ts -> Just $ T.strip $ T.concat ts |
| 189 | + |
| 190 | +parseHomeDeclarationResponse :: Text -> Either Text HomeDeclarationResp |
| 191 | +parseHomeDeclarationResponse xmlText = do |
| 192 | + doc <- case parseText def (TL.fromStrict xmlText) of |
| 193 | + Left err -> Left (T.pack (show err)) |
| 194 | + Right d -> Right d |
| 195 | + let cursor = fromDocument doc |
| 196 | + require fieldName = |
| 197 | + maybe (Left $ "IFFCO Tokio response missing field: " <> fieldName) Right $ |
| 198 | + lookupSoapField cursor fieldName |
| 199 | + certNum <- require "Certificate_spcNumber" |
| 200 | + declId <- require "Declaration_spcId" |
| 201 | + st <- require "Status" |
| 202 | + return HomeDeclarationResp {certificateNumber = certNum, declarationId = declId, status = st} |
0 commit comments