/vtex-ai-skills:payment-idempotencyfrom vtex-ai-skills
Implements idempotency logic for payment connectors using paymentId and requestId as keys. Covers state machine transitions, retry semantics, and duplicate prevention across Create, Cancel, Capture, and Refund endpoints.
Ships in vtex-ai-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Use this skill when:
undefined status paymentsDo not use this skill for:
payment-provider-protocolpayment-async-flowpayment-pci-securityVenda Direta Credito, Venda Direta Debito, overlapping Create Payment while Wait for confirmation polls) — use payment-ppp-pos together with this skillpaymentId as the idempotency key for Create Payment — every call with the same paymentId must return the same result.requestId as the idempotency key for Cancel, Capture, and Refund operations.paymentId, return the stored response without calling the acquirer again. Those retries can overlap; last-write-wins storage can drop a status update — persist with an etag / ifMatch (see payment-ppp-pos for the POS overlay).status: "undefined" — never "approved" until the acquirer confirms.undefined → approved → settled, or undefined → denied, or approved → cancelled. Enforce valid transitions only.The connector MUST check for an existing record with the given paymentId before processing a new payment. If a record exists, return the stored response without calling the acquirer again.
Why this matters
The VTEX Gateway retries Create Payment requests with undefined status for up to 7 days. Without idempotency on paymentId, each retry creates a new charge at the acquirer, resulting in duplicate charges to the customer. This is a financial loss and a critical production incident.
Detection
If the Create Payment handler does not check for an existing paymentId before processing, STOP. The handler must query the data store for the paymentId first.
Correct
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId } = req.body;
// Check for existing payment — idempotency guard
const existingPayment = await paymentStore.findByPaymentId(paymentId);
if (existingPayment) {
// Return the exact same response — no new acquirer call
res.status(200).json(existingPayment.response);
return;
}
// First time seeing this paymentId — process with acquirer
const result = await acquirer.authorize(req.body);
const response = {
paymentId,
status: result.status,
authorizationId: result.authorizationId ?? null,
nsu: result.nsu ?? null,
tid: result.tid ?? null,
acquirer: "MyProvider",
code: result.code ?? null,
message: result.message ?? null,
delayToAutoSettle: 21600,
delayToAutoSettleAfterAntifraud: 1800,
delayToCancel: 21600,
};
// Store the response for future idempotent lookups
await paymentStore.save(paymentId, { request: req.body, response });
res.status(200).json(response);
}
Wrong
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId } = req.body;
// No idempotency check — every call hits the acquirer
// If the Gateway retries this (which it will for undefined status),
// the customer gets charged multiple times
const result = await acquirer.authorize(req.body);
res.status(200).json({
paymentId,
status: result.status,
authorizationId: result.authorizationId ?? null,
nsu: result.nsu ?? null,
tid: result.tid ?? null,
acquirer: "MyProvider",
code: null,
message: null,
delayToAutoSettle: 21600,
delayToAutoSettleAfterAntifraud: 1800,
delayToCancel: 21600,
});
}
When the connector receives a Create Payment request with a paymentId that already exists in the data store, it MUST return the exact stored response. It MUST NOT create a new record, generate new identifiers, or re-process the payment.
Why this matters
The Gateway uses the response fields (authorizationId, tid, nsu, status) to track the transaction. If a retry returns different values, the Gateway loses track of the original transaction, causing reconciliation failures and potential double settlements.
Detection
If the handler creates a new database record or generates new identifiers when it finds an existing paymentId, STOP. The handler must return the previously stored response verbatim.
Correct
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId } = req.body;
const existing = await paymentStore.findByPaymentId(paymentId);
if (existing) {
// Return the EXACT stored response — same authorizationId, tid, nsu, status
res.status(200).json(existing.response);
return;
}
// ... process new payment and store response
}
Wrong
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId } = req.body;
const existing = await paymentStore.findByPaymentId(paymentId);
if (existing) {
// WRONG: Generating new identifiers for an existing payment
// The Gateway will see different tid/nsu and lose track of the transaction
const newTid = generateNewTid();
res.status(200).json({
...existing.response,
tid: newTid, // Different from original — breaks reconciliation
nsu: generateNewNsu(),
});
return;
}
// ... process new payment
}
If a payment method is asynchronous (e.g., Boleto, Pix, bank redirect), the Create Payment response MUST return status: "undefined". It MUST NOT return status: "approved" or status: "denied" until the payment is actually confirmed or rejected by the acquirer.
Why this matters
Returning approved for an async method tells the Gateway the payment is confirmed before the customer has actually paid. The order ships, but no money was collected. The merchant loses the product and the revenue. The correct flow is to return undefined and use the callbackUrl to notify the Gateway when the payment is confirmed.
Detection
If the Create Payment handler returns status: "approved" or status: "denied" for an asynchronous payment method (Boleto, Pix, bank transfer, redirect-based), STOP. Async methods must return "undefined" and use callbacks.
Correct
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId, paymentMethod, callbackUrl } = req.body;
const isAsyncMethod = ["BankInvoice", "Pix"].includes(paymentMethod);
if (isAsyncMethod) {
const pending = await acquirer.initiateAsyncPayment(req.body);
await paymentStore.save(paymentId, {
status: "undefined",
callbackUrl,
acquirerRef: pending.reference,
});
res.status(200).json({
paymentId,
status: "undefined", // Correct for async
authorizationId: pending.authorizationId ?? null,
nsu: pending.nsu ?? null,
tid: pending.tid ?? null,
acquirer: "MyProvider",
code: "ASYNC-PENDING",
message: "Awaiting customer payment",
delayToAutoSettle: 21600,
delayToAutoSettleAfterAntifraud: 1800,
delayToCancel: 604800, // 7 days for async
paymentUrl: pending.paymentUrl,
});
return;
}
// Sync methods can return approved/denied immediately
// ...
}
Wrong
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId, paymentMethod } = req.body;
// WRONG: Approving a Pix payment synchronously
// The customer hasn't paid yet — the order will ship without payment
const result = await acquirer.createPixCharge(req.body);
res.status(200).json({
paymentId,
status: "approved", // WRONG — Pix is async, should be "undefined"
authorizationId: result.authorizationId ?? null,
nsu: null,
tid: null,
acquirer: "MyProvider",
code: null,
message: null,
delayToAutoSettle: 21600,
delayToAutoSettleAfterAntifraud: 1800,
delayToCancel: 21600,
});
}
Payment state store with idempotency support:
interface PaymentRecord {
paymentId: string;
status: "undefined" | "approved" | "denied" | "cancelled" | "settled" | "refunded";
response: Record<string, unknown>;
callbackUrl?: string;
createdAt: Date;
updatedAt: Date;
}
interface OperationRecord {
requestId: string;
paymentId: string;
operation: "cancel" | "capture" | "refund";
response: Record<string, unknown>;
createdAt: Date;
}
Idempotent Create Payment with state machine:
const store = new PaymentStore();
async function createPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId, paymentMethod, callbackUrl } = req.body;
// Idempotency check
const existing = await store.findByPaymentId(paymentId);
if (existing) {
res.status(200).json(existing.response);
return;
}
const isAsync = ["BankInvoice", "Pix"].includes(paymentMethod);
const result = await acquirer.process(req.body);
const status = isAsync ? "undefined" : result.status;
const response = {
paymentId,
status,
authorizationId: result.authorizationId ?? null,
nsu: result.nsu ?? null,
tid: result.tid ?? null,
acquirer: "MyProvider",
code: result.code ?? null,
message: result.message ?? null,
delayToAutoSettle: 21600,
delayToAutoSettleAfterAntifraud: 1800,
delayToCancel: isAsync ? 604800 : 21600,
...(result.paymentUrl ? { paymentUrl: result.paymentUrl } : {}),
};
await store.save(paymentId, {
paymentId, status, response, callbackUrl,
createdAt: new Date(), updatedAt: new Date(),
});
res.status(200).json(response);
}
Idempotent Cancel with requestId guard and state validation:
async function cancelPaymentHandler(req: Request, res: Response): Promise<void> {
const { paymentId } = req.params;
const { requestId } = req.body;
// Operation idempotency check
const existingOp = await store.findOperation(requestId);
if (existingOp) {
res.status(200).json(existingOp.response);
return;
}
// State machine validation
const payment = await store.findByPaymentId(paymentId);
if (!payment || !["undefined", "approved"].includes(payment.status)) {
res.status(200).json({
paymentId,
cancellationId: null,
code: "cancel-failed",
message: `Cannot cancel payment in ${payment?.status ?? "unknown"} state`,
requestId,
});
return;
}
const result = await acquirer.cancel(paymentId);
const response = {
paymentId,
cancellationId: result.cancellationId ?? null,
code: result.code ?? null,
message: result.message ?? "Successfully cancelled",
requestId,
};
await store.updateStatus(paymentId, "cancelled");
await store.saveOperation(requestId, {
requestId, paymentId, operation: "cancel", response, createdAt: new Date(),
});
res.status(200).json(response);
}
paymentId already exists. The Gateway retries undefined payments for up to 7 days, so a single $100 payment can result in hundreds of duplicate charges.status: "approved" immediately for Boleto or Pix before the customer has actually paid. The order ships without payment collected.Map, local variable) instead of a persistent database. On process restart, all state is lost and the next retry creates a duplicate charge.tid, nsu, or authorizationId values when the Gateway retries with the same paymentId. This breaks Gateway reconciliation and can cause double settlements.requestId before processing operations, causing duplicate cancellations or refunds when the Gateway retries.paymentId before calling the acquirer?paymentId requests?requestId before processing?status: "undefined" instead of "approved"?delayToCancel values extended for async methods (e.g., 604800 seconds = 7 days)?payment-provider-protocol — Endpoint contracts and response shapespayment-async-flow — Callback URL notification and the 7-day retry windowpayment-pci-security — PCI compliance and Secure Proxypayment-ppp-pos — POS Create Payment is polled and can overlap for one paymentId; use this skill's paymentId key plus that skill's etag / ifMatch patternvtex-io-application-performance — VBase write correctness (await in critical paths), per-client timeout/retry config, and caching rules for IO-based connectorsWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub vtex/skills --plugin vtex-ai-skills46 skills in this plugin
/architecture-well-architected-commerceApply when scoping, reviewing, or documenting cross-cutting VTEX commerce architecture across storefront, IO, headless, marketplace, payments, or any other VTEX module. Grounds work in the Well-Architected Commerce framework—Technical Foundation (reliability, trust, integrity; security, infrastructure, compliance), Future-proof (innovation, simplicity, efficiency; scalable and adaptable solutions), and Operational Excellence (accuracy, accountability, data-driven improvement; process and customer experience). Routes implementation detail to product tracks (IO caching and paths, Master Data strategy, marketplace integrations). Use for solution design, architecture reviews, and RFP-level technical structure./faststore-storefrontCore coding rules and workflow for developing VTEX FastStore storefronts. Use when starting any FastStore development task, writing TypeScript/React components, creating section overrides, extending the BFF, or styling. Covers all primary conventions, safety rules, and the development workflow used across every FastStore project./headless-bff-architectureApply when designing or modifying a BFF (Backend-for-Frontend) layer, middleware, or API proxy for a headless VTEX storefront. Covers BFF middleware architecture, public vs private API classification, VtexIdclientAutCookie management, API key protection, and secure request proxying. Use for any headless commerce project that must never expose VTEX_APP_KEY or call private VTEX APIs from the browser.Other plugins' skills for the same job