当前端和后端位于不同的端口上时,如何获得DocuSign嵌入式签名

时间:2018-07-30 07:56:55

标签: node.js docusignapi

我正在研究一个集成DocuSign嵌入签名仪式功能的项目。我的前端是Angular 6(端口:4200),它在与后端NodeJS(端口:3000)不同的端口上运行。

前端将调用后端(REST API)以生成PDF文件。完成后,它将触发到嵌入式签名路由的重定向,以连接到DocuSign以获取PDF文件签名。

成功生成PDF文件后,将触发'/api/docusign/signing'路由(请参见下面的代码)。

...
    res.redirect('/api/docusign/signing'); 
...

回调似乎不起作用,因为它会将响应发送回前端,这不是我的意图。需要回调,以便将PDF上传到DocuSign并触发DocuSign以提示签名者对文档进行签名。完成后,后端将更新数据库,然后再传递回前端。

我仍在学习和体验NodeJS,但不确定如何去做。任何有任何想法或经验的人都可以分享如何去做。感谢您的帮助。谢谢。

docusign-routes.js

module.exports = (app) => {
    app.get('/api/docusign/auth', docuSignController.authenticate);
    app.get('/api/docusign/callback', [docuSignController.dsLoginCB1, docuSignController.dsLoginCB2]);
    app.get('/api/docusign/signing', docuSignController.requireDocuSignToken,
        docuSignController.embeddedsigning);
}

docuSignController.js

    exports.authenticate = (req, res, next) => {
        passport.authenticate('docusign')(req, res, next);
    };

    exports.requireDocuSignToken = (req, res, next) => {
        let tokenBufferMin = 30;
        let now = moment();

        console.log('requireDocuSignToken');

        if (tokenBufferMin && req.docusign && req.docusign.accessToken &&
            now.add(tokenBufferMin, 'm').isBefore(req.docusign.expires)) {
            console.log('\nUsing existing access token');
            next();
        } else {
            console.log('\nGet a new access token');
            res.redirect('http://localhost:3000/api/docusign/auth');
        }
   };

   exports.dsLoginCB1 = (req, res, next) => {
       passport.authenticate('docusign', {failureRedirect: '/api/docusign/auth'})(req, res, next);
   };

   exports.dsLoginCB2 = (req, res, next) => {
       res.redirect('/api/docusign/signing');
   }

   exports.embeddedsigning = (req, res, next) => {
    ...
   }

1 个答案:

答案 0 :(得分:1)

问题中显示的代码摘录未显示您的embeddedsigning方法。

该方法应该做什么:

  1. 它已经知道它具有良好的DocuSign API令牌。
  2. 它应调用DocuSign EnvelopeViews::createRecipient方法。 returnUrl参数可以是Angular前端的URL或Node后端的URL。这取决于您的应用程序架构。无论哪种都可以。
  3. 将用户的浏览器重定向到DocuSign API调用响应中的url元素。

您可以在我编写的React / Redux示例中看到一个示例。在此示例中,React应用正在直接调用DocuSign API(使用专用的CORS网关)。参见文件sendSignEnv.js的第31-43行。重定向在第43行完成。

从Node.JS中进行重定向,如下所示:

    res.redirect(results.url);

这是即将发布的代码示例中的一个示例:

// Step 3. create the recipient view, the Signing Ceremony
let viewRequest = makeRecipientViewRequest(envelopeArgs)
  , createRecipientViewP = req.dsAuthCodeGrant.makePromise(
        envelopesApi, 'createRecipientView')
  ;
// call the CreateRecipientView API
results = null; // reset
try {
    results = await createRecipientViewP(accountId, envelopeId,
        {recipientViewRequest: viewRequest});
} 
catch (error) {
    let errorBody = error && error.response && error.response.body
        // we can pull the DocuSign error code and message from the response body
      , errorCode = errorBody && errorBody.errorCode
      , errorMessage = errorBody && errorBody.message
    res.render('pages/error', {err: error, errorCode: errorCode, errorMessage: errorMessage});
}
if (!results) {return}

// Step 4. Redirect the user to the Signing Ceremony
// Don't use an iFrame!
// State can be stored/recovered using the framework's session or a
// query parameter on the returnUrl (see the makeRecipientViewRequest method)
res.redirect(results.url);

这是makeRecipientViewRequest

function makeRecipientViewRequest(args) {
    let viewRequest = new docusign.RecipientViewRequest();

    // Set the url where you want the recipient to go once they are done signing
    // should typically be a callback route somewhere in your app.
    // The query parameter is included as an example of how
    // to save/recover state information during the redirect to
    // the DocuSign signing ceremony. It's usually better to use
    // the session mechanism of your web framework. Query parameters
    // can be changed/spoofed very easily.
    viewRequest.returnUrl = dsReturnUrl + "?state=123";

    // How has your app authenticated the user? In addition to your app's
    // authentication, you can include authenticate steps from DocuSign.
    // Eg, SMS authentication
    viewRequest.authenticationMethod = 'none';

    // Recipient information must match embedded recipient info
    // we used to create the envelope.
    viewRequest.email = args.signerEmail;
    viewRequest.userName = args.signerName;
    viewRequest.clientUserId = args.signerClientId;

    // DocuSign recommends that you redirect to DocuSign for the
    // Signing Ceremony. There are multiple ways to save state.
    // To maintain your application's session, use the pingUrl
    // parameter. It causes the DocuSign Signing Ceremony web page
    // (not the DocuSign server) to send pings via AJAX to your
    // app,
    viewRequest.pingFrequency = 600; // seconds
    // NOTE: The pings will only be sent if the pingUrl is an https address
    viewRequest.pingUrl = dsPingUrl; // optional setting

    return viewRequest
}

这是makePromise代码:

const {promisify} = require('util'); 
// See http://2ality.com/2017/05/util-promisify.html

/**
 * Returns a promise method, {methodName}_promise, that is a
 * promisfied version of the method parameter.
 * The promise method is created if it doesn't already exist.
 * It is cached via attachment to the parent object.
 * @function
 * @param obj An object that has method methodName
 * @param methodName The string name of the existing method
 * @returns {promise} a promise version of the <tt>methodName</tt>.
 */
DSAuthCodeGrant.prototype.makePromise = function _makePromise(obj, methodName){
  let promiseName = methodName + '_promise';
  if (!(promiseName in obj)) {
    obj[promiseName] = promisify(obj[methodName]).bind(obj)
  }
  return obj[promiseName]
}

已添加

(针对评论,请参阅下文。)

我认为您应该从后端使用护照。这样,护照/ DocuSign重定向将进入您的后端。

由于尝试从前端执行太多操作,因此您遇到了CORS问题。

如果您想编写更多的纯前端DocuSign应用程序,可以,但是需要专用的CORS网关。请参阅我的博客文章part 1part 2part 3

注意:对于前端应用,您必须使用隐式授权,而不是授权码授权。