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24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
"""Coursera clone-local checkout and order domain."""

from __future__ import annotations

import sqlite3
import secrets
from typing import Any

from websitebench.site_backend import PaymentConflict, PaymentRejected


TRIAL_PLAN = {
    "course_id": "deep-learning-specialization",
    "currency": "CNY",
    "fingerprint": "a8f095ae0d249f93a5c6adfeb1729f4580139a88046ee210560b71e4a2f83f7e",
    "plan_id": "deep-learning-specialization-trial",
    "plan_label": "Deep Learning Specialization 7-day trial",
    "pricing_evidence": "observed-authenticated-checkout-display",
    "renewal_currency": "CNY",
    "renewal_interval": "month",
    "renewal_minor": 19600,
    "subtotal_minor": 0,
    "tax_minor": 0,
    "total_minor": 0,
    "trial_days": 7,
}
COURSE_ID = str(TRIAL_PLAN["course_id"])
PLAN_ID = str(TRIAL_PLAN["plan_id"])
PLAN_FINGERPRINT = str(TRIAL_PLAN["fingerprint"])
FROZEN_TIME = "2026-08-16T00:00:00Z"

_SCHEMA = (
    """CREATE TABLE IF NOT EXISTS coursera_checkout_drafts (
        draft_id TEXT PRIMARY KEY,
        owner_subject_id TEXT NOT NULL,
        course_id TEXT NOT NULL,
        plan_id TEXT NOT NULL,
        plan_label TEXT NOT NULL,
        subtotal_minor INTEGER NOT NULL,
        tax_minor INTEGER NOT NULL,
        total_minor INTEGER NOT NULL,
        currency TEXT NOT NULL,
        trial_days INTEGER NOT NULL,
        renewal_minor INTEGER NOT NULL,
        renewal_currency TEXT NOT NULL,
        renewal_interval TEXT NOT NULL,
        fingerprint TEXT NOT NULL,
        payment_flow_id TEXT NOT NULL UNIQUE,
        status TEXT NOT NULL CHECK(status IN ('OPEN','COMPLETED','CANCELED')),
        created_at TEXT NOT NULL,
        updated_at TEXT NOT NULL
    )""",
    """CREATE TABLE IF NOT EXISTS coursera_orders (
        order_id TEXT PRIMARY KEY,
        owner_subject_id TEXT NOT NULL,
        draft_id TEXT NOT NULL UNIQUE REFERENCES coursera_checkout_drafts(draft_id),
        payment_flow_id TEXT NOT NULL UNIQUE,
        enrollment_id INTEGER NOT NULL REFERENCES coursera_enrollments(enrollment_id),
        course_id TEXT NOT NULL,
        plan_id TEXT NOT NULL,
        plan_label TEXT NOT NULL,
        subtotal_minor INTEGER NOT NULL,
        tax_minor INTEGER NOT NULL,
        total_minor INTEGER NOT NULL,
        currency TEXT NOT NULL,
        trial_days INTEGER NOT NULL,
        renewal_minor INTEGER NOT NULL,
        renewal_currency TEXT NOT NULL,
        renewal_interval TEXT NOT NULL,
        fingerprint TEXT NOT NULL,
        status TEXT NOT NULL CHECK(status IN ('PAID','CANCELED')),
        created_at TEXT NOT NULL,
        canceled_at TEXT
    )""",
    """CREATE UNIQUE INDEX IF NOT EXISTS coursera_one_active_paid_order
        ON coursera_orders(owner_subject_id,course_id)
        WHERE status='PAID'""",
)


def migrate(connection: sqlite3.Connection) -> None:
    """Install checkout tables in the caller's site migration transaction."""

    for statement in _SCHEMA:
        connection.execute(statement)
    _add_trial_columns(connection, "coursera_checkout_drafts")
    _add_trial_columns(connection, "coursera_orders")


def _add_trial_columns(connection: sqlite3.Connection, table: str) -> None:
    """Preserve legacy snapshots while making new trial facts durable."""

    columns = {row[1] for row in connection.execute(f"PRAGMA table_info({table})")}
    additions = {
        "trial_days": "INTEGER NOT NULL DEFAULT 0",
        "renewal_minor": "INTEGER NOT NULL DEFAULT 0",
        "renewal_currency": "TEXT NOT NULL DEFAULT 'USD'",
        "renewal_interval": "TEXT NOT NULL DEFAULT 'none'",
    }
    for name, definition in additions.items():
        if name not in columns:
            connection.execute(f"ALTER TABLE {table} ADD COLUMN {name} {definition}")


