node.js - 当前端和后端位于不同端口时如何使 DocuSign 嵌入式签名正常工作

标签 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) => {
    ...
   }

最佳答案

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

该方法应该做什么:

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

您可以在我编写的 React/Redux 示例中看到这样的示例。在此示例中,React 应用程序直接调用 DocuSign API(使用私有(private) 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 应用程序,您可以,但您需要一个私有(private) CORS 网关。请参阅我的博文part 1 , part 2 ,和part 3

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

关于node.js - 当前端和后端位于不同端口时如何使 DocuSign 嵌入式签名正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51589381/

相关文章:

html - 如何使用 Node.JS 通过 XMPP 发送 HTML 格式的消息?

node.js - 有没有办法暂停redis服务器?

c# - 如何为信封定义设置过期设置?

python - 使用元素树 findall 解析 XML 命名空间

curl - SSL 证书验证失败

node.js - Node JS - Express JS - 类型错误 : Object #<Object> has no method 'compile'

Node.js 简单 Google API 请求 : error:0906D06C:PEM routines:PEM_read_bio:no start line

mysql - Nodejs node-mysql 查询结果数据类型

java - 使用 Docusign Java 客户端时遇到 java.lang.NoClassDefFoundError : class com. sun.jersey.core.header.MediaTypes

docusignapi - 安全 token 格式不符合预期架构