File size: 49,508 Bytes
3ec0f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
#!/usr/bin/env python3
"""Standalone ChatGPT checkout short-link extractor.

Author: Simon

Default flow:

* checkout billing: PH / PHP
* checkout proxy country: US
* promotion update proxy country: TR
* payment locale: English
* proxy provider: Cliproxy-compatible username routing
* output: https://chatgpt.com/checkout/<processor_entity>/<oaics_id>

The script creates the checkout without a promotion, applies the promotion to
the same checkout through ``checkout/update``, and verifies the payable amount.
An explicit non-zero amount is rejected. If neither the update response nor the
checkout page exposes an amount, the result is returned as ``pending`` so a
payment client can verify it before payment.

Only ``curl_cffi`` is required::

    python -m pip install "curl_cffi>=0.15.0"

Recommended usage keeps credentials out of the process list::

    export CLIPROXY_HOST=proxy.example:8080
    export CLIPROXY_USERNAME=your-user
    export CLIPROXY_PASSWORD=your-password
    python checkout_link_extractor_standalone.py --credential-file session.json --pretty

Use ``--credential-file -`` to read a token or session JSON from stdin.
"""

from __future__ import annotations

import argparse
import getpass
import json
import os
import random
import re
import sys
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from dataclasses import asdict, dataclass
from html import unescape as html_unescape
from pathlib import Path
from typing import Any, Callable
from urllib.parse import quote, unquote, urlsplit, urlunsplit

from curl_ca_bundle import curl_session_kwargs

try:
    from curl_cffi.requests import Session as CurlSession
except ImportError:  # pragma: no cover - exercised by the CLI error path
    CurlSession = None  # type: ignore[assignment]


VERSION = "1.0.0"
CHECKOUT_URL = "https://chatgpt.com/backend-api/payments/checkout"
UPDATE_PATH = "/backend-api/payments/checkout/update"
TRACE_URL = "https://www.cloudflare.com/cdn-cgi/trace"
DEFAULT_USER_AGENT = (
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
    "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
)
DEFAULT_CLIENT_VERSION = "prod-db390ebea64862bf1899c420a4c736e0cf639747"
DEFAULT_CLIENT_BUILD = "7904904"
PAYMENT_COOKIE_NAMES = frozenset(
    {
        "oai-did",
        "oai-hlib",
        "oai-sc",
        "oaicom-stable-id",
        "_account",
        "_account_is_fedramp",
        "__Secure-oai-is",
        "__Secure-next-auth.session-token",
        "__cf_bm",
        "__cflb",
        "_cfuvid",
        "__oailb",
        "cf_clearance",
    }
)
RETRYABLE_STATUSES = frozenset({403, 429, 500, 502, 503, 504})
MAX_PAYLOAD_DEPTH = 128
MAX_PAYLOAD_NODES = 10_000


class ExtractorError(RuntimeError):
    """Base error safe to expose in the CLI JSON response."""


class UpstreamError(ExtractorError):
    def __init__(
        self,
        status_code: int,
        message: str,
        *,
        retryable: bool = False,
        cloudflare: bool = False,
    ) -> None:
        super().__init__(message)
        self.status_code = status_code
        self.retryable = retryable
        self.cloudflare = cloudflare


class InvalidPromotionError(ExtractorError):
    pass


class NonZeroAmountError(ExtractorError):
    pass


@dataclass(frozen=True)
class Credentials:
    access_token: str
    session_cookie: str = ""


@dataclass(frozen=True)
class ExtractorConfig:
    billing_country: str = "PH"
    currency: str = "PHP"
    payment_locale: str = "en"
    checkout_proxy_country: str = "US"
    update_proxy_country: str = "TR"
    cliproxy_host: str = "127.0.0.1:19010"
    cliproxy_username: str = ""
    cliproxy_password: str = ""
    checkout_proxy: str = ""
    update_proxy: str = ""
    plan_name: str = "chatgptplusplan"
    promo_campaign_id: str = "plus-1-month-free"
    checkout_ui_mode: str = "custom"
    timeout: float = 45.0
    checkout_attempts: int = 10
    update_attempts: int = 15
    full_attempts: int = 3
    cf_same_identity_attempts: int = 5
    cf_retry_delay: float = 3.0
    verify_proxy_country: bool = True
    user_agent: str = DEFAULT_USER_AGENT

    def validate(self) -> None:
        if not re.fullmatch(r"[A-Z]{2}", self.billing_country):
            raise ExtractorError("billing country must be a two-letter code")
        if not re.fullmatch(r"[A-Z]{3}", self.currency):
            raise ExtractorError("currency must be a three-letter code")
        for field_name, value in (
            ("checkout proxy country", self.checkout_proxy_country),
            ("update proxy country", self.update_proxy_country),
        ):
            if not re.fullmatch(r"[A-Z]{2}", value):
                raise ExtractorError(f"{field_name} must be a two-letter code")
        for field_name, value in (
            ("checkout attempts", self.checkout_attempts),
            ("update attempts", self.update_attempts),
            ("full attempts", self.full_attempts),
            ("CF same-identity attempts", self.cf_same_identity_attempts),
        ):
            if value < 1:
                raise ExtractorError(f"{field_name} must be at least 1")


@dataclass(frozen=True)
class CheckoutResult:
    ok: bool
    long_url: str
    cs_id: str
    processor_entity: str
    billing_country: str
    currency: str
    payment_locale: str
    amount_verification: str
    amount_minor: int | None
    amount_currency: str