def plan() -> dict[str, Any]:
    """Return a copy of the one current server-owned plan."""

    return dict(TRIAL_PLAN)


def _persisted_plan_facts() -> dict[str, Any]:
    """Return the server-owned trial fields persisted on drafts and orders."""

    return {
        key: value
        for key, value in TRIAL_PLAN.items()
        if key != "pricing_evidence"
    }


def _draft_dict(row: sqlite3.Row) -> dict[str, Any]:
    return dict(row)


def create_draft(
    owner: str,
    *,
    course_id: str,
    plan_id: str,
) -> dict[str, Any]:
    """Create one owner-bound draft and generated sandbox payment flow."""

    if course_id != COURSE_ID or plan_id != PLAN_ID:
        raise ValueError("plan is unavailable")

    from backend import learning_db

    backend, _auth = learning_db.services()
    draft_id = f"checkout_{secrets.token_urlsafe(18)}"
    with learning_db.connection(transaction=True) as opened:
        owner_exists = opened.execute(
            "SELECT 1 FROM coursera_profiles WHERE subject_id=?", (owner,)
        ).fetchone()
        if owner_exists is None:
            raise LookupError("Learner not found")
        flow = backend.payments.create_intent(
            owner=owner,
            amount_minor=int(TRIAL_PLAN["total_minor"]),
            currency=str(TRIAL_PLAN["currency"]),
            fingerprint=PLAN_FINGERPRINT,
            idempotency_key=f"draft-create:{draft_id}",
            adapter="local-sandbox",
            connection=opened,
        )
        opened.execute(
            """INSERT INTO coursera_checkout_drafts(
                draft_id,owner_subject_id,course_id,plan_id,plan_label,
                subtotal_minor,tax_minor,total_minor,currency,fingerprint,
                trial_days,renewal_minor,renewal_currency,renewal_interval,
                payment_flow_id,status,created_at,updated_at)
                VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'OPEN',?,?)""",
            (
                draft_id,
                owner,
                TRIAL_PLAN["course_id"],
                TRIAL_PLAN["plan_id"],
                TRIAL_PLAN["plan_label"],
                TRIAL_PLAN["subtotal_minor"],
                TRIAL_PLAN["tax_minor"],
                TRIAL_PLAN["total_minor"],
                TRIAL_PLAN["currency"],
                PLAN_FINGERPRINT,
                TRIAL_PLAN["trial_days"],
                TRIAL_PLAN["renewal_minor"],
                TRIAL_PLAN["renewal_currency"],
                TRIAL_PLAN["renewal_interval"],
                flow["flow_id"],
                FROZEN_TIME,
                FROZEN_TIME,
            ),
        )
        row = opened.execute(
            "SELECT * FROM coursera_checkout_drafts WHERE draft_id=?",
            (draft_id,),
        ).fetchone()
        if row is None:  # pragma: no cover - insert invariant
            raise RuntimeError("checkout draft insert returned no row")
        return _draft_dict(row)


def get_draft(owner: str, draft_id: str) -> dict[str, Any]:
    """Read one checkout draft without revealing foreign records."""

    from backend import learning_db

    with learning_db.connection() as opened:
        row = opened.execute(
            """SELECT * FROM coursera_checkout_drafts
                WHERE draft_id=? AND owner_subject_id=?""",
            (draft_id, owner),
        ).fetchone()
    if row is None:
        raise LookupError("Checkout not found")
    return _draft_dict(row)


def _order_dict(row: sqlite3.Row) -> dict[str, Any]:
    return dict(row)


def _payment_attempt_dict(
    row: sqlite3.Row, *, replay_approved: bool = False
) -> dict[str, Any]:
    return {
        "attempt_id": row["attempt_id"],
        "site_id": row["site_id"],
        "flow_id": row["flow_id"],
        "owner": row["owner"],
        "scenario_id": row["scenario_id"],
        "amount_minor": int(row["amount_minor"]),
        "currency": row["currency"],
        "fingerprint": row["fingerprint"],
        "status": "APPROVED" if replay_approved else row["status"],
        "is_simulation": bool(row["is_simulation"]),
    }


def list_orders(owner: str) -> list[dict[str, Any]]:
    """List durable order snapshots for one owner."""

    from backend import learning_db

    with learning_db.connection() as opened:
        return [
            _order_dict(row)
            for row in opened.execute(
                """SELECT * FROM coursera_orders WHERE owner_subject_id=?
                    ORDER BY created_at DESC,order_id DESC""",
                (owner,),
            )
        ]


