import { ParmanaClient, ParmanaApiError } from "@parmanasystems/sdk-client";
import type { ExecutionAttestation } from "@parmanasystems/sdk-client";
const client = new ParmanaClient({
baseUrl: process.env.PARMANA_URL ?? "http://localhost:3000",
apiKey: process.env.PARMANA_API_KEY,
});
interface PaymentSignals {
amount: number;
currency: string;
merchantCategory: string;
accountAge: number; // days
fraudScore: number; // 0.0–1.0
velocityCount24h: number;
countryRisk: "LOW" | "MEDIUM" | "HIGH";
}
interface PaymentAuthResult {
transactionId: string;
authorized: boolean;
action: string;
reason: string;
requiresHold: boolean;
attestationSignature: string;
}
async function authorizePayment(
transactionId: string,
signals: PaymentSignals
): Promise<PaymentAuthResult> {
const attestation = await client.execute({
executionId: transactionId, // transaction ID IS the executionId — must be globally unique
policyId: "payment-approval",
policyVersion: "1.0.0",
signals,
});
// Always verify before authorizing — fail closed
const verification = await client.verify(attestation);
if (!verification.valid) {
return {
transactionId,
authorized: false,
action: "reject",
reason: "Governance attestation could not be verified. Payment blocked.",
requiresHold: false,
attestationSignature: "",
};
}
const authorized =
attestation.execution_state === "completed" &&
attestation.decision.action === "approve";
return {
transactionId,
authorized,
action: attestation.decision.action,
reason: attestation.decision.reason,
requiresHold: attestation.execution_state === "pending_override",
attestationSignature: attestation.signature,
};
}
// Post-execution confirmation — prove the payment matched the authorization
async function confirmPaymentExecuted(
transactionId: string,
attestation: ExecutionAttestation,
paymentDetails: {
amount: number;
currency: string;
recipientAccountId: string;
}
) {
const proof = await client.confirmExecution({
attestation,
executedAction: {
actionType: "payment_transfer",
actionId: transactionId,
actionTimestamp: new Date().toISOString(),
actionDetails: paymentDetails,
},
timeWindowSeconds: 60, // action must occur within 60 seconds of authorization
});
return proof;
}
// Example usage
async function main() {
// Standard approval
const approved = await authorizePayment("TXN-20240115-0001", {
amount: 250,
currency: "USD",
merchantCategory: "retail",
accountAge: 730,
fraudScore: 0.02,
velocityCount24h: 3,
countryRisk: "LOW",
});
console.log(approved.action); // "approve"
console.log(approved.authorized); // true
// Rejected: fraud score
const blocked = await authorizePayment("TXN-20240115-0002", {
amount: 500,
currency: "USD",
merchantCategory: "electronics",
accountAge: 90,
fraudScore: 0.92,
velocityCount24h: 15,
countryRisk: "LOW",
});
console.log(blocked.action); // "reject"
console.log(blocked.authorized); // false
// Hold: large transaction
const held = await authorizePayment("TXN-20240115-0003", {
amount: 15000,
currency: "USD",
merchantCategory: "wire_transfer",
accountAge: 1200,
fraudScore: 0.05,
velocityCount24h: 1,
countryRisk: "LOW",
});
console.log(held.action); // "hold"
console.log(held.requiresHold); // true
}
main().catch(console.error);