2. Check Package Status and Download Documents

Following up on the creating and sending a document package quick start guide, this guide will cover some things you can do after sending the package, such as: checking the package/signing status, downloading the e-signed documents, and downloading the evidence summary.

This guide will pick up right where you left off in the previous guide. If you followed along, you had just looked in your OneSpan Sign inbox to see that your document package (transaction in the new UI) was created and sent properly after executing the Apex class built in the previous guide.

Capture

Configuration

Initial Setup

Add an Apex class named “OssCheckStatusAndDownload” with a function “checkStatusAndDownload()”. If you need help with this setup, head over to the previous guide for further instructions.

Locating the Package ID

Since the package you are working with was created previously, we need to find the package ID for use in this example. If you find your transaction from the UI inbox, as seen above, the URL will have the package ID, like this:

https://sandbox.esignlive.com/a/transaction/{packageId}

In the Apex class you created in the last guide, you will see you had access to this package ID when you created the package and used it to send the package. If you were creating a more complex application, you would likely store this package ID for use within other parts of your application, versus looking it up in your OneSpan Sign URLs.

In our example, in order to print out package ID, you can add this code into your function:

System.debug('PackageId: ' + packageId);

After the line:

String packageId = sdk.createPackage(pkg);

After you run this Apex class, you will see package ID printed in the debug console:

Capture

The Code

In this segment, the code will be explained section by section. You can get the complete sample code from the Developer Community Code Share, here.

Access the Package
Before you created an ESL client and connected to OneSpan Sign, you had two prerequisites to do:

  • Create a Custom Setting where your Api Key and Endpoint are assigned.
  • Add your instance endpoint to your Remote Site Settings.

If you haven’t done these or forgot how to deal with it, you can refer to last guidance for detailed steps.

Again, because this is a very simple example, we write our code in a static function within the same “eslController” Apex class. The first line inside the method is creating an ESL client where internally the SDK will read connection info from above two preparation steps.

//connect to OneSpan Sign
ESignLiveSDK sdk= new ESignLiveSDK();

Then, you will use your PackageId obtained from Web UI or Debug Console as an input parameter to grab your previously sent package from the OneSpan Sign client.

//create a packageId using you packageId string
String packageId = '2APPpsAcqVk8QbJKABFqWvHy-kQ='; 
//get your package
ESignLiveAPIObjects.Package_x pack = sdk.getPackage(packageId);

Check the Status

Now that you have access to the document package, you can check your package to see what the package status is. The possible values are ARCHIVED, COMPLETED, DECLINED, DRAFT, EXPIRED, OPTED_OUT, and SENT. Print the status so you can see it in the console when you execute your class.

//retrieve package status
ESignLiveAPIObjects.PackageStatus packageStatus = pack.status;
System.debug('Package Status: '+packageStatus);

Next, check the signing status of the package. The possible values for this one are ARCHIVED, CANCELED, COMPLETE, DECLINED, EXPIRED, INACTIVE, OPTED_OUT, COMPLETED, and SIGNING_PENDING. Print this status to the console, as well.

//retrieve signing status
String signingStatus = sdk.getSigningStatus(packageId);
System.debug('Signing Status: '+signingStatus);  

Download the Files
For the last part, you will check to see if the package is complete. If it is, all documents will be downloaded, as well as the evidence summary. We will first read all files in blob type, and then insert them into a document folder named “onespan”.

if(packageStatus == ESignLiveAPIObjects.PackageStatus.COMPLETED &&
   signingStatus .equals('COMPLETED')
){
System.debug('Transaction is completed and ready to download!');
                              
//download all documents
List docs = new List();
Folder folder = [select id from Folder where name='onespan' LIMIT 1]; 
                
for(ESignLiveAPIObjects.Document document: pack.documents){
    Blob response = sdk.downloadDocument(packageId, document.id);
                    
    Document doc = new Document();
    doc.Body = response;
    doc.FolderId = folder.id;
    doc.Name = pack.name + ' - ' + document.name;
    doc.Type = 'pdf';
    docs.add(doc);
}
                           
//download evidence summary
String auditString= sdk.getAudit(packageId);
Document auditDoc= new Document();
auditDoc.Body = EncodingUtil.base64Decode(auditString);
auditDoc.FolderId = folder.id;
auditDoc.Name = pack.name + ' - evidence summary';
auditDoc.Type = 'pdf';
                
for(Document doc: docs){
    insert doc;
}
insert auditDoc;                
                
}else{
    System.debug('Transaction is not ready to download!');
}

Running the Code

You can now execute your Apex class. Open up your Developer Console and press Ctrl + E to launch the Execute Anonymous Window. Type in:

new OssCheckStatusAndDownload.checkStatusAndDownload();

If you have not yet signed your package, your Console should look like the following:

Capture

After signing your package, you will get the following output in your Console view:

Capture

If you go to your “onespan” document folder and you will see that your files were successfully downloaded.

Capture

Get the Code