@tgbotpress/plugin-premium 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/package.json +3 -2
- package/src/fulfillment.js +45 -4
- package/src/fulfillment.source.ts +24 -4
- package/src/index.js +1 -1
- package/src/purchases.d.ts +4 -3
- package/src/purchases.js +7 -7
- package/src/purchases.source.ts +8 -7
package/README.md
CHANGED
|
@@ -32,6 +32,7 @@ import { PremiumAdminProvider, PremiumPlatformsPage, PremiumPackagesPage, Premiu
|
|
|
32
32
|
- `./server` 的 `createPremiumModuleRuntime` 可在一个 Node 宿主 adapter 中组合管理 API、任务和 Telegram 更新处理器。
|
|
33
33
|
- `./admin-pages` 提供四个会员后台页面;宿主通过 `PremiumAdminProvider` 注入 API client、后台 shell 和基础 UI 组件,页面主体随 npm 包发布。
|
|
34
34
|
- Telegram 购买入口接收数据库连接和 Telegram fetcher;履约函数还接收平台密钥解密器、TON 预检和签名付款函数。npm 包不携带密钥或宿主数据库连接。
|
|
35
|
+
- 使用 Fragment 的购买回调时,宿主应把 `decryptSecret` 作为 `handleTelegramPremiumPackageCallback` / `handleTelegramPremiumUsernameReply` 的最后一个参数传入;这样 RSA v2 和宿主加密的 v3 凭据都能由同一个宿主密钥适配器读取。
|
|
35
36
|
- Telegram Stars 履约按 3/6/12 个月固定消耗 1000/1500/2500 Stars;购买者仍按套餐配置支付 USDT,Stars 不替代订单收款。目标需要数值 Telegram 用户 ID,可从机器人已记录的用户名解析,或由购买者直接提交 ID。余额不足时已收款订单进入等待状态,机器人补充 Stars 后自动重试;外部调用结果不确定时订单进入人工核对,避免重复赠送。
|
|
36
37
|
|
|
37
38
|
## 宿主职责
|
|
@@ -82,8 +83,8 @@ export function PremiumPlatformsRoute() {
|
|
|
82
83
|
|
|
83
84
|
## 开发约定
|
|
84
85
|
|
|
85
|
-
业务 TypeScript 源文件位于 `src/*.source.ts`,页面源文件位于 `src/admin-pages.*.tsx`。运行 `npm run build` 会生成包导出的 ESM `*.js` 和类型声明 `*.d.ts`;运行 `npm run typecheck`
|
|
86
|
+
业务 TypeScript 源文件位于 `src/*.source.ts`,页面源文件位于 `src/admin-pages.*.tsx`。运行 `npm run build` 会生成包导出的 ESM `*.js` 和类型声明 `*.d.ts`;运行 `npm run typecheck` 检查包内源文件;`npm test` 验证 Stars 履约和暂停恢复边界。发布工作流会运行这些检查后再打包。宿主适配器应继续注入所有涉及密钥、数据库、API key 和支付的能力。
|
|
86
87
|
|
|
87
88
|
## 发布
|
|
88
89
|
|
|
89
|
-
|
|
90
|
+
发布前先同步更新 `package.json` 与 `src/index.js` 中的 manifest 版本,并将代码合并到 `main`。随后从 `main` 为该版本创建并推送标签,例如 `plugin-premium-v0.1.1`。GitHub Actions 会构建、类型检查、运行测试、检查 npm 包内容,校验标签版本和提交是否包含在 `main`,再使用仓库 Secret `NPM_TOKEN` 发布到 npm。手动运行发布 workflow 也只能从 `main` 触发,并且需要显式把 `publish` 确认为 `true`。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tgbotpress/plugin-premium",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "TGBotPress Telegram membership plugin with USDT collection and Fragment, TON, or Telegram Stars fulfillment.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tgbotpress",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"type": "module",
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "node scripts/build.mjs",
|
|
27
|
-
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
27
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
28
|
+
"test": "node --test test/*.test.mjs"
|
|
28
29
|
},
|
|
29
30
|
"publishConfig": {
|
|
30
31
|
"registry": "https://registry.npmjs.org"
|
package/src/fulfillment.js
CHANGED
|
@@ -131,6 +131,25 @@ async function failStarsOrder(pool, rid, code, message) {
|
|
|
131
131
|
client.release();
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
+
/** Release a claim only before the external gift/signing call has started. */
|
|
135
|
+
async function releaseUnstartedPayment(pool, rid) {
|
|
136
|
+
const client = await pool.connect();
|
|
137
|
+
try {
|
|
138
|
+
await client.query('BEGIN');
|
|
139
|
+
const removed = await client.query("DELETE FROM t_platform_premium_payment WHERE order_rid=$1 AND state IN ('prepared','submitting') RETURNING order_rid", [rid]);
|
|
140
|
+
if (removed.rowCount === 1)
|
|
141
|
+
await client.query("UPDATE t_premium_platform_order SET status=1,comments='履约任务已暂停,将重新处理',update_time=now() WHERE rid=$1 AND status=6", [rid]);
|
|
142
|
+
await client.query('COMMIT');
|
|
143
|
+
return removed.rowCount === 1;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
await client.query('ROLLBACK');
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
client.release();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
134
153
|
async function telegramStarsCall(fetcher, token, method, payload) {
|
|
135
154
|
if (!/^\d{5,20}:[A-Za-z0-9_-]{20,100}$/u.test(token))
|
|
136
155
|
throw new Error('TELEGRAM_BOT_TOKEN_INVALID');
|
|
@@ -194,8 +213,10 @@ async function fulfillStarsOrder(pool, order, fetcher, shouldContinue, logger) {
|
|
|
194
213
|
if (!platform?.bot_token)
|
|
195
214
|
return 'failed';
|
|
196
215
|
try {
|
|
197
|
-
if (!shouldContinue())
|
|
216
|
+
if (!shouldContinue()) {
|
|
217
|
+
await releaseUnstartedPayment(pool, order.rid);
|
|
198
218
|
return 'skipped';
|
|
219
|
+
}
|
|
199
220
|
let balance;
|
|
200
221
|
try {
|
|
201
222
|
balance = await telegramStarsCall(fetcher, platform.bot_token, 'getMyStarBalance');
|
|
@@ -213,6 +234,10 @@ async function fulfillStarsOrder(pool, order, fetcher, shouldContinue, logger) {
|
|
|
213
234
|
await waitForStars(pool, order.rid, 'BOT_STARS_INSUFFICIENT');
|
|
214
235
|
return 'waiting';
|
|
215
236
|
}
|
|
237
|
+
if (!shouldContinue()) {
|
|
238
|
+
await releaseUnstartedPayment(pool, order.rid);
|
|
239
|
+
return 'skipped';
|
|
240
|
+
}
|
|
216
241
|
const submitting = await pool.connect();
|
|
217
242
|
try {
|
|
218
243
|
const changed = await submitting.query("UPDATE t_platform_premium_payment SET state='submitting',updated_at=now() WHERE order_rid=$1 AND state='prepared' RETURNING order_rid", [order.rid]);
|
|
@@ -222,6 +247,10 @@ async function fulfillStarsOrder(pool, order, fetcher, shouldContinue, logger) {
|
|
|
222
247
|
finally {
|
|
223
248
|
submitting.release();
|
|
224
249
|
}
|
|
250
|
+
if (!shouldContinue()) {
|
|
251
|
+
await releaseUnstartedPayment(pool, order.rid);
|
|
252
|
+
return 'skipped';
|
|
253
|
+
}
|
|
225
254
|
const gift = await telegramStarsCall(fetcher, platform.bot_token, 'giftPremiumSubscription', { user_id: Number(order.premium_tg_user_id), month_count: order.premium_package_month, star_count: required, text: `会员订单 #${order.rid}` });
|
|
226
255
|
if (!gift.ok || gift.result !== true) {
|
|
227
256
|
await failStarsOrder(pool, order.rid, 'TELEGRAM_STARS_GIFT_REJECTED', 'Telegram Stars 官方开通失败,需人工核查');
|
|
@@ -277,8 +306,10 @@ async function fulfillOrder(pool, order, fetcher, options, shouldContinue, logge
|
|
|
277
306
|
return 'failed';
|
|
278
307
|
let signerInvoked = false;
|
|
279
308
|
try {
|
|
280
|
-
if (!shouldContinue())
|
|
309
|
+
if (!shouldContinue()) {
|
|
310
|
+
await releaseUnstartedPayment(pool, order.rid);
|
|
281
311
|
return 'skipped';
|
|
312
|
+
}
|
|
282
313
|
if (Number(platform.platform_name) === 3 || !order.recipient)
|
|
283
314
|
throw new Error('PREMIUM_FRAGMENT_ORDER_PLATFORM_MISMATCH');
|
|
284
315
|
const cookie = options.decryptSecret(platform.platform_cookie), mnemonic = options.decryptSecret(platform.platform_phrase);
|
|
@@ -300,15 +331,25 @@ async function fulfillOrder(pool, order, fetcher, options, shouldContinue, logge
|
|
|
300
331
|
throw new Error('FRAGMENT_TRANSACTION_INVALID');
|
|
301
332
|
const amount = (BigInt(String(rawAmount)) / 1000000000n).toString() + '.' + (BigInt(String(rawAmount)) % 1000000000n).toString().padStart(9, '0');
|
|
302
333
|
const ref = refFromPayload(payload);
|
|
303
|
-
if (!shouldContinue())
|
|
334
|
+
if (!shouldContinue()) {
|
|
335
|
+
await releaseUnstartedPayment(pool, order.rid);
|
|
304
336
|
return 'skipped';
|
|
337
|
+
}
|
|
305
338
|
const submitting = await pool.connect();
|
|
339
|
+
let started = false;
|
|
306
340
|
try {
|
|
307
|
-
await submitting.query("UPDATE t_platform_premium_payment SET state='submitting',updated_at=now() WHERE order_rid=$1 AND state='prepared'", [order.rid]);
|
|
341
|
+
const changed = await submitting.query("UPDATE t_platform_premium_payment SET state='submitting',updated_at=now() WHERE order_rid=$1 AND state='prepared' RETURNING order_rid", [order.rid]);
|
|
342
|
+
started = changed.rowCount === 1;
|
|
308
343
|
}
|
|
309
344
|
finally {
|
|
310
345
|
submitting.release();
|
|
311
346
|
}
|
|
347
|
+
if (!started)
|
|
348
|
+
return 'manual';
|
|
349
|
+
if (!shouldContinue()) {
|
|
350
|
+
await releaseUnstartedPayment(pool, order.rid);
|
|
351
|
+
return 'skipped';
|
|
352
|
+
}
|
|
312
353
|
signerInvoked = true;
|
|
313
354
|
const txHash = await options.runPayment(amount, ref, order.premium_package_month, mnemonic);
|
|
314
355
|
await completeOrder(pool, order.rid, txHash);
|
|
@@ -115,6 +115,18 @@ async function failStarsOrder(pool: PremiumFulfillmentDatabase, rid: number, cod
|
|
|
115
115
|
} catch (error) { await client.query('ROLLBACK'); throw error } finally { client.release() }
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
/** Release a claim only before the external gift/signing call has started. */
|
|
119
|
+
async function releaseUnstartedPayment(pool: PremiumFulfillmentDatabase, rid: number) {
|
|
120
|
+
const client = await pool.connect()
|
|
121
|
+
try {
|
|
122
|
+
await client.query('BEGIN')
|
|
123
|
+
const removed = await client.query("DELETE FROM t_platform_premium_payment WHERE order_rid=$1 AND state IN ('prepared','submitting') RETURNING order_rid", [rid])
|
|
124
|
+
if (removed.rowCount === 1) await client.query("UPDATE t_premium_platform_order SET status=1,comments='履约任务已暂停,将重新处理',update_time=now() WHERE rid=$1 AND status=6", [rid])
|
|
125
|
+
await client.query('COMMIT')
|
|
126
|
+
return removed.rowCount === 1
|
|
127
|
+
} catch (error) { await client.query('ROLLBACK'); throw error } finally { client.release() }
|
|
128
|
+
}
|
|
129
|
+
|
|
118
130
|
async function telegramStarsCall(fetcher: typeof fetch, token: string, method: 'getMyStarBalance'|'giftPremiumSubscription', payload?: Record<string, unknown>) {
|
|
119
131
|
if (!/^\d{5,20}:[A-Za-z0-9_-]{20,100}$/u.test(token)) throw new Error('TELEGRAM_BOT_TOKEN_INVALID')
|
|
120
132
|
const response = await fetcher(`https://api.telegram.org/bot${token}/${method}`, {
|
|
@@ -165,7 +177,7 @@ async function fulfillStarsOrder(pool: PremiumFulfillmentDatabase, order: DueOrd
|
|
|
165
177
|
}catch(error){await client.query('ROLLBACK');throw error}finally{client.release()}
|
|
166
178
|
if(!platform?.bot_token)return 'failed'
|
|
167
179
|
try{
|
|
168
|
-
if(!shouldContinue())return 'skipped'
|
|
180
|
+
if(!shouldContinue()){await releaseUnstartedPayment(pool,order.rid);return 'skipped'}
|
|
169
181
|
let balance:Awaited<ReturnType<typeof telegramStarsCall>>
|
|
170
182
|
try{balance=await telegramStarsCall(fetcher,platform.bot_token,'getMyStarBalance')}
|
|
171
183
|
catch{await waitForStars(pool,order.rid,'STAR_BALANCE_CHECK_FAILED');return 'waiting'}
|
|
@@ -175,11 +187,13 @@ async function fulfillStarsOrder(pool: PremiumFulfillmentDatabase, order: DueOrd
|
|
|
175
187
|
}
|
|
176
188
|
const available=Number((balance.result as {amount:number}).amount),required=Number(order.need_pay_stars)
|
|
177
189
|
if(available<required){await waitForStars(pool,order.rid,'BOT_STARS_INSUFFICIENT');return 'waiting'}
|
|
190
|
+
if(!shouldContinue()){await releaseUnstartedPayment(pool,order.rid);return 'skipped'}
|
|
178
191
|
const submitting=await pool.connect()
|
|
179
192
|
try{
|
|
180
193
|
const changed=await submitting.query("UPDATE t_platform_premium_payment SET state='submitting',updated_at=now() WHERE order_rid=$1 AND state='prepared' RETURNING order_rid",[order.rid])
|
|
181
194
|
if(changed.rowCount!==1)return 'manual'
|
|
182
195
|
}finally{submitting.release()}
|
|
196
|
+
if(!shouldContinue()){await releaseUnstartedPayment(pool,order.rid);return 'skipped'}
|
|
183
197
|
const gift=await telegramStarsCall(fetcher,platform.bot_token,'giftPremiumSubscription',{user_id:Number(order.premium_tg_user_id),month_count:order.premium_package_month,star_count:required,text:`会员订单 #${order.rid}`})
|
|
184
198
|
if(!gift.ok||gift.result!==true){
|
|
185
199
|
await failStarsOrder(pool,order.rid,'TELEGRAM_STARS_GIFT_REJECTED','Telegram Stars 官方开通失败,需人工核查')
|
|
@@ -224,7 +238,7 @@ async function fulfillOrder(pool: PremiumFulfillmentDatabase, order: DueOrder, f
|
|
|
224
238
|
if (!platform) return 'failed'
|
|
225
239
|
let signerInvoked = false
|
|
226
240
|
try {
|
|
227
|
-
if (!shouldContinue()) return 'skipped'
|
|
241
|
+
if (!shouldContinue()) { await releaseUnstartedPayment(pool,order.rid); return 'skipped' }
|
|
228
242
|
if (Number(platform.platform_name) === 3 || !order.recipient) throw new Error('PREMIUM_FRAGMENT_ORDER_PLATFORM_MISMATCH')
|
|
229
243
|
const cookie = options.decryptSecret(platform.platform_cookie), mnemonic = options.decryptSecret(platform.platform_phrase)
|
|
230
244
|
if (!cookie) throw new Error('会员平台未配置 Cookie')
|
|
@@ -239,9 +253,15 @@ async function fulfillOrder(pool: PremiumFulfillmentDatabase, order: DueOrder, f
|
|
|
239
253
|
if (gift.ok !== true || !payload || (typeof rawAmount !== 'number' && typeof rawAmount !== 'string') || !/^\d{1,32}$/.test(String(rawAmount)) || BigInt(String(rawAmount)) <= 0n) throw new Error('FRAGMENT_TRANSACTION_INVALID')
|
|
240
254
|
const amount = (BigInt(String(rawAmount)) / 1_000_000_000n).toString() + '.' + (BigInt(String(rawAmount)) % 1_000_000_000n).toString().padStart(9, '0')
|
|
241
255
|
const ref = refFromPayload(payload)
|
|
242
|
-
if (!shouldContinue()) return 'skipped'
|
|
256
|
+
if (!shouldContinue()) { await releaseUnstartedPayment(pool,order.rid); return 'skipped' }
|
|
243
257
|
const submitting = await pool.connect()
|
|
244
|
-
|
|
258
|
+
let started = false
|
|
259
|
+
try {
|
|
260
|
+
const changed = await submitting.query("UPDATE t_platform_premium_payment SET state='submitting',updated_at=now() WHERE order_rid=$1 AND state='prepared' RETURNING order_rid", [order.rid])
|
|
261
|
+
started = changed.rowCount === 1
|
|
262
|
+
} finally { submitting.release() }
|
|
263
|
+
if (!started) return 'manual'
|
|
264
|
+
if (!shouldContinue()) { await releaseUnstartedPayment(pool,order.rid); return 'skipped' }
|
|
245
265
|
signerInvoked = true
|
|
246
266
|
const txHash = await options.runPayment(amount, ref, order.premium_package_month, mnemonic)
|
|
247
267
|
await completeOrder(pool, order.rid, txHash)
|
package/src/index.js
CHANGED
package/src/purchases.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ type Update = {
|
|
|
33
33
|
export type PremiumTelegramDatabase = Database;
|
|
34
34
|
export type PremiumTelegramMessage = Message;
|
|
35
35
|
export type PremiumTelegramUpdate = Update;
|
|
36
|
+
export type PremiumSecretDecryptor = (value: string | null) => string;
|
|
36
37
|
export declare const premiumPackageCallbackClaimPredicate: string;
|
|
37
38
|
export declare const premiumUsernameReplyClaimPredicate: string;
|
|
38
39
|
export declare function isPremiumUsernameReplyCandidate(update: Update): boolean;
|
|
@@ -41,10 +42,10 @@ export declare const premiumWalletPayClaimPredicate: string;
|
|
|
41
42
|
export declare const premiumCancelUnpaidClaimPredicate: string;
|
|
42
43
|
export declare const premiumChooseAgainClaimPredicate: string;
|
|
43
44
|
export declare function isPremiumCallbackUpdate(update: Update): boolean;
|
|
44
|
-
export declare function handleTelegramPremiumPackageCallback(pool: Queryable, fetcher: typeof fetch, botRid: number, update: Update): Promise<boolean>;
|
|
45
|
-
export declare function handleTelegramPremiumUsernameReply(pool: Database, fetcher: typeof fetch, botRid: number, updateRid: number, message: Message): Promise<boolean>;
|
|
45
|
+
export declare function handleTelegramPremiumPackageCallback(pool: Queryable, fetcher: typeof fetch, botRid: number, update: Update, decryptSecret?: PremiumSecretDecryptor): Promise<boolean>;
|
|
46
|
+
export declare function handleTelegramPremiumUsernameReply(pool: Database, fetcher: typeof fetch, botRid: number, updateRid: number, message: Message, decryptSecret?: PremiumSecretDecryptor): Promise<boolean>;
|
|
46
47
|
export declare function handlePremiumWalletPayCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update): Promise<boolean>;
|
|
47
48
|
export declare function handlePremiumCancelUnpaidCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update): Promise<boolean>;
|
|
48
49
|
export declare function handlePremiumChooseAgainCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update): Promise<boolean>;
|
|
49
|
-
export declare function handlePremiumTelegramCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update): Promise<boolean>;
|
|
50
|
+
export declare function handlePremiumTelegramCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update, decryptSecret?: PremiumSecretDecryptor): Promise<boolean>;
|
|
50
51
|
export {};
|
package/src/purchases.js
CHANGED
|
@@ -137,8 +137,8 @@ function decryptPlatformCookie(value) {
|
|
|
137
137
|
: { key, padding: constants.RSA_PKCS1_PADDING }, encrypted.subarray(offset, offset + bytes)));
|
|
138
138
|
return Buffer.concat(chunks).toString('utf8');
|
|
139
139
|
}
|
|
140
|
-
async function searchFragmentRecipient(fetcher, pkg, username) {
|
|
141
|
-
const cookie = decryptPlatformCookie(pkg.platform_cookie);
|
|
140
|
+
async function searchFragmentRecipient(fetcher, pkg, username, decryptSecret) {
|
|
141
|
+
const cookie = decryptSecret ? decryptSecret(pkg.platform_cookie) : decryptPlatformCookie(pkg.platform_cookie);
|
|
142
142
|
const url = new URL('https://fragment.com/api');
|
|
143
143
|
url.searchParams.set('hash', String(pkg.platform_hash ?? ''));
|
|
144
144
|
const form = new URLSearchParams({ query: username, months: String(pkg.package_month), method: 'searchPremiumGiftRecipient' });
|
|
@@ -181,7 +181,7 @@ function packageKeyboard(packages) {
|
|
|
181
181
|
}
|
|
182
182
|
return { inline_keyboard: rows };
|
|
183
183
|
}
|
|
184
|
-
export async function handleTelegramPremiumPackageCallback(pool, fetcher, botRid, update) {
|
|
184
|
+
export async function handleTelegramPremiumPackageCallback(pool, fetcher, botRid, update, decryptSecret) {
|
|
185
185
|
const selected = callbackSelection(update);
|
|
186
186
|
if (!selected)
|
|
187
187
|
return false;
|
|
@@ -343,7 +343,7 @@ function uniqueRandomPrice(base) {
|
|
|
343
343
|
}
|
|
344
344
|
return offsets.map(offset => priceText(base + BigInt(offset)));
|
|
345
345
|
}
|
|
346
|
-
export async function handleTelegramPremiumUsernameReply(pool, fetcher, botRid, updateRid, message) {
|
|
346
|
+
export async function handleTelegramPremiumUsernameReply(pool, fetcher, botRid, updateRid, message, decryptSecret) {
|
|
347
347
|
const selected = premiumMessage(message);
|
|
348
348
|
if (!selected)
|
|
349
349
|
return false;
|
|
@@ -417,7 +417,7 @@ export async function handleTelegramPremiumUsernameReply(pool, fetcher, botRid,
|
|
|
417
417
|
return true;
|
|
418
418
|
}
|
|
419
419
|
try {
|
|
420
|
-
fragment = await searchFragmentRecipient(fetcher, packageData, selected.username);
|
|
420
|
+
fragment = await searchFragmentRecipient(fetcher, packageData, selected.username, decryptSecret);
|
|
421
421
|
premiumName = fragment.displayName;
|
|
422
422
|
}
|
|
423
423
|
catch (error) {
|
|
@@ -616,8 +616,8 @@ export async function handlePremiumChooseAgainCallback(pool, fetcher, botRid, up
|
|
|
616
616
|
throw new Error('PREMIUM_CHOOSE_AGAIN_ALERT_FAILED');
|
|
617
617
|
return true;
|
|
618
618
|
}
|
|
619
|
-
export async function handlePremiumTelegramCallback(pool, fetcher, botRid, update) {
|
|
620
|
-
if (await handleTelegramPremiumPackageCallback(pool, fetcher, botRid, update))
|
|
619
|
+
export async function handlePremiumTelegramCallback(pool, fetcher, botRid, update, decryptSecret) {
|
|
620
|
+
if (await handleTelegramPremiumPackageCallback(pool, fetcher, botRid, update, decryptSecret))
|
|
621
621
|
return true;
|
|
622
622
|
if (await handlePremiumWalletPayCallback(pool, fetcher, botRid, update))
|
|
623
623
|
return true;
|
package/src/purchases.source.ts
CHANGED
|
@@ -12,6 +12,7 @@ type TelegramResult = { ok?: boolean; result?: { message_id?: number }; descript
|
|
|
12
12
|
export type PremiumTelegramDatabase = Database
|
|
13
13
|
export type PremiumTelegramMessage = Message
|
|
14
14
|
export type PremiumTelegramUpdate = Update
|
|
15
|
+
export type PremiumSecretDecryptor = (value: string | null) => string
|
|
15
16
|
type PremiumPackage = { rid: number; premium_platform_rid: number; platform_name: number; package_name: string; package_month: number; usdt_price: string; show_notes: string | null; package_pic: string | null; receive_wallet: string | null; platform_hash: string | null; platform_cookie: string | null }
|
|
16
17
|
type Purchase = { update_rid: number; bot_rid: number; chat_id: string; premium_tg_username: string; premium_name: string; order_rid: number; package_name: string; premium_package_month: number; fulfillment_method: 'fragment_ton'|'telegram_stars'; need_pay_usdt: string; need_pay_stars: number; premium_tg_user_id: string|null; receive_wallet: string; created_at: string; expire_time: string; notification_sent: boolean }
|
|
17
18
|
|
|
@@ -146,8 +147,8 @@ function decryptPlatformCookie(value: string | null) {
|
|
|
146
147
|
return Buffer.concat(chunks).toString('utf8')
|
|
147
148
|
}
|
|
148
149
|
|
|
149
|
-
async function searchFragmentRecipient(fetcher: typeof fetch, pkg: PremiumPackage, username: string) {
|
|
150
|
-
const cookie = decryptPlatformCookie(pkg.platform_cookie)
|
|
150
|
+
async function searchFragmentRecipient(fetcher: typeof fetch, pkg: PremiumPackage, username: string, decryptSecret?: PremiumSecretDecryptor) {
|
|
151
|
+
const cookie = decryptSecret ? decryptSecret(pkg.platform_cookie) : decryptPlatformCookie(pkg.platform_cookie)
|
|
151
152
|
const url = new URL('https://fragment.com/api')
|
|
152
153
|
url.searchParams.set('hash', String(pkg.platform_hash ?? ''))
|
|
153
154
|
const form = new URLSearchParams({ query: username, months: String(pkg.package_month), method: 'searchPremiumGiftRecipient' })
|
|
@@ -183,7 +184,7 @@ function packageKeyboard(packages: Array<{ package_name: string; callback_data:
|
|
|
183
184
|
return { inline_keyboard: rows }
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
export async function handleTelegramPremiumPackageCallback(pool: Queryable, fetcher: typeof fetch, botRid: number, update: Update) {
|
|
187
|
+
export async function handleTelegramPremiumPackageCallback(pool: Queryable, fetcher: typeof fetch, botRid: number, update: Update, decryptSecret?: PremiumSecretDecryptor) {
|
|
187
188
|
const selected = callbackSelection(update)
|
|
188
189
|
if (!selected) return false
|
|
189
190
|
const token = await botTokenFor(pool, botRid)
|
|
@@ -329,7 +330,7 @@ function uniqueRandomPrice(base: bigint) {
|
|
|
329
330
|
return offsets.map(offset => priceText(base + BigInt(offset)))
|
|
330
331
|
}
|
|
331
332
|
|
|
332
|
-
export async function handleTelegramPremiumUsernameReply(pool: Database, fetcher: typeof fetch, botRid: number, updateRid: number, message: Message) {
|
|
333
|
+
export async function handleTelegramPremiumUsernameReply(pool: Database, fetcher: typeof fetch, botRid: number, updateRid: number, message: Message, decryptSecret?: PremiumSecretDecryptor) {
|
|
333
334
|
const selected = premiumMessage(message)
|
|
334
335
|
if (!selected) return false
|
|
335
336
|
const token = await botTokenFor(pool, botRid)
|
|
@@ -400,7 +401,7 @@ export async function handleTelegramPremiumUsernameReply(pool: Database, fetcher
|
|
|
400
401
|
await sendText(fetcher, token, selected.chatId, 'Fragment 平台配置不完整,暂时无法创建订单,请联系管理员。', selected.messageId)
|
|
401
402
|
return true
|
|
402
403
|
}
|
|
403
|
-
try { fragment = await searchFragmentRecipient(fetcher, packageData, selected.username); premiumName = fragment.displayName }
|
|
404
|
+
try { fragment = await searchFragmentRecipient(fetcher, packageData, selected.username, decryptSecret); premiumName = fragment.displayName }
|
|
404
405
|
catch (error) {
|
|
405
406
|
const reason = error instanceof Error && error.message !== 'RSA_PRIVATE_KEY_UNAVAILABLE' && error.message !== 'PREMIUM_PLATFORM_COOKIE_MISSING' && error.message !== 'RSA_CIPHERTEXT_INVALID'
|
|
406
407
|
? escapeHtml(error.message.replace(/[\u0000-\u001f<>]/gu, ' ').slice(0, 180)) : '平台暂时无法验证该用户'
|
|
@@ -559,8 +560,8 @@ export async function handlePremiumChooseAgainCallback(pool: Database, fetcher:
|
|
|
559
560
|
return true
|
|
560
561
|
}
|
|
561
562
|
|
|
562
|
-
export async function handlePremiumTelegramCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update) {
|
|
563
|
-
if (await handleTelegramPremiumPackageCallback(pool, fetcher, botRid, update)) return true
|
|
563
|
+
export async function handlePremiumTelegramCallback(pool: Database, fetcher: typeof fetch, botRid: number, update: Update, decryptSecret?: PremiumSecretDecryptor) {
|
|
564
|
+
if (await handleTelegramPremiumPackageCallback(pool, fetcher, botRid, update, decryptSecret)) return true
|
|
564
565
|
if (await handlePremiumWalletPayCallback(pool, fetcher, botRid, update)) return true
|
|
565
566
|
if (await handlePremiumCancelUnpaidCallback(pool, fetcher, botRid, update)) return true
|
|
566
567
|
return handlePremiumChooseAgainCallback(pool, fetcher, botRid, update)
|