Fraud Detection with Human Review
For high-value transactions, you need a fraud detection pipeline that auto-approves low-risk payments, auto-rejects high-risk ones, and routes medium-risk transactions to a human reviewer with a configurable timeout. Orcher's wait_for_event_with_timeout makes the human-in-the-loop step trivial — the workflow pauses until a reviewer signals approval or the timeout expires.
How it works
One risk assessment, three routes. The two automatic routes finish in milliseconds; the third parks the workflow in front of a human — and only that route pays any waiting cost.
Example: triage with an event gate
1use orcher::prelude::*;2use anyhow::Result;3use std::time::Duration;45#[derive(Debug, Clone, Serialize, Deserialize)]6enum RiskLevel { Low, Medium, High }78#[derive(Debug, Clone, Serialize, Deserialize)]9struct FraudScore {10 risk_level: RiskLevel,11 score: f64,12 reasons: Vec<String>,13}1415#[derive(Debug, Clone, Serialize, Deserialize)]16struct ReviewDecision {17 approved: bool,18 reviewer: String,19 notes: String,20}2122#[task]23async fn assess_fraud_risk(input: PaymentInput) -> Result<FraudScore> {24 // Call fraud detection service (ML model, rules engine, etc.)25 let score = fraud::assess(&input).await?;26 Ok(score)27}2829#[task]30async fn notify_fraud_team(input: PaymentInput, score: FraudScore) -> Result<()> {31 // Send to review queue with all context32 fraud::create_review_case(&input, &score).await?;33 Ok(())34}3536#[task]37async fn record_fraud_decision(order_id: String, approved: bool, reason: String) -> Result<()> {38 audit::log_fraud_decision(&order_id, approved, &reason).await?;39 Ok(())40}4142#[workflow(name = "fraud_checked_payment", version = "1.0.0")]43pub async fn fraud_checked_payment(44 ctx: WorkflowContext,45 input: PaymentInput,46) -> Result<PaymentResult> {47 // Step 1: Assess fraud risk48 let fraud_score = ctx.execute_task(assess_fraud_risk, input.clone()).await?;4950 match fraud_score.risk_level {51 RiskLevel::Low => {52 // Auto-approve: proceed directly to payment53 ctx.execute_task(record_fraud_decision, (54 input.order_id.clone(), true, "auto-approved: low risk".to_string()55 )).await?;56 }57 RiskLevel::High => {58 // Auto-reject: block the payment59 ctx.execute_task(record_fraud_decision, (60 input.order_id.clone(), false, format!("auto-rejected: {}", fraud_score.reasons.join(", "))61 )).await?;62 anyhow::bail!("Payment rejected: high fraud risk");63 }64 RiskLevel::Medium => {65 // Route to human reviewer66 ctx.execute_task(notify_fraud_team, (input.clone(), fraud_score)).await?;6768 // Wait for reviewer decision with 4-hour timeout69 let decision: Option<ReviewDecision> = ctx70 .wait_for_event_with_timeout("fraud_review_decision", Duration::from_secs(14400))71 .await?;7273 match decision {74 Some(d) if d.approved => {75 ctx.execute_task(record_fraud_decision, (76 input.order_id.clone(), true, format!("approved by {}", d.reviewer)77 )).await?;78 }79 Some(d) => {80 ctx.execute_task(record_fraud_decision, (81 input.order_id.clone(), false, format!("rejected by {}: {}", d.reviewer, d.notes)82 )).await?;83 anyhow::bail!("Payment rejected by fraud reviewer");84 }85 None => {86 // Timeout — auto-reject for safety87 ctx.execute_task(record_fraud_decision, (88 input.order_id.clone(), false, "auto-rejected: review timeout".to_string()89 )).await?;90 anyhow::bail!("Payment rejected: fraud review timeout");91 }92 }93 }94 }9596 // Fraud check passed — proceed to payment97 let result = ctx.execute_task(charge_payment_gateway, (98 input.amount, input.currency.clone(), input.idempotency_key.clone()99 )).await?;100101 Ok(PaymentResult {102 charge_id: result.charge_id,103 status: "completed".to_string(),104 order_id: input.order_id,105 })106}The review gate
wait_for_event_with_timeout is a race between a human and a clock. The workflow suspends — holding nothing — until either the reviewer's decision arrives as an event or the four-hour deadline fires, and the return value tells you which one won.
Note the ordering: the charge happens after the gate. A suspicious transaction waits as an intent, not as captured money — there is nothing to refund if the answer is no. The same suspension mechanics as durable timers apply: a deploy during the four-hour window changes nothing, and the reviewer's decision wakes the workflow wherever it lands.
Design notes
- Timeout must fail safe. For payments, no answer means no — an expired review auto-rejects. If rejection is too costly for your risk profile, escalate instead: re-notify and wait again, but never silently approve.
- Route the decision back with a correlation ID.
notify_fraud_teamshould include the workflow or order ID in the review case, so the reviewer UI can address itsfraud_review_decisionevent to this exact workflow instance. See Events for the delivery mechanics. - Record every decision, on every path. All five outcomes — auto-approve, auto-reject, human approve, human reject, timeout — pass through
record_fraud_decisionbefore the workflow acts. Combined with the journal, that's a complete who-decided-what audit trail with no extra logging code. - Keep thresholds out of the workflow.
assess_fraud_riskreturns the risk level; the scoring model and its thresholds live in the fraud service. Tuning them doesn't touch versioned workflow logic — and the journaled score keeps replay deterministic even as the model evolves.
Next Steps
- Next pattern: Refund Processing
- Previous: Subscription Billing with Dunning
- Back to Payment Workflows overview