跳到正文
Longplay
开发文档
章节目录 / 记录商户业务事件
本页内容

记录商户业务事件

操作名
recordMerchantEvent
服务端 SDK
recordMerchantEvent
Python
record_merchant_event
Go
RecordMerchantEvent
POST/api/v1/merchant/events/record

通过统一入口记录商户上报的计费、用券、履约、权益、授权、用量和余额结果,按 eventId 幂等。非财务事件不会创建发票、收入或 Longplay 可消费余额,也不改变平台用量。

Markdown
API v1更新于

请求#

除 coupon.redeemed 外,新事件要求当前 App 环境的商户执行模式为 record_only。这是环境配置,不是本接口的请求字段。

为每个商户事件保存稳定的 eventId。重试时复用同一个 eventId 和相同的事件内容;同一 eventId 携带不同内容会返回幂等冲突。

凭据
Server Key,权限 billing:write。只在后端调用。
SDK
JavaScript createLongplayServer().recordMerchantEvent · Python record_merchant_event · Go RecordMerchantEvent
成功状态
HTTP 200
request.shShell
curl -X POST "$LONGPLAY_API_URL/api/v1/merchant/events/record" \  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \  -H "X-Indiana-Protocol: 0.10" \  -H "Content-Type: application/json" \  -d '{  "type": "purchase.closed",  "eventId": "merchant-order-123-closed",  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",  "occurredAt": "2026-10-03T12:00:00.000Z",  "operationId": "a8b14e0c-6576-49a4-812c-7a1587e55255",  "status": "failed"}'

响应为示例数据。

请求体参数#

application/json

eventIdstring | string | string | string | string | string | string | string | string | string | string | string | string | string | string | string | string必填

事件 ID,用于去重;重试时保持不变。

customerIduuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid | uuid必填

Customer 的 UUID。用 Server Key 调用时指定 Customer;Customer Token 调用时省略。

occurredAtdate-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time | date-time必填

时间(ISO 8601)。

type"purchase.completed" | "purchase.closed" | "subscription.observed" | "subscription.change.completed" | "subscription.change.closed" | "invoice.paid" | "refund.updated" | "fulfillment.updated" | "benefit.claimed" | "benefit.closed" | "benefit.updated" | "authorization.updated" | "balance.observed" | "coupon.redeemed" | "coupon.redeemed" | "usage.observed" | "usage.observed"必填

事件或操作类型;按此值选择对应的请求分支。

operationIduuid | uuid | uuid | null | uuid | uuid | uuid | null | uuid可选

业务操作 UUID;订阅执行引用 previewAttach 返回的 operationId,余额退款引用原消费操作。

merchantOrderIdstring可选

商户原生订单的稳定标识,用于核对原购买及重放。

minLength: 1 · maxLength: 200

paymentStatusenum可选

商户已验证的支付结果:已支付或无需支付。

查看允许值

"paid""trialing""no_payment_required"

invoiceobject | null | object | null | object可选

发票。

展开子字段
merchantInvoiceIdstring | string | string必填

商户原生发票稳定标识,用于发票事实及重放校验。

reasonenum | enum | enum可选

拒绝原因或账本操作说明;结合 accepted / 状态判断结果。

currencystring | string | string必填

已发布价格支持的币种代码。

subtotalMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

discountMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

taxMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

totalMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

paidMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

periodStartsAtdate-time | null | date-time | null | date-time | null必填

时间(ISO 8601)。

periodEndsAtdate-time | null | date-time | null | date-time | null必填

时间(ISO 8601)。

linesarray | array | array必填

明细行。

展开子字段
merchantLineIdstring | string | string | string | string | string必填

商户原生发票行稳定标识;同一发票内不能重复。

quantityinteger | integer | integer | integer | integer | integer必填

数量(整数)。

subtotalMinorinteger | integer | integer | integer | integer | integer必填

金额,以最小货币单位计(如美分)。

discountMinorinteger | integer | integer | integer | integer | integer必填

金额,以最小货币单位计(如美分)。

taxMinorinteger | integer | integer | integer | integer | integer必填

金额,以最小货币单位计(如美分)。

totalMinorinteger | integer | integer | integer | integer | integer必填

金额,以最小货币单位计(如美分)。

periodStartsAtdate-time | null | date-time | null | date-time | null | date-time | null | date-time | null | date-time | null必填

时间(ISO 8601)。

periodEndsAtdate-time | null | date-time | null | date-time | null | date-time | null | date-time | null | date-time | null必填

时间(ISO 8601)。

priceIduuid | uuid | uuid可选

该发票行对应的 Longplay Price UUID,须与原报价一致。

nativePriceobject | object | object可选