def find_value(payload: Any, keys: tuple[str, ...]) -> str:
    stack: list[tuple[Any, int]] = [(payload, 0)]
    visited: set[int] = set()
    while stack and len(visited) < MAX_PAYLOAD_NODES:
        current, depth = stack.pop()
        if not isinstance(current, (dict, list)):
            continue
        identity = id(current)
        if identity in visited:
            continue
        visited.add(identity)
        if isinstance(current, dict):
            for key in keys:
                value = current.get(key)
                if isinstance(value, str) and value.strip():
                    return value.strip()
            children = list(current.values())
        else:
            children = current
        if depth < MAX_PAYLOAD_DEPTH:
            stack.extend((value, depth + 1) for value in reversed(children))
    return ""


def parse_credentials(raw: str, explicit_session_cookie: str = "") -> Credentials:
    raw = str(raw or "").strip()
    if not raw:
        raise ExtractorError("access token or session JSON is required")
    payload: Any = None
    if raw.startswith(("{", "[")):
        try:
            payload = json.loads(raw)
        except json.JSONDecodeError as exc:
            raise ExtractorError(f"credential JSON is invalid: {exc.msg}") from exc
        access_token = find_value(payload, ("accessToken", "access_token", "token"))
    else:
        access_token = raw
    if not access_token:
        raise ExtractorError("credential payload does not contain an access token")
    session_cookie = str(explicit_session_cookie or "").strip()
    if not session_cookie and payload is not None:
        session_cookie = find_value(
            payload,
            (
                "chatgpt_session_cookie",
                "chatgptSessionCookie",
                "session_cookie",
                "sessionCookie",
                "sessionToken",
                "__Secure-next-auth.session-token",
            ),
        )
    return Credentials(access_token=access_token, session_cookie=session_cookie)


def cookie_header_value(cookie_header: str, name: str) -> str:
    for item in str(cookie_header or "").split(";"):
        key, separator, value = item.strip().partition("=")
        if separator and key == name:
            return value.strip()
    return ""


def filter_payment_cookie_header(cookie_header: str) -> str:
    values: dict[str, str] = {}
    for item in str(cookie_header or "").split(";"):
        name, separator, value = item.strip().partition("=")
        if separator and name in PAYMENT_COOKIE_NAMES:
            values[name] = value
    return "; ".join(f"{name}={value}" for name, value in values.items())


def replace_cookie_value(cookie_header: str, name: str, value: str) -> str:
    values: list[str] = []
    replaced = False
    for item in str(cookie_header or "").split(";"):
        key, separator, _ = item.strip().partition("=")
        if not separator:
            continue
        if key == name:
            if not replaced:
                values.append(f"{name}={value}")
                replaced = True
        else:
            values.append(item.strip())
    if not replaced:
        values.insert(0, f"{name}={value}")
    return "; ".join(values)


def initial_cookie_header(session_cookie: str, device_id: str) -> str:
    raw_cookie = str(session_cookie or "").strip()
    if raw_cookie and "=" not in raw_cookie.split(";", 1)[0]:
        return f"oai-did={device_id}; __Secure-next-auth.session-token={raw_cookie}"
    filtered = filter_payment_cookie_header(raw_cookie)
    if not filtered:
        return f"oai-did={device_id}"
    return replace_cookie_value(filtered, "oai-did", device_id)


def merged_cookie_header(cookie_header: str, cookies: Any) -> str:
    values: dict[str, str] = {}
    for item in str(cookie_header or "").split(";"):
        name, separator, value = item.strip().partition("=")
        if separator and name:
            values[name] = value
    try:
        for name, value in cookies.items():
            if name:
                values[str(name)] = str(value)
    except Exception:
        pass
    return "; ".join(f"{name}={value}" for name, value in values.items())


def refresh_cookie_header(session: Any, fallback: str = "") -> None:
    headers = getattr(session, "headers", None)
    if headers is None:
        return
    cookie_header = filter_payment_cookie_header(
        merged_cookie_header(
            fallback or str(headers.get("Cookie") or ""),
            getattr(session, "cookies", None),
        )
    )
    if cookie_header:
        headers["Cookie"] = cookie_header


def sync_cookies(target: Any, source: Any) -> None:
    if target is source:
        refresh_cookie_header(target)
        return
    try:
        target.cookies.update(source.cookies)
    except Exception:
        pass
    refresh_cookie_header(target, str(getattr(source, "headers", {}).get("Cookie") or ""))


def normalize_proxy_url(proxy: str) -> str:
    proxy = str(proxy or "").strip()
    if not proxy:
        return ""
    if "://" in proxy:
        return proxy
    parts = proxy.split(":", 3)
    if len(parts) == 4 and parts[1].isdigit() and "@" not in proxy:
        host, port, username, password = parts
        return (
            f"http://{quote(username, safe='-._~')}:"
            f"{quote(password, safe='-._~')}@{host}:{port}"
        )
    return f"http://{proxy}"


def new_proxy_session_id() -> str:
    return str(random.randint(10_000_000, 99_999_999))


def cliproxy_proxy_url(
    host: str,
    username: str,
    password: str,
    country: str,
    session_id: str = "",
) -> str:
    host = re.sub(r"^https?://", "", str(host or "").strip()).strip("/")
    username = str(username or "").strip()
    password = str(password or "").strip()
    country = str(country or "").strip().upper()
    if not host or not username or not password:
        raise ExtractorError(
            "Cliproxy host, username and password are required unless both stage proxies are supplied"
        )
    if not re.fullmatch(r"[A-Z]{2}", country):
        raise ExtractorError("Cliproxy country must be a two-letter code")
    username_base = re.split(r"-region-[A-Za-z]{2}", username, maxsplit=1)[0]
    routed_username = (
        f"{username_base}-region-{country}-sid-{session_id or new_proxy_session_id()}"
    )
    return (
        f"http://{quote(routed_username, safe='-._~')}:"
        f"{quote(password, safe='-._~')}@{host}"
    )


