@telegram-app-sdk/core
Version:
`Telegram SDK` 是集成 `TelegramWebJs` 的基础合集
299 lines (294 loc) • 9.11 kB
TypeScript
import { TonConnectUiCreateOptions, TonConnectUI } from '@tonconnect/ui';
import { TonClient } from 'ton';
type Indexes<T = any> = {
[s: string | number]: T;
};
/**
* 字符串 token 转对象
*/
declare function parseCookies(cookieString: string): {
[key: string]: string;
};
/**
* 对象转url查询参数
* @returns '&a=1&b=2'
*/
declare function objectToQueryString(params: Indexes): string;
/**
* 复制内容
* @param {string} value 需要复制的内容
* @param {Function} cb 复制成功回调函数
*/
declare function copyText(value: string, cb?: Function): void;
/**
* base64 加密
*/
declare function base64UrlEncode(str: string): string;
/**
* base64 解密
*/
declare function decodeFromBase64Url(base64UrlStr: string): string;
/**
* @remarks TG_SDK 类中所需的类型命名空间
*/
declare namespace TG_SDK_NAMESPACE {
/**
* @remarks 原来 TG_SDKOptions 类型
*/
export interface Options {
/**
* @remarks id 标识
*/
appid: string;
/**
* @remarks token 在 localStorage 中的 key 名称
* @default '_TG_SDK_Token'
*/
tokenKey?: string;
/**
* @remarks user_id 在外部环境打开时由于获取不到 TG 用户信息,故此需要传入,仅在 debug 为 true 生效
* @default 9527
*/
user_id?: number;
/**
* @remarks 是否开启调试模式,开启后 日志会显示在控制台 以及不会进入支付流程,直接返回成功(Ton 除外,因为 debug 情况下可以使用测试网络支付)
* @default false
*/
debug?: boolean;
/**
* @remarks ton 配置
* @document https://ton-connect.github.io/sdk/types/_tonconnect_ui.TonConnectUiCreateOptions.html
*/
tonConfig: TonConnectUiCreateOptions;
}
/**
* @remarks 可用的支付方式
*/
export type PayTypes = 'Ton' | 'Stars' | 'Usdt';
/**
* @remarks 支付状态 paid - 成功 cancelled - 取消 failed - 失败 pending - 等待中(只有 star 支付时会有完整状态,ton 支付只会有 paid |
cancelled)
*/
export type InvoiceStatus = 'paid' | 'cancelled' | 'failed' | 'pending';
interface OpenPayBaseParams {
/**
* @remarks 当前支付弹窗类型
* @default 'PAY'
*/
type?: 'PAY' | 'PAY_LIST';
/**
* @remarks 扩展信息
* @default '''
*/
extra?: string;
/**
* @remarks 开始支付回调
*/
start?: (payItem: PayListResponse['support_token'][number]) => void;
/**
* @remarks 支付结果回调
*/
result?: ({ status, errorMsg, extra, }: {
status: TG_SDK_NAMESPACE.InvoiceStatus;
errorMsg?: string;
extra: string | undefined;
}) => void;
}
export interface OpenPayPopupParams extends OpenPayBaseParams {
type: 'PAY';
title: string;
message: string;
order_id: string;
amount: string;
}
export interface OpenPayListPopupParams extends OpenPayBaseParams {
type: 'PAY_LIST';
item_id: string;
}
/**
* @remarks 打开支付弹窗载荷
*/
export type OpenPayPopupPayload = OpenPayPopupParams | OpenPayListPopupParams;
export { };
}
/**
* @remarks 创建订单返回的数据
*/
interface CreateOrderResponse {
/**
* @remarks Ton 支付时为交易备注信息,Stars 时无用
*/
invoice_code: string;
/**
* @remarks Ton 支付时为收款地址,Stars 时为发票链接
*/
recharge_address: string;
/**
* @remarks USDT 支付数额
*/
amout: string;
/**
* @remarks USDT 合约地址
*/
contract_address: string;
/**
* @remarks 精度
*/
contract_wei: number;
/**
* @remarks 是否口开启测试链
*/
is_test: true;
}
/**
* @remarks 支付档位接口返回数据
*/
interface PayListResponse {
/**
* @remarks 挡位id
*/
id: string;
/**
* @remarks 标题
*/
title: string;
/**
* @remarks 描述
*/
description: string;
/**
* @remarks 金额美元
*/
dollar_amount: string;
/**
* @remarks 支持支付方式
*/
support_token: {
token: TG_SDK_NAMESPACE.PayTypes;
amount: string | number;
}[];
}
/**
* @remarks 原来 TG_SDK
*/
declare class TG_SDK {
/**
* @remarks 是否开启调试模式,开启后 日志会显示在控制台
*/
readonly debug: boolean;
/**
* @remarks TG WebApp 对象,等同于 window.Telegram.WebApp
* @document https://core.telegram.org/bots/webapps#initializing-mini-apps
*/
readonly WebApp: any;
readonly APPID: string;
/**
* @remarks Ton UI 实例
* @document https://ton-connect.github.io/sdk/modules/_tonconnect_ui.html#change-options-if-needed
*/
readonly tonConnectUI: TonConnectUI;
/**
* @remarks TonClient 实例
* @document https://ton-community.github.io/ton/classes/TonClient4.html
*/
readonly tonClient: TonClient;
version: string;
/**
* @remarks 构造函数中除开 'appName' | 'appid' | 'botName' | 'debug' | 'tonConfig' 以外的值
*/
readonly params: Omit<TG_SDK_NAMESPACE.Options, 'appName' | 'appid' | 'botName' | 'debug' | 'tonConfig'>;
private payOptions;
/**
* @remarks 是否 TG 宿主环境中
*/
readonly isTG: boolean;
static utils: {
parseCookies: typeof parseCookies;
objectToQueryString: typeof objectToQueryString;
copyText: typeof copyText;
base64UrlEncode: typeof base64UrlEncode;
decodeFromBase64Url: typeof decodeFromBase64Url;
};
/**
* @param {TG_SDKOptions} payload
*/
constructor({ appid, debug, tonConfig, ...params }: TG_SDK_NAMESPACE.Options);
/**
* @remarks 连接钱包
*/
connect(): Promise<unknown>;
/**
* @remarks 断开连接
*/
disconnect(): Promise<void>;
/**
* @remarks stars 支付
* @param {CreateOrderResponse} response
* @example
* window.TG_SDK.tonTransaction({
* recharge_address: "https://t.me/$ZR439oScwErACQAAH40H8p-Q3Qo"
* })
*/
starsTransaction(response: CreateOrderResponse): Promise<Parameters<NonNullable<TG_SDK_NAMESPACE.OpenPayPopupPayload['result']>>[0]>;
/**
* @remarks ton 支付
* @param {CreateOrderResponse} response
* @example
* window.TG_SDK.starsTransaction({
* invoice_code: 'Hello Word!',
* recharge_address: '0QB4bOw8W7eNXp6fhMZNiivCiNxvZDF2sRRz6MtDyiMUUc-n'
* })
*/
tonTransaction(response: CreateOrderResponse): Promise<Parameters<NonNullable<TG_SDK_NAMESPACE.OpenPayPopupPayload['result']>>[0]>;
/**
* @remarks ton 支付
* @param {CreateOrderResponse} response
* @example
window.TG_SDK.tonChainUsdtTransaction({
invoice_code: 'Hello Word!',
recharge_address: '0QB4bOw8W7eNXp6fhMZNiivCiNxvZDF2sRRz6MtDyiMUUc-n',
amout: "1",
contract_address: "kQCTbNrH_ddAL48e87HIu7-6kQnGWHpJdcwwfX5c0LROQrHi",
contract_wei: 6,
invoice_code: "CE8t2BfsJHHqgB41cdSWqM",
is_test: true,
recharge_address: "0QA4lgvi12S5VWWBk6ThaERiYbkwFIK0bp2fZGy9s5aEXeIU"
})
*/
tonChainUsdtTransaction(response: CreateOrderResponse): Promise<Parameters<NonNullable<TG_SDK_NAMESPACE.OpenPayPopupPayload['result']>>[0]>;
private toNanoWei;
/**
* @remarks 获取 TG 进入时 Start 携带的参数信息
*/
get StartData(): string;
/**
* @remarks GET 获取唤起弹窗的 options 参数
*/
get PopupPayOptions(): TG_SDK_NAMESPACE.OpenPayPopupPayload | undefined;
/**
* @remarks SET 非 TG 情况下需要调用,不然支付成功后没有回调
*/
set PopupPayOptions(value: TG_SDK_NAMESPACE.OpenPayPopupPayload | undefined);
/**
* @remarks 获取 Token
*/
get Token(): string;
private get TonOwner();
/**
* @remarks 错误抛出,并且在 this.debug 为 false 时没有控制台打印
*/
onError(funName: string, error: unknown): Error;
}
/**
* @ignore
* @remarks 初始化 TG_SDK 函数,用于加载 Telegram-web.js
* @param {T} SDKClass SDK类型名称
* @param {TG_SDK_NAMESPACE.Options} config SDK类型参数
*/
declare const initializeTelegramSDK: <T extends new (config: TG_SDK_NAMESPACE.Options) => InstanceType<T>>(SDKClass: T, config: TG_SDK_NAMESPACE.Options) => Promise<InstanceType<T>>;
/**
* @remarks 初始化 TG_SDK 函数,用于加载 Telegram-web.js
* @param {TG_SDK_NAMESPACE.Options} config SDK类型参数
*/
declare const _setTelegramSDKConfig: (config: TG_SDK_NAMESPACE.Options) => Promise<any>;
export { type CreateOrderResponse, type PayListResponse, TG_SDK as TG_SDK_CORE, TG_SDK_NAMESPACE, _setTelegramSDKConfig, initializeTelegramSDK };