原生续费明细的价格引用,和 priceId 二选一。已确认操作沿用报价的 priceId。

展开子字段
merchantPriceIdstring | string | string必填

商户目录中的原生价格标识,按当前 App 和环境解析。

productIdstring | string | string必填

Product 的 ID。

kindenum | enum | enum必填

类型,取值见允许值。

currencystring | string | string必填

已发布价格支持的币种代码。

unitAmountMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

intervalenum | enum | enum必填

周期,取值见允许值。

intervalCountinteger | integer | integer必填

Stripe Price 的计费间隔数量,最多 52。

subscriptionobject | null | object | object可选

商户验证后的订阅快照;无订阅的购买填 null。

展开子字段
merchantSubscriptionIdstring | string | string必填

此订阅快照对应的商户原生订阅标识。

revisioninteger | integer | integer必填

特定资源的版本号;Customer 资料更新不使用版本号。

statusenum | enum | enum必填

状态,取值见允许值。

periodStartsAtdate-time | date-time | date-time必填

时间(ISO 8601)。

periodEndsAtdate-time | date-time | date-time必填

时间(ISO 8601)。

cancelAtPeriodEndboolean | boolean | boolean必填

是否在当前周期结束时取消。

hasDiscountsboolean | boolean | boolean必填

商户订阅是否含折扣;用于判断后续变更能否按标准报价执行。

itemsarray | array | array必填

列表项。

展开子字段
merchantItemIdstring | string | string | string | string | string必填

商户原生订阅项稳定标识,用于核对后续变更的原项。

quantityinteger | integer | integer | integer | integer | integer必填

数量(整数)。

priceIduuid | uuid | uuid可选

此订阅项对应的 Longplay Price UUID。

nativePriceobject | object | object可选

自主订阅观察中的原生价格引用,和 priceId 二选一。已确认操作沿用报价的 priceId。

展开子字段
merchantPriceIdstring | string | string必填

商户目录中的原生价格标识,按当前 App 和环境解析。

productIdstring | string | string必填

Product 的 ID。

kindenum | enum | enum必填

类型,取值见允许值。

currencystring | string | string必填

已发布价格支持的币种代码。

unitAmountMinorinteger | integer | integer必填

金额,以最小货币单位计(如美分)。

intervalenum | enum | enum必填

周期,取值见允许值。

intervalCountinteger | integer | integer必填

Stripe Price 的计费间隔数量,最多 52。

statusenum | enum | enum | enum | enum | enum | enum | enum可选

状态,取值见允许值。

scheduledFordate-time | null可选

已接受的订阅变更生效时间;必须与原指令一致。

merchantSubscriptionIdstring | string | null可选

商户原生订阅的稳定标识,用于关联该订阅的后续事实。

merchantInvoiceIdstring | null | string可选

商户原生发票的稳定标识;没有操作 ID 的原生退款(含拒付资金)用它指明被退款的发票。

merchantRefundIdstring可选

商户原生退款的稳定标识;重复回报同一退款时保持不变。

minLength: 1 · maxLength: 200

amountMinorinteger可选

金额,以最小货币单位计(如美分)。

min: 0 · max: 100000000

currencystring可选

已发布价格支持的币种代码。

pattern: ^[A-Z]{3}$

merchantFulfillmentIdstring可选

商户原生履约的稳定标识。

minLength: 1 · maxLength: 200

merchantBenefitIdstring | string | string可选

商户原生权益的稳定标识;同一权益的后续事实沿用它。

resourcestring | string | string | string可选

商户原生资源的标识,例如积分或某个模型。

unitstring | string | string | string可选

数量使用的商户原生单位。

quantityinteger | integer | integer可选

数量(整数)。

expiresAtdate-time | null | date-time | null可选

到期时间(ISO 8601,UTC)。

revisioninteger | integer | integer | integer | integer可选

特定资源的版本号;Customer 资料更新不使用版本号。

remainingQuantityinteger | null可选

权益当前剩余数量;商户无法提供时为 null。

merchantAuthorizationIdstring可选

商户原生授权的稳定标识;同一授权的后续状态沿用它。

minLength: 1 · maxLength: 200

kind"auto_topup"可选

类型,取值见允许值。

termsReferencestring | null可选

该授权绑定的商户原生条款标识;没有时为 null。

availableQuantityinteger可选

商户原生余额中当前可用的数量。

min: 0 · max: 1000000000000

reservedQuantityinteger可选

商户原生余额中已预留、尚未完成的数量。

min: 0 · max: 1000000000000

periodStartsAtdate-time | null可选

时间(ISO 8601)。

periodEndsAtdate-time | null可选

时间(ISO 8601)。