def rotate_proxy_session(proxy: str, country: str) -> str:
    proxy = normalize_proxy_url(proxy)
    if not proxy:
        return proxy
    parsed = urlsplit(proxy)
    username = unquote(parsed.username or "")
    password = unquote(parsed.password or "")
    hostname = parsed.hostname or ""
    if not username or not password or not hostname:
        return proxy
    rotated_username = re.sub(
        r"region-[A-Za-z]{2}", f"region-{country.upper()}", username
    )
    if re.search(r"sid-[A-Za-z0-9]+", rotated_username):
        rotated_username = re.sub(
            r"sid-[A-Za-z0-9]+", f"sid-{new_proxy_session_id()}", rotated_username
        )
    elif re.search(r"region-[A-Za-z]{2}", rotated_username):
        rotated_username += f"-sid-{new_proxy_session_id()}"
    else:
        return proxy
    host = f"[{hostname}]" if ":" in hostname and not hostname.startswith("[") else hostname
    if parsed.port:
        host = f"{host}:{parsed.port}"
    netloc = (
        f"{quote(rotated_username, safe='-._~')}:"
        f"{quote(password, safe='-._~')}@{host}"
    )
    return urlunsplit((parsed.scheme or "http", netloc, parsed.path, parsed.query, parsed.fragment))


def set_proxy(session: Any, proxy: str) -> None:
    proxy = normalize_proxy_url(proxy)
    session.proxies = {"http": proxy, "https": proxy} if proxy else {}


def safe_close(session: Any) -> None:
    if session is None:
        return
    try:
        session.close()
    except Exception:
        pass


def money_minor_units(value: Any) -> int | None:
    stack: list[tuple[Any, int]] = [(value, 0)]
    visited: set[int] = set()
    while stack and len(visited) < MAX_PAYLOAD_NODES:
        current, depth = stack.pop()
        if isinstance(current, bool):
            continue
        if isinstance(current, int):
            return current
        if isinstance(current, str) and re.fullmatch(r"-?\d+", current.strip()):
            return int(current.strip())
        if not isinstance(current, dict):
            continue
        identity = id(current)
        if identity in visited:
            continue
        visited.add(identity)
        if depth < MAX_PAYLOAD_DEPTH:
            children = [
                current[key]
                for key in ("minorUnitsAmount", "minor_units_amount", "amount")
                if current.get(key) is not None
            ]
            stack.extend((child, depth + 1) for child in reversed(children))
    return None


def nested_value(payload: Any, path: tuple[str, ...]) -> Any:
    current = payload
    for key in path:
        if not isinstance(current, dict):
            return None
        current = current.get(key)
    return current


def checkout_amount_minor(data: Any) -> int | None:
    wrapper_keys = (
        "checkout_session",
        "checkoutSession",
        "session",
        "checkout",
        "data",
        "result",
        "payload",
        "response",
        "checkout_state",
        "checkoutState",
        "checkout_snapshot",
        "checkoutSnapshot",
    )
    amount_paths = (
        ("checkout_amount_minor",),
        ("total_summary", "due"),
        ("totalSummary", "due"),
        ("invoice", "amount_due"),
        ("invoice", "amountDue"),
        ("amount_due",),
        ("amountDue",),
        ("amount_total",),
        ("amountTotal",),
        ("total", "total"),
        ("total", "due"),
        ("total", "taxInclusive"),
        ("total", "taxInclusiveAmount"),
    )
    visited: set[int] = set()

    stack: list[tuple[Any, int]] = [(data, 0)]
    while stack and len(visited) < MAX_PAYLOAD_NODES:
        payload, depth = stack.pop()
        if not isinstance(payload, dict) or id(payload) in visited:
            continue
        visited.add(id(payload))
        for path in amount_paths:
            amount = money_minor_units(nested_value(payload, path))
            if amount is not None:
                return amount
        line_items = payload.get("lineItems") or payload.get("line_items")
        if isinstance(line_items, list):
            total = 0
            found = False
            for item in line_items:
                if not isinstance(item, dict):
                    continue
                for key in ("total", "subtotal", "unitAmount", "unit_amount"):
                    amount = money_minor_units(item.get(key))
                    if amount is not None:
                        total += amount
                        found = True
                        break
            if found:
                return total
        if depth < MAX_PAYLOAD_DEPTH:
            children = [payload.get(key) for key in wrapper_keys]
            stack.extend((child, depth + 1) for child in reversed(children))
    return None


def checkout_currency(data: Any) -> str:
    visited: set[int] = set()
    wrapper_keys = (
        "checkout_state",
        "checkoutState",
        "checkout_snapshot",
        "checkoutSnapshot",
        "checkout_session",
        "checkoutSession",
        "session",
        "checkout",
        "data",
        "result",
        "payload",
        "response",
        "total",
        "total_summary",
        "totalSummary",
    )
    stack: list[tuple[Any, int]] = [(data, 0)]
    while stack and len(visited) < MAX_PAYLOAD_NODES:
        payload, depth = stack.pop()
        if not isinstance(payload, dict) or id(payload) in visited:
            continue
        visited.add(id(payload))
        for key in ("currency", "currency_code", "currencyCode"):
            value = payload.get(key)
            if isinstance(value, str) and re.fullmatch(r"[A-Za-z]{3}", value.strip()):
                return value.strip().upper()
        if depth < MAX_PAYLOAD_DEPTH:
            children = [payload.get(key) for key in wrapper_keys]
            stack.extend((child, depth + 1) for child in reversed(children))
    return ""


