Send Document using DocuSign in Codeigniter PHP

|
| By Babita Kapoor

The instructions for integrating DocuSign with a CodeIgniter project using the given DocuSignService.php code are provided below. The following instructions presume that you have previously created a CodeIgniter project and installed the required dependencies.

Set Up CodeIgniter Project

Make sure you have a CodeIgniter project set up and running. If you haven’t already, you can follow the official documentation to set up a new project.

Install Required Dependencies

Ensure that the necessary dependencies are installed. Run the following command in your CodeIgniter project directory:

composer require firebase/php-jwt guzzlehttp/guzzle

Create a Configuration File

Create a configuration file, for example, app/Config/Docusign.php, to store your DocuSign-related configuration:

// app/Config/Docusign.php
namespace Config;
use CodeIgniter\Config\BaseConfig;
class Docusign extends BaseConfig
{
public $clientId = '00a13340-c633-40c4-8f60-b48b01e0d918';
public $userId = '695c2569-91f8-4d69-b2bc-c9128456ca10';
public $accountId = '998ca982-2fa6-4a93-9fb4-65941e0b9570';
public $privateKeyPath = '/home/webner/Desktop/keys/docusign/private.key';
}

Modify DocuSignService

Update the DocuSignService.php file to use the CodeIgniter configuration:

// app/Services/DocuSignService.php
namespace App\Services;

use CodeIgniter\Config\Services;
use Firebase\JWT\JWT;
use GuzzleHttp\Client;

class DocuSignService
{
private $config;

public function __construct()
{
$this->config = config('Docusign');
}

public function generateJwt()
{
// JWT generation code
$privateKey = file_get_contents($this->config->privateKeyPath);

$jwtPayload = [
'iss' => $this->config->clientId,
'sub' => $this->config->userId,
'aud' => 'account-d.docusign.com',
'iat' => time(),
'exp' => time() + 6000,
"scope" => "signature impersonation"
];

return JWT::encode($jwtPayload, $privateKey, 'RS256');
}

public function getAccessToken()
{
// Get access token using JWT
$jwt = $this->generateJwt();
$client = new Client();

$response = $client->post('https://account-d.docusign.com/oauth/token', [
'form_params' => [
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt,
],
]);

return json_decode($response->getBody())->access_token;
}

public function sendDocumentForSigning($documentPath)
{
$accessToken = $this->getAccessToken();
$client = new Client();

$response = $client->post('https://demo.docusign.net/restapi/v2.1/accounts/' . $this->config->accountId . '/envelopes', [
'headers' => [
'Authorization' => "Bearer $accessToken",
],
'json' => [
'emailSubject' => 'Sign this document',
'status' => 'sent',
'recipients' => [
'signers' => [
[
'email' => 'skip.gilleland@tranzpay.io',
'name' => 'Skip',
'recipientId' => '1',
'routingOrder' => '1',
],
],
],
'documents' => [
[
'documentId' => '1',
'name' => 'Sample.pdf',
'documentBase64' => base64_encode(file_get_contents($documentPath)),
],
],
],
]);

return $response->getBody();
}
}

Create a Controller

Create a controller to handle the integration. For example, create
app/Controllers/DocusignController.php:

// app/Controllers/DocusignController.php
namespace App\Controllers;

use App\Services\DocuSignService;

class DocusignController extends BaseController
{
public function sendDocument()
{
$documentPath = '/path/to/your/document.pdf'; // Set the actual path to your document
$docuSignService = new DocuSignService();

try {
$response = $docuSignService->sendDocumentForSigning($documentPath);

// Handle the response as needed
echo 'Document sent successfully. Response: ' . $response;

} catch (\Exception $e) {
// Handle exceptions
echo 'Error: ' . $e->getMessage();
}
}
}

Test the Integration

Visit the appropriate URL in your CodeIgniter project to trigger the sendDocument method. For example, http://yourdomain/index.php/docusign/sendDocument.
Make sure to replace placeholder values like ‘/path/to/your/document.pdf’ with the actual path to your document.
You will receive an email at the corresponding email address.

Leave a Reply

Your email address will not be published. Required fields are marked *