AutoIt
AutoIt
Create ITIDA Signed JSON and Send to ETA (Egypt Tax Authority) Portal
See more Egypt ITIDA Examples
Demonstrates how to create a .p7s signature that fits Egypt's ITIDA requirements where Chilkat automatically does the ITIDA JSON canonicalization. Also shows the code to send to the ETA Portal.Chilkat AutoIt Downloads
Local $bSuccess = False
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oCrypt = ObjCreate("Chilkat.Crypt2")
$oCrypt.VerboseLogging = True
$oCert = ObjCreate("Chilkat.Cert")
$oCert.VerboseLogging = True
; Set the smart card PIN, which will be needed for signing.
$oCert.SmartCardPin = "12345678"
; There are many ways to load the certificate.
; This example was created for a customer using an ePass2003 USB token.
; Assuming the USB token is the only source of a hardware-based private key..
$bSuccess = $oCert.LoadFromSmartcard("")
If ($bSuccess = False) Then
ConsoleWrite($oCert.LastErrorText & @CRLF)
Exit
EndIf
; Tell the crypt class to use this cert.
$bSuccess = $oCrypt.SetSigningCert($oCert)
If ($bSuccess = False) Then
ConsoleWrite($oCrypt.LastErrorText & @CRLF)
Exit
EndIf
$oCmsOptions = ObjCreate("Chilkat.JsonObject")
; Setting "DigestData" causes OID 1.2.840.113549.1.7.5 (digestData) to be used.
$oCmsOptions.UpdateBool("DigestData",True)
$oCmsOptions.UpdateBool("OmitAlgorithmIdNull",True)
; Indicate that we are passing normal JSON and we want Chilkat do automatically
; do the ITIDA JSON canonicalization:
$oCmsOptions.UpdateBool("CanonicalizeITIDA",True)
$oCrypt.CmsOptions = $oCmsOptions.Emit()
; The CadesEnabled property applies to all methods that create CMS/PKCS7 signatures.
; To create a CAdES-BES signature, set this property equal to true.
$oCrypt.CadesEnabled = True
$oCrypt.HashAlgorithm = "sha256"
$oJsonSigningAttrs = ObjCreate("Chilkat.JsonObject")
$oJsonSigningAttrs.UpdateInt("contentType",1)
$oJsonSigningAttrs.UpdateInt("signingTime",1)
$oJsonSigningAttrs.UpdateInt("messageDigest",1)
$oJsonSigningAttrs.UpdateInt("signingCertificateV2",1)
$oCrypt.SigningAttributes = $oJsonSigningAttrs.Emit()
; By default, all the certs in the chain of authentication are included in the signature.
; If desired, we can choose to only include the signing certificate:
$oCrypt.IncludeCertChain = False
; Pass a JSON document such as the following. Chilkat will do the ITIDA canonicalization.
; (It is the canonicalized JSON that gets signed.)
; {
; "issuer":{
; "address":{
; "branchID":"0",
; "country":"EG",
; "regionCity":"Cairo",
; "postalCode":"",
; "buildingNumber":"0",
; "street":"123rd Street",
; "governate":"GOVERNATE"
; },
; "type":"B",
; "id":"209999899",
; "name":"Xyz SAE"
; },
; "receiver":{
; "address":{
; "country":"EG",
; "regionCity":"CAIRO",
; "postalCode":"11435",
; "buildingNumber":"0",
; "street":"Autostrad Road Abc",
; "governate":"GOVERNATE"
; },
; "type":"B",
; "id":"999999999",
; "name":"XYZ EGYPT FOR TRADE"
; },
; "documentType":"I",
; "documentTypeVersion":"1.0",
; "dateTimeIssued":"2020-11-15T11:04:53Z",
; "taxpayerActivityCode":"1073",
; "internalID":"ZZZZ999",
; "purchaseOrderReference":"2009199918",
; "salesOrderReference":"",
; "payment":{
; "bankName":"",
; "bankAddress":"",
; "bankAccountNo":"",
; "bankAccountIBAN":"",
; "swiftCode":"",
; "terms":""
; },
; "delivery":{
; "approach":"",
; "packaging":"",
; "dateValidity":"",
; "exportPort":"",
; "countryOfOrigin":"EG",
; "grossWeight":0,
; "netWeight":0,
; "terms":""
; },
; "invoiceLines":[
; {
; "description":"CDM Widget 48GX99X12BA",
; "itemType":"GS1",
; "itemCode":"7622213335056",
; "unitType":"CS",
; "quantity":1.00,
; "unitValue":{
; "currencySold":"EGP",
; "amountEGP":588.67,
; "amountSold":0,
; "currencyExchangeRate":0
; },
; "salesTotal":588.67,
; "total":603.97,
; "valueDifference":0,
; "totalTaxableFees":0,
; "netTotal":529.8,
; "itemsDiscount":0,
; "discount":{
; "rate":10.00,
; "amount":58.87
; },
; "taxableItems":[
; {
; "taxType":"T1",
; "amount":74.17,
; "subType":"No sub",
; "rate":14.00
; }
; ],
; "internalCode":"9099994"
; }
; ],
; "totalSales":588.67,
; "totalSalesAmount":588.67,
; "totalDiscountAmount":58.87,
; "netAmount":529.80,
; "taxTotals":[
; {
; "taxType":"T1",
; "amount":74.17
; }
; ],
; "extraDiscountAmount":0,
; "totalItemsDiscountAmount":0,
; "totalAmount":603.97,
; }
; Build the above JSON..
; Use this online tool to generate code from sample JSON:
; Generate Code to Create JSON
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("issuer.address.branchID","0")
$oJson.UpdateString("issuer.address.country","EG")
$oJson.UpdateString("issuer.address.regionCity","Cairo")
$oJson.UpdateString("issuer.address.postalCode","")
$oJson.UpdateString("issuer.address.buildingNumber","0")
$oJson.UpdateString("issuer.address.street","123rd Street")
$oJson.UpdateString("issuer.address.governate","GOVERNATE")
$oJson.UpdateString("issuer.type","B")
$oJson.UpdateString("issuer.id","209999899")
$oJson.UpdateString("issuer.name","Xyz SAE")
$oJson.UpdateString("receiver.address.country","EG")
$oJson.UpdateString("receiver.address.regionCity","CAIRO")
$oJson.UpdateString("receiver.address.postalCode","11435")
$oJson.UpdateString("receiver.address.buildingNumber","0")
$oJson.UpdateString("receiver.address.street","Autostrad Road Abc")
$oJson.UpdateString("receiver.address.governate","GOVERNATE")
$oJson.UpdateString("receiver.type","B")
$oJson.UpdateString("receiver.id","999999999")
$oJson.UpdateString("receiver.name","XYZ EGYPT FOR TRADE")
$oJson.UpdateString("documentType","I")
$oJson.UpdateString("documentTypeVersion","1.0")
$oJson.UpdateString("dateTimeIssued","2020-11-15T11:04:53Z")
$oJson.UpdateString("taxpayerActivityCode","1073")
$oJson.UpdateString("internalID","ZZZZ999")
$oJson.UpdateString("purchaseOrderReference","2009199918")
$oJson.UpdateString("salesOrderReference","")
$oJson.UpdateString("payment.bankName","")
$oJson.UpdateString("payment.bankAddress","")
$oJson.UpdateString("payment.bankAccountNo","")
$oJson.UpdateString("payment.bankAccountIBAN","")
$oJson.UpdateString("payment.swiftCode","")
$oJson.UpdateString("payment.terms","")
$oJson.UpdateString("delivery.approach","")
$oJson.UpdateString("delivery.packaging","")
$oJson.UpdateString("delivery.dateValidity","")
$oJson.UpdateString("delivery.exportPort","")
$oJson.UpdateString("delivery.countryOfOrigin","EG")
$oJson.UpdateInt("delivery.grossWeight",0)
$oJson.UpdateInt("delivery.netWeight",0)
$oJson.UpdateString("delivery.terms","")
$oJson.UpdateString("invoiceLines[0].description","CDM Widget 48GX99X12BA")
$oJson.UpdateString("invoiceLines[0].itemType","GS1")
$oJson.UpdateString("invoiceLines[0].itemCode","7622213335056")
$oJson.UpdateString("invoiceLines[0].unitType","CS")
$oJson.UpdateNumber("invoiceLines[0].quantity","1.00")
$oJson.UpdateString("invoiceLines[0].unitValue.currencySold","EGP")
$oJson.UpdateNumber("invoiceLines[0].unitValue.amountEGP","588.67")
$oJson.UpdateInt("invoiceLines[0].unitValue.amountSold",0)
$oJson.UpdateInt("invoiceLines[0].unitValue.currencyExchangeRate",0)
$oJson.UpdateNumber("invoiceLines[0].salesTotal","588.67")
$oJson.UpdateNumber("invoiceLines[0].total","603.97")
$oJson.UpdateInt("invoiceLines[0].valueDifference",0)
$oJson.UpdateInt("invoiceLines[0].totalTaxableFees",0)
$oJson.UpdateNumber("invoiceLines[0].netTotal","529.8")
$oJson.UpdateInt("invoiceLines[0].itemsDiscount",0)
$oJson.UpdateNumber("invoiceLines[0].discount.rate","10.00")
$oJson.UpdateNumber("invoiceLines[0].discount.amount","58.87")
$oJson.UpdateString("invoiceLines[0].taxableItems[0].taxType","T1")
$oJson.UpdateNumber("invoiceLines[0].taxableItems[0].amount","74.17")
$oJson.UpdateString("invoiceLines[0].taxableItems[0].subType","No sub")
$oJson.UpdateNumber("invoiceLines[0].taxableItems[0].rate","14.00")
$oJson.UpdateString("invoiceLines[0].internalCode","9099994")
$oJson.UpdateNumber("totalSales","588.67")
$oJson.UpdateNumber("totalSalesAmount","588.67")
$oJson.UpdateNumber("totalDiscountAmount","58.87")
$oJson.UpdateNumber("netAmount","529.80")
$oJson.UpdateString("taxTotals[0].taxType","T1")
$oJson.UpdateNumber("taxTotals[0].amount","74.17")
$oJson.UpdateInt("extraDiscountAmount",0)
$oJson.UpdateInt("totalItemsDiscountAmount",0)
$oJson.UpdateNumber("totalAmount","603.97")
$oJson.EmitCompact = True
Local $sJsonToSign = $oJson.Emit()
; Create the CAdES-BES signature.
$oCrypt.EncodingMode = "base64"
; Make sure we sign the utf-8 byte representation of the JSON string
$oCrypt.Charset = "utf-8"
Local $sigBase64 = $oCrypt.SignStringENC($sJsonToSign)
If ($oCrypt.LastMethodSuccess = False) Then
ConsoleWrite($oCrypt.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Base64 signature:" & @CRLF)
ConsoleWrite($sigBase64 & @CRLF)
; Insert the base64 signature into the JSON to be sent
$oJson.UpdateString("signatures[0].signatureType","I")
$oJson.UpdateString("signatures[0].value",$sigBase64)
; Wrap the JSON in {"documents":[ ... ]}
$oSbToSend = ObjCreate("Chilkat.StringBuilder")
$oSbToSend.Append("{""documents"":[")
$oSbToSend.Append($oJson.Emit())
$oSbToSend.Append("]}")
; ------------------------------------------------------------------------
; Get an access token using our client ID and client secret key
Local $sClientId = "abc999ff-1234"
Local $sClientSecretKey = "123fff22-1234-abcd"
$oHttp = ObjCreate("Chilkat.Http")
; Causes the Authorization: Basic header to be added..
$oHttp.Login = $sClientId
$oHttp.Password = $sClientSecretKey
$oHttp.BasicAuth = True
$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.HttpVerb = "POST"
$oReq.Path = "/connect/token"
$oReq.ContentType = "application/x-www-form-urlencoded"
$oReq.AddParam "grant_type","client_credentials"
$oReq.AddHeader "Connection","close"
$oHttp.Accept = "application/json"
$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpReq("https://id.preprod.eta.gov.eg/connect/token",$oReq,$oResp)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
$oHttp.CloseAllConnections()
ConsoleWrite("Response status code: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Response body:" & @CRLF)
ConsoleWrite($oResp.BodyStr & @CRLF)
If ($oResp.StatusCode <> 200) Then
ConsoleWrite("Failed." & @CRLF)
Exit
EndIf
$oJsonToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJsonToken.Load($oResp.BodyStr)
Local $sAccessToken = $oJsonToken.StringOf("access_token")
ConsoleWrite("access_token = " & $sAccessToken & @CRLF)
; ------------------------------------------------------------------------
; Submit the signed JSON to the ETA (Egypt Tax Authority) Portal
; No longer sending basic authentication...
$oHttp.Login = ""
$oHttp.Password = ""
$oHttp.BasicAuth = False
; Setting the AuthToken property causes the "Authorization: Bearer <token>" header to be added to each request.
$oHttp.AuthToken = $sAccessToken
Local $sJsonStr = $oSbToSend.GetAsString()
Local $sUrl = "https://api.preprod.invoicing.eta.gov.eg/api/v1/documentsubmissions"
$bSuccess = $oHttp.HttpStr("POST",$sUrl,$sJsonStr,"utf-8","application/json; charset=utf-8",$oResp)
If ($bSuccess = False) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Response status code: " & $oResp.StatusCode & @CRLF)
ConsoleWrite("Response body:" & @CRLF)
ConsoleWrite($oResp.BodyStr & @CRLF)