@seaverse/payment-sdk 0.8.1 → 0.9.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  SeaVerse Payment SDK - 一站式支付解决方案,提供积分管理、支付弹窗、订阅管理和订单跟踪功能。
4
4
 
5
- > **最新版本**: v0.8.1 | **文档语言**: 中文优先(包含英文说明)
5
+ > **最新版本**: v0.8.2 | **文档语言**: 中文优先(包含英文说明)
6
6
 
7
7
  ## 📦 安装
8
8
 
@@ -95,6 +95,46 @@ const modal = new GenericPackageModal({
95
95
  modal.open();
96
96
  ```
97
97
 
98
+ ## 🏗️ 架构设计
99
+
100
+ ### 代码重构 (v0.8.1)
101
+
102
+ 为了提高代码可维护性和消除重复逻辑,SDK 进行了架构重构:
103
+
104
+ **核心改进**:
105
+ - ✅ **抽象基类设计** - 创建 `BasePackageModal<TPackage, TOptions>` 抽象基类
106
+ - ✅ **消除重复代码** - 减少 ~400 行重复代码(SDK 初始化、支付流程、事件处理)
107
+ - ✅ **类型安全** - 使用 TypeScript 泛型确保类型安全
108
+ - ✅ **向后兼容** - 公开 API 完全不变,现有代码无需修改
109
+
110
+ **类继承结构**:
111
+
112
+ ```
113
+ BasePackageModal<TPackage, TOptions> (抽象基类)
114
+ ├── CreditPackageModal (积分套餐弹框)
115
+ └── GenericPackageModal (通用套餐弹框)
116
+ ```
117
+
118
+ **BasePackageModal 提供的共享功能**:
119
+ - SDK 初始化和状态管理
120
+ - 支付流程处理(订单创建、支付弹框)
121
+ - 事件监听和按钮处理
122
+ - 工具方法(formatNumber、cleanup 等)
123
+
124
+ **子类只需实现**:
125
+ - `createModal()` - 创建和配置 PaymentModal
126
+ - `renderContent()` - 渲染弹框内容(UI 特定逻辑)
127
+ - `getPackages()` - 获取套餐列表
128
+ - `getPackageDisplayName()` - 获取套餐显示名称
129
+ - `getLoadingButtonHTML()` - 获取加载按钮 HTML
130
+
131
+ 这种设计确保:
132
+ - **单一职责** - 基类负责逻辑,子类负责 UI
133
+ - **DRY 原则** - 共享逻辑只需维护一处
134
+ - **易于扩展** - 添加新套餐类型只需继承基类
135
+
136
+ ---
137
+
98
138
  ## 📖 核心功能模块
99
139
 
100
140
  ### ⭐️ 推荐功能(新用户优先使用)
@@ -179,6 +219,164 @@ SDK 内置以下积分套餐:
179
219
  | Pro | 520 | $1.99 | 专业套餐 |
180
220
  | Ultra | 1200 | $4.99 | 旗舰套餐 |
181
221
 
222
+ ---
223
+
224
+ ### PaymentCheckoutModal - 完整收银台流程
225
+
226
+ **适用场景**:
227
+ - ✅ 需要完整的支付收银台体验(支付方式选择 + 订单信息展示)
228
+ - ✅ 自动创建订单并完成支付
229
+ - ✅ 支持三种支付方式:Link Payment、Dropin Payment、BindCard Payment
230
+ - ✅ 适合需要更多控制和自定义的场景
231
+
232
+ #### 基础用法
233
+
234
+ ```typescript
235
+ import { PaymentCheckoutModal, type Product, type AutoCreateOrderConfig } from '@seaverse/payment-sdk';
236
+
237
+ // 定义产品信息
238
+ const product: Product = {
239
+ id: 'pkg_credit_100',
240
+ name: '100 Credits Package',
241
+ price: 9.99,
242
+ currency: 'USD',
243
+ type: 'credit_package', // 'credit_package' | 'subscription' | 'change_subscription'
244
+ };
245
+
246
+ // 创建收银台 modal
247
+ const modal = new PaymentCheckoutModal({
248
+ // 产品信息
249
+ product: product,
250
+
251
+ // 自动创建订单配置
252
+ autoCreateOrder: {
253
+ productId: product.id,
254
+ purchaseType: 1, // 1=一次性购买, 2=订阅
255
+ apiHost: 'https://api.example.com',
256
+ accountToken: 'user-token',
257
+ clientId: 'your-client-id',
258
+ countryCode: 'SG',
259
+ },
260
+
261
+ // 可选配置
262
+ accountName: 'John Doe', // 显示在订单信息中
263
+ language: 'en', // 'en' | 'zh-CN'
264
+
265
+ // 回调函数
266
+ onPaymentSuccess: (payload) => {
267
+ console.log('支付成功!', payload);
268
+ // payload 包含: orderId, transactionId, 等信息
269
+ },
270
+
271
+ onPaymentFailed: (error) => {
272
+ console.error('支付失败:', error.message);
273
+ },
274
+
275
+ onPaymentMethodSelect: (method) => {
276
+ console.log('选择支付方式:', method.name);
277
+ },
278
+
279
+ onLinkPaymentStart: (methodName) => {
280
+ console.log('Link 支付开始:', methodName);
281
+ // 可以在这里显示 "请在新窗口完成支付" 提示
282
+ },
283
+
284
+ onLoading: (loading) => {
285
+ console.log('加载状态:', loading);
286
+ },
287
+ });
288
+
289
+ // 打开收银台
290
+ modal.open();
291
+ ```
292
+
293
+ #### 三种初始化模式
294
+
295
+ PaymentCheckoutModal 支持三种初始化模式(三选一):
296
+
297
+ **模式 1: autoCreateOrder(推荐)**
298
+ ```typescript
299
+ const modal = new PaymentCheckoutModal({
300
+ product: { id: 'pkg_1', name: 'Product', price: 9.99, currency: 'USD', type: 'credit_package' },
301
+ autoCreateOrder: {
302
+ productId: 'pkg_1',
303
+ purchaseType: 1,
304
+ apiHost: 'https://api.example.com',
305
+ accountToken: 'token',
306
+ clientId: 'client-id',
307
+ countryCode: 'SG',
308
+ },
309
+ onPaymentSuccess: (payload) => { /* ... */ },
310
+ });
311
+ ```
312
+
313
+ **模式 2: payment(提前创建 OrderPayment 实例)**
314
+ ```typescript
315
+ import { OrderPayment } from '@seaverse/payment-sdk';
316
+
317
+ const payment = new OrderPayment({
318
+ transactionId: 'txn_123',
319
+ apiHost: 'https://api.example.com',
320
+ accountToken: 'token',
321
+ });
322
+
323
+ const modal = new PaymentCheckoutModal({
324
+ product: { /* ... */ },
325
+ payment: payment,
326
+ paymentMethods: [...], // 需要手动提供支付方式列表
327
+ onPaymentSuccess: (payload) => { /* ... */ },
328
+ });
329
+ ```
330
+
331
+ **模式 3: transactionId(延迟初始化)**
332
+ ```typescript
333
+ const modal = new PaymentCheckoutModal({
334
+ product: { /* ... */ },
335
+ transactionId: 'txn_123',
336
+ paymentMethods: [...], // 需要手动提供支付方式列表
337
+ onPaymentSuccess: (payload) => { /* ... */ },
338
+ });
339
+ ```
340
+
341
+ #### 配置选项说明
342
+
343
+ | 选项 | 类型 | 必填 | 说明 |
344
+ |------|------|------|------|
345
+ | `product` | `Product` | 否 | 产品信息(id, name, price, currency, type) |
346
+ | `autoCreateOrder` | `AutoCreateOrderConfig` | 三选一 | 自动创建订单配置(推荐) |
347
+ | `payment` | `OrderPayment` | 三选一 | 提前创建的 OrderPayment 实例 |
348
+ | `transactionId` | `string` | 三选一 | 订单 transaction_id(延迟初始化) |
349
+ | `paymentMethods` | `PaymentMethod[]` | 否 | 支付方式列表(autoCreateOrder 模式会自动获取) |
350
+ | `accountName` | `string` | 否 | 用户账号名称(显示在订单信息中) |
351
+ | `language` | `'en' \| 'zh-CN'` | 否 | 语言设置(默认 'en') |
352
+ | `onPaymentSuccess` | `(payload) => void` | 否 | 支付成功回调 |
353
+ | `onPaymentFailed` | `(error) => void` | 否 | 支付失败回调 |
354
+ | `onPaymentMethodSelect` | `(method) => void` | 否 | 选择支付方式回调 |
355
+ | `onLinkPaymentStart` | `(methodName) => void` | 否 | Link 支付开始回调 |
356
+ | `onLoading` | `(loading) => void` | 否 | 加载状态回调 |
357
+
358
+ #### 产品类型说明
359
+
360
+ | 类型 | 值 | 说明 |
361
+ |------|------|------|
362
+ | 信用包 | `'credit_package'` | 一次性购买积分包 |
363
+ | 订阅 | `'subscription'` | 新订阅购买 |
364
+ | 变更订阅 | `'change_subscription'` | 订阅升级/降级 |
365
+
366
+ #### 支付方式说明
367
+
368
+ PaymentCheckoutModal 支持三种支付方式:
369
+
370
+ 1. **Link Payment**: 跳转到外部支付页面,自动轮询订单状态
371
+ 2. **Dropin Payment**: 嵌入式支付表单,支持信用卡、PayPal 等
372
+ 3. **BindCard Payment**: 使用已保存的卡片支付(显示卡片列表)
373
+
374
+ #### 完整示例
375
+
376
+ 查看完整示例代码:`examples/payment-sdk-demo/src/components/new-sdk/PaymentCheckoutSection.tsx`
377
+
378
+ ---
379
+
182
380
  #### 环境配置
183
381
 
184
382
  SDK 根据 `environment` 自动配置以下参数:
@@ -1002,5 +1200,27 @@ MIT License
1002
1200
 
1003
1201
  ---
1004
1202
 
1005
- **更新日期**: 2026-01-31
1006
- **SDK 版本**: v0.8.0
1203
+ **更新日期**: 2026-02-02
1204
+ **SDK 版本**: v0.8.2
1205
+
1206
+ ## 📝 更新日志
1207
+
1208
+ ### v0.8.2 (2026-02-02)
1209
+
1210
+ **架构重构**:
1211
+ - 🏗️ 创建 `BasePackageModal` 抽象基类,消除 `CreditPackageModal` 和 `GenericPackageModal` 之间的重复代码
1212
+ - 📉 减少约 400 行重复代码(25% 代码量减少)
1213
+ - 🎯 `CreditPackageModal`: 从 1042 行减少到 515 行
1214
+ - 🎯 `GenericPackageModal`: 从 752 行减少到 340 行
1215
+ - ✅ 完全向后兼容,现有代码无需修改
1216
+ - 🔒 通过 TypeScript 泛型确保类型安全
1217
+
1218
+ **类型系统改进**:
1219
+ - 新增 `BasePackage` 接口 - 所有套餐类型的基础接口
1220
+ - 新增 `BasePackageModalOptions<TPackage>` 接口 - 通用配置接口
1221
+ - `CreditPackage` 和 `GenericPackage` 现在继承自 `BasePackage`
1222
+
1223
+ **开发体验提升**:
1224
+ - 更容易添加新的套餐类型(只需继承 `BasePackageModal`)
1225
+ - 修复 bug 只需在基类中修改一处
1226
+ - 更清晰的职责分离(基类处理逻辑,子类处理 UI)