use_voucher
Mark loyalty vouchers as redeemed when customers use them at merchants. This merchant-only operation updates voucher status in on-chain loyalty programs.
Instructions
Mark a voucher as used (redeemed by customer at merchant). Merchant-only operation.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| voucher_code | No | Voucher code (e.g. LOYAL-XXXX-XXXX-XXXX-XXXX) | |
| voucher_id | No | Voucher UUID (alternative to code) |
Implementation Reference
- Tool implementation for 'use_voucher'. This handler authenticates the request, verifies if the voucher is active and belongs to the merchant, and updates its status to 'used'.
mcpServer.tool("use_voucher", { description: "Mark a voucher as used (redeemed by customer at merchant). Merchant-only operation.", inputSchema: { type: "object" as const, properties: { voucher_code: { type: "string", description: "Voucher code (e.g. LOYAL-XXXX-XXXX-XXXX-XXXX)" }, voucher_id: { type: "string", description: "Voucher UUID (alternative to code)" }, }, }, handler: async ({ voucher_code, voucher_id }: any) => { const err = authGuard(["manage_rewards"]); if (err) return T(err); if (!voucher_code && !voucher_id) return T(JSON.stringify({ error: "Provide voucher_code or voucher_id" })); const d = db(); let q = d.from("vouchers").select("*").eq("merchant_address", agent.ownerAddress.toLowerCase()); if (voucher_code) q = q.eq("code", voucher_code); else q = q.eq("id", voucher_id); const { data: v } = await q.maybeSingle(); if (!v) return T(JSON.stringify({ error: "Voucher not found" })); if (v.status === "used") return T(JSON.stringify({ error: "Already used", used_at: v.used_at })); if (v.status !== "active") return T(JSON.stringify({ error: `Not active (status: ${v.status})` })); const { error: ue } = await d.from("vouchers").update({ status: "used", used_at: new Date().toISOString() }).eq("id", v.id); if (ue) return T(JSON.stringify({ error: ue.message })); return T(JSON.stringify({ success: true, voucher: { id: v.id, code: v.code, reward_name: v.reward_name, customer_address: v.customer_address, status: "used" } })); }, });