You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Node.js复制DocuSign中被拒签的作废信封并重发?

用Node.js复制并重新发送DocuSign拒签信封

前置准备

  1. 安装DocuSign Node.js SDK:
npm install docusign-esign
  1. 准备好有效的访问令牌(accessToken)、账户ID(accountId)、被拒签的原信封ID(envelopeId),访问令牌可通过JWT授权或OAuth流程获取。

完整实现代码与步骤

const docusign = require('docusign-esign');
const fs = require('fs');

// 替换为你的实际配置参数
const config = {
  accountId: 'YOUR_ACCOUNT_ID',
  rejectedEnvelopeId: 'REJECTED_ENVELOPE_ID',
  integrationKey: 'YOUR_INTEGRATION_KEY',
  userId: 'YOUR_USER_ID',
  privateKeyPath: './private.key', // 私钥文件路径
  basePath: 'https://demo.docusign.net/restapi' // 生产环境替换为 https://www.docusign.net/restapi
};

// 通过JWT获取访问令牌(已有accessToken可跳过此函数)
async function getAccessToken() {
  const apiClient = new docusign.ApiClient();
  apiClient.setOAuthBasePath('account-d.docusign.com'); // 生产环境替换为 account.docusign.com
  const privateKey = fs.readFileSync(config.privateKeyPath, 'utf8');
  
  const jwtResponse = await apiClient.requestJWTUserToken(
    config.integrationKey,
    config.userId,
    'signature',
    privateKey,
    3600
  );
  return jwtResponse.body.access_token;
}

// 获取文档的Base64内容(原信封未返回时调用)
async function getDocumentContent(apiClient, accountId, envelopeId, documentId) {
  const envelopesApi = new docusign.EnvelopesApi(apiClient);
  const docStream = await envelopesApi.getDocument(accountId, envelopeId, documentId);
  return docStream.toString('base64');
}

// 复制并重新发送拒签信封
async function copyAndResend() {
  const accessToken = await getAccessToken();
  const apiClient = new docusign.ApiClient();
  apiClient.setBasePath(config.basePath);
  apiClient.addDefaultHeader('Authorization', `Bearer ${accessToken}`);
  
  const envelopesApi = new docusign.EnvelopesApi(apiClient);
  
  // 1. 获取原信封的核心信息:文档、签署人
  const originalEnvelope = await envelopesApi.getEnvelope(config.accountId, config.rejectedEnvelopeId, {
    include: 'documents,recipients'
  });
  
  // 2. 构造新信封定义
  const newEnvelope = new docusign.EnvelopeDefinition();
  newEnvelope.subject = originalEnvelope.subject;
  newEnvelope.emailBlurb = originalEnvelope.emailBlurb;
  newEnvelope.status = 'sent'; // 设置为立即发送状态
  
  // 复制文档并处理Base64内容
  newEnvelope.documents = await Promise.all(originalEnvelope.documents.map(async doc => {
    const docBase64 = doc.documentBase64 || await getDocumentContent(apiClient, config.accountId, config.rejectedEnvelopeId, doc.documentId);
    return {
      documentId: doc.documentId,
      name: doc.name,
      documentBase64: docBase64
    };
  }));
  
  // 复制签署人并重置状态
  newEnvelope.recipients = {
    signers: originalEnvelope.recipients.signers.map(signer => ({
      email: signer.email,
      name: signer.name,
      recipientId: signer.recipientId,
      routingOrder: signer.routingOrder,
      status: 'created' // 重置为待处理状态
    }))
  };
  
  // 3. 创建并发送新信封
  const result = await envelopesApi.createEnvelope(config.accountId, { envelopeDefinition: newEnvelope });
  console.log('新信封已发送,ID:', result.envelopeId);
}

// 执行操作
copyAndResend().catch(err => {
  console.error('操作失败:', err.response?.body || err.message);
});

关键注意事项

  • 若原信封包含CC收件人或其他类型收件人,需在recipients中对应复制并重置状态;
  • 生产环境需替换basePath和OAuth域名(去掉后缀-d);
  • 私钥需妥善保管,避免泄露;
  • 可根据需求修改新信封的主题、邮件正文,无需完全沿用原信封内容。

内容的提问来源于stack exchange,提问作者sasuke39

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:01:07