def get_order(owner: str, order_id: str) -> dict[str, Any]:
    """Read one order snapshot without revealing foreign records."""

    from backend import learning_db

    with learning_db.connection() as opened:
        row = opened.execute(
            """SELECT * FROM coursera_orders
                WHERE order_id=? AND owner_subject_id=?""",
            (order_id, owner),
        ).fetchone()
    if row is None:
        raise LookupError("Order not found")
    return _order_dict(row)


def get_order_for_enrollment(owner: str, enrollment_id: int) -> dict[str, Any]:
    """Resolve the current paid order for an owner-scoped enrollment."""

    from backend import learning_db

    with learning_db.connection() as opened:
        row = opened.execute(
            """SELECT * FROM coursera_orders
                WHERE enrollment_id=? AND owner_subject_id=?
                ORDER BY CASE status WHEN 'PAID' THEN 0 ELSE 1 END,rowid DESC
                LIMIT 1""",
            (enrollment_id, owner),
        ).fetchone()
    if row is None:
        raise LookupError("Order not found")
    return _order_dict(row)


def attempt(
    owner: str,
    draft_id: str,
    *,
    scenario_id: str,
    idempotency_key: str,
) -> dict[str, Any]:
    """Attempt sandbox payment and atomically materialize approved business state."""

    from backend import learning_db

    backend, _auth = learning_db.services()
    with learning_db.connection(transaction=True) as opened:
        draft = opened.execute(
            """SELECT * FROM coursera_checkout_drafts
                WHERE draft_id=? AND owner_subject_id=?""",
            (draft_id, owner),
        ).fetchone()
        if draft is None:
            raise LookupError("Checkout not found")
        current_facts = _persisted_plan_facts()
        if any(draft[key] != value for key, value in current_facts.items()):
            raise PaymentRejected("checkout facts are stale")
        if draft["status"] == "COMPLETED":
            order = opened.execute(
                """SELECT * FROM coursera_orders
                    WHERE draft_id=? AND owner_subject_id=? AND payment_flow_id=?""",
                (draft_id, owner, draft["payment_flow_id"]),
            ).fetchone()
            prior_attempt = opened.execute(
                """SELECT * FROM websitebench_payment_attempts
                    WHERE flow_id=? AND owner=? AND scenario_id=?
                    AND idempotency_key=? AND status='CONSUMED'""",
                (
                    draft["payment_flow_id"],
                    owner,
                    scenario_id,
                    idempotency_key,
                ),
            ).fetchone()
            order_facts = _persisted_plan_facts()
            if (
                order is not None
                and prior_attempt is not None
                and all(order[key] == value for key, value in order_facts.items())
            ):
                return {
                    "attempt": _payment_attempt_dict(
                        prior_attempt, replay_approved=True
                    ),
                    "order": _order_dict(order),
                    "outcome": "approved",
                }
            raise PaymentRejected("checkout is no longer open")
        if draft["status"] != "OPEN":
            raise PaymentRejected("checkout is no longer open")

        existing_paid_order = opened.execute(
            """SELECT order_id FROM coursera_orders
                WHERE owner_subject_id=? AND course_id=? AND status='PAID'""",
            (owner, COURSE_ID),
        ).fetchone()
        if existing_paid_order is not None:
            raise PaymentConflict("active paid order already exists")

        payment_attempt = backend.payments.attempt(
            flow_id=str(draft["payment_flow_id"]),
            owner=owner,
            amount_minor=int(TRIAL_PLAN["total_minor"]),
            currency=str(TRIAL_PLAN["currency"]),
            fingerprint=PLAN_FINGERPRINT,
            scenario_id=scenario_id,
            idempotency_key=idempotency_key,
            connection=opened,
        )
        outcome = {
            "APPROVED": "approved",
            "DECLINED": "declined",
            "RETRYABLE": "retryable",
        }[str(payment_attempt["status"])]
        if outcome != "approved":
            return {
                "attempt": payment_attempt,
                "order": None,
                "outcome": outcome,
            }

        backend.payments.consume_approval(
            opened,
            flow_id=str(draft["payment_flow_id"]),
            owner=owner,
            amount_minor=int(TRIAL_PLAN["total_minor"]),
            currency=str(TRIAL_PLAN["currency"]),
            fingerprint=PLAN_FINGERPRINT,
        )
        enrollment = opened.execute(
            """INSERT INTO coursera_enrollments(
                owner_subject_id,course_id,track,status,created_at,canceled_at)
                VALUES (?,?,'paid','active',?,NULL)
                ON CONFLICT(owner_subject_id,course_id) DO UPDATE SET
                track='paid',status='active'
                RETURNING enrollment_id""",
            (owner, COURSE_ID, FROZEN_TIME),
        ).fetchone()
        if enrollment is None:  # pragma: no cover - SQLite RETURNING invariant
            raise RuntimeError("paid enrollment upsert returned no row")

        order_id = f"order_{secrets.token_urlsafe(18)}"
        opened.execute(
            """INSERT INTO coursera_orders(
                order_id,owner_subject_id,draft_id,payment_flow_id,enrollment_id,
                course_id,plan_id,plan_label,subtotal_minor,tax_minor,total_minor,
                currency,fingerprint,trial_days,renewal_minor,renewal_currency,
                renewal_interval,status,created_at,canceled_at)
                VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'PAID',?,NULL)""",
            (
                order_id,
                owner,
                draft_id,
                draft["payment_flow_id"],
                int(enrollment["enrollment_id"]),
                TRIAL_PLAN["course_id"],
                TRIAL_PLAN["plan_id"],
                TRIAL_PLAN["plan_label"],
                TRIAL_PLAN["subtotal_minor"],
                TRIAL_PLAN["tax_minor"],
                TRIAL_PLAN["total_minor"],
                TRIAL_PLAN["currency"],
                PLAN_FINGERPRINT,
                TRIAL_PLAN["trial_days"],
                TRIAL_PLAN["renewal_minor"],
                TRIAL_PLAN["renewal_currency"],
                TRIAL_PLAN["renewal_interval"],
                FROZEN_TIME,
            ),
        )
        changed = opened.execute(
            """UPDATE coursera_checkout_drafts
                SET status='COMPLETED',updated_at=?
                WHERE draft_id=? AND owner_subject_id=? AND status='OPEN'""",
            (FROZEN_TIME, draft_id, owner),
        ).rowcount
        if changed != 1:  # pragma: no cover - write transaction invariant
            raise RuntimeError("checkout draft was concurrently completed")
        order = opened.execute(
            "SELECT * FROM coursera_orders WHERE order_id=?", (order_id,)
        ).fetchone()
        if order is None:  # pragma: no cover - insert invariant
            raise RuntimeError("order insert returned no row")
        return {
            "attempt": payment_attempt,
            "order": _order_dict(order),
            "outcome": outcome,
        }


