DocuSign - 发送给多个收件人

时间:2017-03-21 15:58:32

标签: docusignapi

我试图找出为什么当我尝试向多个签名者发送文档时我得到了一个" DocuSign \ eSign \ ApiException:[400]错误连接到API({{ 3}})&#34 ;.

当我将代码更改为仅发送给一位签名者时,一切正常。

以下是我使用的代码:

public static function sendForSignature($filenames,  //The name of the file that will show up in the email
                                        $document_paths, //The path to the file on the server
                                        $templates, //The json template file for this document
                                        $opportunity=null, //The opportunity if applicable
                                        $company_id=-1, //The company id if applicable
                                        $contact_id=-1) //The contact if applicable
{   
    if(!is_array($filenames)) $filenames = [$filenames];
    if(!is_array($document_paths)) $document_paths = [$document_paths];
    if(!is_array($templates)) $templates = [$templates];

    $envelop_summary = null;
    $company = null;
    $contact = null;

    $sign_here_tabs=[];
    $full_name_tabs=[];
    $date_tabs=[];
    $text_tabs=[];
    $init_tabs=[];
    $all_documents=[];

    $basePath = Yii::getAlias('@frontend').'/doclib';
    $mappingPath = $basePath.'/mapping';

    $config = new \DocuSign\eSign\Configuration();
    $config->setHost(\Yii::$app->params['docusign_host']);
    $config->addDefaultHeader("X-DocuSign-Authentication", 
        "{\"Username\":\"" . \Yii::$app->params['docusign_username'] . 
        "\",\"Password\":\"" . \Yii::$app->params['docusign_password'] . 
        "\",\"IntegratorKey\":\"" . \Yii::$app->params['docusign_integrator_key'] . "\"}");
    $apiClient = new \DocuSign\eSign\ApiClient($config);

    $accountId = null;

    $organization = Organization::findOne(['id' => Yii::$app->user->getIdentity()->org_id]);
    $user = User::findOne(['id' => Yii::$app->user->getIdentity()->id]);

    $sign_user = User::findOne(['id' => $organization->signer]);

    //Lets grab the 

    if(!is_null($opportunity)){
        $company = $opportunity->company;
        $contact = $opportunity->contact;
    }else{
        $company = Company::findOne(['company_id' => $company_id]);
        $contact = Contact::findOne(['contact_id' => $contact_id]);
    }

    try 
    {
        //*** STEP 1 - Login API
        $authenticationApi = new \DocuSign\eSign\Api\AuthenticationApi($apiClient);
        $options = new \DocuSign\eSign\Api\AuthenticationApi\LoginOptions();
        $loginInformation = $authenticationApi->login($options);

        if(isset($loginInformation) && count($loginInformation) > 0)
        {
            $loginAccount = $loginInformation->getLoginAccounts()[0];
            $host = $loginAccount->getBaseUrl();
            $host = explode("/v2",$host);
            $host = $host[0];

            $config->setHost($host);
            $apiClient = new \DocuSign\eSign\ApiClient($config);

            if(isset($loginInformation))
            {
                $accountId = $loginAccount->getAccountId();

                $envelopeApi = new \DocuSign\eSign\Api\EnvelopesApi($apiClient);

                $index = 0;

                foreach($document_paths as $d){

                    $pdf = new FPDI();
                    $page_count = $pdf->setSourceFile($d);

                    $document = new \DocuSign\eSign\Model\Document();
                    $document->setDocumentBase64(base64_encode(file_get_contents($d)));
                    $document->setName($filenames[$index]);
                    $document->setDocumentId($index+1);
                    $all_documents[]=$document;

                    $template_file = json_decode(file_get_contents($mappingPath .'/'. $templates[$index]));

                    foreach($template_file as $t){
                        if($t->type == 'signHereTab'){
                            $signHere = new \DocuSign\eSign\Model\SignHere();

                            if(isset($t->anchorString)){
                                $signHere->setAnchorString($t->anchorString);
                                $signHere->setAnchorXOffset($t->anchorXOffset);
                                $signHere->setAnchorYOffset($t->anchorYOffset);
                                $signHere->setAnchorUnits($t->anchorUnits);
                            }else if(isset($t->xPosition)){
                                $signHere->setXPosition($t->xPosition);
                                $signHere->setYPosition($t->yPosition);
                            }

                            // $signHere->setAnchorIgnoreIfNotPresent($t->anchorIgnoreIfNotPresent);
                            $signHere->setDocumentId($index+1);
                            if($t->page == -1){
                                $signHere->setPageNumber($page_count);
                            }else{
                                $signHere->setPageNumber($t->page);
                            }

                            if(!isset($t->recipient_id)){
                                $signHere->setRecipientId("1");
                                $sign_here_tabs[0][] = $signHere;
                            }else{
                                $signHere->setRecipientId($t->recipient_id);
                                $sign_here_tabs[1][] = $signHere;
                            }

                        }else if($t->type == 'fullNameTab'){
                            $fullName = new \DocuSign\eSign\Model\FullName;
                            if(isset($t->anchorString)){
                                $fullName->setAnchorString($t->anchorString);
                                $fullName->setAnchorXOffset($t->anchorXOffset);
                                $fullName->setAnchorYOffset($t->anchorYOffset);
                                $fullName->setAnchorUnits($t->anchorUnits);
                            }else if(isset($t->xPosition)){
                                $fullName->setXPosition($t->xPosition);
                                $fullName->setYPosition($t->yPosition);
                            }

                            // $fullName->setAnchorIgnoreIfNotPresent($t->anchorIgnoreIfNotPresent);
                            $fullName->setDocumentId($index+1);
                            if($t->page == -1){
                                $fullName->setPageNumber($page_count);
                            }else{
                                $fullName->setPageNumber($t->page);
                            }

                            if(!isset($t->recipient_id)){
                                $fullName->setRecipientId(1);
                                $full_name_tabs[0][] = $fullName;
                            }else{
                                $fullName->setRecipientId($t->recipient_id);
                                $full_name_tabs[1][] = $fullName;
                            }

                        }else if($t->type == 'textTab'){
                            $text = new \DocuSign\eSign\Model\Text;
                            if(isset($t->anchorString)){
                                $text->setAnchorString($t->anchorString);
                                $text->setAnchorXOffset($t->anchorXOffset);
                                $text->setAnchorYOffset($t->anchorYOffset);
                                $text->setAnchorUnits($t->anchorUnits);
                            }else if(isset($t->xPosition)){
                                $text->setXPosition($t->xPosition);
                                $text->setYPosition($t->yPosition);
                            }

                            if(isset($t->label))
                                $text->setTabLabel($t->label);
                            // $text->setAnchorIgnoreIfNotPresent($t->anchorIgnoreIfNotPresent);
                            $text->setDocumentId($index+1);
                            if($t->page == -1){
                                $text->setPageNumber($page_count);
                            }else{
                                $text->setPageNumber($t->page);
                            }

                            if(!isset($t->recipient_id)){
                                $text->setRecipientId("1");
                                $text_tabs[0][] = $text;
                            }else{
                                $text->setRecipientId($t->recipient_id);
                                $text_tabs[1][] = $text;
                            }

                        }else if($t->type == 'dateTab'){

                            $date = new \DocuSign\eSign\Model\Date;

                            if(isset($t->anchorString)){
                                $date->setAnchorString($t->anchorString);
                                $date->setAnchorXOffset($t->anchorXOffset);
                                $date->setAnchorYOffset($t->anchorYOffset);
                                $date->setAnchorUnits($t->anchorUnits);
                            }else if(isset($t->xPosition)){
                                $date->setXPosition($t->xPosition);
                                $date->setYPosition($t->yPosition);
                            }

                            // $date->setAnchorIgnoreIfNotPresent($t->anchorIgnoreIfNotPresent);
                            $date->setDocumentId($index+1);
                            if($t->page == -1){
                                $date->setPageNumber($page_count);
                            }else{
                                $date->setPageNumber($t->page);
                            }

                            if(!isset($t->recipient_id)){
                                $date->setRecipientId(1);
                                $date_tabs[0][] = $date;
                            }
                            else{
                                $date->setRecipientId($t->recipient_id);
                                $date_tabs[1][] = $date;
                            }

                        }else if($t->type == 'initialHereTab'){
                            $iht = new \DocuSign\eSign\Model\InitialHere;

                            if(isset($t->anchorString)){
                                $iht->setAnchorString($t->anchorString);
                                $iht->setAnchorXOffset($t->anchorXOffset);
                                $iht->setAnchorYOffset($t->anchorYOffset);
                                $iht->setAnchorUnits($t->anchorUnits);
                            }else if(isset($t->xPosition)){
                                $iht->setXPosition($t->xPosition);
                                $iht->setYPosition($t->yPosition);
                            }

                            if($t->page == -1){
                                $iht->setPageNumber($page_count);
                            }else{
                                $iht->setPageNumber($t->page);
                            }

                            if(!isset($t->recipient_id)){
                                $iht->setRecipientId(1);
                                $init_tabs[0][] = $iht;
                            }else{
                                $iht->setRecipientId($t->recipient_id);
                                $init_tabs[1][] = $iht;
                            }                           
                        }
                    }

                    $index++;
                }                       

                $tabs = new \DocuSign\eSign\Model\Tabs();

                if(isset($sign_here_tabs[0]) && count($sign_here_tabs[0]) > 0)
                    $tabs->SetSignHereTabs($sign_here_tabs[0]);
                if(isset($full_name_tabs[0]) && count($full_name_tabs[0]) > 0)
                    $tabs->setFullNameTabs($full_name_tabs[0]);
                if(isset($text_tabs[0]) && count($text_tabs[0]) > 0)
                    $tabs->setTextTabs($text_tabs[0]);
                if(isset($date_tabs[0]) && count($date_tabs[0]) > 0)
                    $tabs->setDateTabs($date_tabs[0]);
                if(isset($init_tabs[0]) && count($init_tabs[0]) > 0)
                    $tabs->setInitialHereTabs($init_tabs[0]);

                $signer = new \DocuSign\eSign\Model\Signer();
                $signer->setEmail(trim($contact->email));
                $signer->setName($contact->first_name.' '.$contact->last_name);                                     
                $signer->setRecipientId("1");                                                   
                $signer->setTabs($tabs);
                $signer->setClientUserId("1234");

                $c_tabs = new \DocuSign\eSign\Model\Tabs();

                if(isset($sign_here_tabs[1]) && count($sign_here_tabs[1]) > 0)
                    $c_tabs->SetSignHereTabs($sign_here_tabs[1]);
                if(isset($full_name_tabs[1]) && count($full_name_tabs[1]) > 0)
                    $c_tabs->setFullNameTabs($full_name_tabs[1]);
                if(isset($text_tabs[1]) && count($text_tabs[1]) > 0)
                    $c_tabs->setTextTabs($text_tabs[1]);
                if(isset($date_tabs[1]) && count($date_tabs[1]) > 0)
                    $c_tabs->setDateTabs($date_tabs[1]);
                if(isset($init_tabs[1]) && count($init_tabs[1]) > 0)
                    $c_tabs->setInitialHereTabs($init_tabs[1]);

                $c_signer = new \DocuSign\eSign\Model\Signer();
                $c_signer->setEmail(trim($sign_user->email));
                $c_signer->setName($sign_user->first_name.' '.$sign_user->last_name);
                $c_signer->setRecipientId("2");
                $c_signer->setTabs($c_tabs);
                $c_signer->setClientUserId("2345");

                $emailNotification = new \DocuSign\eSign\Model\RecipientEmailNotification();
                $emailNotification->setEmailSubject('Please sign these document(s).');
                $signer->setEmailNotification($emailNotification);
                $c_signer->setEmailNotification($emailNotification);

                // Add a recipient to sign the document
                $recipients = new \DocuSign\eSign\Model\Recipients();
                $recipients->setSigners([$signer,$c_signer]);
                $recipients->setRecipientCount(2);

                $envelope_events = [
                    // (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("sent"),
                    // (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("delivered"),
                    (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("completed"),
                    (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("declined"),
                    // (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("voided"),
                ];

                $recipient_events = [
                    // (new \DocuSign\eSign\Model\RecipientEvent())->setRecipientEventStatusCode("Sent"),
                    // (new \DocuSign\eSign\Model\RecipientEvent())->setRecipientEventStatusCode("Delivered"),
                    // (new \DocuSign\eSign\Model\RecipientEvent())->setRecipientEventStatusCode("Completed"),
                    // (new \DocuSign\eSign\Model\RecipientEvent())->setRecipientEventStatusCode("Declined"),
                    // (new \DocuSign\eSign\Model\RecipientEvent())->setRecipientEventStatusCode("AuthenticationFailed"),
                    // (new \DocuSign\eSign\Model\RecipientEvent())->setRecipientEventStatusCode("AutoResponded")
                ];

                $event_notification = new \DocuSign\eSign\Model\EventNotification();
                $event_notification->setUrl(\Yii::$app->params['docusign_callback_url']);
                $event_notification->setLoggingEnabled("true");
                $event_notification->setRequireAcknowledgment("false");
                $event_notification->setUseSoapInterface("false");
                $event_notification->setIncludeCertificateWithSoap("false");
                $event_notification->setSignMessageWithX509Cert("false");
                $event_notification->setIncludeDocuments("true");
                $event_notification->setIncludeEnvelopeVoidReason("true");
                $event_notification->setIncludeTimeZone("true");
                $event_notification->setIncludeSenderAccountAsCustomField("true");
                $event_notification->setIncludeDocumentFields("true");
                $event_notification->setIncludeCertificateOfCompletion("false");
                $event_notification->setEnvelopeEvents($envelope_events);
                // $event_notification->setRecipientEvents($recipient_events);

                $email_settings = new \DocuSign\eSign\Model\EmailSettings();
                $email_settings->setReplyEmailAddressOverride($user->email);
                $email_settings->setReplyEmailNameOverride($user->first_name.' '.$user->last_name);

                $envelop_definition = new \DocuSign\eSign\Model\EnvelopeDefinition();
                $envelop_definition->setEmailSubject("Document(s) from: ".$user->first_name.' '.$user->last_name.' '.$organization->name);
                $envelop_definition->setEventNotification($event_notification);
                $envelop_definition->setEmailSettings($email_settings);

                // set envelope status to "sent" to immediately send the signature request
                $status = 'sent';
                $envelop_definition->setStatus($status);
                $envelop_definition->setRecipients($recipients);
                $envelop_definition->setDocuments($all_documents);

                $options = new \DocuSign\eSign\Api\EnvelopesApi\CreateEnvelopeOptions();
                $options->setCdseMode(null);
                $options->setMergeRolesOnDraft(null);

                $envelop_summary = $envelopeApi->createEnvelope($accountId, $envelop_definition, $options);

                foreach($all_documents as $d){
                    $sd = new SignedDocument;
                    $sd->sent_datetime = gmdate('Y-m-d H:i:s');
                    if($opportunity !== null)
                        $sd->opportunity_id = $opportunity->opportunity_id;
                    $sd->company_id = ($company == null) ? -1 : $company->company_id;
                    $sd->contact_id = ($contact == null) ? -1 : $contact->contact_id;
                    $sd->user_id = \Yii::$app->user->getIdentity()->id;
                    $sd->signature_id = $envelop_summary->getEnvelopeId();
                    $sd->save();
                }

                return 201;             
            }
        }
    }
    catch (DocuSign\eSign\ApiException $ex)
    {           
        Yii::error("Exception: " . $ex->getMessage().' '.$ex->getResponseBody(),'Signature');
        return 500;
    }
}

2 个答案:

答案 0 :(得分:1)

登录()后,您没有正确设置BaseUrl。

请参阅示例代码here

       //*** STEP 1 - Login API: get first Account ID and baseURL
        $authenticationApi = new DocuSign\eSign\Api\AuthenticationApi($apiClient);
        $options = new \DocuSign\eSign\Api\AuthenticationApi\LoginOptions();
        $loginInformation = $authenticationApi->login($options);
        if(isset($loginInformation) && count($loginInformation) > 0)
        {
            $loginAccount = $loginInformation->getLoginAccounts()[0];
            $host = $loginAccount->getBaseUrl();
            $host = explode("/v2",$host);
            $host = $host[0];

            // UPDATE configuration object
            $config->setHost($host);

答案 1 :(得分:0)

我能够弄清楚发生了什么。事实证明,当你有多个签名者时,你必须在Signer类上使用“setRoutingOrder”设置路由顺序。现在它完美无缺。

相关问题