IAPImpl.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. export interface IAPProduct {
  2. productId: string;
  3. title: string;
  4. description: string;
  5. price: string;
  6. localizedPrice: string;
  7. currencyCode: string;
  8. }
  9. export type IAPErrorCode = 'INVALID_PRODUCT_ID' | 'PRODUCT_NOT_LOADED' | 'PAYMENTS_NOT_ALLOWED'
  10. | 'OPERATION_IN_PROGRESS' | 'PENDING_TRANSACTIONS' | 'PAYMENT_FAILED'
  11. | 'TIMEOUT' | 'BRIDGE_ERROR' | 'UNSUPPORTED_PLATFORM' | 'UNKNOWN';
  12. export interface IAPResult {
  13. status: 'purchased' | 'restored' | 'cancelled' | 'deferred' | 'error';
  14. /** 请求失败原因;超时不代表 Apple 已取消或支付失败。 */
  15. reason?: string;
  16. errorCode?: IAPErrorCode;
  17. /** Apple 原始错误,仅用于诊断,不依赖本地化 message 判断逻辑。 */
  18. nativeError?: { domain: string; code: number };
  19. productId: string;
  20. transactionId?: string;
  21. originalTransactionId?: string;
  22. /** Base64 App Store receipt; send to your backend for validation. */
  23. receipt?: string;
  24. }
  25. export interface IAPProductsResult {
  26. products: IAPProduct[];
  27. invalidProductIds: string[];
  28. }
  29. export abstract class IAPImpl {
  30. abstract init(products: string[]): Promise<IAPProductsResult>;
  31. abstract purchase(product: string): Promise<IAPResult>;
  32. abstract restore(): Promise<IAPResult[]>;
  33. abstract getPendingTransactions(): Promise<IAPResult[]>;
  34. /** Only call after verification and idempotent delivery of the purchase. */
  35. abstract finishTransaction(transactionId: string): Promise<void>;
  36. abstract onTransaction(listener: (transaction: IAPResult) => void): () => void;
  37. }