Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Vishwa-9106/PayGuard_Streaming_Project

Open more actions menu

Repository files navigation

PayGuard Streaming Fraud Detection

PayGuard is an end-to-end, near-real-time fraud-monitoring demonstration. It simulates card transactions, publishes them to Kafka, processes the streams in Databricks using a medallion architecture, produces alert tables, sends email notifications, and visualizes operational metrics in a Databricks Lakeview dashboard.

This is a demo project using generated data. It is not a production fraud-decisioning system.

Architecture

Python transaction simulator                 Fraud watchlist CSV
  normal / high-value / watchlist traffic          |
                 |                                 | one JSON file per record
                 v                                 v
       Confluent Cloud Kafka                 Unity Catalog volume
                 |                                 |
                 +----------> Databricks Bronze <---+
                                      |
                                      v
                              Databricks Silver
                       transactions | customers | watchlist
                                      |
                 +--------------------+--------------------+
                 v                    v                    v
      Watchlist-match alerts   High-value alerts    Windowed volumes
                 |                    |                    |
                 +------> email notification      Lakeview dashboard

Repository layout

Path Purpose
kafka_producer/ Python-based customer/merchant generator and Kafka transaction producers.
databricks notebooks and pipelines/payguard_project/ Databricks exploratory notebooks, secret setup, ingestion code, and streaming pipeline definitions.
dashboard/PayGuard Fraud Detection Monitoring.lvdash.json Lakeview dashboard definition.
postgres sql/ Customer seed (customers_historic.sql) and incremental-change (customers_incremental.sql) scripts.

Data flow

1. Source simulation

The kafka_producer application generates deterministic synthetic customers and merchants from RANDOM_SEED, persists them to kafka_producer/data/, and publishes JSON transactions to Kafka. It uses SASL/SSL and reads connection details from kafka_producer/.env.

Available producer entry points:

Command Behaviour
python producer_normal.py Generates normal transaction traffic; it removes fraud score/reason fields from the Kafka payload.
python producer_fraud_transaction.py Generates high-value transactions (at least 100001.00) to exercise the high-value alert path.
python producer_fraud_card.py Generates transactions using a configured fraud/watchlist card scenario.
python consumer.py Reads and logs topic messages for development verification.

The simulator has rules for high-value transactions, impossible travel, new devices, high-risk or blacklisted merchants, international usage, velocity, and card-testing patterns. These scores are used by the simulator but are not written to the Kafka payload used by the pipeline.

2. Bronze ingestion

payguard.bronze.transactions reads credit_card_transactions from Kafka, retaining the raw JSON payload and Kafka metadata (topic, partition, offset, timestamp) with an ingestion timestamp.

payguard.bronze.fraud_watchlist uses Databricks Auto Loader to read JSON files from:

/Volumes/payguard/source/fraud_watchlist/source_data/

The watchlist generator notebook converts the repository CSV into one JSON file per record so it can be ingested incrementally.

3. Silver transformation

payguard.silver.transactions parses the JSON payload into typed fields and preserves Kafka lineage. It drops records missing transaction, customer, card, or merchant identifiers, and records an expectation for positive amounts.

payguard.silver.fraud_watchlist normalizes IDs, risk levels, and actions; converts effective_from to a timestamp; and retains source-file lineage.

payguard.silver.customers cleans a Bronze customer stream. The Bronze customer source is expected to be provisioned separately; the included PostgreSQL scripts provide the customer seed and incremental data.

4. Gold outputs and alerts

Table Rule/output
payguard.gold.fraud_card_alert Joins transactions to fraud-watchlist card IDs and enriches matches with customer details. Both stream inputs use five-minute watermarks.
payguard.gold.high_value_transactions_alert Flags a transaction when its amount exceeds the customer’s transaction_limit.
payguard.gold.transaciton_count_by_minute One-minute event-time transaction counts with a five-minute watermark.
payguard.gold.transaciton_count_by_minute_sliding_window Five-minute window counts updated every minute.

