Docusign系列(三) - SOAP 方式调用Docusign

xieyixiao 2020-04-16

经过之前的操作,对Docusign的基本功能有了一个比较全面的认识,那么这篇博客将教你如何调用Docusign的SOAP API 接口来实现电子签名的功能

首先在Docusign提供的官方文档中找到SOAP API文档,Docusign有两个SOAP API的服务端点:API.ASMX和DSAPI.ASMX

  • https://demo.docusign.net/api/3.0/dsapi.asmx
  • https://demo.docusign.net/api/3.0/api.asmx

Docusign官方建议使用DSAPI.ASMX端点集成,其主要优点是它使用X-DocuSign-Authentication标准,而不是WSE3 Username Token执行认证,与之相比,X-DocuSign-Authentication对调用方法而言更容易实现

那么在打开https://demo.docusign.net/api/3.0/dsapi.asmx后,这是一个XML的文件,将这个XML通过WSDL的方式生成Salesforce中的Apex类(操作方法可以参考我的博客:Salesforce SOAP 接口集成服务), 直接导入会遇到一个ERROR信息:

  • Failed to parse wsdl: Found more than one wsdl:binding. WSDL with multiple binding not supported

这是因为daspi.XML这个文件的最后有这样两行

<wsdl:port name="DSAPIServiceSoap" binding="tns:DSAPIServiceSoap">
      <soap:address location="https://demo.docusign.net/api/3.0/dsapi.asmx" />
</wsdl:port>
<wsdl:port name="DSAPIServiceSoap12" binding="tns:DSAPIServiceSoap12">
      <soap12:address location="https://demo.docusign.net/api/3.0/dsapi.asmx" />
</wsdl:port>

以及与这两个Port对应的Bing

<wsdl:binding name="DSAPIServiceSoap" type="tns:DSAPIServiceSoap">
<wsdl:binding name="DSAPIServiceSoap12" type="tns:DSAPIServiceSoap">

删除其中之一,即可重新导入,这里我们删除后面的DSAPIServiceSoap12, 记得将生成的Apex命名为: DocusignApi

OK, 完成这一步之后,我们需要准备Docusign账号的信息,包括

  • UserId:docusign账号的36位Id
  • AccountId:API Account Id
  • password:Docusign 账号的密码
  • IntegratoryKey:接口的key值
  • endpoint:https://demo.docusign.net/api/3.0/dsapi.asmx

其中 IntegratoryKey 的值需要在Docusign中申请, Docusign Admin  →  INTEGRATIONS → API and Keys

Docusign系列(三) -  SOAP 方式调用Docusign

Tips: 在API and Key 里,你也可以找到自己账户的UserId和AccountId

准备好上述内容后,我们上传一个需要电子签名的模板到Docusign中

首先进入Salesforce → Docusign eSignature → Docusign Admin → custom tag,新建一个签名的tag:SignName,然后在准备上传的DOC模板加上这个Tag

Docusign系列(三) -  SOAP 方式调用Docusign Docusign系列(三) -  SOAP 方式调用Docusign

选择Template页签,新建模板, 上传完成演示模板后,将模板的Id取出来保存:TemplateId

Docusign系列(三) -  SOAP 方式调用DocusignDocusign系列(三) -  SOAP 方式调用Docusign 

我们创建一个class: DocusignDemo

/**
 * @Author:Ricardo
 * @funtion:test docusign soap Api
 * @time:2020-04-16
 * */
