prepare( 'SELECT a.*, x.receipt FROM approvals a LEFT JOIN refund_attempts x ON x.intent_id = a.intent_id WHERE a.intent_id = ? AND a.tenant = ?' ); $query->execute([$intent, $tenant]); $approved = $query->fetch(PDO::FETCH_ASSOC); $query->closeCursor(); if (!$approved || $approved['order_id'] !== $proposal->order || $approved['amount_minor'] !== $proposal->amountMinor || $approved['currency'] !== $proposal->currency) { throw new Denied('Approval does not match request'); } if ($approved['receipt'] !== null) { return $approved['receipt']; } if ($now >= $approved['expires_at']) { throw new Denied('Approval expired'); } // Persist the attempt before the external call. This fixture uses autocommit. $db->prepare('INSERT INTO refund_attempts (intent_id) VALUES (?) ON CONFLICT (intent_id) DO NOTHING')->execute([$intent]); $receipt = $provider->refund('refund:'.$intent, $proposal); $db->prepare('UPDATE refund_attempts SET receipt = ? WHERE intent_id = ?') ->execute([$receipt, $intent]); return $receipt; }