def decode_react_router_payload(payload: Any) -> Any:
    if not isinstance(payload, list):
        return None
    resolved: dict[int, Any] = {}

    def resolve(reference: Any, depth: int = 0) -> Any:
        if depth > MAX_PAYLOAD_DEPTH:
            return None
        if not isinstance(reference, int):
            return reference
        if reference < 0 or reference >= len(payload):
            return None
        if reference in resolved:
            return resolved[reference]
        value = payload[reference]
        if isinstance(value, dict):
            output: dict[str, Any] = {}
            resolved[reference] = output
            for encoded_key, encoded_value in value.items():
                key_text = str(encoded_key)
                if not key_text.startswith("_") or not key_text[1:].isdigit():
                    continue
                key = resolve(int(key_text[1:]), depth + 1)
                if key is not None:
                    output[str(key)] = resolve(encoded_value, depth + 1)
            return output
        if isinstance(value, list):
            output_list: list[Any] = []
            resolved[reference] = output_list
            output_list.extend(resolve(item, depth + 1) for item in value)
            return output_list
        resolved[reference] = value
        return value

    return resolve(0)


def checkout_state_from_html(html: str) -> dict[str, Any]:
    def find_state(payload: Any) -> dict[str, Any]:
        stack: list[tuple[Any, int]] = [(payload, 0)]
        visited: set[int] = set()
        while stack and len(visited) < MAX_PAYLOAD_NODES:
            current, depth = stack.pop()
            if not isinstance(current, (dict, list)):
                continue
            identity = id(current)
            if identity in visited:
                continue
            visited.add(identity)
            if isinstance(current, dict):
                for key in ("checkout_state", "checkoutState"):
                    state = current.get(key)
                    if isinstance(state, dict):
                        return state
                if checkout_amount_minor(current) is not None and any(
                    key in current
                    for key in ("total", "total_summary", "totalSummary", "lineItems", "line_items")
                ):
                    return current
                children = list(current.values())
            else:
                children = current
            if depth < MAX_PAYLOAD_DEPTH:
                stack.extend((value, depth + 1) for value in reversed(children))
        return {}

    def state_from_serialized(serialized: str) -> dict[str, Any]:
        try:
            decoded: Any = json.loads(serialized)
        except (json.JSONDecodeError, RecursionError, TypeError, ValueError):
            return {}
        if isinstance(decoded, list):
            state = find_state(decode_react_router_payload(decoded))
            if state:
                return state
        return find_state(decoded)

    enqueue_pattern = re.compile(
        r'window\.__reactRouterContext\.streamController\.enqueue\(("(?:\\.|[^"\\])*")\)',
        re.DOTALL,
    )
    chunks: list[str] = []
    for match in enqueue_pattern.finditer(str(html or "")):
        try:
            raw_payload = json.loads(match.group(1))
        except (json.JSONDecodeError, TypeError, ValueError):
            continue
        if not isinstance(raw_payload, str):
            continue
        chunks.append(raw_payload)
        state = state_from_serialized(raw_payload)
        if state:
            return state
    if len(chunks) > 1:
        state = state_from_serialized("".join(chunks))
        if state:
            return state
    application_json_pattern = re.compile(
        r"<script\b[^>]*\btype=(['\"])application/json\1[^>]*>(.*?)</script>",
        re.IGNORECASE | re.DOTALL,
    )
    for match in application_json_pattern.finditer(str(html or "")):
        state = state_from_serialized(html_unescape(match.group(2)).strip())
        if state:
            return state
    return {}


def payload_has_invalid_promotion(payload: Any) -> bool:
    stack: list[tuple[Any, int]] = [(payload, 0)]
    visited: set[int] = set()
    while stack and len(visited) < MAX_PAYLOAD_NODES:
        current, depth = stack.pop()
        if isinstance(current, str) and current.strip().lower() == "invalid_promotion":
            return True
        if not isinstance(current, (dict, list)):
            continue
        identity = id(current)
        if identity in visited:
            continue
        visited.add(identity)
        children = list(current.values()) if isinstance(current, dict) else current
        if depth < MAX_PAYLOAD_DEPTH:
            stack.extend((value, depth + 1) for value in reversed(children))
    return False


def extract_processor_entity(payload: Any) -> str:
    wrapper_keys = ("checkout_session", "checkoutSession", "session", "checkout", "data")
    stack: list[tuple[Any, int]] = [(payload, 0)]
    visited: set[int] = set()
    while stack and len(visited) < MAX_PAYLOAD_NODES:
        current, depth = stack.pop()
        if not isinstance(current, dict) or id(current) in visited:
            continue
        visited.add(id(current))
        direct = current.get("processor_entity") or current.get("processorEntity")
        if direct:
            return str(direct).strip()
        if depth < MAX_PAYLOAD_DEPTH:
            children = [current.get(key) for key in wrapper_keys]
            stack.extend((child, depth + 1) for child in reversed(children))
    return ""


def processor_entity_for_country(country: str, processor_entity: str = "") -> str:
    if str(processor_entity or "").strip():
        return str(processor_entity).strip()
    return "openai_llc" if str(country or "").upper() == "US" else "openai_ie"


