Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radhack/e7c10afc80b4fddc3ed737a9c5e883ab to your computer and use it in GitHub Desktop.
Save radhack/e7c10afc80b4fddc3ed737a9c5e883ab to your computer and use it in GitHub Desktop.
HelloSign PHP POST with Template Example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Embedded Signing with Template</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="//s3.amazonaws.com/cdn.hellosign.com/public/js/hellosign-embedded.LATEST.min.js"></script>
<link rel="stylesheet" type="text/css" href="newcss.css" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" href="/favicon-32x32.png"/>
<link rel="icon" type="image/png" href="/favicon-16x16.png"/>
<link rel="manifest" href="/manifest.json" />
<link rel="mask-icon" href="/safari-pinned-tab.svg"/>
</head>
<body>
<?php
require_once 'vendor/autoload.php';
include('auth.php'); // authentication stored here
if (isset($_SESSION['signature_id'])) {
// this is just in case the signer refreshes the page, as we store the 'signature_id' in the $_SESSION below
// you could add additional conditions for this test for your case (like testing for $_GET params etc)
try {
$client = new HelloSign\Client($api_key);
$embedded_response = $client->getEmbeddedSignUrl($_SESSION['signature_id']);
$sign_url = $embedded_response->getSignUrl();
include('signerpage.php');
} catch (Exception $e) {
if ($e->getMessage() == "This request has already been signed") {
$filename = "downloaded_files/".$_SESSION['signature_request_id'].".pdf";
$client = new HelloSign\Client($api_key);
$file = $client->getFiles($_SESSION['signature_request_id'], $filename , HelloSign\SignatureRequest::FILE_TYPE_PDF);
echo "<iframe src=\"$filename\" width=\"100%\" style=\"height:880px\"></iframe>";
} else {
echo "there was a different value";
}
}
} else {
// Instance of a client for you to use for calls
$client = new HelloSign\Client($api_key);
// Example call with logging for embedded requests
$request = new HelloSign\TemplateSignatureRequest;
$request->enableTestMode(); // remove for production
$request->setTitle("Testing"); // optional
$request->setSubject('Embedded Signing With Template');// optional
$request->setMessage('Awesome, right?'); // optional
$request->setSigner('Role1', 'jack@example.com', 'and Jill');
$request->setCustomFieldValue('Cost', "$100,000,000 and all of the things that go along with things like this are too much for lorem ipsum to handle and all that it represents and all that accounts for all the things in Maui and Thailand.");
$request->setCustomFieldValue('Amount', "There's not much", "Role1");
$request->setCustomFieldValue("Applicant", "Bobs's the name", "Role1");
$request->setCustomFieldValue('Test Merge', '$100,000,000');
// $request->enableAllowDecline();
$request->setTemplateId('5eafe0c773034d3d1e5dffda2580ae3b46014b44');
// $request->addMetadata("herp", "derp");
// $request->addMetadata("derp1", "herp1");
}
// Turn it into an embedded request
$embedded_request = new HelloSign\EmbeddedSignatureRequest($request, $client_id);
// Send it to HelloSign
$response = $client->createEmbeddedSignatureRequest($embedded_request);
// BEST PRACTICE: wait for callback with signature_request_sent event
// Grab the signature ID for the signature page that will be embedded in the page
$signature_request_id = $response->signature_request_id;
$_SESSION['signature_request_id'] = $signature_request_id;
$signatures = $response->getSignatures();
$signature_id = $signatures[0]->getId(); // in single signer situations, you could just grab the first signer's signature_id (since there's only one signer)
$_SESSION['signature_id'] = $signature_id; // store in case the signer refreshes the page
// Retrieve the URL to sign the document
$embedded_response = $client->getEmbeddedSignUrl($signature_id);
// Store it to use with the embedded.js HelloSign.open() call
$sign_url = $embedded_response->getSignUrl();
// call the html page with the embedded.js lib and HelloSign.open()
include('signerpage.php');
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment