浏览文档 文档联系我们
文档 / 基于 Rokn 构建

通过 API 发送发票和报价单

发送是异步的:发送路由返回 202,一次发送从排队变为已发送或失败,失败的发送会告诉你原因。

发送发票或报价单是异步的。POST …/invoices/{id}/send 和 POST …/quotes/{id}/send 会把文档加入队列,并立即返回 202 Accepted 以及这次发送(send)。通过这次发送或文档的 sendState 读取结果。下面每个路径都位于 /api/v1/workspaces/{slug} 之下。

流程

  • POST …/invoices/{id}/send(invoices:write,工具 send_invoice)和 POST …/quotes/{id}/send(quotes:write,工具 send_quote)返回 202 和这次发送:id、documentId、documentType、state、failureReason、emailOutcome、attempts、createdAt 和 finishedAt。
  • GET …/invoices/{id}/sends/{sendId}(invoices:read,工具 get_invoice_send)和 GET …/quotes/{id}/sends/{sendId}(quotes:read,工具 get_quote_send)返回这次发送的当前状态。属于其他文档的 send id 返回 404。
  • 在后台任务运行期间,state 为 queued 或 working,之后为 sent 或 failed。发送未结束时,发票或报价单的 sendState 为 queued 或 working;否则为 null。
  • 在发送提交之前,文档一直是草稿。之后它变为已发送并被锁定:发票带有其付款链接,报价单带有客户的批准链接,同时客户会收到带有 PDF 的邮件。

结果

  • 状态为 sent 的发送带有 emailOutcome:sent、skipped(客户没有邮箱地址)或 failed(文档已发送,但邮件未能送达)。
  • 状态为 failed 的发送带有 failureReason:gateway_unavailable、pdf_unavailable 或 conflict(暂时性失败,在发送失败之前会重试三次,每次失败后分别不早于 15 秒、1 分钟和 5 分钟);gateway_rejected(支付网关拒绝了其凭据)或 gateway_test_mode(生产环境中使用了测试模式密钥),这两种会立即导致失败;以及 quote_expired、not_sendable、feature_locked、billing_read_only、interrupted、requester_not_allowed 和 unexpected。
  • 发送失败时不会发送任何内容,文档保持草稿状态。请排除原因后再次 POST。

拒绝与重试

  • 在一次发送未结束时再次 POST,返回 409 send_in_progress。已不再是草稿的文档返回 409 invalid_status_transition。有效期已过的报价单返回 409 quote_expired。
  • 两个路由都支持 Idempotency-Key:重放会返回同一次发送。
  • 后台任务每分钟处理一次排队中的发送。

得知发送失败

当某个人请求了一次发送,而它最终失败时,Rokn 会向该人发布 invoice.send_failed 或 quote.send_failed,并通知他们。由 API 密钥或自动化排队的发送没有请求者,也不会发布任何事件:请改为轮询这次发送或文档的 sendState。

HTTP · 发送发票并读取结果
POST /api/v1/workspaces/acme/invoices/inv_123/send
Authorization: Bearer <token>
Idempotency-Key: 6f1c2e0a-…

# 202 → { "id": "snd_1", "documentType": "invoice", "state": "queued", … }

GET /api/v1/workspaces/acme/invoices/inv_123/sends/snd_1
Authorization: Bearer <token>

# 200 → { "state": "sent", "emailOutcome": "sent", "failureReason": null, … }

界面版本见指南 发票或报价单发不出去时。