def checkout_short_url(cs_id: str, country: str, processor_entity: str = "") -> str:
    entity = processor_entity_for_country(country, processor_entity)
    return f"https://chatgpt.com/checkout/{entity}/{cs_id}"


def is_retryable_network_error(exc: Exception) -> bool:
    retryable_names = {"ReadTimeout", "ConnectTimeout", "ConnectionError", "Timeout", "SSLError"}
    if any(item.__name__ in retryable_names for item in type(exc).mro()):
        return True
    text = str(exc).lower()
    return any(
        marker in text
        for marker in (
            "read timed out",
            "connect timed out",
            "connection aborted",
            "proxy connect aborted",
            "curl: (56)",
            "unexpected_eof",
            "eof occurred in violation of protocol",
            "max retries exceeded",
        )
    )


def response_error(response: Any, prefix: str) -> UpstreamError:
    status = int(getattr(response, "status_code", 0) or 0)
    headers = getattr(response, "headers", {}) or {}
    content_type = str(headers.get("content-type") or "")
    server = str(headers.get("server") or "")
    body = str(getattr(response, "text", "") or "")
    lowered = body[:4000].lower()
    is_html = "html" in content_type.lower() or body.lstrip().lower().startswith(("<!doctype", "<html"))
    cloudflare = status == 403 and (
        "challenge-platform" in lowered
        or "__cf$cv$params" in lowered
        or "cloudflare" in server.lower()
        or is_html
    )
    if is_html:
        title_match = re.search(r"<title[^>]*>(.*?)</title>", body, re.IGNORECASE | re.DOTALL)
        detail = re.sub(r"\s+", " ", title_match.group(1)).strip() if title_match else "HTML verification page"
    else:
        detail = re.sub(r"\s+", " ", body).strip()[:300] or "empty response"
    return UpstreamError(
        status,
        f"{prefix}: HTTP {status}; {detail}",
        retryable=status in RETRYABLE_STATUSES,
        cloudflare=cloudflare,
    )


