Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift 3,4,5...) Create Signed SOAP XML for Albanian Fiscalization ServiceImportant: This example requires Chilkat v9.5.0.80 or greater. This example is created for a customer that needs to send signed billing/invoicing SOAP XML requests to http://efiskalizimi-test.tatime.gov.al:80/FiscalizationService The example demonstrates how to create and signed the SOAP XML message that is to be sent.
func chilkatTest() { // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // -------------------------------------------------------------------------------- // Also see Chilkat's Online WSDL Code Generator // to generate code and SOAP Request and Response XML for each operation in a WSDL. // -------------------------------------------------------------------------------- // We want to build and send a signed SOAP XML that looks like the following: // // <?xml version="1.0" encoding="utf-8"?> // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> // <SOAP-ENV:Header/> // <SOAP-ENV:Body> // <RegisterWTNRequest xmlns="https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" Id="Request"> // <Header SendDateTime="2019-09-03T14:19:01+02:00" UUID="42db3af5-0d9f-4dea-95b4-4b947ab8d8e7"/> // <WTN BusinUnit="bb123bb123" DateTimeCreated="2019-09-03T14:19:01+02:00" DestinAddr="Destination address" // DestinCity="Destination city" IsAfterDel="false" OperatorCode="oo123oo123" // SoftNum="ss123ss123" StartAddr="Start address" StartCity="Start city" TransDate="2019-09-03T14:19:01+02:00" // VehPlates="AA0000AA" WTNIC="5C5E58580D0A24E1F7A5E5E011929511" WTNICSignature="82D69C38206D ... F5FCA48" WTNNum="12345678901"> // <Issuer NUIS="I12345678I" Name="Issuer name"/> // <Items> // <I C="501234567890" N="Item name" Q="1.0" U="piece"/> // </Items> // </WTN> // <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> // <SignedInfo> // <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> // <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> // <Reference URI="#Request"> // <Transforms> // <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> // <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> // </Transforms> // <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> // <DigestValue>aNa0r4RW04BlsAciTUrWHGW9PADskl2op315BAQ0hTg=</DigestValue> // </Reference> // </SignedInfo> // <SignatureValue>4/j/d4j/5xCJ2YUP+XTC6li0B94...........................KESd38NT5+puArBcNgLYIjLx/dh6Q==</SignatureValue> // <KeyInfo> // <X509Data> // <X509Certificate>MIIE6TCCAtGgAwIBAgICEA8wDQYJKoZIhvcNA......................uoqWsSuLmA==</X509Certificate> // </X509Data> // </KeyInfo> // </Signature> // </RegisterWTNRequest> // </SOAP-ENV:Body> // </SOAP-ENV:Envelope> var success: Bool = true // Create the XML to be signed... let xmlToSign = CkoXml()! xmlToSign.tag = "SOAP-ENV:Envelope" xmlToSign.addAttribute("xmlns:SOAP-ENV", value: "http://schemas.xmlsoap.org/soap/envelope/") xmlToSign.updateChildContent("SOAP-ENV:Header", value: "") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest", autoCreate: true, attrName: "xmlns", attrValue: "https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest", autoCreate: true, attrName: "xmlns:ns2", attrValue: "http://www.w3.org/2000/09/xmldsig#") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest", autoCreate: true, attrName: "Id", attrValue: "Request") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|Header", autoCreate: true, attrName: "SendDateTime", attrValue: "2019-09-03T14:19:01+02:00") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|Header", autoCreate: true, attrName: "UUID", attrValue: "42db3af5-0d9f-4dea-95b4-4b947ab8d8e7") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "BusinUnit", attrValue: "bb123bb123") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "DateTimeCreated", attrValue: "2019-09-03T14:19:01+02:00") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "DestinAddr", attrValue: "Destination address") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "DestinCity", attrValue: "Destination city") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "IsAfterDel", attrValue: "false") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "OperatorCode", attrValue: "oo123oo123") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "SoftNum", attrValue: "ss123ss123") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "StartAddr", attrValue: "Start address") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "StartCity", attrValue: "Start city") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "TransDate", attrValue: "2019-09-03T14:19:01+02:00") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "VehPlates", attrValue: "AA0000AA") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "WTNIC", attrValue: "5C5E58580D0A24E1F7A5E5E011929511") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "WTNICSignature", attrValue: "82D69C38206D ... F5FCA48") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN", autoCreate: true, attrName: "WTNNum", attrValue: "12345678901") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer", autoCreate: true, attrName: "NUIS", attrValue: "I12345678I") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer", autoCreate: true, attrName: "Name", attrValue: "Issuer name") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", autoCreate: true, attrName: "C", attrValue: "501234567890") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", autoCreate: true, attrName: "N", attrValue: "Item name") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", autoCreate: true, attrName: "Q", attrValue: "1.0") xmlToSign.updateAttr(at: "SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I", autoCreate: true, attrName: "U", attrValue: "piece") let gen = CkoXmlDSigGen()! gen.sigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Body|RegisterWTNRequest" gen.sigLocationMod = 0 gen.sigNamespacePrefix = "" gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" gen.signedInfoCanonAlg = "EXCL_C14N" gen.signedInfoDigestMethod = "sha256" // -------- Reference 1 -------- gen.addSameDocRef("Request", digestMethod: "sha256", canonMethod: "EXCL_C14N", prefixList: "", refType: "") // Provide a certificate + private key. (PFX password is test123) let cert = CkoCert()! success = cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", password: "test123") if success != true { print("\(cert.lastErrorText!)") return } gen.setX509Cert(cert, usePrivateKey: true) gen.keyInfoType = "X509Data" gen.x509Type = "Certificate" // Load XML to be signed... let sbXml = CkoStringBuilder()! xmlToSign.getSb(sbXml) gen.behaviors = "CompactSignedXml,ForceAddEnvelopedSignatureTransform" // Sign the XML... success = gen.createXmlDSigSb(sbXml) if success != true { print("\(gen.lastErrorText!)") return } // ----------------------------------------------- // Now to send the SOAP request.... // Note: Chilkat did not actually test this by sending the request to the server. // The example is a best-guess for how to do it... var strXml: String? = sbXml.getAsString() var responseStatusCode: Int // We'll need to add this in the HTTP header: // SOAPAction: "https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN" // Note: This is for the RegisterWTN request. // You may wish to load the WSDL at https://efiskalizimi-test.tatime.gov.al/FiscalizationService-v1/FiscalizationService.wsdl into the SoapUI application to see // the SOAP requests.. let http = CkoHttp()! http.setRequestHeader("SOAPAction", value: "https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN") // Some services expect the content-type in the HTTP header to be "application/xml" while // other expect text/xml. The default sent by Chilkat is "application/xml", but this web service // likely expects "text/xml". Therefore, change the content-type: http.setRequestHeader("Content-Type", value: "text/xml; charset=utf-8") // The testing endpoint for this soap service is likely: var endPoint: String? = "https://efiskalizimi-test.tatime.gov.al/FiscalizationService-v1" var resp: CkoHttpResponse? = http.postXml(endPoint, xmlDoc: strXml, charset: "utf-8") if http.lastMethodSuccess != true { print("\(http.lastErrorText!)") print("LastHeader:") print("\(http.lastHeader!)") return } responseStatusCode = resp!.statusCode.intValue // Check that the responseStatusCode equals 200... print("Response Status Code: \(responseStatusCode)") // Examine the exact HTTP header sent with the POST like this: print("LastHeader:") print("\(http.lastHeader!)") // Examine the XML returned by the web service: print("XML Response:") let xmlResp = CkoXml()! xmlResp.load(resp!.bodyStr) print("\(xmlResp.getXml()!)") // Use this online tool to generate parsing code from response XML: // Generate Parsing Code from XML resp = nil } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.