StableOps
指南

Next.js

在 Next.js App Router 中集成 StableOps。

本指南使用 App Router 和当前的 TypeScript SDK。

安装

pnpm add @stableops/api-sdk

配置

STABLEOPS_API_KEY=sk_sandbox_...
STABLEOPS_WEBHOOK_SECRET=whsec_...
STABLEOPS_API_URL=https://api.stableops.dev

创建 lib/stableops.ts

import { StableOps } from '@stableops/api-sdk'

export const stableops = new StableOps({
  apiKey: process.env.STABLEOPS_API_KEY!,
  environment: 'sandbox',
  baseUrl: process.env.STABLEOPS_API_URL,
})

创建订单

创建 app/actions/payment.ts

'use server'

import { revalidatePath } from 'next/cache'
import { stableops } from '@/lib/stableops'

export async function createPaymentOrder(formData: FormData) {
  const amount = String(formData.get('amount') ?? '')
  const merchantOrderId = String(formData.get('orderId') ?? '')

  const order = await stableops.paymentOrders.create(
    {
      merchantOrderId,
      amount,
      settlementAsset: 'USDC',
      acceptedAssets: [{ chain: 'base', asset: 'USDC' }],
      expiresAt: new Date(Date.now() + 30 * 60 * 1000).toISOString(),
      metadata: { source: 'nextjs_checkout' },
    },
    { idempotencyKey: merchantOrderId },
  )

  revalidatePath('/payments')
  return order
}

把返回的一条候选支付指令展示给用户:

{
  order.paymentInstructions[0] ? (
    <div>
      在 {order.paymentInstructions[0].chain} 上向以下地址发送 {order.amount}{' '}
      {order.paymentInstructions[0].asset}:
      <code>{order.paymentInstructions[0].address}</code>
    </div>
  ) : null
}

验证 Webhook

创建 app/api/webhooks/stableops/route.ts

import { SIGNATURE_HEADER, verifySignature } from '@stableops/api-sdk/webhooks'

const WEBHOOK_SECRET = process.env.STABLEOPS_WEBHOOK_SECRET!

export async function POST(req: Request) {
  const rawBody = await req.text()
  const result = verifySignature({
    secrets: [WEBHOOK_SECRET],
    header: req.headers.get(SIGNATURE_HEADER) ?? undefined,
    rawBody,
  })

  if (!result.ok) {
    return new Response(`invalid signature: ${result.reason}`, { status: 400 })
  }

  const event = JSON.parse(rawBody) as {
    type: string
    data: { payment_order_id?: string }
  }

  switch (event.type) {
    case 'payment.detected':
      break
    case 'payment.confirmed':
      break
    case 'payment.finalized':
      // 做完自家幂等检查后再履约。
      break
    case 'payment.expired':
    case 'payment.reverted':
      break
  }

  return new Response('ok')
}

修改本地订单状态前请先在数据库里登记 X-Event-Id,避免 Webhook 重投导致重复履约。

本页内容