public without sharing class DocusignDemo {
    public static String USERID = ‘XXXX‘;
    public static String ACCOUNTID = ‘XXXXX‘;
    public static String PASSWORD = ‘XXXXX‘;
    public static String INTEGRATORKEY = ‘XXXXXX‘;
    public static String ENDPOINT = ‘https://demo.docusign.net/api/3.0/dsapi.asmx‘;
    
    @future(callout=true)
    public static void sendWithDocusign(String recordId){
        Contract con = [SELECT id FROM Contract WHERE Id=:recordId];
        
        DocusignApi.DSAPIServiceSoap dsApi = new DocusignApi.DSAPIServiceSoap();
        dsApi.endpoint_x = ENDPOINT;
        
        //Set Authentication
        String auth = ‘<DocuSignCredentials>‘
                        + ‘<Username>‘+ USERID + ‘</Username>‘ 
                        + ‘<Password>‘ + PASSWORD + ‘</Password>‘ 
                        + ‘<IntegratorKey>‘ + INTEGRATORKEY + ‘</IntegratorKey>‘ 
                    + ‘</DocuSignCredentials>‘;
        System.debug(‘Setting authentication to: ‘ + auth);
            
        dsApi.inputHttpHeaders_x = new Map<String, String>();
        dsApi.inputHttpHeaders_x.put(‘X-DocuSign-Authentication‘, auth);
        
        // 发送Docusign中的模板
        DocuSignAPI.TemplateReference templateReference = new DocuSignAPI.TemplateReference();
        templateReference.Template = ‘templateId‘;// 传入模板ID
        templateReference.TemplateLocation = ‘Server‘;
        
        DocusignApi.ArrayOfTemplateReference templateReferences = new DocusignApi.ArrayOfTemplateReference();
        templateReferences.TemplateReference = new DocusignApi.TemplateReference[]{templateReference};
            
       // 第一个签字
        DocusignApi.Recipient firstSigner = new DocusignApi.Recipient();
        firstSigner.ID = 1;
        firstSigner.Type_x = ‘Signer‘;// 收件人类型 - Signer表示需要签字
        firstSigner.RoutingOrder = 1;// 签字顺序
        firstSigner.Email = ‘‘;
        firstSigner.UserName = ‘第一个签字‘;
        firstSigner.RequireIDLookup = false;
        
        // 第二个签字
        DocusignApi.Recipient secondSigner = new DocusignApi.Recipient();
        secondSigner.ID = 2;
        secondSigner.Type_x = ‘Signer‘;
        secondSigner.RoutingOrder = 2;
        secondSigner.Email = ‘‘;
        secondSigner.UserName = ‘第二个签字‘;
        secondSigner.RequireIDLookup = false;
        
        // 第三个收件人
        DocusignApi.Recipient ccCopy = new DocusignApi.Recipient();
        ccCopy.ID = 3;
        ccCopy.Type_x = ‘CarbonCopy‘;// 抄送给这个收件人,并不需要其签字
        ccCopy.RoutingOrder = 3;
        ccCopy.Email = ‘‘;
        ccCopy.UserName = ‘不用签字‘;
        ccCopy.RequireIDLookup = false;
        
        DocusignApi.ArrayOfRecipient1 recipients = new DocusignApi.ArrayOfRecipient1();
        recipients.Recipient = new DocusignApi.Recipient[]{firstSigner,secondSigner,ccCopy};
        
        DocusignApi.EnvelopeInformation envelopeInfo = new DocusignApi.EnvelopeInformation();
        envelopeInfo.AccountId = ACCOUNTID;
        envelopeInfo.Subject = ‘email subject‘;// Max 100 characters
        envelopeInfo.EmailBlurb =  ‘email message.‘;// Max 10000 characters
            
        DocusignApi.CustomField platformName = new DocusignApi.CustomField (); 
        platformName.Name = ‘PlatformName‘;
        platformName.Value = ‘Salesforce‘;
        platformName.Show = ‘False‘;
        
        DocusignApi.CustomField appName = new DocusignApi.CustomField (); 
        appName.Name = ‘AppName‘;
        appName.Value = ‘DocuSignForSalesforce‘;
        appName.Show = ‘False‘;
        
        DocusignApi.CustomField SFContract = new DocusignApi.CustomField (); 
        SFContract.Name = ‘##SFContract‘;
        SFContract.Value = recordId;
        SFContract.Show = ‘False‘;
        
        DocusignApi.CustomField DSFSSourceObjectId = new DocusignApi.CustomField (); 
        DSFSSourceObjectId.Name = ‘DSFSSourceObjectId‘;
        DSFSSourceObjectId.Value = recordId + ‘~Contract‘;
        DSFSSourceObjectId.Show = ‘False‘;
        
        DocusignApi.ArrayOfCustomField arrayOfCustomFields = new DocusignApi.ArrayOfCustomField();
        arrayOfCustomFields.CustomField  = new list<DocusignApi.CustomField>();
        arrayOfCustomFields.CustomField.add(platformName);
        arrayOfCustomFields.CustomField.add(appName);
        arrayOfCustomFields.CustomField.add(SFContract);
        arrayOfCustomFields.CustomField.add(DSFSSourceObjectId);        
            
        System.debug(‘*** arrayOfCustomFields:‘ + arrayOfCustomFields);        
        envelopeInfo.CustomFields = arrayOfCustomFields;
        
        try {
            DocusignApi.EnvelopeStatus es = dsApi.CreateEnvelopeFromTemplates(templateReferences,recipients,envelopeInfo,true);
            String envelopeId = es.EnvelopeID;
            System.debug(LoggingLevel.INFO, ‘*** EnvelopeID:‘ + es.EnvelopeID);
            System.debug(LoggingLevel.INFO, ‘*** Status:‘ + es.Status);
        } catch ( CalloutException e) {
            System.debug(‘Exception - ‘ + e );
        }
    }
}

最后,在Salesforce的Console控制台中,执行一段代码:

DocusignDemo.sendWithDocusign(‘8002w000000HXkJ‘);

 在设定的邮件中可以查看到需要签字的邮件,签字的文档显示如下:

Docusign系列(三) -  SOAP 方式调用Docusign

以上,是关于在Salesforce中使用SOPA API的方式调用Docusign的演示demo,如有问题,欢迎留言交流

相关推荐

amberom / 0评论 2014-03-02