@seaverse/payment-sdk 0.3.1 → 0.4.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/dist/index.cjs CHANGED
@@ -233,23 +233,28 @@ class PaymentClient {
233
233
  */
234
234
  /**
235
235
  * 环境配置
236
- * - development: 开发/测试环境
237
- * - production: 生产环境
238
236
  */
239
237
  const ENV_CONFIG = {
240
- development: {
238
+ develop: {
241
239
  apiHost: 'https://aiart-openresty.dev.seaart.dev',
240
+ iframeUrl: 'https://aiart-payment-page.dev.seaart.dev',
242
241
  },
243
- production: {
242
+ release: {
244
243
  apiHost: 'https://www.seaart.ai',
244
+ iframeUrl: 'https://pay.seaart.ai',
245
245
  },
246
246
  };
247
+ /**
248
+ * SDK 加载超时时间(毫秒)
249
+ * @deprecated 使用 COMPONENT_LOAD_TIMEOUT 替代
250
+ */
251
+ const SDK_LOAD_TIMEOUT = 15000;
247
252
  /**
248
253
  * Web Component 加载超时时间(毫秒)
249
254
  */
250
255
  const COMPONENT_LOAD_TIMEOUT = 15000;
251
256
  /**
252
- * Web Component 自定义元素名称
257
+ * Web Component 元素名称
253
258
  */
254
259
  const PAYMENT_ELEMENT_NAME = 'seaart-payment';
255
260
  /**
@@ -263,9 +268,11 @@ const API_ENDPOINTS$1 = {
263
268
  * 默认配置
264
269
  */
265
270
  const DEFAULT_CHECKOUT_CONFIG = {
266
- environment: 'development',
271
+ environment: 'develop',
267
272
  debug: false,
268
273
  language: 'en',
274
+ /** @deprecated 使用 componentTimeout 替代 */
275
+ sdkTimeout: SDK_LOAD_TIMEOUT,
269
276
  componentTimeout: COMPONENT_LOAD_TIMEOUT,
270
277
  };
271
278
  /**
@@ -287,6 +294,13 @@ const BIZ_CODE = {
287
294
  BAD_REQUEST: 400,
288
295
  UNAUTHORIZED: 401,
289
296
  SERVER_ERROR: 500,
297
+ // 业务错误码 (4001-4006)
298
+ DAILY_LIMIT_EXCEEDED: 4001,
299
+ PRODUCT_NOT_FOUND: 4002,
300
+ PRODUCT_DISABLED: 4003,
301
+ INSUFFICIENT_BALANCE: 4004,
302
+ ORDER_NOT_FOUND: 4005,
303
+ INVALID_ORDER_STATUS: 4006,
290
304
  };
291
305
 
292
306
  /**
@@ -308,7 +322,7 @@ class SeaArtPayLoader {
308
322
  this.config = {
309
323
  timeout: config.timeout ?? COMPONENT_LOAD_TIMEOUT,
310
324
  debug: config.debug ?? false,
311
- environment: config.environment ?? 'development',
325
+ environment: config.environment ?? 'develop',
312
326
  };
313
327
  }
314
328
  /**
@@ -397,19 +411,17 @@ class SeaArtPayLoader {
397
411
  */
398
412
  async loadComponent() {
399
413
  try {
400
- // 动态导入 @seaart/payment-component
401
- // 注意:消费方需要在项目中安装此依赖
402
- await Promise.resolve().then(function () { return seaartPayment; });
414
+ // 动态导入内部打包的 Web Component
415
+ // Web Component 已经打包在 SDK 中,用户无需单独安装
416
+ await Promise.resolve().then(function () { return componentBundle; });
403
417
  this.log('模块导入成功');
404
418
  // 等待自定义元素注册完成
405
419
  await this.waitForElementDefined();
406
420
  this.log('自定义元素注册完成');
407
421
  }
408
422
  catch (error) {
409
- // 如果动态导入失败,可能是消费方没有安装依赖
410
423
  const errorMessage = error instanceof Error ? error.message : String(error);
411
- throw new Error(`无法加载 @seaart/payment-component: ${errorMessage}\n` +
412
- '请确保已安装依赖: npm install @seaart/payment-component');
424
+ throw new Error(`无法加载 Web Component: ${errorMessage}`);
413
425
  }
414
426
  }
415
427
  /**
@@ -489,6 +501,7 @@ class CheckoutAPI {
489
501
  price: params.price,
490
502
  purchase_type: 1,
491
503
  extra: params.extra,
504
+ redirect_url: params.redirectUrl,
492
505
  });
493
506
  }
494
507
  /**
@@ -502,6 +515,7 @@ class CheckoutAPI {
502
515
  purchase_type: 2,
503
516
  subscription: params.subscription,
504
517
  extra: params.extra,
518
+ redirect_url: params.redirectUrl,
505
519
  });
506
520
  }
507
521
  /**
@@ -527,7 +541,8 @@ class CheckoutAPI {
527
541
  const data = await response.json();
528
542
  this.log('结账响应:', data);
529
543
  if (data.code !== BIZ_CODE.SUCCESS) {
530
- throw this.createError(this.mapErrorCode(data.code), data.msg || '结账失败');
544
+ throw this.createError(this.mapErrorCode(data.code), data.msg || '结账失败', undefined, data.code // 传递原始业务错误码
545
+ );
531
546
  }
532
547
  if (!data.data) {
533
548
  throw this.createError('CHECKOUT_FAILED', '结账响应数据为空');
@@ -577,6 +592,7 @@ class CheckoutAPI {
577
592
  transactionId: response.transaction_id,
578
593
  price: response.price,
579
594
  currency: response.currency,
595
+ redirectUrl: response.redirect_url,
580
596
  sdkConfig: {
581
597
  appId: response.sdk_config.app_id,
582
598
  apiHost: response.sdk_config.api_host,
@@ -595,6 +611,14 @@ class CheckoutAPI {
595
611
  return 'UNAUTHORIZED';
596
612
  case BIZ_CODE.SERVER_ERROR:
597
613
  return 'API_ERROR';
614
+ // 业务错误码统一映射为 CHECKOUT_FAILED,通过 bizCode 区分具体原因
615
+ case BIZ_CODE.DAILY_LIMIT_EXCEEDED:
616
+ case BIZ_CODE.PRODUCT_NOT_FOUND:
617
+ case BIZ_CODE.PRODUCT_DISABLED:
618
+ case BIZ_CODE.INSUFFICIENT_BALANCE:
619
+ case BIZ_CODE.ORDER_NOT_FOUND:
620
+ case BIZ_CODE.INVALID_ORDER_STATUS:
621
+ return 'CHECKOUT_FAILED';
598
622
  default:
599
623
  return 'CHECKOUT_FAILED';
600
624
  }
@@ -602,8 +626,8 @@ class CheckoutAPI {
602
626
  /**
603
627
  * 创建支付错误
604
628
  */
605
- createError(code, message, cause) {
606
- return { code, message, cause };
629
+ createError(code, message, cause, bizCode) {
630
+ return { code, message, cause, bizCode };
607
631
  }
608
632
  /**
609
633
  * 检查是否是支付错误
@@ -645,7 +669,7 @@ class CheckoutAPI {
645
669
  * const checkoutClient = new PaymentCheckoutClient({
646
670
  * apiHost: 'https://payment.sg.seaverse.dev',
647
671
  * authToken: 'your-jwt-token',
648
- * environment: 'development',
672
+ * environment: 'develop',
649
673
  * debug: true,
650
674
  * });
651
675
  *
@@ -751,6 +775,7 @@ class PaymentCheckoutClient {
751
775
  productName: options.productName,
752
776
  price: options.price,
753
777
  extra: options.extra,
778
+ redirectUrl: options.redirectUrl,
754
779
  });
755
780
  // 自动打开支付弹窗
756
781
  this.showPaymentModal({
@@ -792,6 +817,7 @@ class PaymentCheckoutClient {
792
817
  firstDays: options.firstDays ?? 0,
793
818
  },
794
819
  extra: options.extra,
820
+ redirectUrl: options.redirectUrl,
795
821
  });
796
822
  // 自动打开支付弹窗
797
823
  this.showPaymentModal({
@@ -868,6 +894,7 @@ class PaymentCheckoutClient {
868
894
  payment.setAttribute('transaction-id', options.transactionId);
869
895
  payment.setAttribute('environment', this.config.environment);
870
896
  payment.setAttribute('language', options.language ?? DEFAULT_CHECKOUT_CONFIG.language);
897
+ payment.setAttribute('api-host', this.config.apiHost);
871
898
  // 设置可选属性
872
899
  if (options.userName) {
873
900
  payment.setAttribute('user-name', options.userName);
@@ -17991,9 +18018,20 @@ if (typeof window !== "undefined" && !customElements.get("seaart-payment")) {
17991
18018
  customElements.define("seaart-payment", SeaArtPayment);
17992
18019
  }
17993
18020
 
17994
- var seaartPayment = /*#__PURE__*/Object.freeze({
17995
- __proto__: null,
17996
- default: SeaArtPayment
18021
+ /**
18022
+ * Internal module: SeaArt Payment Component Bundle
18023
+ *
18024
+ * This module re-exports @seaart/payment-component for internal bundling.
18025
+ * End users don't need to install @seaart/payment-component separately -
18026
+ * it's bundled into the SDK during the build process.
18027
+ *
18028
+ * @internal
18029
+ */
18030
+ // Import and register the Web Component
18031
+ // This side-effect will register <seaart-payment> custom element
18032
+
18033
+ var componentBundle = /*#__PURE__*/Object.freeze({
18034
+ __proto__: null
17997
18035
  });
17998
18036
 
17999
18037
  var __defProp = Object.defineProperty;