# srainney/aap_pay architecture

> Architecture diagram of the GitHub repository srainney/aap_pay, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/srainney/aap_pay
- Repository: https://github.com/srainney/aap_pay

## Overview

##### Project type and purpose
- This is a full-stack payment-processing application split into two pieces:
- A lightweight JavaScript/HTML client (“JSClient”) that drives the UI for Agent-Assisted Pay.
- A backend of Twilio Serverless Functions (“Server”) that orchestrates calls, tokenizes/charges payments, and syncs real-time state.
- Purpose: enable agents to place outbound calls (PSTN or SIP), capture call SIDs, accept payment data, process charges, and broadcast updates via Twilio Sync.

##### File structure
- Top level:
- JSClient/ – the static frontend (index.html, payment.html, payment.js, img folder).
- Server/ – Node.js Twilio Serverless project (functions/*, config & lock files).
- “Server/functions” is subdivided by domain:
- aap/ → changeCapture.js, changeStatus.js, startCapture.js
- connector/ → tokenization (tokenize.js) and charging (charge.js)
- pv/ → call initiation (callToPSTN.js, callToSIP.js)
- sync/ → real-time updates (getSyncToken.js, setupSyncServices.js, uuiSyncUpdate.js, paySyncUpdate.protected.js)
- Config & deployment: .env (Twilio credentials), .twilioserverlessrc, .nvmrc, pnpm-lock, package.json.

##### Key insights from README
- Dev workflow: local JSClient at localhost:1234, Server via `twilio serverless:start` (Twilio CLI + plugin).
- Production: deploy Server via `pnpm run deploy` into Twilio Runtime.
- Payment flow: outbound call → capture Call SID → tokenize card → charge → sync updates.

##### System components & interactions
a. Main components
- Frontend (JSClient) – static pages + Twilio Sync JS SDK + HTTP calls to Server.
- Backend (Twilio Serverless Functions) – microservices handling calls, payments, and sync logic.
- Twilio REST API Suite
– Voice API (connect to PSTN/SIP)
– Pay API (tokenize & charge)
– Sync API (real-time data and events)
- External endpoints
– PSTN carriers
– SIP endpoints
b. Relationships & data flows
- User/agent web UI → HTTP(S) → Serverless Functions
- Payment Function calls Twilio Pay REST → returns token/charge response
- Call Functions call Twilio Voice REST → initiate call, return Call SID
- Sync Functions call Twilio Sync REST → create/update documents/channels
- JSClient listens on Sync events → updates UI in real time
c. Architectural patterns & principles
- Serverless microservices: each Twilio Function addresses a single responsibility.
- Event-driven updates via Twilio Sync (pub/sub).
- Clear separation of concerns: “pv” for calls, “connector” for payments, “sync” for real-time.
d. Key technologies & frameworks
- Node.js Twilio Serverless Functions
- Twilio REST APIs: Voice, Pay, Sync
- Static JS/HTML frontend (bundled with pnpm & a dev server)
- Twilio CLI + Serverless plugin (local dev & deploy)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (JSClient)" 
        direction TB
        JSClient["JSClient/src/"]:::frontend
        Index["index.html"]:::frontend
        PaymentPage["payment.html"]:::frontend
        PaymentJS["payment.js"]:::frontend
        Images["img/"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend (Twilio Serverless Functions)"
        direction TB
        subgraph "aap (Call Capture)" 
            direction TB
            ChangeCapture["changeCapture.js"]:::backend
            ChangeStatus["changeStatus.js"]:::backend
            StartCapture["startCapture.js"]:::backend
        end
        subgraph "pv (Call Initiation)"
            direction TB
            CallPSTN["callToPSTN.js"]:::backend
            CallSIP["callToSIP.js"]:::backend
        end
        subgraph "connector (Payment Processing)"
            direction TB
            Tokenize["tokenize.js"]:::backend
            Charge["charge.js"]:::backend
        end
        subgraph "sync (Real-Time Sync)"
            direction TB
            GetSyncToken["getSyncToken.js"]:::backend
            SetupSync["setupSyncServices.js"]:::backend
            UIUpdate["uuiSyncUpdate.js"]:::backend
            ProtectedUpdate["paySyncUpdate.protected.js"]:::backend
        end
        Env[".env"]:::config
        TwilioConfig[".twilioserverlessrc"]:::config
        Nvm[".nvmrc"]:::config
        Dependencies["package.json & pnpm-lock.yaml"]:::config
    end

    %% Twilio Services
    subgraph "Twilio Cloud"
        direction TB
        VoiceAPI("Voice API"):::twilio
        PayAPI("Pay API"):::twilio
        SyncAPI("Sync API"):::twilio
    end

    %% External Endpoints
    PSTN("PSTN Carrier"):::external
    SIP("SIP Endpoint"):::external
    PaymentProcessor("External Payment Processor"):::external

    %% Data Flows
    Index -->|"Trigger Call"| CallPSTN
    Index -->|"Trigger Call"| CallSIP
    CallPSTN -->|"Initiate Call"| VoiceAPI
    CallSIP -->|"Initiate Call"| VoiceAPI
    VoiceAPI -->|"Call SID"| StartCapture
    StartCapture -->|"Capture SID"| Index

    Index -->|"Enter Card Data"| Tokenize
    Tokenize -->|"Token request"| PayAPI
    PayAPI -->|"Token response"| Charge
    Charge -->|"Charge request"| PayAPI
    PayAPI -->|"Charge response"| ProtectedUpdate

    ProtectedUpdate -->|"Sync Update"| SyncAPI
    UIUpdate -->|"Sync Update"| SyncAPI
    SetupSync -->|"Initialize Services"| SyncAPI
    GetSyncToken -->|"Get Token"| SyncAPI
    SyncAPI -->|"Realtime Events"| PaymentJS

    %% Twilio API to External
    VoiceAPI --> PSTN
    VoiceAPI --> SIP
    PayAPI --> PaymentProcessor

    %% Frontend uses Sync SDK
    PaymentJS -->|"WebSocket/Sync"| SyncAPI

    %% Config and Deployment Path
    Dependencies -.-> TwilioConfig
    TwilioConfig -.-> Env
    Env -.-> Nvm

    %% Click Events
    click JSClient "https://github.com/srainney/aap_pay/tree/main/JSClient/src/"
    click Index "https://github.com/srainney/aap_pay/blob/main/JSClient/src/index.html"
    click PaymentPage "https://github.com/srainney/aap_pay/blob/main/JSClient/src/payment.html"
    click PaymentJS "https://github.com/srainney/aap_pay/blob/main/JSClient/src/payment.js"
    click Images "https://github.com/srainney/aap_pay/tree/main/JSClient/src/img/"
    click ChangeCapture "https://github.com/srainney/aap_pay/blob/main/Server/functions/aap/changeCapture.js"
    click ChangeStatus "https://github.com/srainney/aap_pay/blob/main/Server/functions/aap/changeStatus.js"
    click StartCapture "https://github.com/srainney/aap_pay/blob/main/Server/functions/aap/startCapture.js"
    click Tokenize "https://github.com/srainney/aap_pay/blob/main/Server/functions/connector/tokenize.js"
    click Charge "https://github.com/srainney/aap_pay/blob/main/Server/functions/connector/charge.js"
    click CallPSTN "https://github.com/srainney/aap_pay/blob/main/Server/functions/pv/callToPSTN.js"
    click CallSIP "https://github.com/srainney/aap_pay/blob/main/Server/functions/pv/callToSIP.js"
    click GetSyncToken "https://github.com/srainney/aap_pay/blob/main/Server/functions/sync/getSyncToken.js"
    click SetupSync "https://github.com/srainney/aap_pay/blob/main/Server/functions/sync/setupSyncServices.js"
    click UIUpdate "https://github.com/srainney/aap_pay/blob/main/Server/functions/sync/uuiSyncUpdate.js"
    click ProtectedUpdate "https://github.com/srainney/aap_pay/blob/main/Server/functions/sync/paySyncUpdate.protected.js"
    click Env "https://github.com/srainney/aap_pay/blob/main/Server/.env"
    click TwilioConfig "https://github.com/srainney/aap_pay/blob/main/Server/.twilioserverlessrc"
    click Nvm "https://github.com/srainney/aap_pay/blob/main/Server/.nvmrc"
    click Dependencies "https://github.com/srainney/aap_pay/blob/main/Server/package.json"
    click Dependencies "https://github.com/srainney/aap_pay/blob/main/Server/pnpm-lock.yaml"

    %% Styles
    classDef frontend fill:#D0E6FF,stroke:#0366d6,stroke-width:1px
    classDef backend fill:#DFFFE0,stroke:#28a745,stroke-width:1px
    classDef twilio fill:#F3E5F5,stroke:#6f42c1,stroke-width:1px,stroke-dasharray: 5 5
    classDef external fill:#E2E2E2,stroke:#6c757d,stroke-width:1px,stroke-dasharray: 2 2
    classDef config fill:#FFF4E6,stroke:#d15704,stroke-width:1px,stroke-dasharray: 2 2
```
