Take Payments
Checkout across Razorpay and Stripe with a verified webhook inbox, a ledger, store purchases and daily reconciliation.
Install
npm i @dapplelight/signet-paymentsCreate the payments service
import { createPayments, sqlStore } from "@dapplelight/signet-payments"
import { razorpay } from "@dapplelight/signet-payments/razorpay"
import { stripe } from "@dapplelight/signet-payments/stripe"
import type { Env } from "./env"
// Build per request: Workers have no env at module scope.
export const paymentsFor = (env: Env) =>
createPayments({
gateways: [
razorpay({ keyId: env.RAZORPAY_KEY_ID, keySecret: env.RAZORPAY_KEY_SECRET, webhookSecret: env.RAZORPAY_WEBHOOK_SECRET }),
stripe({ secretKey: env.STRIPE_SECRET_KEY, webhookSecret: env.STRIPE_WEBHOOK_SECRET }),
],
route: (i) => (i.currency === "INR" ? "razorpay" : "stripe"),
fallback: true, // on an outage only, never on a decline
db: sqlStore({ query: async (s, p) => (await env.DB.prepare(s).bind(...p).all()).results }, { dialect: "sqlite" }),
})Create the tables with the statements from paymentsSchema("sqlite").
Start a checkout
import { app } from "../app"
import { paymentsFor } from "../payments"
app.post("/api/checkout", async (c) => {
const { orderId } = await c.req.json()
const userId = c.get("userId")
const checkout = await paymentsFor(c.env).checkout({
amount: 49900, // minor units
currency: "INR",
idempotencyKey: orderId,
reference: orderId,
userId,
})
return c.json(checkout)
})Receive webhooks
import { app } from "../app"
import { paymentsFor } from "../payments"
app.post("/webhooks/:gateway", (c) => paymentsFor(c.env).handleWebhook(c.req.param("gateway"), c.req.raw))The signature is verified on the raw body, the event is stored once, and captures, fees and refunds post to the ledger exactly once.
Verify store purchases
import { appstore } from "@dapplelight/signet-payments/appstore"
import { googleplay } from "@dapplelight/signet-payments/googleplay"
export const storeGateways = (env: Env) => {
const { issuerId, keyId, p8, bundleId, appAppleId, packageName, client_email, private_key, pubsub } = env
return [
appstore({ issuerId, keyId, privateKey: p8, bundleId, appAppleId }),
googleplay({ packageName, serviceAccount: { client_email, private_key }, pubsub }),
]
}import { app } from "../app"
import { paymentsFor } from "../payments"
app.post("/api/purchases", async (c) => {
const { transactionId, purchases } = await c.req.json()
const userId = c.get("userId")
const payments = paymentsFor(c.env)
await payments.verifyPurchase({ gateway: "appstore", id: transactionId, userId })
const { restored, refused } = await payments.restorePurchases({ userId, purchases })
return c.json({ restored, refused })
})Reconcile daily
import { reconcileJob } from "@dapplelight/signet-payments"
import type { Env } from "../server/env"
import { paymentsFor } from "../server/payments"
import { store } from "../server/store"
import { alert } from "./alerts"
export default {
async scheduled(_event: ScheduledController, env: Env) {
const payments = paymentsFor(env)
const job = reconcileJob(store, payments.gateway("razorpay"), { onMismatch: alert })
await job() // yesterday, UTC: call from a cron
},
}Mismatches are missing, amount_differs or extra.
Give agents refunds behind approval
import { paymentTools } from "@dapplelight/signet-payments/agent"
import { approvalGate } from "@dapplelight/signet-server/mcp"
import type { Env } from "./env"
import { paymentsFor } from "./payments"
export function toolsFor(env: Env, userId: string) {
const payments = paymentsFor(env)
const tools = paymentTools(payments, { userId, approve: approvalGate })
return tools
}Without approve, refund_payment returns APPROVAL_REQUIRED and refunds nothing.