Try it out
Apps
Blog
About
Contact
Developers
Docs
Login
Developers
docusign@obindo.com
Written by
Chuck
Send a document to Obindo and it will save it as a DocuSign draft for you.
/************************** Links: API References: https://www.docusign.com/p/RESTAPIGuide/RESTAPIGuide.htm Revoke access : https://demo.docusign.net/MEMBER/MemberConnectedApps.aspx Manage app : Preferences > Account Administration > API **************************/ //#region PRIVATE set my oauth2 credentials var accountsPath= '/restapi/v2/accounts/'; var tokenPath= '/restapi/v2/oauth2/token'; var loginUrl= "https://www.docusign.net/restapi/v2/login_information?include_account_id_guid=true"; var authError= 'Sorry, we could not authorize your account. Please try again.'; /** LOGIN USER **/ authForm.init({ css: 'body { background: #1B49A0; }' + ' summary { font-size: 18px; font-weight: bold; }' + ' header { display: none; }', title: 'DocuSign Login', formHeader: 'Please enter your DocuSign credentials and we\'ll post your file to your account.', button: 'Authorize', fields: [ { name: 'emailAddress', title: 'Email Address' }, { name: 'password', title: 'Password', type: 'password' } ], onSubmit: onFormSubmit }); function validResponse(response) { return (response.status >= 200 && response.status < 300); } function getAccessToken(emailAddress, password, rootUrl) { var args= { grant_type: 'password', client_id: integratorKey, username: emailAddress, password: password, scope: 'api' }; var response= post({ url: rootUrl + tokenPath, args: args }); return (validResponse(response)) ? util.jsonify(response.text) : null; } function getLoginInfo(emailAddress, password) { var args= { Username: emailAddress, Password: password, IntegratorKey: integratorKey }; var header= { 'X-DocuSign-Authentication': util.stringify(args) }; var response= get({ url: loginUrl, header: header }); if (!validResponse(response)) return null; var accounts= util.jsonify(response.text).loginAccounts; return accounts[0]; } function getRootUrl(baseUrl) { if (!baseUrl) return null; var i= baseUrl.indexOf('docusign'); if (i < 0) return null; i= baseUrl.indexOf("/", i); if (i < 0) return null; return baseUrl.substring(0, i); } function onFormSubmit(f) { var email= f.emailAddress; var pass= f.password; var errors= []; if (!email || email.length < 0) errors.push({ field: 'emailAddress', text: 'Please enter an email address'}); if (!pass || pass.length < 0) errors.push({ field: 'password', text: 'Please enter a password'}); if (errors.length > 0) return { success: false, errors: errors }; var loginInfo= getLoginInfo(email, pass); if (!loginInfo) return { success: false, text: authError }; var rootUrl= getRootUrl(loginInfo.baseUrl); var token= getAccessToken(email, pass, rootUrl); if (!token) return { success: false, text: authError }; tokens.accessToken= { access_token: token.access_token, token_type: token.token_type, scope: token.scope, rootUrl: rootUrl, accountIdGuid: loginInfo.accountIdGuid }; if (f.redirect_uri) return { success: true, url: f.redirect_uri }; var text= 'Congratulations! You\'re all set. Now just click here to access <a href="'+ rootUrl +'">DocuSign</a>.'; return { success: true, text: text }; } /** UPLOAD FILES **/ function isDocument(file) { var types= [ 'application/pdf', 'application/msword', 'application/rtf', 'application/vnd.', //lotta Microsoft 'text/plain', 'text/html' ]; var ct= file.contentType.toLowerCase(); for (var i=0; i < types.length; i++) if (ct.indexOf(types[i]) == 0) return true; return false; } function getDocuments(files) { if (!files || files.length == 0) return []; var docs= []; for (var i=0; i < files.length; i++) //filter out images if (isDocument(files[i])) docs.push(files[i]); return docs; } function uploadFiles(files, subject) { var header= { 'Authorization': 'bearer ' + tokens.accessToken.access_token }; var url= tokens.accessToken.rootUrl + accountsPath + tokens.accessToken.accountIdGuid + '/envelopes'; var args= [{ contentType: 'application/json' }]; //build json details var info= { status: 'created', emailSubject: subject, documents: [] }; for (var i=0; i < files.length; i++) { var name= files[i].name; //.replace(/[^\x00-\x7F]/g,'-'); //fix for international name= name.replace(/;/g,'-'); //fix for semicolon info.documents.push({ name: name, documentId: (i+1), order: (i+1) }); args.push({ fileName: name, value: files[i] }); } args[0].value= util.stringify(info); post({ url: url, header: header, multipartMode: 'RFC6532', args: args }); } function sendConfirm(docs, subject) { var text= ''; var count= docs.length; if (count == 0) { var src= message.source; if (src && src.type && src.type == 'getstarted') //no empty message from other source return; text= 'Sorry we didn\'t see any documents, so we didn\'t know what you wanted to save.' + '\n\nPlease try again the next time you\'d like to send documents to DocuSign.' + '\n\nThanks for using Obindo!'; sendEmail({ to: message.from.emailAddress, text: text, subject: 'Sorry, we didn\'t know what to do with that one', templateFields: { recipientName: message.from.name } }); return; } message.appUrl= tokens.accessToken.rootUrl + '/Member/ManageEnvelopes.aspx'; text= 'We created an envelope for you' text+= (subject && subject.length > 0) ? ': ' + subject : '!'; text+= '\n\nIt contains ' + count + ' document'; text+= ((count > 1) ? 's' : '') + ':'; for (var i = 0; i < count; i++) text+= '\n' + docs[i].name; text+= '\n\nYou can view it in the Draft folder.'; text+= '\n\nThanks for using Obindo!'; sendEmail({ to: message.from.emailAddress, text: text, subject: 'Obindo created a DocuSign envelope for you!', templateFields: { recipientName: message.from.name, buttonText: 'View in DocuSign', buttonLink: message.appUrl } }); } $(function() { var docs= getDocuments(message.files); if (docs.length > 0) uploadFiles(docs, message.subject); sendConfirm(docs, message.subject); });