The two alert notifier modules use foreachBatch and Gmail SMTP credentials stored in Databricks Secrets to email alerts.

Prerequisites

  • Python 3.10+ and pip
  • A Kafka-compatible cluster (the current code is configured for Confluent Cloud SASL/SSL)
  • A Databricks workspace with Unity Catalog, access to the required volume paths, and permissions to create/read the payguard catalog schemas and tables
  • A Databricks pipeline environment supporting pyspark.pipelines
  • A configured Gmail App Password if email notifications will be enabled
  • PostgreSQL if you use the included customer bootstrap scripts

Local Kafka producer setup

From the repository root:

cd kafka_producer
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txt

Create kafka_producer/.env with your own Kafka connection details:

BOOTSTRAP_SERVERS=your-cluster-host:9092
API_KEY=your-kafka-api-key
API_SECRET=your-kafka-api-secret
TOPIC_NAME=credit_card_transactions
TRANSACTIONS_PER_SECOND=5
FRAUD_PERCENTAGE=0.08
TOTAL_CUSTOMERS=1000
TOTAL_MERCHANTS=200
RANDOM_SEED=42

Start normal traffic:

python producer_normal.py

Run the specialised producers in separate terminals when testing each alert scenario.

Databricks deployment order

  1. Create the payguard catalog/schemas and Unity Catalog volumes referenced by the code.
  2. Put Kafka credentials in the Databricks secret scope payguard-scope under kafka_connection_details as JSON with bootstrap_servers, api_key, api_secret, and topic keys.
  3. Put the Gmail App Password in payguard-scope under the key expected by the notifier modules.
  4. Load the historic customer dataset into the Bronze customer table expected by payguard.silver.customers; then apply the incremental SQL script when needed.
  5. Upload or generate fraud-watchlist JSON files in the configured volume path.
  6. Deploy the Bronze, Silver, Gold, and alert Python modules as one Databricks pipeline. Keep dependent tables in the same pipeline or ensure upstream tables are already available.
  7. Import dashboard/PayGuard Fraud Detection Monitoring.lvdash.json, map its datasets to the deployed tables, and refresh the dashboard.

The 01_kafka_streaming_test.py and 04_Autoloader_test.py notebooks are useful for validating connectivity and ingestion before deploying the full pipeline.

Dashboard

The Lakeview dashboard includes alert and transaction counters, alert trends, high-risk activity, top customers and merchants, payment-channel and international/domestic splits, a country heatmap, and windowed transaction-volume views.

Security and repository hygiene

Before creating or pushing a new remote repository:

  • Rotate all Kafka API keys, passwords, Gmail App Passwords, and Databricks tokens that have appeared in local files or notebook history.
  • Remove hard-coded credentials from 01_kafka_streaming_test.py and 02_Setup_Secret_Scope.py. Store secrets only in Databricks Secrets or local untracked environment files.
  • Do not commit kafka_producer/.env, .venv/, generated data/, Databricks personal-access tokens, or real cardholder data. Add them to .gitignore.
  • The included SQL and generated data contain email addresses and card-like numbers. Treat them as demo data and review them before publishing the repository.

Current implementation notes

  • Table names transaciton_count_by_minute* retain the source spelling for compatibility.
  • The customer Bronze ingestion definition is not included in this repository, although the Silver customer pipeline requires payguard.bronze.customers.
  • The pipeline code uses fixed catalog, secret-scope, and Unity Catalog volume names. Parameterize these before deploying to another workspace.
  • There are no automated tests or CI workflows in the repository at present.

About

PayGuard Streaming Fraud Detection is an end-to-end real-time fraud monitoring project that simulates card transactions with Python and Kafka, processes them through Databricks Bronze/Silver/Gold pipelines, generates fraud and high-value transaction alerts, sends email notifications, and visualizes insights in a Lakeview dashboard.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.