apig-pay.html 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>API 服务充值 — BrainHack</title>
  7. <script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.4/build/qrcode.min.js"></script>
  8. <script>
  9. // Fallback: if QRCode lib not loaded, provide a shim using img
  10. if (typeof QRCode === 'undefined') {
  11. console.warn('QRCode lib not loaded, will use img fallback');
  12. window.QRCode = null;
  13. }
  14. </script>
  15. <style>
  16. /* ═══════════════════════════════════════════════════
  17. BrainHack Dark Neumorphic Payment Page
  18. Color Palette: #0A0A0A bg, #00D4FF neon, #00B6C0 tech
  19. ═══════════════════════════════════════════════════ */
  20. :root {
  21. --bg: #0A0A0A;
  22. --surface: #141414;
  23. --surface-alt: #1A1A1A;
  24. --neon: #00D4FF;
  25. --neon-dim: #00B6C0;
  26. --neon-glow: rgba(0, 212, 255, 0.15);
  27. --neon-glow-s: rgba(0, 212, 255, 0.25);
  28. --text: #E0E0E0;
  29. --text-dim: #888;
  30. --text-bright: #FFFFFF;
  31. --success: #00FF88;
  32. --success-dim: #059660;
  33. --danger: #FF4D6A;
  34. --shadow-dark: rgba(0, 0, 0, 0.7);
  35. --shadow-light: rgba(255, 255, 255, 0.04);
  36. --radius: 12px;
  37. }
  38. * { margin: 0; padding: 0; box-sizing: border-box; }
  39. body {
  40. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  41. background: var(--bg);
  42. color: var(--text);
  43. min-height: 100vh;
  44. display: flex;
  45. flex-direction: column;
  46. align-items: center;
  47. overflow-x: hidden;
  48. }
  49. /* ─── Background Grid Effect ─── */
  50. body::before {
  51. content: '';
  52. position: fixed;
  53. top: 0; left: 0; right: 0; bottom: 0;
  54. background-image:
  55. radial-gradient(ellipse 600px 400px at 50% 0%, rgba(0,212,255,0.06) 0%, transparent 70%),
  56. linear-gradient(rgba(0,212,255,0.03) 1px, transparent 1px),
  57. linear-gradient(90deg, rgba(0,212,255,0.03) 1px, transparent 1px);
  58. background-size: 100% 100%, 40px 40px, 40px 40px;
  59. pointer-events: none;
  60. z-index: 0;
  61. }
  62. /* ─── Neumorphic Mixins (Dark Mode) ─── */
  63. .neu-raised {
  64. background: var(--surface);
  65. background-image: linear-gradient(145deg, rgba(255,255,255,0.03), rgba(0,0,0,0.2));
  66. border-radius: var(--radius);
  67. box-shadow:
  68. 4px 4px 8px var(--shadow-dark),
  69. -4px -4px 8px var(--shadow-light);
  70. transition: box-shadow 0.25s ease, background 0.25s ease, transform 0.15s ease;
  71. }
  72. .neu-raised:hover {
  73. box-shadow:
  74. 2px 2px 5px var(--shadow-dark),
  75. -2px -2px 5px var(--shadow-light);
  76. }
  77. .neu-pressed {
  78. box-shadow:
  79. inset 3px 3px 6px var(--shadow-dark),
  80. inset -3px -3px 6px var(--shadow-light);
  81. background-image: linear-gradient(145deg, rgba(0,0,0,0.25), rgba(255,255,255,0.02));
  82. }
  83. .neu-groove {
  84. background: rgba(10,10,10,0.6);
  85. box-shadow:
  86. inset 2px 2px 4px rgba(0,0,0,0.5),
  87. inset -2px -2px 4px rgba(255,255,255,0.03);
  88. border-radius: var(--radius);
  89. }
  90. /* ─── Header ─── */
  91. .page-header {
  92. position: relative;
  93. z-index: 1;
  94. width: 100%;
  95. padding: 20px 32px;
  96. display: flex;
  97. align-items: center;
  98. gap: 12px;
  99. border-bottom: 1px solid rgba(0,212,255,0.1);
  100. }
  101. .page-header .logo {
  102. font-size: 20px;
  103. font-weight: 700;
  104. letter-spacing: 2px;
  105. color: var(--text-bright);
  106. }
  107. .page-header .logo span { color: var(--neon); }
  108. .page-header .subtitle {
  109. font-size: 14px;
  110. color: var(--text-dim);
  111. margin-left: 8px;
  112. }
  113. /* ─── Main Container ─── */
  114. .main-container {
  115. position: relative;
  116. z-index: 1;
  117. width: 100%;
  118. max-width: 720px;
  119. padding: 32px 24px;
  120. flex: 1;
  121. }
  122. /* ─── API Info Card ─── */
  123. .api-info-card {
  124. padding: 28px;
  125. margin-bottom: 24px;
  126. }
  127. .api-info-card .api-title {
  128. font-size: 22px;
  129. font-weight: 700;
  130. color: var(--text-bright);
  131. margin-bottom: 8px;
  132. }
  133. .api-info-card .api-title .badge {
  134. display: inline-block;
  135. font-size: 11px;
  136. font-weight: 500;
  137. padding: 2px 8px;
  138. border: 1px solid var(--neon);
  139. color: var(--neon);
  140. border-radius: 4px;
  141. margin-left: 10px;
  142. vertical-align: middle;
  143. }
  144. .api-info-card .api-desc {
  145. font-size: 14px;
  146. color: var(--text-dim);
  147. line-height: 1.6;
  148. }
  149. .api-info-card .api-stats {
  150. display: flex;
  151. gap: 24px;
  152. margin-top: 16px;
  153. padding-top: 16px;
  154. border-top: 1px solid rgba(255,255,255,0.06);
  155. }
  156. .api-info-card .stat-item {
  157. display: flex;
  158. flex-direction: column;
  159. }
  160. .api-info-card .stat-label {
  161. font-size: 12px;
  162. color: var(--text-dim);
  163. }
  164. .api-info-card .stat-value {
  165. font-size: 18px;
  166. font-weight: 600;
  167. color: var(--neon);
  168. }
  169. /* ─── Tier Selection ─── */
  170. .section-title {
  171. font-size: 14px;
  172. color: var(--text-dim);
  173. margin-bottom: 12px;
  174. letter-spacing: 1px;
  175. }
  176. .tier-grid {
  177. display: grid;
  178. grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  179. gap: 14px;
  180. margin-bottom: 28px;
  181. }
  182. .tier-card {
  183. padding: 18px;
  184. cursor: pointer;
  185. border: 1px solid transparent;
  186. position: relative;
  187. overflow: hidden;
  188. }
  189. .tier-card::after {
  190. content: '';
  191. position: absolute;
  192. top: 0; left: 0; right: 0;
  193. height: 2px;
  194. background: transparent;
  195. transition: background 0.3s ease;
  196. }
  197. .tier-card:hover {
  198. transform: translateY(-1px);
  199. }
  200. .tier-card.active {
  201. border-color: var(--neon);
  202. box-shadow:
  203. 4px 4px 8px var(--shadow-dark),
  204. -4px -4px 8px var(--shadow-light),
  205. 0 0 15px var(--neon-glow);
  206. }
  207. .tier-card.active::after {
  208. background: linear-gradient(90deg, var(--neon), var(--neon-dim));
  209. }
  210. .tier-price {
  211. font-size: 28px;
  212. font-weight: 700;
  213. color: var(--neon);
  214. }
  215. .tier-price .symbol { font-size: 16px; }
  216. .tier-count {
  217. font-size: 14px;
  218. color: var(--text);
  219. margin-top: 4px;
  220. }
  221. .tier-unit-price {
  222. display: inline-block;
  223. font-size: 11px;
  224. color: var(--text-dim);
  225. background: rgba(255,255,255,0.05);
  226. padding: 2px 6px;
  227. border-radius: 4px;
  228. margin-top: 8px;
  229. }
  230. /* ─── Summary ─── */
  231. .summary-bar {
  232. padding: 20px 24px;
  233. margin-bottom: 24px;
  234. display: flex;
  235. justify-content: space-between;
  236. align-items: center;
  237. }
  238. .summary-bar .label {
  239. font-size: 14px;
  240. color: var(--text-dim);
  241. }
  242. .summary-bar .total {
  243. font-size: 28px;
  244. font-weight: 700;
  245. color: var(--neon);
  246. }
  247. .summary-bar .validity {
  248. font-size: 12px;
  249. color: var(--text-dim);
  250. margin-top: 2px;
  251. }
  252. /* ─── Pay Button (Neumorphic) ─── */
  253. .pay-btn {
  254. display: block;
  255. width: 100%;
  256. padding: 16px;
  257. border: none;
  258. cursor: pointer;
  259. font-size: 16px;
  260. font-weight: 600;
  261. letter-spacing: 2px;
  262. color: var(--text-bright);
  263. background: var(--surface);
  264. background-image: linear-gradient(145deg, rgba(0,212,255,0.15), rgba(0,0,0,0.3));
  265. border-radius: var(--radius);
  266. box-shadow:
  267. 5px 5px 10px var(--shadow-dark),
  268. -5px -5px 10px var(--shadow-light),
  269. 0 0 20px var(--neon-glow);
  270. transition: all 0.25s ease;
  271. position: relative;
  272. overflow: hidden;
  273. }
  274. .pay-btn::before {
  275. content: '';
  276. position: absolute;
  277. top: 0; left: 0; right: 0;
  278. height: 1px;
  279. background: linear-gradient(90deg, transparent, var(--neon), transparent);
  280. }
  281. .pay-btn:hover {
  282. box-shadow:
  283. 3px 3px 6px var(--shadow-dark),
  284. -3px -3px 6px var(--shadow-light),
  285. 0 0 30px var(--neon-glow-s);
  286. transform: translateY(-1px);
  287. }
  288. .pay-btn:active {
  289. box-shadow:
  290. inset 3px 3px 6px var(--shadow-dark),
  291. inset -3px -3px 6px var(--shadow-light);
  292. background-image: linear-gradient(145deg, rgba(0,0,0,0.3), rgba(0,212,255,0.1));
  293. transform: translateY(0);
  294. }
  295. .pay-btn:disabled {
  296. opacity: 0.5;
  297. cursor: not-allowed;
  298. box-shadow:
  299. 2px 2px 4px var(--shadow-dark),
  300. -2px -2px 4px var(--shadow-light);
  301. }
  302. .pay-btn .btn-icon { margin-right: 8px; }
  303. /* ─── QR Modal ─── */
  304. .modal-overlay {
  305. display: none;
  306. position: fixed;
  307. top: 0; left: 0; right: 0; bottom: 0;
  308. background: rgba(0,0,0,0.85);
  309. z-index: 100;
  310. align-items: center;
  311. justify-content: center;
  312. }
  313. .modal-overlay.show { display: flex; }
  314. .modal-box {
  315. width: 380px;
  316. max-width: 90vw;
  317. padding: 32px;
  318. text-align: center;
  319. position: relative;
  320. border: 1px solid rgba(0,212,255,0.15);
  321. }
  322. .modal-box .modal-close {
  323. position: absolute;
  324. top: 12px;
  325. right: 16px;
  326. background: none;
  327. border: none;
  328. color: var(--text-dim);
  329. font-size: 20px;
  330. cursor: pointer;
  331. transition: color 0.2s;
  332. }
  333. .modal-box .modal-close:hover { color: var(--text-bright); }
  334. .modal-box .modal-title {
  335. font-size: 18px;
  336. font-weight: 600;
  337. color: var(--text-bright);
  338. margin-bottom: 6px;
  339. }
  340. .modal-box .modal-amount {
  341. font-size: 32px;
  342. font-weight: 700;
  343. color: var(--neon);
  344. margin: 8px 0;
  345. }
  346. .modal-box .modal-amount .symbol { font-size: 18px; }
  347. .qr-wrapper {
  348. display: inline-block;
  349. padding: 16px;
  350. background: #fff;
  351. border-radius: 8px;
  352. margin: 16px 0;
  353. box-shadow: 0 0 20px var(--neon-glow);
  354. }
  355. .qr-wrapper canvas { display: block; }
  356. .modal-box .scan-hint {
  357. font-size: 13px;
  358. color: var(--text-dim);
  359. margin-top: 8px;
  360. }
  361. .modal-box .scan-hint svg {
  362. vertical-align: middle;
  363. margin-right: 4px;
  364. }
  365. .polling-status {
  366. font-size: 12px;
  367. color: var(--text-dim);
  368. margin-top: 12px;
  369. display: flex;
  370. align-items: center;
  371. justify-content: center;
  372. gap: 6px;
  373. }
  374. .polling-dot {
  375. display: inline-block;
  376. width: 6px; height: 6px;
  377. background: var(--neon);
  378. border-radius: 50%;
  379. animation: pulse 1.5s infinite;
  380. }
  381. @keyframes pulse {
  382. 0%, 100% { opacity: 0.3; }
  383. 50% { opacity: 1; }
  384. }
  385. /* ─── Success Screen ─── */
  386. .success-screen {
  387. display: none;
  388. text-align: center;
  389. padding: 48px 24px;
  390. }
  391. .success-screen.show { display: block; }
  392. .success-icon {
  393. width: 80px; height: 80px;
  394. margin: 0 auto 20px;
  395. border-radius: 50%;
  396. background: var(--surface);
  397. box-shadow:
  398. 5px 5px 10px var(--shadow-dark),
  399. -5px -5px 10px var(--shadow-light),
  400. 0 0 20px rgba(0,255,136,0.15);
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. }
  405. .success-icon svg {
  406. width: 40px; height: 40px;
  407. stroke: var(--success);
  408. }
  409. .success-screen .success-title {
  410. font-size: 24px;
  411. font-weight: 700;
  412. color: var(--text-bright);
  413. margin-bottom: 8px;
  414. }
  415. .success-screen .success-detail {
  416. font-size: 14px;
  417. color: var(--text-dim);
  418. line-height: 1.8;
  419. margin-bottom: 28px;
  420. }
  421. .success-screen .back-btn {
  422. display: inline-block;
  423. padding: 12px 40px;
  424. border: none;
  425. cursor: pointer;
  426. font-size: 14px;
  427. font-weight: 600;
  428. color: var(--text-bright);
  429. background: var(--surface);
  430. background-image: linear-gradient(145deg, rgba(0,255,136,0.12), rgba(0,0,0,0.3));
  431. border-radius: var(--radius);
  432. box-shadow:
  433. 4px 4px 8px var(--shadow-dark),
  434. -4px -4px 8px var(--shadow-light),
  435. 0 0 15px rgba(0,255,136,0.1);
  436. transition: all 0.25s ease;
  437. }
  438. .success-screen .back-btn:hover {
  439. transform: translateY(-1px);
  440. box-shadow:
  441. 3px 3px 6px var(--shadow-dark),
  442. -3px -3px 6px var(--shadow-light),
  443. 0 0 25px rgba(0,255,136,0.15);
  444. }
  445. .success-screen .back-btn:active {
  446. box-shadow:
  447. inset 3px 3px 6px var(--shadow-dark),
  448. inset -3px -3px 6px var(--shadow-light);
  449. transform: translateY(0);
  450. }
  451. /* ─── Loading Skeleton ─── */
  452. .skeleton {
  453. background: linear-gradient(90deg, var(--surface) 25%, var(--surface-alt) 50%, var(--surface) 75%);
  454. background-size: 200% 100%;
  455. animation: shimmer 1.5s infinite;
  456. border-radius: 6px;
  457. }
  458. @keyframes shimmer {
  459. 0% { background-position: 200% 0; }
  460. 100% { background-position: -200% 0; }
  461. }
  462. /* ─── Error State ─── */
  463. .error-bar {
  464. display: none;
  465. padding: 12px 16px;
  466. background: rgba(255,77,106,0.1);
  467. border: 1px solid rgba(255,77,106,0.3);
  468. border-radius: 8px;
  469. color: var(--danger);
  470. font-size: 13px;
  471. margin-bottom: 16px;
  472. }
  473. .error-bar.show { display: block; }
  474. /* ─── Responsive ─── */
  475. @media (max-width: 480px) {
  476. .main-container { padding: 20px 16px; }
  477. .api-info-card { padding: 20px; }
  478. .tier-grid { grid-template-columns: 1fr; }
  479. .modal-box { padding: 24px 20px; }
  480. }
  481. </style>
  482. </head>
  483. <body>
  484. <!-- Header -->
  485. <div class="page-header">
  486. <div class="logo">BRAIN<span>HACK</span></div>
  487. <div class="subtitle">API 服务充值</div>
  488. </div>
  489. <!-- Main -->
  490. <div class="main-container" id="mainContent">
  491. <!-- API Info Card -->
  492. <div class="api-info-card neu-raised" id="apiInfoCard">
  493. <div class="api-title" id="apiTitle">
  494. <span class="skeleton" style="display:inline-block;width:200px;height:24px;">&nbsp;</span>
  495. </div>
  496. <div class="api-desc" id="apiDesc">
  497. <span class="skeleton" style="display:inline-block;width:100%;height:16px;margin-top:8px;">&nbsp;</span>
  498. </div>
  499. <div class="api-stats" id="apiStats" style="display:none;">
  500. <div class="stat-item">
  501. <span class="stat-label">当前余额</span>
  502. <span class="stat-value" id="statCount">—</span>
  503. </div>
  504. <div class="stat-item">
  505. <span class="stat-label">服务状态</span>
  506. <span class="stat-value" id="statStatus">—</span>
  507. </div>
  508. </div>
  509. </div>
  510. <!-- Error -->
  511. <div class="error-bar" id="errorBar"></div>
  512. <!-- Tier Selection -->
  513. <div class="section-title">选择套餐</div>
  514. <div class="tier-grid" id="tierGrid">
  515. <div class="tier-card neu-raised skeleton" style="height:100px;"></div>
  516. <div class="tier-card neu-raised skeleton" style="height:100px;"></div>
  517. <div class="tier-card neu-raised skeleton" style="height:100px;"></div>
  518. </div>
  519. <!-- Summary -->
  520. <div class="summary-bar neu-groove" id="summaryBar" style="display:none;">
  521. <div>
  522. <div class="label">套餐配额</div>
  523. <div id="summaryCount" style="font-size:16px;font-weight:600;color:var(--text-bright);margin-top:2px;">—</div>
  524. <div class="validity">有效期 730 天(自购买之日起)</div>
  525. </div>
  526. <div style="text-align:right;">
  527. <div class="label">应付金额</div>
  528. <div class="total"><span class="symbol">¥</span><span id="summaryPrice">0</span></div>
  529. </div>
  530. </div>
  531. <!-- Pay Button -->
  532. <button class="pay-btn" id="payBtn" disabled>
  533. <span class="btn-icon">💳</span> 微信支付
  534. </button>
  535. <!-- Success Screen -->
  536. <div class="success-screen" id="successScreen">
  537. <div class="success-icon">
  538. <svg viewBox="0 0 24 24" fill="none" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
  539. <polyline points="20 6 9 17 4 12"></polyline>
  540. </svg>
  541. </div>
  542. <div class="success-title">充值成功</div>
  543. <div class="success-detail" id="successDetail"></div>
  544. <button class="back-btn" onclick="handleDone()">完成</button>
  545. </div>
  546. </div>
  547. <!-- QR Modal -->
  548. <div class="modal-overlay" id="qrModal">
  549. <div class="modal-box neu-raised">
  550. <button class="modal-close" onclick="cancelPayment()">&times;</button>
  551. <div class="modal-title">微信扫码支付</div>
  552. <div class="modal-amount"><span class="symbol">¥</span><span id="modalPrice">0</span></div>
  553. <div class="qr-wrapper" id="qrWrapper">
  554. <canvas id="qrCanvas"></canvas>
  555. </div>
  556. <div class="scan-hint">
  557. <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"></path><circle cx="12" cy="13" r="4"></circle></svg>
  558. 打开微信扫一扫
  559. </div>
  560. <div class="polling-status" id="pollingStatus">
  561. <span class="polling-dot"></span> 等待支付中...
  562. </div>
  563. </div>
  564. </div>
  565. <script>
  566. // ═══════════════════════════════════════════════════
  567. // APIG Payment Logic
  568. // ═══════════════════════════════════════════════════
  569. const API_BASE = 'https://server.fmode.cn';
  570. const PARSE_BASE = API_BASE + '/parse/functions';
  571. const PAY_COMPANY = '1AiWpTEDH9';
  572. const APP_ID = 'ncloudmaster';
  573. // URL params
  574. const params = new URLSearchParams(window.location.search);
  575. const authId = params.get('authid') || '';
  576. const userId = params.get('user') || params.get('userid') || '';
  577. let apig = null;
  578. let selectedIndex = 0;
  579. let order = null;
  580. let tradeNo = '';
  581. let nonceStr = '';
  582. let pollTimer = null;
  583. // ─── Init ───
  584. document.addEventListener('DOMContentLoaded', () => {
  585. if (!authId) {
  586. showError('缺少 authid 参数,请通过正确链接访问本页面。');
  587. return;
  588. }
  589. loadApig();
  590. });
  591. // ─── Load APIG Info ───
  592. async function loadApig() {
  593. try {
  594. // Try getApig endpoint first (works when authId is an APIGAuth objectId)
  595. const resp = await postJSON(API_BASE + '/api/apig/getApig', { authid: authId });
  596. if (resp.code === 200 && resp.data) {
  597. apig = resp.data;
  598. renderApigInfo();
  599. renderTiers();
  600. return;
  601. }
  602. // Fallback: query APIG class directly via Parse (when authId is APIG objectId)
  603. console.log('getApig returned no data, trying Parse direct query...');
  604. const parseResp = await fetch(API_BASE + '/parse/classes/APIG/' + authId, {
  605. headers: { 'X-Parse-Application-Id': APP_ID }
  606. }).then(r => r.json());
  607. if (parseResp && parseResp.objectId) {
  608. apig = parseResp;
  609. // Add test tier if ?test=1 in URL
  610. if (params.get('test') === '1' && apig.priceStep) {
  611. apig.priceStep.unshift({ count: 1, price: 0.01 });
  612. }
  613. renderApigInfo();
  614. renderTiers();
  615. return;
  616. }
  617. showError('无法加载接口信息,请检查 authid 参数。');
  618. } catch (e) {
  619. showError('网络错误: ' + e.message);
  620. }
  621. }
  622. function renderApigInfo() {
  623. const titleEl = document.getElementById('apiTitle');
  624. titleEl.innerHTML = apig.title + '<span class="badge">HTTPS</span>';
  625. const descEl = document.getElementById('apiDesc');
  626. descEl.textContent = apig.content || '专业API数据服务';
  627. const statsEl = document.getElementById('apiStats');
  628. statsEl.style.display = 'flex';
  629. document.getElementById('statCount').textContent =
  630. (apig.count != null ? apig.count + ' 次' : '—');
  631. document.getElementById('statStatus').textContent = '正常';
  632. document.getElementById('statStatus').style.color = 'var(--success)';
  633. }
  634. function renderTiers() {
  635. const grid = document.getElementById('tierGrid');
  636. if (!apig.priceStep || apig.priceStep.length === 0) {
  637. grid.innerHTML = '<div style="color:var(--text-dim);">暂无可用套餐</div>';
  638. return;
  639. }
  640. grid.innerHTML = '';
  641. apig.priceStep.forEach((tier, i) => {
  642. const card = document.createElement('div');
  643. card.className = 'tier-card neu-raised' + (i === 0 ? ' active' : '');
  644. const unitLabel = (apig.objectId === 'MYM5zJBKgw' || apig.objectId === 'FQtTgjcqIZ') ? 'token' : '次';
  645. const unitPrice = (tier.price / tier.count).toFixed(3);
  646. card.innerHTML = `
  647. <div class="tier-price"><span class="symbol">¥</span>${tier.price}</div>
  648. <div class="tier-count">含 ${tier.count} ${unitLabel}</div>
  649. <div class="tier-unit-price">≈ ${unitPrice} 元/${unitLabel}</div>
  650. `;
  651. card.addEventListener('click', () => selectTier(i));
  652. grid.appendChild(card);
  653. });
  654. selectTier(0);
  655. document.getElementById('payBtn').disabled = false;
  656. document.getElementById('payBtn').addEventListener('click', startPayment);
  657. }
  658. function selectTier(idx) {
  659. selectedIndex = idx;
  660. document.querySelectorAll('.tier-card').forEach((el, i) => {
  661. el.classList.toggle('active', i === idx);
  662. if (i === idx) {
  663. el.classList.remove('neu-raised');
  664. el.classList.add('neu-pressed');
  665. } else {
  666. el.classList.remove('neu-pressed');
  667. el.classList.add('neu-raised');
  668. }
  669. });
  670. const tier = apig.priceStep[idx];
  671. const unitLabel = (apig.objectId === 'MYM5zJBKgw' || apig.objectId === 'FQtTgjcqIZ') ? 'token' : '次';
  672. document.getElementById('summaryBar').style.display = 'flex';
  673. document.getElementById('summaryCount').textContent = tier.count + ' ' + unitLabel;
  674. document.getElementById('summaryPrice').textContent = tier.price;
  675. }
  676. // ─── Payment Flow ───
  677. async function startPayment() {
  678. const btn = document.getElementById('payBtn');
  679. btn.disabled = true;
  680. btn.textContent = '正在生成支付码...';
  681. hideError();
  682. try {
  683. // 1. Generate trade number
  684. const now = new Date();
  685. tradeNo = 'C' + (userId || 'U') +
  686. now.getFullYear() +
  687. String(now.getMonth() + 1).padStart(2, '0') +
  688. String(now.getDate()).padStart(2, '0') +
  689. String(now.getHours()).padStart(2, '0') +
  690. String(now.getMinutes()).padStart(2, '0') +
  691. String(now.getSeconds()).padStart(2, '0') +
  692. now.getMilliseconds();
  693. const tier = apig.priceStep[selectedIndex];
  694. // 2. Call pay_code2 to get QR URL
  695. const payResp = await postJSON(PARSE_BASE + '/pay_code2', {
  696. _ApplicationId: APP_ID,
  697. company: PAY_COMPANY,
  698. out_trade_no: tradeNo,
  699. total_fee: +tier.price,
  700. body: apig.title + ' 接口充值'
  701. });
  702. if (!payResp.result || !payResp.result.code_url) {
  703. throw new Error('获取支付码失败');
  704. }
  705. const codeUrl = payResp.result.code_url[0];
  706. nonceStr = payResp.result.nonce_str;
  707. // 3. Create order
  708. createOrder(tier);
  709. // 4. Show QR modal
  710. document.getElementById('modalPrice').textContent = tier.price;
  711. document.getElementById('qrModal').classList.add('show');
  712. document.getElementById('pollingStatus').innerHTML =
  713. '<span class="polling-dot"></span> 等待支付中...';
  714. // Generate QR code
  715. await renderQR(codeUrl);
  716. // 5. Start polling
  717. startPolling();
  718. } catch (e) {
  719. showError('支付发起失败: ' + e.message);
  720. } finally {
  721. btn.disabled = false;
  722. btn.innerHTML = '<span class="btn-icon">💳</span> 微信支付';
  723. }
  724. }
  725. async function createOrder(tier) {
  726. try {
  727. const body = {
  728. type: 'wxpay',
  729. authid: authId,
  730. params: {
  731. out_trade_no: tradeNo,
  732. total_fee: +tier.price,
  733. body: apig.title + ' 接口充值'
  734. },
  735. apigid: apig.objectId,
  736. oldCount: apig.count || 0,
  737. count: tier.count
  738. };
  739. // Use user field if available, fallback to fcompany
  740. if (userId) {
  741. body.user = userId;
  742. body.fcompany = userId;
  743. }
  744. const resp = await postJSON(API_BASE + '/api/apig/created-apigorder', body);
  745. if (resp.code === 200 && resp.data) {
  746. order = resp.data;
  747. console.log('Order created:', order);
  748. }
  749. } catch (e) {
  750. console.warn('Order creation error (non-blocking):', e.message);
  751. }
  752. }
  753. function startPolling() {
  754. if (pollTimer) clearInterval(pollTimer);
  755. pollTimer = setInterval(async () => {
  756. try {
  757. const resp = await postJSON(PARSE_BASE + '/order_status2', {
  758. _ApplicationId: APP_ID,
  759. out_trade_no: tradeNo,
  760. nonce_str: nonceStr,
  761. company: PAY_COMPANY
  762. });
  763. if (resp.result && resp.result.status && resp.result.status[0] === 'SUCCESS') {
  764. clearInterval(pollTimer);
  765. pollTimer = null;
  766. document.getElementById('pollingStatus').innerHTML =
  767. '<span style="color:var(--success);">✓ 支付成功,正在充值...</span>';
  768. await doRecharge();
  769. }
  770. } catch (e) {
  771. console.warn('Poll error:', e.message);
  772. }
  773. }, 3000);
  774. }
  775. async function doRecharge() {
  776. // Show success first, then try recharge in background (saveRecharge may timeout)
  777. showSuccess();
  778. try {
  779. const tier = apig.priceStep[selectedIndex];
  780. // Build recharge body — use order data if available
  781. const apigAuthId = order?.detail?.apigAuth?.objectId || authId;
  782. const body = {
  783. authid: authId,
  784. apigid: apigAuthId,
  785. oldCount: apig.count || 0,
  786. count: tier.count,
  787. orderid: order ? order.objectId : ''
  788. };
  789. if (userId) {
  790. body.user = userId;
  791. body.authComp = userId;
  792. }
  793. // Also pass fcompany for compatibility
  794. body.fcompany = userId || '';
  795. console.log('saveRecharge body:', JSON.stringify(body));
  796. // Use AbortController for 15s timeout
  797. const controller = new AbortController();
  798. const timer = setTimeout(() => controller.abort(), 15000);
  799. const resp = await fetch(API_BASE + '/api/apig/saveRecharge', {
  800. method: 'POST',
  801. headers: { 'Content-Type': 'application/json' },
  802. body: JSON.stringify(body),
  803. signal: controller.signal
  804. });
  805. clearTimeout(timer);
  806. const result = await resp.json().catch(() => null);
  807. console.log('saveRecharge response:', result);
  808. } catch (e) {
  809. console.warn('Recharge call failed (non-critical):', e.message);
  810. }
  811. }
  812. function showSuccess() {
  813. document.getElementById('qrModal').classList.remove('show');
  814. document.getElementById('mainContent').querySelector('.api-info-card').style.display = 'none';
  815. document.querySelector('.section-title').style.display = 'none';
  816. document.getElementById('tierGrid').style.display = 'none';
  817. document.getElementById('summaryBar').style.display = 'none';
  818. document.getElementById('payBtn').style.display = 'none';
  819. const tier = apig.priceStep[selectedIndex];
  820. const unitLabel = (apig.objectId === 'MYM5zJBKgw' || apig.objectId === 'FQtTgjcqIZ') ? 'token' : '次';
  821. document.getElementById('successDetail').innerHTML =
  822. '已充值 <strong style="color:var(--neon);">' + apig.title + '</strong> 接口<br>' +
  823. '获得 <strong style="color:var(--success);">' + tier.count + ' ' + unitLabel + '</strong>,有效期 730 天';
  824. document.getElementById('successScreen').classList.add('show');
  825. }
  826. async function renderQR(url) {
  827. const wrapper = document.getElementById('qrWrapper');
  828. const canvas = document.getElementById('qrCanvas');
  829. // Try canvas-based QRCode lib first
  830. if (typeof QRCode !== 'undefined' && QRCode && QRCode.toCanvas) {
  831. try {
  832. await QRCode.toCanvas(canvas, url, {
  833. width: 200,
  834. margin: 0,
  835. color: { dark: '#000000', light: '#ffffff' }
  836. });
  837. console.log('QR rendered via canvas');
  838. return;
  839. } catch (e) {
  840. console.warn('QRCode.toCanvas failed:', e);
  841. }
  842. }
  843. // Fallback: use img with QR server API
  844. console.log('Using img fallback for QR');
  845. canvas.style.display = 'none';
  846. const img = document.createElement('img');
  847. img.src = 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=' + encodeURIComponent(url);
  848. img.width = 200;
  849. img.height = 200;
  850. img.alt = 'Payment QR Code';
  851. img.style.display = 'block';
  852. wrapper.appendChild(img);
  853. }
  854. function handleDone() {
  855. // Try to close window (works if opened via window.open)
  856. // Otherwise reload to allow another purchase
  857. try { window.close(); } catch(e) {}
  858. setTimeout(() => {
  859. // If still here, window.close didn't work — reload page
  860. window.location.reload();
  861. }, 300);
  862. }
  863. function cancelPayment() {
  864. if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
  865. document.getElementById('qrModal').classList.remove('show');
  866. }
  867. // ─── Helpers ───
  868. function showError(msg) {
  869. const el = document.getElementById('errorBar');
  870. el.textContent = msg;
  871. el.classList.add('show');
  872. }
  873. function hideError() {
  874. document.getElementById('errorBar').classList.remove('show');
  875. }
  876. async function postJSON(url, data) {
  877. const resp = await fetch(url, {
  878. method: 'POST',
  879. headers: { 'Content-Type': 'application/json' },
  880. body: JSON.stringify(data)
  881. });
  882. return resp.json();
  883. }
  884. </script>
  885. </body>
  886. </html>