2017-03-17 59 views
0

我正在使用REST API來創建將由多個組織使用的解決方案。DocuSign - 如何通過API控制通知發件人

在API中是否有一種方法來控制發件人端收到通知的人?默認情況下,所有通知電子郵件都將發送到我在創建API帳戶時設置的電子郵件中。但我真的需要能夠將這些通知發送給實際發送文檔的人員。

這裏是我正在使用的代碼:

public static function sendForSignature($filename, //The name of the file that will show up in the email 
             $document_path, //The path to the file on the server 
             $template, //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 
{  
    $envelop_summary = null; 
    $company = null; 
    $contact = null; 

    $sign_here_tabs=[]; 
    $full_name_tabs=[]; 
    $date_tabs=[]; 
    $text_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]); 

    if($opportunity !== null){ 
     $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]; 
      if(isset($loginInformation)) 
      { 
       $accountId = $loginAccount->getAccountId(); 

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

       $index = 0; 

       if(is_array($document_path)){ 

        foreach($document_path 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($filename[$index]); 
         $document->setDocumentId($index+1); 
         $all_documents[]=$document; 

         $template_file = json_decode(file_get_contents($mappingPath .'/'. $template[$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); 
           } 
           $signHere->setRecipientId("1"); 

           $sign_here_tabs[]=$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); 
           } 
           $fullName->setRecipientId(1); 

           $full_name_tabs[]=$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); 
           } 
           $text->setRecipientId("1"); 

           $text_tabs[]=$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); 
           } 
           $date->setRecipientId(1); 

           $date_tabs[] = $date; 
          } 
         } 

         $index++; 
        } 

       }else{ 
        // Add a document to the envelope 
        $document = new \DocuSign\eSign\Model\Document(); 
        $document->setDocumentBase64(base64_encode(file_get_contents($document_path))); 
        $document->setName($filename); 
        $document->setDocumentId("1"); 
        $all_documents[] = $document; 

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

        $template_file = json_decode(file_get_contents($mappingPath .'/'. $template)); 

        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("1"); 
          if($t->page == -1){ 
           $signHere->setPageNumber($page_count); 
          }else{ 
           $signHere->setPageNumber($t->page); 
          } 
          $signHere->setRecipientId("1"); 

          $sign_here_tabs[]=$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(1); 
          if($t->page == -1){ 
           $fullName->setPageNumber($page_count); 
          }else{ 
           $fullName->setPageNumber($t->page); 
          } 
          $fullName->setRecipientId(1); 

          $full_name_tabs[]=$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("1"); 
          if($t->page == -1){ 
           $text->setPageNumber($page_count); 
          }else{ 
           $text->setPageNumber($t->page); 
          } 
          $text->setRecipientId("1"); 

          $text_tabs[]=$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(1); 
          if($t->page == -1){ 
           $date->setPageNumber($page_count); 
          }else{ 
           $date->setPageNumber($t->page); 
          } 
          $date->setRecipientId(1); 

          $date_tabs[] = $date; 
         } 
        }    
       } 

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

       if(count($sign_here_tabs) > 0) 
        $tabs->SetSignHereTabs($sign_here_tabs); 
       if(count($full_name_tabs) > 0) 
        $tabs->setFullNameTabs($full_name_tabs); 
       if(count($text_tabs) > 0) 
        $tabs->setTextTabs($text_tabs); 
       if(count($date_tabs) > 0) 
        $tabs->setDateTabs($date_tabs); 

       $signer = new \DocuSign\eSign\Model\Signer(); 
       $signer->setEmail(trim($contact->email)); 
       if(!is_null($contact)) 
        $signer->setName($contact->first_name.' '.$contact->last_name); 

       $signer->setRecipientId("1");             
       $signer->setTabs($tabs); 

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

       // Add a recipient to sign the document 
       $recipients = new \DocuSign\eSign\Model\Recipients(); 
       $recipients->setSigners(array($signer)); 

       $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"), 
        // (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("sent"), 
        // (new \DocuSign\eSign\Model\EnvelopeEvent())->setEnvelopeEventStatusCode("sent") 
       ]; 

       $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); 

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

       foreach($all_documents as $d){ 
        $sd = new SignedDocument; 
        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; 
    } 
} 
+0

您使用哪種認證機制?你可以發佈一些你的信封創作代碼嗎? –

+0

Hi @CodingDawg只是添加了一組完整的代碼來發送文檔進行簽名。謝謝。 – O2U

回答

2

被在驗證頭中指定的證書將被用於確定發送者的帳戶。在您的示例中,$app->params['docusign_username']將收到發件人的電子郵件,因爲這是發送文檔的人員。

如果您正在創建將由多個組織使用的應用程序,則Legacy Authentication不是正確的方法。

您應該使用oAuth2流程作爲您的用例。

用戶應用程序

用戶應用程序是驗證與每個的DocuSign終端用戶的客戶端。這些應用程序通常是Web服務,移動應用程序或在DocuSign平臺上驗證個人用戶的桌面程序。一旦通過身份驗證,用戶同意應用程序在其帳戶中顯示,發送或簽名信封。對於用戶應用程序,建議使用OAuth2身份驗證流程。

服務集成

服務集成是一個的DocuSign帳戶直接集成的服務。這種集成通常保留用於在DocuSign平臺上進行身份驗證的後端服務,而不需要最終用戶的參與。例如,後端應用程序可以集成到業務線應用程序中,以自動發送新成員註冊。對於服務集成,推薦使用定製的X-DocuSign-Authentication頭。

+0

嗨@CodingDawg感謝這些信息。那麼我可以繼續使用帶有oAuth的PHP SDK嗎?還是必須直接對REST Api進行編碼? – O2U

+0

PHP sdk支持Docusign oAuth api。請參閱[這裏](https://github.com/docusign/docusign-php-client/blob/master/src/Api/AuthenticationApi.php#L305-L378)..另請參閱[更多信息](https:/ /github.com/docusign/docusign-php-client#user-applications-that-use-oauth-authentication) –

相關問題