class CheckoutExtractor:
    def __init__(
        self,
        credentials: Credentials,
        config: ExtractorConfig | None = None,
        *,
        session_factory: Callable[[], Any] | None = None,
        sleeper: Callable[[float], None] = time.sleep,
        logger: Callable[[str], None] | None = None,
    ) -> None:
        self.credentials = credentials
        self.config = config or ExtractorConfig()
        self.config.validate()
        if session_factory is None:
            if CurlSession is None:
                raise ExtractorError(
                    'curl_cffi is required; install it with: python -m pip install "curl_cffi>=0.15.0"'
                )
            session_factory = lambda: CurlSession(
                impersonate="chrome136", **curl_session_kwargs()
            )
        self.session_factory = session_factory
        self.sleep = sleeper
        self.log = logger or (lambda message: print(message, file=sys.stderr, flush=True))
        self.device_id = cookie_header_value(credentials.session_cookie, "oai-did") or str(uuid.uuid4())
        self.chatgpt_session_id = str(uuid.uuid4())
        self.last_session_cookies: dict[str, str] = {}
        self.last_checkout_proxy = ""

    def _new_identity_session(self, proxy: str) -> Any:
        session = self.session_factory()
        if hasattr(session, "trust_env"):
            session.trust_env = False
        session.headers.update(
            {
                "User-Agent": self.config.user_agent,
                "Accept": "*/*",
                "Accept-Language": "en-US,en;q=0.9",
                "Authorization": f"Bearer {self.credentials.access_token}",
                "Origin": "https://chatgpt.com",
                "Referer": "https://chatgpt.com/",
                "Content-Type": "application/json",
                "oai-device-id": self.device_id,
                "oai-language": "en-US",
                "oai-session-id": self.chatgpt_session_id,
                "oai-client-version": DEFAULT_CLIENT_VERSION,
                "oai-client-build-number": DEFAULT_CLIENT_BUILD,
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-origin",
                "sec-ch-ua": '"Google Chrome";v="136", "Not.A/Brand";v="8", "Chromium";v="136"',
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": '"Windows"',
                "Cookie": initial_cookie_header(self.credentials.session_cookie, self.device_id),
            }
        )
        set_proxy(session, proxy)
        return session

    def _clone_session(self, source: Any, proxy: str) -> Any:
        session = self.session_factory()
        if hasattr(session, "trust_env"):
            session.trust_env = False
        session.headers.update(dict(getattr(source, "headers", {})))
        try:
            session.cookies.update(source.cookies)
        except Exception:
            pass
        refresh_cookie_header(session, str(getattr(source, "headers", {}).get("Cookie") or ""))
        set_proxy(session, proxy)
        return session

    def _stage_proxies(self) -> tuple[str, str]:
        checkout_proxy = normalize_proxy_url(self.config.checkout_proxy)
        update_proxy = normalize_proxy_url(self.config.update_proxy)
        if not checkout_proxy:
            checkout_proxy = cliproxy_proxy_url(
                self.config.cliproxy_host,
                self.config.cliproxy_username,
                self.config.cliproxy_password,
                self.config.checkout_proxy_country,
            )
        if not update_proxy:
            update_proxy = cliproxy_proxy_url(
                self.config.cliproxy_host,
                self.config.cliproxy_username,
                self.config.cliproxy_password,
                self.config.update_proxy_country,
            )
        return checkout_proxy, update_proxy

    def _probe_country(self, proxy: str, expected_country: str) -> None:
        session = self.session_factory()
        try:
            set_proxy(session, proxy)
            response = session.get(TRACE_URL, timeout=min(self.config.timeout, 12.0))
            if int(getattr(response, "status_code", 0) or 0) >= 400:
                raise response_error(response, "proxy country probe")
            fields = dict(
                line.split("=", 1)
                for line in str(getattr(response, "text", "") or "").splitlines()
                if "=" in line
            )
            observed = str(fields.get("loc") or "").upper()
            if observed != expected_country:
                raise ExtractorError(
                    f"proxy country mismatch: expected {expected_country}, observed {observed or 'unknown'}"
                )
        finally:
            safe_close(session)

    def _preflight(self, checkout_proxy: str, update_proxy: str) -> None:
        if not self.config.verify_proxy_country:
            return
        with ThreadPoolExecutor(max_workers=2) as executor:
            checkout_future = executor.submit(
                self._probe_country, checkout_proxy, self.config.checkout_proxy_country
            )
            update_future = executor.submit(
                self._probe_country, update_proxy, self.config.update_proxy_country
            )
            checkout_future.result()
            update_future.result()
        self.log(
            f"Proxy check passed: checkout={self.config.checkout_proxy_country}, "
            f"update={self.config.update_proxy_country}."
        )

    def _create_checkout(self, session: Any) -> dict[str, Any]:
        refresh_cookie_header(session)
        body = {
            "entry_point": "all_plans_pricing_modal",
            "plan_name": self.config.plan_name,
            "billing_details": {
                "country": self.config.billing_country,
                "currency": self.config.currency,
            },
            "checkout_ui_mode": self.config.checkout_ui_mode,
        }
        response = session.post(
            CHECKOUT_URL,
            json=body,
            headers={
                "Referer": "https://chatgpt.com/",
                "x-openai-target-path": "/backend-api/payments/checkout",
                "x-openai-target-route": "/backend-api/payments/checkout",
            },
            timeout=self.config.timeout,
        )
        if int(getattr(response, "status_code", 0) or 0) >= 400:
            try:
                error_payload = response.json() or {}
            except Exception:
                error_payload = {}
            if str(error_payload.get("detail") or "") == "User is already paid":
                raise UpstreamError(400, "the account is already paid and cannot create a Plus checkout")
            raise response_error(response, "checkout create failed")
        try:
            payload = response.json() or {}
        except Exception as exc:
            raise UpstreamError(502, "checkout create returned invalid JSON", retryable=True) from exc
        cs_id = payload.get("checkout_session_id") or payload.get("session_id") or payload.get("id")
        cs_id = str(cs_id or "").strip()
        if not cs_id.startswith(("oaics_", "cs_")):
            raise UpstreamError(502, "checkout response did not contain a supported session id")
        return {
            "cs_id": cs_id,
            "processor_entity": extract_processor_entity(payload),
            "billing_country": self.config.billing_country,
            "currency": self.config.currency,
            "checkout_amount_minor": checkout_amount_minor(payload),
        }

    def _create_checkout_with_retry(self, checkout_proxy: str) -> tuple[Any, dict[str, Any]]:
        session = self._new_identity_session(checkout_proxy)
        current_proxy = checkout_proxy
        cloudflare_failures = 0
        exit_rotated = False
        last_error: Exception | None = None
        for attempt in range(1, self.config.checkout_attempts + 1):
            self.log(f"Checkout attempt {attempt}/{self.config.checkout_attempts}.")
            try:
                return session, self._create_checkout(session)
            except UpstreamError as exc:
                last_error = exc
                if not exc.retryable or attempt >= self.config.checkout_attempts:
                    safe_close(session)
                    raise
                if exc.cloudflare:
                    cloudflare_failures += 1
                    if cloudflare_failures >= self.config.cf_same_identity_attempts and not exit_rotated:
                        rotated = rotate_proxy_session(current_proxy, self.config.checkout_proxy_country)
                        if rotated != current_proxy:
                            current_proxy = rotated
                            set_proxy(session, current_proxy)
                            cloudflare_failures = 0
                            exit_rotated = True
                            self.log("Checkout CF retries exhausted; rotated the proxy exit once.")
                    if self.config.cf_retry_delay > 0:
                        self.sleep(self.config.cf_retry_delay)
            except Exception as exc:
                last_error = exc
                if not is_retryable_network_error(exc) or attempt >= self.config.checkout_attempts:
                    safe_close(session)
                    raise ExtractorError(f"checkout network error: {exc}") from exc
        safe_close(session)
        raise ExtractorError(f"checkout retries exhausted: {last_error}")

    def _update_promotion(
        self,
        checkout_session: Any,
        checkout: dict[str, Any],
        checkout_proxy: str,
        update_proxy: str,
    ) -> dict[str, Any]:
        processor_entity = processor_entity_for_country(
            checkout["billing_country"], checkout.get("processor_entity", "")
        )
        body = {
            "checkout_session_id": checkout["cs_id"],
            "processor_entity": processor_entity,
            "plan_name": self.config.plan_name,
            "price_interval": "month",
            "seat_quantity": 1,
            "promo_campaign": {
                "promo_campaign_id": self.config.promo_campaign_id,
                "is_coupon_from_query_param": False,
            },
        }
        headers = {
            "Referer": checkout_short_url(
                checkout["cs_id"], checkout["billing_country"], processor_entity
            ),
            "x-openai-target-path": UPDATE_PATH,
            "x-openai-target-route": UPDATE_PATH,
        }
        same_session = normalize_proxy_url(checkout_proxy) == normalize_proxy_url(update_proxy)
        session = checkout_session if same_session else self._clone_session(checkout_session, update_proxy)
        owns_session = not same_session
        current_proxy = update_proxy
        cloudflare_failures = 0
        exit_rotated = False
        try:
            for attempt in range(1, self.config.update_attempts + 1):
                self.log(f"Promotion update attempt {attempt}/{self.config.update_attempts}.")
                try:
                    refresh_cookie_header(session)
                    response = session.post(
                        f"https://chatgpt.com{UPDATE_PATH}",
                        json=body,
                        headers=headers,
                        timeout=self.config.timeout,
                    )
                except Exception as exc:
                    if not is_retryable_network_error(exc) or attempt >= self.config.update_attempts:
                        raise ExtractorError(f"promotion update network error: {exc}") from exc
                    current_proxy = rotate_proxy_session(
                        current_proxy, self.config.update_proxy_country
                    )
                    if owns_session:
                        safe_close(session)
                    session = self._clone_session(checkout_session, current_proxy)
                    owns_session = True
                    continue
                if int(getattr(response, "status_code", 0) or 0) >= 400:
                    error = response_error(response, "checkout/update failed")
                    if not error.retryable or attempt >= self.config.update_attempts:
                        raise error
                    if error.cloudflare:
                        cloudflare_failures += 1
                        if cloudflare_failures >= self.config.cf_same_identity_attempts and not exit_rotated:
                            rotated = rotate_proxy_session(
                                current_proxy, self.config.update_proxy_country
                            )
                            if rotated != current_proxy:
                                current_proxy = rotated
                                set_proxy(session, current_proxy)
                                cloudflare_failures = 0
                                exit_rotated = True
                                self.log("Update CF retries exhausted; rotated the proxy exit once.")
                        if self.config.cf_retry_delay > 0:
                            self.sleep(self.config.cf_retry_delay)
                    else:
                        current_proxy = rotate_proxy_session(
                            current_proxy, self.config.update_proxy_country
                        )
                        if owns_session:
                            safe_close(session)
                        session = self._clone_session(checkout_session, current_proxy)
                        owns_session = True
                    continue
                try:
                    payload = response.json() or {}
                except Exception as exc:
                    raise UpstreamError(502, "checkout/update returned invalid JSON") from exc
                if payload_has_invalid_promotion(payload):
                    raise InvalidPromotionError("checkout/update returned invalid_promotion")
                if payload.get("success") is False:
                    raise UpstreamError(502, "checkout/update explicitly rejected the promotion")
                sync_cookies(checkout_session, session)
                return payload
            raise ExtractorError("promotion update retries exhausted")
        finally:
            if owns_session:
                safe_close(session)

    def _page_amount(self, session: Any, checkout: dict[str, Any]) -> tuple[int | None, str]:
        refresh_cookie_header(session)
        response = session.get(
            checkout_short_url(
                checkout["cs_id"],
                checkout["billing_country"],
                checkout.get("processor_entity", ""),
            ),
            headers={"Referer": "https://chatgpt.com/"},
            timeout=self.config.timeout,
        )
        if int(getattr(response, "status_code", 0) or 0) >= 400:
            raise response_error(response, "checkout page amount query failed")
        state = checkout_state_from_html(str(getattr(response, "text", "") or ""))
        return checkout_amount_minor(state), checkout_currency(state) or self.config.currency

    def _verify_amount(
        self,
        checkout_session: Any,
        checkout: dict[str, Any],
        update_payload: dict[str, Any],
    ) -> tuple[str, int | None, str]:
        amount = checkout_amount_minor(update_payload)
        currency = checkout_currency(update_payload) or self.config.currency
        if amount is None:
            try:
                amount, currency = self._page_amount(checkout_session, checkout)
            except UpstreamError as exc:
                self.log(f"Amount page probe was unavailable ({exc.status_code}); returning pending.")
                amount = None
        if amount is None:
            return "pending", None, currency
        if amount != 0:
            raise NonZeroAmountError(
                f"promotion did not produce a zero checkout: amount_minor={amount} {currency}"
            )
        return "verified_zero", 0, currency

    def extract(self) -> CheckoutResult:
        last_promotion_error: InvalidPromotionError | None = None
        for full_attempt in range(1, self.config.full_attempts + 1):
            checkout_session = None
            checkout_proxy, update_proxy = self._stage_proxies()
            try:
                self.log(f"Full checkout flow {full_attempt}/{self.config.full_attempts}.")
                self._preflight(checkout_proxy, update_proxy)
                checkout_session, checkout = self._create_checkout_with_retry(checkout_proxy)
                self.log(f"Checkout created: {checkout['cs_id'][:12]}...")
                update_payload = self._update_promotion(
                    checkout_session, checkout, checkout_proxy, update_proxy
                )
                verification, amount, amount_currency = self._verify_amount(
                    checkout_session, checkout, update_payload
                )
                self.last_checkout_proxy = checkout_proxy
                try:
                    self.last_session_cookies = {
                        str(name): str(value)
                        for name, value in dict(checkout_session.cookies.get_dict()).items()
                        if str(name) and str(value)
                    }
                except Exception:
                    self.last_session_cookies = {}
                return CheckoutResult(
                    ok=True,
                    long_url=checkout_short_url(
                        checkout["cs_id"],
                        checkout["billing_country"],
                        checkout.get("processor_entity", ""),
                    ),
                    cs_id=checkout["cs_id"],
                    processor_entity=processor_entity_for_country(
                        checkout["billing_country"], checkout.get("processor_entity", "")
                    ),
                    billing_country=self.config.billing_country,
                    currency=self.config.currency,
                    payment_locale=self.config.payment_locale,
                    amount_verification=verification,
                    amount_minor=amount,
                    amount_currency=amount_currency,
                )
            except InvalidPromotionError as exc:
                last_promotion_error = exc
                if full_attempt >= self.config.full_attempts:
                    raise
                self.log("Promotion was invalid; discarding this checkout and rebuilding the full flow.")
            finally:
                safe_close(checkout_session)
        raise last_promotion_error or ExtractorError("checkout flow did not return a result")