grantIduuid | uuid可选

发放记录的 ID。

productVersionIduuid可选

Product Version 的 ID。

subscriptionIdstring可选

订阅的 ID。

minLength: 1 · maxLength: 255

merchandiseIduuid可选

当前 App 和环境中已登记商品的内部 UUID。

merchantTransactionIdstring | string可选

商户原生用量交易的稳定标识,用于去重。

operation"consume" | "reverse"可选

要执行的操作,决定其余字段。

reversalOfstring可选

被冲正的原用量交易标识。

minLength: 1 · maxLength: 200

响应字段#

成功响应都包含 requestId(本次请求 ID,排查时提供)和 scope(App 与环境)。

HTTP 200 · application/json

requestIduuid必填

服务端为这次 HTTP 请求生成的 UUID,排查时提供;它不是你的业务 requestId。

scopeobject必填

响应实际所属的 App 和环境。Header 只能核对,不能切换 Key 范围。

展开子字段
projectIdstring必填

凭据绑定的 App ID。

minLength: 1

environmentenum必填

独立数据范围:sandbox 或 live。

查看允许值

"sandbox""live"

recordobject必填

Longplay 已持久保存的商户事件回执。

展开子字段
iduuid必填

Longplay 商户事件记录 UUID;幂等重放返回同一记录。

eventIdstring必填

事件 ID,用于去重;重试时保持不变。

minLength: 1 · maxLength: 200

source"merchant"必填

事件来源标记 merchant,表明事实由商户回报。

providerVerifiedfalse必填

固定为 false;该回执代表商户回报,Longplay 未直接验证支付渠道。

outcomestring必填

事件持久化结果,包括对应业务对象和已处理状态。

minLength: 1 · maxLength: 100

错误#

失败时返回错误对象,按 error.code 与 error.recovery 处理。下表是这个接口的凭据、作用域和幂等字段决定的错误;视请求内容还可能返回 NOT_FOUND(引用的资源不存在)、NOT_CONFIGURED(所需的集成未连接)或 PAYLOAD_TOO_LARGE。有些拒绝(如余额不足)不是错误,而是成功响应里的 reason。

可能的错误
codeHTTP何时recovery
INVALID_REQUEST400请求不符合本页的参数。缺少或无法解析协议头时分别返回 UNSUPPORTED_PROTOCOL_VERSION 或 INVALID_PROTOCOL_VERSION。fix_request
UNAUTHENTICATED401凭据缺失、无效、过期或已撤销。authenticate
FORBIDDEN403凭据没有 billing:write 权限。check_permissions
SCOPE_MISMATCH403X-Indiana-Project 或 X-Indiana-Environment 与凭据所属的 App 和环境不一致。check_scope
CONFLICT409同一个 eventId 用了不同的输入(reason: IDEMPOTENCY_CONFLICT)。resolve_conflict
RATE_LIMITED429请求过多;按 Retry-After 等待后用同一请求重试。retry_same_request
SERVICE_UNAVAILABLE503服务暂时不可用;用同一请求重试。retry_same_request
INTERNAL_ERROR500结果未知;先读取原操作,或用原 ID 和原输入重放。recover_operation

可能的错误

  • INVALID_REQUEST

    HTTP
    400
    何时
    请求不符合本页的参数。缺少或无法解析协议头时分别返回 UNSUPPORTED_PROTOCOL_VERSION 或 INVALID_PROTOCOL_VERSION。
    recovery
    fix_request
  • UNAUTHENTICATED

    HTTP
    401
    何时
    凭据缺失、无效、过期或已撤销。
    recovery
    authenticate
  • FORBIDDEN

    HTTP
    403
    何时
    凭据没有 billing:write 权限。
    recovery
    check_permissions
  • SCOPE_MISMATCH

    HTTP
    403
    何时
    X-Indiana-Project 或 X-Indiana-Environment 与凭据所属的 App 和环境不一致。
    recovery
    check_scope
  • CONFLICT

    HTTP
    409
    何时
    同一个 eventId 用了不同的输入(reason: IDEMPOTENCY_CONFLICT)。
    recovery
    resolve_conflict
  • RATE_LIMITED

    HTTP
    429
    何时
    请求过多;按 Retry-After 等待后用同一请求重试。
    recovery
    retry_same_request
  • SERVICE_UNAVAILABLE

    HTTP
    503
    何时
    服务暂时不可用;用同一请求重试。
    recovery
    retry_same_request
  • INTERNAL_ERROR

    HTTP
    500
    何时
    结果未知;先读取原操作,或用原 ID 和原输入重放。
    recovery
    recover_operation

相关:错误码与恢复·请求约定·全部接口