def cancel_order(owner: str, order_id: str) -> dict[str, Any]:
    """Cancel one paid order and its enrollment in the same transaction."""

    from backend import learning_db

    with learning_db.connection(transaction=True) as opened:
        order = opened.execute(
            """SELECT * FROM coursera_orders
                WHERE order_id=? AND owner_subject_id=?""",
            (order_id, owner),
        ).fetchone()
        if order is None:
            raise LookupError("Order not found")
        if order["status"] == "PAID":
            opened.execute(
                """UPDATE coursera_orders SET status='CANCELED',canceled_at=?
                    WHERE order_id=? AND owner_subject_id=? AND status='PAID'""",
                (FROZEN_TIME, order_id, owner),
            )
            changed = opened.execute(
                """UPDATE coursera_enrollments
                    SET status='canceled',canceled_at=?
                    WHERE enrollment_id=? AND owner_subject_id=?""",
                (FROZEN_TIME, int(order["enrollment_id"]), owner),
            ).rowcount
            if changed != 1:
                raise RuntimeError("paid enrollment was not found")
            order = opened.execute(
                "SELECT * FROM coursera_orders WHERE order_id=?", (order_id,)
            ).fetchone()
        return _order_dict(order)


def reset(connection: sqlite3.Connection) -> None:
    """Clear mutable checkout state before learning and runtime reset."""

    connection.execute("DELETE FROM coursera_orders")
    connection.execute("DELETE FROM coursera_checkout_drafts")


def snapshot_queries() -> dict[str, str]:
    """Expose deterministic checkout rows to the site reset snapshot."""

    return {
        "checkout_drafts": ("SELECT * FROM coursera_checkout_drafts ORDER BY draft_id"),
        "orders": "SELECT * FROM coursera_orders ORDER BY order_id",
    }