def read_credential_input(args: argparse.Namespace) -> str:
    if args.credential:
        return str(args.credential)
    if args.credential_file:
        if args.credential_file == "-":
            if sys.stdin.isatty():
                return getpass.getpass("Access token or session JSON: ")
            return sys.stdin.read()
        try:
            return Path(args.credential_file).expanduser().read_text(encoding="utf-8")
        except OSError as exc:
            raise ExtractorError(f"cannot read credential file: {exc}") from exc
    if not sys.stdin.isatty():
        return sys.stdin.read()
    return getpass.getpass("Access token or session JSON: ")


def build_argument_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Create a PH/PHP ChatGPT checkout short link through US checkout and TR update proxies."
    )
    parser.add_argument("--version", action="version", version=f"%(prog)s {VERSION}")
    parser.add_argument("--credential-file", default="", help="token/session JSON file, or '-' for stdin")
    parser.add_argument(
        "--credential",
        default="",
        help="raw token/session JSON (less secure than --credential-file)",
    )
    parser.add_argument("--session-cookie", default="", help="optional ChatGPT payment cookie header")
    parser.add_argument(
        "--cliproxy-host",
        default=os.getenv("CLIPROXY_HOST", os.getenv("OPENAI_PAY_CLIPROXY_HOST", "127.0.0.1:19010")),
    )
    parser.add_argument("--cliproxy-username", default=os.getenv("CLIPROXY_USERNAME", ""))
    parser.add_argument("--cliproxy-password", default=os.getenv("CLIPROXY_PASSWORD", ""))
    parser.add_argument("--checkout-proxy", default="", help="explicit proxy; overrides Cliproxy for checkout")
    parser.add_argument("--update-proxy", default="", help="explicit proxy; overrides Cliproxy for update")
    parser.add_argument("--billing-country", default="PH")
    parser.add_argument("--currency", default="PHP")
    parser.add_argument("--checkout-proxy-country", default="US")
    parser.add_argument("--update-proxy-country", default="TR")
    parser.add_argument("--payment-locale", default="en")
    parser.add_argument("--plan-name", default="chatgptplusplan")
    parser.add_argument("--promo-campaign-id", default="plus-1-month-free")
    parser.add_argument("--timeout", type=float, default=45.0)
    parser.add_argument("--checkout-attempts", type=int, default=10)
    parser.add_argument("--update-attempts", type=int, default=15)
    parser.add_argument("--full-attempts", type=int, default=3)
    parser.add_argument("--cf-same-identity-attempts", type=int, default=5)
    parser.add_argument("--cf-retry-delay", type=float, default=3.0)
    parser.add_argument("--skip-proxy-check", action="store_true")
    parser.add_argument("--pretty", action="store_true", help="pretty-print JSON output")
    return parser


