Procedure for Receiving an EDI Document using AS2
This procedure of receiving an EDI document using AS2 is based on asynchronous mode of communication. That is, the HTTP connection has disconnected and this program will reconnect on a new HTTP connection when it sends back the MDN response. The step are as follows:
Example in Visual Basic
This example will use the following:
In this example, the EDI document received is has been encrypted and signed.
Create an instance of ediDocument, and apply environment settings. The ediDocument object is the top level application instance for Framework EDI (FREDI). This object is the topmost object in the object model hierarchy of FREDI. This instance is always created at the start of a program.
Dim oEdiDoc As Fredi.ediDocument
Dim oSchemas As Fredi.ediSchemas
Dim oSchema As Fredi.ediSchema
' Instance of FREDI document.
Set oEdiDoc = New Fredi.ediDocument
' Default to use Local Machine key containers.
oEdiDoc.Option(OptDocument_MachineKeySet) = 1
' To traverse all the data segments in the document at the best possible speed, enable Cursor_ForwardOnly.
oEdiDoc.CursorType = Cursor_ForwardOnly
' Disable standard reference library.
Set oSchemas = oEdiDoc.GetSchemas
oSchemas.EnableStandardReference = False
' Load implementation guideline used by the EDI document.
Set oSchema = oEdiDoc.LoadSchema(App.Path & "\SampleSefX12_850.SEF", Schema_Standard_Exchange_Format)
Set up digital certificates. The ediSecurities object is used to configure default security settings.
Dim oSecurities As Fredi.ediSecurities
Set oSecurities = oEdiDoc.GetSecurities
' Set the default service provider, which provides the necessary cryptographic services.
oSecurities.DefaultProviderName = "Microsoft Base Cryptographic Provider v1.0"
The received document is encrypted and signed. The document must have been encrypted using the public key of this receiver's certificate "Edidev XYZ Test Company", and in turn the same certificate is used to decrypt the document. When verifying the signature of the document, the public key of the sender's certificate "Edidev ABC Test Company" is used. To reference the certificates, they have to be already in the certificate store otherwise they have to be imported to the certificate store. To verify if the certificate already exists in the certificate store, call the IsCertificateExists method in the ediSecurities object. To import the certificate, call the ImportCertificate method.
Dim oVerifyCert As Fredi.ediSecurityCertificate
Dim oDecrypt As Fredi.ediSecurityCertificate
If Not oSecurities.IsCertificateExists("Edidev XYZ Test Company") Then
Set oDecrypt = oSecurities.ImportCertificate(App.Path & "\XyzTestCompany.cer")
Set oDecrypt = oSecurities.GetCertificateBySubjectName("Edidev XYZ Test Company")
If Not oSecurities.IsCertificateExists("Edidev ABC Test Company") Then
Set oVerifyCert = oSecurities.ImportCertificate(App.Path & "\AbcTestCompany.cer")
Set oVerifyCert = oSecurities.GetCertificateBySubjectName("Edidev ABC Test Company")
Since the certificate can only hold the public key, it must be associated to a private key in a key container. The associated private key is then used to decrypt the document. In this example, however, the certificate was created from a key container and hence the private key in the public/private key pair will be used. Knowing the name of the key container where the public key was extracted for the certificate, the private key is associated to the certificate by calling the UpdateCSP method.
Get instance of mailDocument. The mailDocument is used process the AS2 document. The document is not in raw EDI format but rather in S/MIME format which the mailDocument is responsible for handling.
Dim oMailDocument As Fredi.mailDocument
Set oMailDocument = oEdiDoc.GetMailDocument
Load AS2 document. Load the AS2 document through the mailDocument object. When loaded successfully, the EDI file in the AS2 document is extracted and is made available in the ediDocument object, and data can then be processed. Traverse the data segments in the EDI document.
' Load AS2 document via the mailDocument object.
oMailDocument.Load App.Path & "\SampleEdiX12_850.AS2.TXT"
' The EDI data is now made available in the ediDocument object.
Dim oSegment As Fredi.ediDataSegment
' Traverse all the data segments in the document.
Set oSegment = oEdiDoc.FirstDataSegment
While Not oSegment Is NothingSet oSegment = oEdiDoc.NextDataSegmentWend
' Do something ...
Generate the MDN response. The mailDocument object consists of two MIME messages: the main MIME message and the Message Disposition Notification (MDN) message. When the AS2 document is loaded it resides in the main MIME message. Prepare the MDN message for sending as follows:
Dim oMDN As Fredi.mailMessage
' Get the MDN part of the mail object.
Set oMDN = oMailDocument.GetMDN
' Get the security of the MDN object so that it can be configured.
Set oSecurity = oMDN.GetSecurity
' Specify the subject name of the certificate that will be used to sign the MDN, if required.
oSecurity.CertificateSignerName = "Edidev XYZ Test Company"
' Refresh the MDN to regenerate it after all errors have been detected.
Add AS2 specific headers to MDN message. Most of the AS2 specific headers in the MDN would already be populated based on the AS2 message received and processed. Add additional headers depending on your business specifications. In this example, we add:
To set the header, set the HeaderFieldValue with the correct parameter. The source code looks as follows:
oMDN.HeaderFieldValue("Message-ID") = "firstname.lastname@example.org"
Configure ediTransport for destination. Get the ediTransport object of the MDN and configure it with the destination address that the message is to be sent to.
Dim oTransports As Fredi.ediTransports
Dim oMdnDestination As Fredi.ediTransport
' Get transports object from MDN
Set oTransports = oMailDocument.GetMdnTransports
' Create a single transport destination
Set oMdnDestination = oTransports.CreateTransport
' Set the properties of the transport object to specify where to send MDN
oMdnDestination.User = "john_smith"
oMdnDestination.Password = "password"
oMdnDestination.Address = "www.global.net"
oMdnDestination.TargetPath = "/testfiles/"
Send MDN response. Call the Send method to send the MDN message. The file name "MDN.AS2.TXT" is the name of the file when it arrives at the destination server.