docusign / code-examples-php

Docusign PHP code examples and launcher
https://developers.docusign.com/
MIT License
47 stars 42 forks source link

Need Solution for the Issue :Object reference not set to an instance of an object #30

Closed iyyappanpwilco closed 3 years ago

iyyappanpwilco commented 3 years ago

Hi I am using the template ID with embedded sign using the example 32 and envelope update for the 33 Kindly help me to resolve the issue this is for the envelope Update

namespace Example\Controllers\Examples\eSignature;

use DocuSign\eSign\Api\EnvelopesApi\UpdateOptions;
use DocuSign\eSign\Client\ApiException;
use DocuSign\eSign\Model\EnvelopeDefinition;
use DocuSign\eSign\Model\Workflow;
use DocuSign\eSign\Model\Recipients;
use DocuSign\eSign\Model\Signer;
use DocuSign\eSign\Model\RecipientViewRequest;
use Example\Controllers\eSignBaseController;
use Example\Services\SignatureClientService;
use Example\Services\RouterService;

class EG033UnpauseSignatureWorkflow extends eSignBaseController
{
    /** signatureClientService */
    private $clientService;

    /** RouterService */
    private $routerService;

    /** Specific template arguments */
    private $args;

    private $eg = "eg033"; # Reference (and URL) for this example

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->args = $this->getTemplateArgs();
        $this->clientService = new SignatureClientService($this->args);
        $this->routerService = new RouterService();
        parent::controller($this->eg, $this->routerService, basename(__FILE__));
    }

    /**
     * 1. Check the token
     * 2. Call the worker method
     *
     * @return void
     * @throws ApiException for API problems and perhaps file access \Exception, too
     */
    public function createController(): void
    {
        $minimum_buffer_min = 3;
        if ($this->routerService->ds_token_ok($minimum_buffer_min)) {
            # 1. Call the worker method
            # More data validation would be a good idea here
            # Strip anything other than characters listed
            $results = $this->worker($this->args);

            if ($results) {
                $_SESSION['pause_envelope_id'] = false;
                # Redirect the user to the embedded signing
                # Don't use an iFrame!
                # State can be stored/recovered using the framework's session or a
                # query parameter on the returnUrl (see the make recipient_view_request method)
                header('Location: ' . $results["redirect_url"]);
                exit;
              } else {
            $this->clientService->needToReAuth($this->eg);
        }
    }

    public function worker($args): array
    {

          $envelope_api = $this->clientService->getEnvelopeApi();
         $env = new EnvelopeDefinition([
            'workflow' => new Workflow(['workflow_status' => 'in_progress']
        ),

        ]); 

        $envelope_option = new UpdateOptions();
        $envelope_option -> setAdvancedUpdate('true');
        $envelope_option -> setResendEnvelope('true');
        $signer1 = new Signer([ # The signer1
                'email' => ' '#### ',
                'name' => 'GH',
                'recipient_id' => "1",
                'routing_order' => "1",
                'role_name' => 'd',
                'client_user_id' =>  '#### ',
        ]);

        $signer2 = new Signer([ # The signer2
            'email' => '#### ',
            'name' => 'MH',
            'recipient_id' => "2",
            'routing_order' => "2",
            'role_name' => 'p',
            'client_user_id' =>  '#### ',
        ]);
            $recipients = new Recipients([
                'signers' => [$signer1, $signer2, ],
            ]) ;
            $env -> setTemplateId( '#### ');
            $env->setTemplateRoles([$signer1, $signer2]);

       $authentication_method = 'None';
        $recipient_view_request = $this->clientService->getRecipientViewRequest(
            $authentication_method,
            $args['receipent_arguments']);
            $envelope = $envelope_api->update(
                $args['account_id'],
                $args['pause_envelope_id'],
                $envelope=$env,
                $options=$envelope_option
            );
            $envelope_id = $envelope['envelope_id'];

                 $results = $this->clientService->getRecipientView($args['account_id'], $envelope_id,
                 $recipient_view_request);
                return ['envelope_id' => $envelope_id, 'redirect_url' => $results['url']];

    }

    /**
     * Get specific template arguments
     *
     * @return array
     */
    private function getTemplateArgs(): array
    {
        $signer2_name  = preg_replace('/([^\w \-\@\.\,])+/', '', $_SESSION['signer2_name']);
        $signer2_email = preg_replace('/([^\w \-\@\.\,])+/', '', $_SESSION['signer2_email']);
        $receipent_arguments =[
            'signer_email' =>  '#### ',
            'signer_name' => 'MH',
            'signer_client_id' => '#### ',,
            'ds_return_url' => $GLOBALS['app_url'] . 'index.php?page=ds_return',
            'recipient_id' => '2',
            'routing_order' => '2',
            'role_name' => 'p',
        ];
        $args = [      
            'account_id' => $_SESSION['ds_account_id'],
            'base_path' => $_SESSION['ds_base_path'],
            'ds_access_token' => $_SESSION['ds_access_token'],
            'pause_envelope_id' => '#### ',
            'receipent_arguments' => $receipent_arguments,
        ];
       return $args;
    }
}
AaronWDS commented 3 years ago
     * Get specific template arguments
     *
     * @return array
     */
    private function getTemplateArgs(): array
    {
        $signer2_name  = preg_replace('/([^\w \-\@\.\,])+/', '', $_SESSION['signer2_name']);
        $signer2_email = preg_replace('/([^\w \-\@\.\,])+/', '', $_SESSION['signer2_email']);
        $receipent_arguments =[
            'signer_email' =>  '#### ',
            'signer_name' => 'MH',
            'signer_client_id' => '#### ',,
            'ds_return_url' => $GLOBALS['app_url'] . 'index.php?page=ds_return',
            'recipient_id' => '2',
            'routing_order' => '2',
            'role_name' => 'p',
        ];
        $args = [      
            'account_id' => $_SESSION['ds_account_id'],
            'base_path' => $_SESSION['ds_base_path'],
            'ds_access_token' => $_SESSION['ds_access_token'],
            'pause_envelope_id' => '#### ',
            'receipent_arguments' => $receipent_arguments,
        ];
       return $args;
    }
}

Certain parts of this look like they need to be reworked - there's an extra comma next to the signer_client_id that could be causing you problems. I see that the recipient id routing order and role name are hardcoded as args for the 2nd user. The view request client user id, name, and email need to match or the view request will not work. You're sending those off to the view request that presumably generates per each user - that would mean those args need to be tailored to each user per each getRecipientViewRequest.

also 'email' => ' '#### ', had an extra single quote but I don't think that's your issue.

I tried to run your code but was unable to replicate your issue (and ran into my own separate unrelated issue that made it impossible to get your snippet working. Is there a specific stacktrace your getting this in relation to in the code, like a line number for this file or perhaps it leads from an error in a different file that ex 33 is using?

LarryKlugerDS commented 3 years ago

Closing