def config_from_args(args: argparse.Namespace) -> ExtractorConfig:
    return ExtractorConfig(
        billing_country=str(args.billing_country).upper(),
        currency=str(args.currency).upper(),
        payment_locale=str(args.payment_locale),
        checkout_proxy_country=str(args.checkout_proxy_country).upper(),
        update_proxy_country=str(args.update_proxy_country).upper(),
        cliproxy_host=str(args.cliproxy_host),
        cliproxy_username=str(args.cliproxy_username),
        cliproxy_password=str(args.cliproxy_password),
        checkout_proxy=str(args.checkout_proxy),
        update_proxy=str(args.update_proxy),
        plan_name=str(args.plan_name),
        promo_campaign_id=str(args.promo_campaign_id),
        timeout=float(args.timeout),
        checkout_attempts=int(args.checkout_attempts),
        update_attempts=int(args.update_attempts),
        full_attempts=int(args.full_attempts),
        cf_same_identity_attempts=int(args.cf_same_identity_attempts),
        cf_retry_delay=max(0.0, float(args.cf_retry_delay)),
        verify_proxy_country=not bool(args.skip_proxy_check),
    )


def print_json(payload: dict[str, Any], pretty: bool) -> None:
    print(
        json.dumps(
            payload,
            ensure_ascii=False,
            indent=2 if pretty else None,
            separators=None if pretty else (",", ":"),
        )
    )


def main(argv: list[str] | None = None) -> int:
    parser = build_argument_parser()
    args = parser.parse_args(argv)
    try:
        credentials = parse_credentials(read_credential_input(args), args.session_cookie)
        result = CheckoutExtractor(credentials, config_from_args(args)).extract()
        print_json(asdict(result), args.pretty)
        return 0
    except (ExtractorError, UpstreamError) as exc:
        print_json(
            {
                "ok": False,
                "error_type": type(exc).__name__,
                "error": str(exc),
            },
            args.pretty,
        )
        return 1
    except KeyboardInterrupt:
        print_json({"ok": False, "error_type": "Cancelled", "error": "cancelled"}, args.pretty)
        return 130


if __name__ == "__main__":
    raise SystemExit(main())