chargeback-guard 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +311 -0
- package/docs/api.md +278 -0
- package/docs/architecture.md +281 -0
- package/docs/configuration.md +292 -0
- package/docs/getting-started.md +155 -0
- package/examples/advancedConfig.ts +123 -0
- package/examples/basicUsage.ts +98 -0
- package/examples/stripeIntegration.ts +106 -0
- package/package.json +181 -0
- package/src/ai/fraudDetection.ts +261 -0
- package/src/ai/patternRecognition.ts +218 -0
- package/src/analytics/dashboard.ts +195 -0
- package/src/analytics/metrics.ts +175 -0
- package/src/analytics/predictions.ts +135 -0
- package/src/analytics/reports.ts +221 -0
- package/src/api/controllers.ts +339 -0
- package/src/api/middleware.ts +172 -0
- package/src/api/routes.ts +141 -0
- package/src/config.ts +231 -0
- package/src/core/chargebackGuard.ts +616 -0
- package/src/core/eventEmitter.ts +118 -0
- package/src/core/lifecycle.ts +215 -0
- package/src/database/schema.ts +392 -0
- package/src/dispute/analyzer.ts +317 -0
- package/src/dispute/bankIntegration.ts +274 -0
- package/src/dispute/detector.ts +239 -0
- package/src/dispute/responseEngine.ts +440 -0
- package/src/evidence/collector.ts +426 -0
- package/src/evidence/encryption.ts +168 -0
- package/src/evidence/storage.ts +197 -0
- package/src/evidence/validator.ts +184 -0
- package/src/index.ts +43 -0
- package/src/integrations/paypal.ts +258 -0
- package/src/integrations/stripe.ts +280 -0
- package/src/integrations/webhook.ts +332 -0
- package/src/notifications/email.ts +161 -0
- package/src/notifications/inApp.ts +319 -0
- package/src/notifications/sms.ts +58 -0
- package/src/security/auth.ts +153 -0
- package/src/security/rateLimit.ts +77 -0
- package/src/security/validation.ts +166 -0
- package/src/server.ts +122 -0
- package/src/types/index.ts +790 -0
- package/src/utils/formatters.ts +72 -0
- package/src/utils/helpers.ts +193 -0
- package/src/utils/logger.ts +88 -0
- package/src/utils/validators.ts +39 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# chargeback-guard — Architecture
|
|
2
|
+
|
|
3
|
+
This document describes the internal design of chargeback-guard v2.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## High-Level Overview
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
11
|
+
│ Your Application │
|
|
12
|
+
│ │
|
|
13
|
+
│ registerPayment() handleDispute() │
|
|
14
|
+
│ │ │ │
|
|
15
|
+
└─────────┼─────────────────────────┼─────────────────────────────┘
|
|
16
|
+
│ │
|
|
17
|
+
▼ ▼
|
|
18
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
19
|
+
│ ChargebackGuard Core │
|
|
20
|
+
│ │
|
|
21
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────┐ │
|
|
22
|
+
│ │ Evidence │ │ Dispute │ │ Fraud Detection │ │
|
|
23
|
+
│ │ Collection │ │ Analysis │ │ (AI / ML) │ │
|
|
24
|
+
│ └──────┬───────┘ └──────┬───────┘ └────────┬─────────┘ │
|
|
25
|
+
│ │ │ │ │
|
|
26
|
+
│ └──────────────────┴──────────────────────┘ │
|
|
27
|
+
│ │ │
|
|
28
|
+
│ ┌────────▼────────┐ │
|
|
29
|
+
│ │ Response Engine│ ←── AI reply generation │
|
|
30
|
+
│ └────────┬────────┘ │
|
|
31
|
+
│ │ │
|
|
32
|
+
│ ┌─────────────┼──────────────┐ │
|
|
33
|
+
│ ▼ ▼ ▼ │
|
|
34
|
+
│ Stripe API PayPal API Square API │
|
|
35
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
36
|
+
│
|
|
37
|
+
▼
|
|
38
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
39
|
+
│ Infrastructure Layer │
|
|
40
|
+
│ │
|
|
41
|
+
│ Database (SQLite/PG/MySQL) Redis Cache Notifications │
|
|
42
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Module Structure
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
src/
|
|
51
|
+
├── core/
|
|
52
|
+
│ ├── chargebackGuard.ts # Main orchestrator class
|
|
53
|
+
│ ├── eventEmitter.ts # Typed event bus
|
|
54
|
+
│ └── lifecycle.ts # Startup / shutdown hooks
|
|
55
|
+
│
|
|
56
|
+
├── evidence/
|
|
57
|
+
│ ├── collector.ts # Collects device, geo, behavior signals
|
|
58
|
+
│ ├── validator.ts # Validates evidence completeness & quality
|
|
59
|
+
│ └── storage.ts # Persists/retrieves evidence bundles
|
|
60
|
+
│
|
|
61
|
+
├── dispute/
|
|
62
|
+
│ ├── detector.ts # Parses incoming dispute webhooks
|
|
63
|
+
│ ├── analyzer.ts # Selects response strategy per reason code
|
|
64
|
+
│ └── responseEngine.ts # Builds & submits evidence reply to provider
|
|
65
|
+
│
|
|
66
|
+
├── ai/
|
|
67
|
+
│ └── fraudDetection.ts # AI-powered fraud score & reply generation
|
|
68
|
+
│
|
|
69
|
+
├── integrations/
|
|
70
|
+
│ ├── stripe.ts # Stripe Disputes API wrapper
|
|
71
|
+
│ ├── paypal.ts # PayPal Disputes API wrapper
|
|
72
|
+
│ └── square.ts # Square Disputes API wrapper
|
|
73
|
+
│
|
|
74
|
+
├── database/
|
|
75
|
+
│ └── schema.ts # Database schema manager + query helpers
|
|
76
|
+
│
|
|
77
|
+
├── notifications/
|
|
78
|
+
│ ├── email.ts # SMTP email sender
|
|
79
|
+
│ ├── sms.ts # Twilio SMS sender
|
|
80
|
+
│ ├── webhook.ts # Outbound webhook dispatcher
|
|
81
|
+
│ └── inApp.ts # Notification service coordinator
|
|
82
|
+
│
|
|
83
|
+
├── security/
|
|
84
|
+
│ ├── encryption.ts # AES-256 encryption for sensitive fields
|
|
85
|
+
│ ├── rateLimit.ts # Per-IP and per-key rate limiting
|
|
86
|
+
│ └── auth.ts # JWT auth middleware
|
|
87
|
+
│
|
|
88
|
+
├── analytics/
|
|
89
|
+
│ └── stats.ts # Aggregation queries for ProtectionStats
|
|
90
|
+
│
|
|
91
|
+
├── api/
|
|
92
|
+
│ └── router.ts # Optional Express/Fastify API server
|
|
93
|
+
│
|
|
94
|
+
├── types/
|
|
95
|
+
│ └── index.ts # All interfaces, enums, and shared types
|
|
96
|
+
│
|
|
97
|
+
├── config.ts # Environment variable loading
|
|
98
|
+
├── utils/
|
|
99
|
+
│ └── logger.ts # Structured logger
|
|
100
|
+
└── index.ts # Public API surface
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Data Flow: Payment Registration
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
registerPayment(paymentData)
|
|
109
|
+
│
|
|
110
|
+
├── 1. validatePaymentData() — required fields, amount > 0
|
|
111
|
+
│
|
|
112
|
+
├── 2. FraudDetection.preCheck() — AI trust score (0–100)
|
|
113
|
+
│ └── riskLevel assigned from score
|
|
114
|
+
│
|
|
115
|
+
├── 3. EvidenceCollector.collect() — device FP, geo, behavior
|
|
116
|
+
│
|
|
117
|
+
├── 4. EvidenceStorage.save() — persist bundle, indexed by orderId
|
|
118
|
+
│
|
|
119
|
+
├── 5. emit('payment:registered')
|
|
120
|
+
│
|
|
121
|
+
└── 6. return RegistrationResult { trustScore, riskLevel, ... }
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## Data Flow: Dispute Handling
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
handleDispute(dispute)
|
|
130
|
+
│
|
|
131
|
+
├── 1. DisputeDetector.parse() — normalize & validate dispute
|
|
132
|
+
│
|
|
133
|
+
├── 2. EvidenceStorage.getByOrderId()— fetch pre-collected evidence
|
|
134
|
+
│
|
|
135
|
+
├── 3. DisputeAnalyzer.analyze() — strategy per reason code
|
|
136
|
+
│ ├── 'product_not_received' → delivery proof emphasis
|
|
137
|
+
│ ├── 'unauthorized_transaction' → device FP + geo
|
|
138
|
+
│ └── 'fraudulent' → full AI analysis
|
|
139
|
+
│
|
|
140
|
+
├── 4. FraudDetection.analyze() — AI confidence score
|
|
141
|
+
│
|
|
142
|
+
├── 5a. confidence ≥ threshold AND autoReply = true
|
|
143
|
+
│ └── ResponseEngine.submit() → provider API
|
|
144
|
+
│ └── actionTaken = 'auto-reply_submitted'
|
|
145
|
+
│
|
|
146
|
+
├── 5b. confidence < threshold
|
|
147
|
+
│ └── actionTaken = 'manual_review_required'
|
|
148
|
+
│ └── emit('dispute:detected') for notification
|
|
149
|
+
│
|
|
150
|
+
├── 6. updateDisputeRecord() — persist outcome
|
|
151
|
+
│
|
|
152
|
+
└── 7. return DisputeHandlingResult
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Evidence Bundle
|
|
158
|
+
|
|
159
|
+
The evidence bundle assembled per order contains:
|
|
160
|
+
|
|
161
|
+
| Evidence Type | Source | Description |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `delivery_proof` | Metadata field / shipping API | Tracking number, carrier, delivered timestamp |
|
|
164
|
+
| `device_fingerprint` | Client SDK / metadata | Browser fingerprint, device ID |
|
|
165
|
+
| `geolocation` | IP geolocation | Country, city, ISP |
|
|
166
|
+
| `customer_communication` | CRM integration | Email thread, support tickets |
|
|
167
|
+
| `transaction_log` | Payment provider | All charges on the card |
|
|
168
|
+
| `behavior_analysis` | Analytics | Session duration, page views, cart events |
|
|
169
|
+
| `customer_history` | Your database | Previous orders, dispute history |
|
|
170
|
+
| `email_confirmation` | Email service | Order confirmation email sent timestamp |
|
|
171
|
+
| `payment_log` | Provider | Auth + capture log |
|
|
172
|
+
| `browser_screenshot` | Optional | Checkout page screenshot at purchase time |
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## AI Fraud Detection
|
|
177
|
+
|
|
178
|
+
The `FraudDetection` module uses a multi-signal scoring model:
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
trustScore = weighted_average([
|
|
182
|
+
customerHistory: 30%, // past orders, first-time buyer, return rate
|
|
183
|
+
deviceFingerprint: 20%, // known device vs new device
|
|
184
|
+
geolocation: 15%, // IP country matches billing country
|
|
185
|
+
behaviorAnalysis: 15%, // time on site, scrolling, clicks
|
|
186
|
+
transactionPattern: 20%, // amount vs average, velocity
|
|
187
|
+
])
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
When `ai.apiKey` is configured, an LLM call supplements the heuristic score with natural language reasoning. The LLM also generates the evidence reply text sent to the bank.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Database Schema
|
|
195
|
+
|
|
196
|
+
Four core tables:
|
|
197
|
+
|
|
198
|
+
### `payments`
|
|
199
|
+
| Column | Type | Description |
|
|
200
|
+
|---|---|---|
|
|
201
|
+
| `id` | TEXT PK | Internal UUID |
|
|
202
|
+
| `order_id` | TEXT UNIQUE | Your order ID |
|
|
203
|
+
| `customer_id` | TEXT | Your customer ID |
|
|
204
|
+
| `amount` | INTEGER | Cents |
|
|
205
|
+
| `currency` | TEXT | ISO 4217 |
|
|
206
|
+
| `provider` | TEXT | Payment provider |
|
|
207
|
+
| `transaction_id` | TEXT | Provider transaction ID |
|
|
208
|
+
| `trust_score` | REAL | 0–100 |
|
|
209
|
+
| `risk_level` | TEXT | Risk enum value |
|
|
210
|
+
| `metadata` | JSON | Raw payment metadata |
|
|
211
|
+
| `created_at` | INTEGER | Unix ms |
|
|
212
|
+
|
|
213
|
+
### `evidence_bundles`
|
|
214
|
+
| Column | Type | Description |
|
|
215
|
+
|---|---|---|
|
|
216
|
+
| `id` | TEXT PK | Bundle UUID |
|
|
217
|
+
| `order_id` | TEXT FK | Links to `payments.order_id` |
|
|
218
|
+
| `evidence_types` | JSON | Array of collected evidence types |
|
|
219
|
+
| `data` | JSON (encrypted) | Evidence payload |
|
|
220
|
+
| `collected_at` | INTEGER | Unix ms |
|
|
221
|
+
|
|
222
|
+
### `disputes`
|
|
223
|
+
| Column | Type | Description |
|
|
224
|
+
|---|---|---|
|
|
225
|
+
| `id` | TEXT PK | Provider dispute ID |
|
|
226
|
+
| `order_id` | TEXT FK | Links to `payments.order_id` |
|
|
227
|
+
| `amount` | INTEGER | Disputed amount |
|
|
228
|
+
| `reason` | TEXT | DisputeReason value |
|
|
229
|
+
| `status` | TEXT | DisputeStatus value |
|
|
230
|
+
| `action_taken` | TEXT | What ChargebackGuard did |
|
|
231
|
+
| `reply_submitted` | INTEGER | 0 or 1 |
|
|
232
|
+
| `confidence` | REAL | AI confidence |
|
|
233
|
+
| `due_by` | INTEGER | Evidence deadline (Unix ms) |
|
|
234
|
+
| `created_at` | INTEGER | Unix ms |
|
|
235
|
+
| `updated_at` | INTEGER | Unix ms |
|
|
236
|
+
|
|
237
|
+
### `notifications`
|
|
238
|
+
| Column | Type | Description |
|
|
239
|
+
|---|---|---|
|
|
240
|
+
| `id` | TEXT PK | Notification UUID |
|
|
241
|
+
| `event_type` | TEXT | NotificationEvent value |
|
|
242
|
+
| `channel` | TEXT | Email / SMS / webhook |
|
|
243
|
+
| `payload` | JSON | Event payload snapshot |
|
|
244
|
+
| `status` | TEXT | `pending` / `sent` / `failed` |
|
|
245
|
+
| `sent_at` | INTEGER | Unix ms |
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Lifecycle Hooks
|
|
250
|
+
|
|
251
|
+
`ChargebackGuard.lifecycle` is a `LifecycleManager` that manages startup and shutdown phases:
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
guard.lifecycle.on('initialized', () => console.log('Ready'));
|
|
255
|
+
guard.lifecycle.on('shutting_down', () => console.log('Draining...'));
|
|
256
|
+
guard.lifecycle.on('shutdown', () => console.log('Stopped'));
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Startup sequence (in order)
|
|
260
|
+
1. Load and validate configuration
|
|
261
|
+
2. Connect to database, run migrations
|
|
262
|
+
3. Connect to Redis (if configured)
|
|
263
|
+
4. Start notification services
|
|
264
|
+
5. Register health checkers
|
|
265
|
+
6. Mark lifecycle as `initialized`
|
|
266
|
+
|
|
267
|
+
### Shutdown sequence
|
|
268
|
+
1. Stop accepting new `registerPayment()` / `handleDispute()` calls
|
|
269
|
+
2. Flush in-flight evidence and reply submissions
|
|
270
|
+
3. Close database connections
|
|
271
|
+
4. Disconnect Redis
|
|
272
|
+
5. Emit `shutdown` event
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Security Considerations
|
|
277
|
+
|
|
278
|
+
- **Webhook signature verification** — always verify `stripe-signature` or `PAYPAL-TRANSMISSION-SIG` before calling `handleDispute()`.
|
|
279
|
+
- **Evidence encryption** — evidence bundles are AES-256 encrypted at rest when `security.encryptionKey` is set.
|
|
280
|
+
- **Rate limiting** — the optional HTTP server enforces per-IP rate limits to prevent replay attacks.
|
|
281
|
+
- **PII handling** — customer email and IP are hashed before logging; raw values stored in encrypted evidence only.
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# chargeback-guard — Configuration Reference
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Environment Variables
|
|
6
|
+
|
|
7
|
+
chargeback-guard uses `dotenv` and reads configuration from environment variables. Create a `.env` file at your project root:
|
|
8
|
+
|
|
9
|
+
```dotenv
|
|
10
|
+
# ── Application ────────────────────────────────────────────────
|
|
11
|
+
APP_NAME=ChargebackGuard
|
|
12
|
+
APP_VERSION=2.0.0
|
|
13
|
+
NODE_ENV=production # development | staging | production
|
|
14
|
+
PORT=3000
|
|
15
|
+
APP_URL=https://api.mystore.com
|
|
16
|
+
LOG_LEVEL=info # debug | info | warn | error
|
|
17
|
+
|
|
18
|
+
# ── Payment Provider ───────────────────────────────────────────
|
|
19
|
+
STRIPE_SECRET_KEY=sk_live_...
|
|
20
|
+
STRIPE_WEBHOOK_SECRET=whsec_...
|
|
21
|
+
PAYPAL_CLIENT_ID=...
|
|
22
|
+
PAYPAL_CLIENT_SECRET=...
|
|
23
|
+
|
|
24
|
+
# ── Database ───────────────────────────────────────────────────
|
|
25
|
+
DATABASE_TYPE=sqlite # sqlite | postgresql | mysql
|
|
26
|
+
DATABASE_URL=sqlite://./data/chargeback-guard.db
|
|
27
|
+
DATABASE_POOL_MIN=2
|
|
28
|
+
DATABASE_POOL_MAX=10
|
|
29
|
+
DATABASE_DEBUG=false
|
|
30
|
+
|
|
31
|
+
# ── Redis (optional) ──────────────────────────────────────────
|
|
32
|
+
REDIS_URL=redis://localhost:6379
|
|
33
|
+
REDIS_PASSWORD=
|
|
34
|
+
REDIS_DB=0
|
|
35
|
+
CACHE_TTL=3600
|
|
36
|
+
|
|
37
|
+
# ── AI / Fraud Detection ──────────────────────────────────────
|
|
38
|
+
OPENAI_API_KEY=sk-...
|
|
39
|
+
AI_MODEL=gpt-4o-mini
|
|
40
|
+
AI_CONFIDENCE_THRESHOLD=0.75
|
|
41
|
+
FRAUD_SCORE_THRESHOLD=0.65
|
|
42
|
+
|
|
43
|
+
# ── Notifications: Email ──────────────────────────────────────
|
|
44
|
+
SMTP_HOST=smtp.sendgrid.net
|
|
45
|
+
SMTP_PORT=587
|
|
46
|
+
SMTP_SECURE=false
|
|
47
|
+
SMTP_USER=apikey
|
|
48
|
+
SMTP_PASS=SG.xxx
|
|
49
|
+
EMAIL_FROM_NAME=ChargebackGuard Alerts
|
|
50
|
+
EMAIL_FROM_ADDRESS=alerts@mystore.com
|
|
51
|
+
ALERT_EMAIL=ops@mystore.com
|
|
52
|
+
|
|
53
|
+
# ── Notifications: SMS ────────────────────────────────────────
|
|
54
|
+
TWILIO_ACCOUNT_SID=ACxxx
|
|
55
|
+
TWILIO_AUTH_TOKEN=xxx
|
|
56
|
+
TWILIO_FROM_NUMBER=+15551234567
|
|
57
|
+
ALERT_PHONE=+15557654321
|
|
58
|
+
|
|
59
|
+
# ── Security ──────────────────────────────────────────────────
|
|
60
|
+
API_KEY=your-internal-api-key
|
|
61
|
+
JWT_SECRET=your-jwt-secret
|
|
62
|
+
RATE_LIMIT_MAX=100
|
|
63
|
+
RATE_LIMIT_WINDOW=900 # seconds
|
|
64
|
+
ENCRYPTION_KEY=32-byte-hex
|
|
65
|
+
|
|
66
|
+
# ── Subscription ──────────────────────────────────────────────
|
|
67
|
+
SUBSCRIPTION_PLAN=pro # free | starter | pro | enterprise | custom
|
|
68
|
+
MAX_PAYMENTS_PER_MONTH=50000
|
|
69
|
+
MAX_DISPUTES_PER_MONTH=5000
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Programmatic Configuration
|
|
75
|
+
|
|
76
|
+
Options passed to `createChargebackGuard()` (or `new ChargebackGuard()`) override environment variables:
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { createChargebackGuard } from 'chargeback-guard';
|
|
80
|
+
|
|
81
|
+
const guard = await createChargebackGuard({
|
|
82
|
+
apiKey: process.env.STRIPE_SECRET_KEY!,
|
|
83
|
+
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET!,
|
|
84
|
+
environment: 'production',
|
|
85
|
+
|
|
86
|
+
autoReply: true,
|
|
87
|
+
evidenceCollection: true,
|
|
88
|
+
|
|
89
|
+
database: {
|
|
90
|
+
type: 'postgresql',
|
|
91
|
+
url: process.env.DATABASE_URL!,
|
|
92
|
+
poolMin: 2,
|
|
93
|
+
poolMax: 20,
|
|
94
|
+
debug: false,
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
redis: {
|
|
98
|
+
url: process.env.REDIS_URL!,
|
|
99
|
+
ttl: 7200,
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
notifications: {
|
|
103
|
+
channels: ['email', 'webhook'],
|
|
104
|
+
email: {
|
|
105
|
+
host: 'smtp.sendgrid.net',
|
|
106
|
+
port: 587,
|
|
107
|
+
secure: false,
|
|
108
|
+
user: 'apikey',
|
|
109
|
+
pass: process.env.SMTP_PASS!,
|
|
110
|
+
fromName: 'ChargebackGuard',
|
|
111
|
+
fromAddress: 'alerts@mystore.com',
|
|
112
|
+
},
|
|
113
|
+
webhooks: [
|
|
114
|
+
{ url: 'https://hooks.slack.com/services/...', events: ['dispute:detected', 'dispute:won'] },
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
ai: {
|
|
119
|
+
model: 'gpt-4o-mini',
|
|
120
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
121
|
+
confidenceThreshold: 0.80,
|
|
122
|
+
fraudScoreThreshold: 0.60,
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
security: {
|
|
126
|
+
encryptionKey: process.env.ENCRYPTION_KEY!,
|
|
127
|
+
jwtSecret: process.env.JWT_SECRET!,
|
|
128
|
+
rateLimitMax: 100,
|
|
129
|
+
rateLimitWindowSeconds: 900,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## `ChargebackGuardConfig`
|
|
137
|
+
|
|
138
|
+
Top-level config object passed to the constructor.
|
|
139
|
+
|
|
140
|
+
| Field | Type | Default | Description |
|
|
141
|
+
|---|---|---|---|
|
|
142
|
+
| `apiKey` | `string` | required | Stripe secret key (or provider API key) |
|
|
143
|
+
| `webhookSecret` | `string?` | — | Webhook signing secret for verification |
|
|
144
|
+
| `environment` | `'development' \| 'staging' \| 'production'` | `'development'` | Controls logging verbosity and safety checks |
|
|
145
|
+
| `autoReply` | `boolean` | `true` | Automatically submit replies to the payment provider |
|
|
146
|
+
| `evidenceCollection` | `boolean` | `true` | Collect device/behavior evidence on `registerPayment()` |
|
|
147
|
+
| `database` | `DatabaseConfig` | SQLite at `./data/...` | Database connection settings |
|
|
148
|
+
| `redis` | `RedisConfig?` | — | Redis for caching (optional) |
|
|
149
|
+
| `notifications` | `NotificationConfig?` | — | Email / SMS / webhook alerts |
|
|
150
|
+
| `security` | `SecurityConfig?` | — | Encryption and rate-limiting |
|
|
151
|
+
| `ai` | `AIConfig?` | — | AI/fraud-detection model settings |
|
|
152
|
+
| `billing` | `BillingConfig?` | — | Subscription plan and usage limits |
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## Database Configuration
|
|
157
|
+
|
|
158
|
+
### SQLite (default)
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
database: {
|
|
162
|
+
type: 'sqlite',
|
|
163
|
+
url: 'sqlite://./data/chargeback-guard.db',
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
No extra dependencies needed. Suitable for single-instance deployments up to ~10 k disputes/month.
|
|
168
|
+
|
|
169
|
+
### PostgreSQL
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
database: {
|
|
173
|
+
type: 'postgresql',
|
|
174
|
+
url: 'postgresql://user:pass@host:5432/chargebacks',
|
|
175
|
+
poolMin: 2,
|
|
176
|
+
poolMax: 20,
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Requires `pg` installed: `npm install pg`.
|
|
181
|
+
|
|
182
|
+
### MySQL
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
database: {
|
|
186
|
+
type: 'mysql',
|
|
187
|
+
url: 'mysql://user:pass@host:3306/chargebacks',
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Requires `mysql2` installed: `npm install mysql2`.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Notification Configuration
|
|
196
|
+
|
|
197
|
+
### Email
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
notifications: {
|
|
201
|
+
channels: ['email'],
|
|
202
|
+
email: {
|
|
203
|
+
host: 'smtp.sendgrid.net',
|
|
204
|
+
port: 587,
|
|
205
|
+
secure: false,
|
|
206
|
+
user: 'apikey',
|
|
207
|
+
pass: 'SG.xxx',
|
|
208
|
+
fromName: 'ChargebackGuard Alerts',
|
|
209
|
+
fromAddress: 'alerts@mystore.com',
|
|
210
|
+
},
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Outbound Webhooks
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
notifications: {
|
|
218
|
+
channels: ['webhook'],
|
|
219
|
+
webhooks: [
|
|
220
|
+
{
|
|
221
|
+
url: 'https://hooks.slack.com/services/...',
|
|
222
|
+
events: ['dispute:detected', 'dispute:won', 'dispute:lost'],
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
url: 'https://yourapp.com/internal/chargeback-hook',
|
|
226
|
+
secret: 'hmac-signing-secret', // HMAC-SHA256 signature header
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### SMS (Twilio)
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
notifications: {
|
|
236
|
+
channels: ['sms'],
|
|
237
|
+
sms: {
|
|
238
|
+
accountSid: process.env.TWILIO_ACCOUNT_SID!,
|
|
239
|
+
authToken: process.env.TWILIO_AUTH_TOKEN!,
|
|
240
|
+
fromNumber: '+15551234567',
|
|
241
|
+
toNumber: '+15557654321',
|
|
242
|
+
},
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## AI / Fraud Detection Configuration
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
ai: {
|
|
252
|
+
model: 'gpt-4o-mini', // OpenAI model for dispute reply generation
|
|
253
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
254
|
+
confidenceThreshold: 0.75, // Minimum AI confidence to auto-reply (0–1)
|
|
255
|
+
fraudScoreThreshold: 0.65, // Score above which to flag as high-risk
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
When `autoReply: true` and AI confidence is below `confidenceThreshold`, the dispute is routed to `actionTaken: 'manual_review_required'` instead of being auto-submitted.
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## Security Configuration
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
security: {
|
|
267
|
+
encryptionKey: '32-byte-hex-string', // AES-256 key for sensitive data at rest
|
|
268
|
+
jwtSecret: 'your-jwt-secret', // API authentication (if using built-in server)
|
|
269
|
+
rateLimitMax: 100, // Max requests per window
|
|
270
|
+
rateLimitWindowSeconds: 900, // 15-minute window
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Subscription Plans
|
|
277
|
+
|
|
278
|
+
| Plan | Max Payments/mo | Max Disputes/mo | AI Auto-Reply |
|
|
279
|
+
|---|---|---|---|
|
|
280
|
+
| `free` | 500 | 50 | ✗ |
|
|
281
|
+
| `starter` | 5 000 | 500 | ✓ |
|
|
282
|
+
| `pro` | 50 000 | 5 000 | ✓ |
|
|
283
|
+
| `enterprise` | Unlimited | Unlimited | ✓ |
|
|
284
|
+
| `custom` | Negotiated | Negotiated | ✓ |
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
billing: {
|
|
288
|
+
plan: 'pro',
|
|
289
|
+
maxPaymentsPerMonth: 50_000,
|
|
290
|
+
maxDisputesPerMonth: 5_000,
|
|
291
|
+
}
|
|
292
|
+
```
|