@youidian/sdk 3.0.0

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.
@@ -0,0 +1,385 @@
1
+ /**
2
+ * Youidian Payment SDK - Server Module
3
+ * 用于服务端集成,包含签名、订单创建、回调解密等功能
4
+ */
5
+ /**
6
+ * Order status response
7
+ */
8
+ interface OrderStatus {
9
+ orderId: string;
10
+ status: "PENDING" | "PAID" | "CANCELLED" | "REFUNDED" | "FAILED";
11
+ paidAt?: string;
12
+ channelTransactionId?: string;
13
+ }
14
+ /**
15
+ * Order details response (full order information)
16
+ */
17
+ interface OrderDetails {
18
+ orderId: string;
19
+ internalId: string;
20
+ status: "PENDING" | "PAID" | "CANCELLED" | "REFUNDED" | "FAILED";
21
+ amount: number;
22
+ currency: string;
23
+ description?: string;
24
+ paidAt?: string;
25
+ createdAt: string;
26
+ channel?: string;
27
+ product?: {
28
+ code: string;
29
+ type: string;
30
+ name: string;
31
+ description?: string;
32
+ entitlements: ProductEntitlements;
33
+ metadata?: ProductMetadata | null;
34
+ };
35
+ }
36
+ /**
37
+ * Product Entitlements
38
+ */
39
+ interface ProductEntitlements {
40
+ [key: string]: any;
41
+ }
42
+ /**
43
+ * Product Price
44
+ */
45
+ interface ProductPrice {
46
+ id: string;
47
+ currency: string;
48
+ amount: number;
49
+ displayAmount: string;
50
+ locale: string | null;
51
+ isDefault: boolean;
52
+ }
53
+ interface ProductSubscriptionPeriod {
54
+ value: number;
55
+ unit: "days" | "months" | "years";
56
+ }
57
+ interface ProductResetRule {
58
+ resetInterval: "month";
59
+ }
60
+ interface ProductMetadata {
61
+ subscriptionPeriod?: ProductSubscriptionPeriod;
62
+ expiringEntitlements?: string[];
63
+ resetEntitlements?: Record<string, ProductResetRule>;
64
+ autoAssignOnNewUser?: boolean;
65
+ trialDurationDays?: number;
66
+ }
67
+ /**
68
+ * Product Data
69
+ */
70
+ interface Product {
71
+ id: string;
72
+ code: string;
73
+ type: string;
74
+ name: string;
75
+ description?: string;
76
+ entitlements: ProductEntitlements;
77
+ prices: ProductPrice[];
78
+ metadata?: ProductMetadata | null;
79
+ }
80
+ /**
81
+ * WeChat JSAPI Payment Parameters (for wx.requestPayment)
82
+ */
83
+ interface WechatJsapiPayParams {
84
+ appId: string;
85
+ timeStamp: string;
86
+ nonceStr: string;
87
+ package: string;
88
+ signType: "RSA";
89
+ paySign: string;
90
+ }
91
+ /**
92
+ * Verified hosted login token payload.
93
+ */
94
+ interface VerifiedLoginToken {
95
+ appId: string;
96
+ userId: string;
97
+ legacyCasdoorId?: string | null;
98
+ channel: string;
99
+ email?: string | null;
100
+ name?: string | null;
101
+ avatar?: string | null;
102
+ wechatOpenId?: string | null;
103
+ wechatUnionId?: string | null;
104
+ expiresAt: string;
105
+ }
106
+ /**
107
+ * SDK Client Options
108
+ */
109
+ interface PaymentClientOptions {
110
+ /** Application ID (Required) */
111
+ appId: string;
112
+ /** Application Secret (Required for server-side operations) */
113
+ appSecret: string;
114
+ /**
115
+ * @deprecated Use apiUrl and checkoutUrl instead
116
+ * API Base URL (e.g. https://pay.youidian.com)
117
+ * If apiUrl or checkoutUrl is not provided, this will be used as fallback
118
+ * Default: https://pay.imgto.link
119
+ */
120
+ baseUrl?: string;
121
+ /**
122
+ * API server URL for backend requests (e.g. https://api.youidian.com)
123
+ * Default: https://pay-api.imgto.link
124
+ */
125
+ apiUrl?: string;
126
+ /**
127
+ * Checkout page URL for client-side payment (e.g. https://pay.youidian.com)
128
+ * Default: https://pay.imgto.link
129
+ */
130
+ checkoutUrl?: string;
131
+ }
132
+ /**
133
+ * Create Order Parameters
134
+ */
135
+ interface CreateOrderParams {
136
+ productId?: string;
137
+ priceId?: string;
138
+ channel?: string;
139
+ userId: string;
140
+ returnUrl?: string;
141
+ metadata?: Record<string, any>;
142
+ merchantOrderId?: string;
143
+ openid?: string;
144
+ locale?: string;
145
+ }
146
+ /**
147
+ * Create WeChat Mini Program Order Parameters
148
+ */
149
+ type CreateMiniProgramOrderParams = {
150
+ userId: string;
151
+ openid: string;
152
+ merchantOrderId?: string;
153
+ priceId: string;
154
+ };
155
+ /**
156
+ * Create Order Response
157
+ */
158
+ interface CreateOrderResponse {
159
+ orderId: string;
160
+ internalId: string;
161
+ amount: number;
162
+ currency: string;
163
+ payParams: any;
164
+ }
165
+ /**
166
+ * Payment Callback Notification
167
+ */
168
+ interface PaymentNotification {
169
+ iv: string;
170
+ encryptedData: string;
171
+ authTag: string;
172
+ }
173
+ /**
174
+ * Decrypted Payment Callback Data
175
+ */
176
+ interface PaymentCallbackData {
177
+ orderId: string;
178
+ merchantOrderId?: string;
179
+ status: "PAID" | "CANCELLED" | "REFUNDED" | "FAILED";
180
+ amount: number;
181
+ currency: string;
182
+ paidAt: string;
183
+ channelTransactionId?: string;
184
+ metadata?: Record<string, any>;
185
+ }
186
+ /**
187
+ * Get Orders Parameters
188
+ */
189
+ interface GetOrdersParams {
190
+ page?: number;
191
+ pageSize?: number;
192
+ userId?: string;
193
+ status?: "PENDING" | "PAID" | "CANCELLED" | "REFUNDED" | "FAILED";
194
+ startDate?: string;
195
+ endDate?: string;
196
+ }
197
+ /**
198
+ * Order List Item
199
+ */
200
+ interface OrderListItem {
201
+ orderId: string;
202
+ internalId: string;
203
+ merchantUserId: string;
204
+ status: "PENDING" | "PAID" | "CANCELLED" | "REFUNDED" | "FAILED";
205
+ amount: number;
206
+ currency: string;
207
+ channel?: string;
208
+ paidAt?: string;
209
+ createdAt: string;
210
+ }
211
+ /**
212
+ * Get Orders Response
213
+ */
214
+ interface GetOrdersResponse {
215
+ orders: OrderListItem[];
216
+ pagination: {
217
+ total: number;
218
+ page: number;
219
+ pageSize: number;
220
+ totalPages: number;
221
+ };
222
+ }
223
+ /**
224
+ * Entitlement Detail Item
225
+ */
226
+ interface EntitlementDetailItem {
227
+ type: string;
228
+ current: number | boolean;
229
+ limit?: number;
230
+ expiresAt?: string | null;
231
+ resetInterval?: string | null;
232
+ nextResetAt?: string | null;
233
+ sourceKind?: string | null;
234
+ }
235
+ /**
236
+ * Entitlement Detail - returned by getEntitlementsDetail
237
+ */
238
+ type EntitlementDetail = Record<string, EntitlementDetailItem>;
239
+ /**
240
+ * Ensure User With Trial Response
241
+ */
242
+ interface EnsureUserWithTrialResponse {
243
+ isNew: boolean;
244
+ trialAssigned: boolean;
245
+ trialProductCode?: string;
246
+ entitlements: EntitlementDetail;
247
+ }
248
+ /**
249
+ * Server-side Payment Client
250
+ * 服务端支付客户端,用于创建订单、查询状态、解密回调
251
+ */
252
+ declare class PaymentClient {
253
+ private readonly appId;
254
+ private readonly appSecret;
255
+ private readonly apiUrl;
256
+ private readonly checkoutUrl;
257
+ constructor(options: PaymentClientOptions);
258
+ /**
259
+ * Generate SHA256 signature for the request
260
+ * Logic: SHA256(appId + appSecret + timestamp)
261
+ */
262
+ private generateSignature;
263
+ /**
264
+ * Internal request helper for Gateway API
265
+ */
266
+ private request;
267
+ /**
268
+ * Decrypts the callback notification payload using AES-256-GCM.
269
+ * @param notification - The encrypted notification from payment webhook
270
+ * @returns Decrypted payment callback data
271
+ */
272
+ decryptCallback(notification: PaymentNotification): PaymentCallbackData;
273
+ /**
274
+ * Fetch products for the configured app.
275
+ */
276
+ getProducts(options?: {
277
+ locale?: string;
278
+ currency?: string;
279
+ }): Promise<Product[]>;
280
+ /**
281
+ * Create a new order
282
+ * @param params - Order creation parameters
283
+ * @returns Order details with payment parameters
284
+ */
285
+ createOrder(params: CreateOrderParams): Promise<CreateOrderResponse>;
286
+ /**
287
+ * Create a WeChat Mini Program order (channel fixed to WECHAT_MINI)
288
+ * @param params - Mini program order parameters
289
+ * @returns Order details with payment parameters
290
+ */
291
+ createMiniProgramOrder(params: CreateMiniProgramOrderParams): Promise<CreateOrderResponse>;
292
+ /**
293
+ * Pay for an existing order
294
+ * @param orderId - The order ID to pay
295
+ * @param params - Payment parameters including channel
296
+ */
297
+ payOrder(orderId: string, params: {
298
+ channel: string;
299
+ returnUrl?: string;
300
+ openid?: string;
301
+ [key: string]: any;
302
+ }): Promise<CreateOrderResponse>;
303
+ /**
304
+ * Query order status
305
+ * @param orderId - The order ID to query
306
+ */
307
+ getOrderStatus(orderId: string): Promise<OrderStatus>;
308
+ /**
309
+ * Get order details (full order information)
310
+ * @param orderId - The order ID to query
311
+ */
312
+ getOrderDetails(orderId: string): Promise<OrderDetails>;
313
+ /**
314
+ * Get orders list with pagination
315
+ * @param params - Query parameters (pagination, filters)
316
+ * @returns Orders list and pagination info
317
+ */
318
+ getOrders(params?: GetOrdersParams): Promise<GetOrdersResponse>;
319
+ /**
320
+ * Get user entitlements in the legacy flat shape.
321
+ * @param userId - User ID
322
+ */
323
+ getEntitlements(userId: string): Promise<Record<string, any>>;
324
+ /**
325
+ * Get user entitlements with full details (type, expiry, reset config, source)
326
+ * @param userId - User ID
327
+ */
328
+ getEntitlementsDetail(userId: string): Promise<EntitlementDetail>;
329
+ /**
330
+ * Ensure user exists and auto-assign trial product if new user
331
+ * This should be called when user first logs in or registers
332
+ * @param userId - User ID
333
+ */
334
+ ensureUserWithTrial(userId: string): Promise<EnsureUserWithTrialResponse>;
335
+ /**
336
+ * Get a single entitlement value
337
+ * @param userId - User ID
338
+ * @param key - Entitlement key
339
+ */
340
+ getEntitlementValue(userId: string, key: string): Promise<any>;
341
+ /**
342
+ * Consume numeric entitlement
343
+ * @param userId - User ID
344
+ * @param key - Entitlement key
345
+ * @param amount - Amount to consume
346
+ */
347
+ consumeEntitlement(userId: string, key: string, amount: number, options?: {
348
+ idempotencyKey?: string;
349
+ metadata?: Record<string, any>;
350
+ }): Promise<{
351
+ balance: number;
352
+ }>;
353
+ /**
354
+ * Add numeric entitlement (e.g. refund)
355
+ * @param userId - User ID
356
+ * @param key - Entitlement key
357
+ * @param amount - Amount to add
358
+ */
359
+ addEntitlement(userId: string, key: string, amount: number): Promise<{
360
+ balance: number;
361
+ }>;
362
+ /**
363
+ * Toggle boolean entitlement
364
+ * @param userId - User ID
365
+ * @param key - Entitlement key
366
+ * @param enabled - Whether to enable
367
+ */
368
+ toggleEntitlement(userId: string, key: string, enabled: boolean): Promise<{
369
+ isEnabled: boolean;
370
+ }>;
371
+ /**
372
+ * Generate checkout URL for client-side payment
373
+ * @param productId - Product ID
374
+ * @param priceId - Price ID
375
+ * @returns Checkout page URL
376
+ */
377
+ getCheckoutUrl(productId: string, priceId: string): string;
378
+ /**
379
+ * Verify a hosted login token and return the normalized login profile.
380
+ * This request is signed with your app credentials and routed through the worker API.
381
+ */
382
+ verifyLoginToken(token: string): Promise<VerifiedLoginToken>;
383
+ }
384
+
385
+ export { type CreateMiniProgramOrderParams, type CreateOrderParams, type CreateOrderResponse, type EnsureUserWithTrialResponse, type EntitlementDetail, type EntitlementDetailItem, type GetOrdersParams, type GetOrdersResponse, type OrderDetails, type OrderListItem, type OrderStatus, type PaymentCallbackData, PaymentClient, type PaymentClientOptions, type PaymentNotification, type Product, type ProductEntitlements, type ProductMetadata, type ProductPrice, type ProductResetRule, type ProductSubscriptionPeriod, type VerifiedLoginToken, type WechatJsapiPayParams };