更新记录

1.0.1(2026-05-07)

更新日志

v1.0.1 (2026-05-07)

修复

  • 恢复购买功能重构restorePurchases 回调改为返回 JSON 字符串,避免 UTS 到 TypeScript 的类型转换崩溃问题
  • Swift 层 restorePurchases 添加完整的 try-catch 异常处理
  • 添加详细的调试日志便于排查问题

    变更

  • RestorePurchasesOptions.success 参数类型从 IAPTransaction[] 改为 string(JSON 字符串)
  • 前端层需要自行解析 JSON 字符串获取交易列表

    README.md

    hj-AppleIAP

    Apple IAP 内购插件(StoreKit 2,iOS 15.0+)

    安装

    uni_modules/hj-AppleIAP 目录复制到项目的 uni_modules 目录下。

    使用说明

    普通购买

    
    import { purchaseProduct, finishTransaction } from '@/uni_modules/hj-AppleIAP';
    
    // 发起购买
    purchaseProduct({
    productId: 'your_product_id',
    appAccountToken: '', // 可选,用户标识
    useStoreKit1: false, // 使用 StoreKit 2
    success: (transaction) => {
      console.log('购买成功:', transaction.transactionId);
      console.log('jwsToken:', transaction.jwsToken);
    
      // 向后端校验 jwsToken 后关闭交易
      finishTransaction({
        transactionId: transaction.transactionId,
        success: () => console.log('交易已关闭')
      });
    },
    fail: (err) => {
      console.error('购买失败:', err.errMsg);
    }
    });
    
    恢复购买
    
    重要:v1.0.1 版本恢复购买返回 JSON 字符串,需要前端层解析
    
    import { restorePurchases, finishTransaction } from '@/uni_modules/hj-AppleIAP';
    
    // 发起恢复购买
    restorePurchases({
    success: (jsonStr) => {
      // 解析 JSON 字符串获取交易列表
      const transactions = JSON.parse(jsonStr);
    
      console.log('恢复购买成功,交易数:', transactions.length);
    
      // 处理每个交易
      for (const tx of transactions) {
        console.log('交易ID:', tx.transactionId);
        console.log('商品ID:', tx.productId);
        console.log('jwsToken:', tx.jwsToken);
    
        // 向后端校验 jwsToken
        // 校验成功后关闭交易
        finishTransaction({
          transactionId: tx.transactionId,
          success: () => console.log('交易已关闭:', tx.transactionId)
        });
      }
    },
    fail: (err) => {
      console.error('恢复购买失败:', err.errMsg);
    }
    });
    
    关闭交易
    
    import { finishTransaction } from '@/uni_modules/hj-AppleIAP';
    
    finishTransaction({
    transactionId: '20000001165856623',
    success: () => {
      console.log('交易已关闭');
    },
    fail: (err) => {
      console.error('关闭失败:', err.errMsg);
    }
    });
    
    查询商品信息
    
    import { queryProducts } from '@/uni_modules/hj-AppleIAP';
    
    queryProducts({
    productIds: ['product_id_1', 'product_id_2'],
    success: (products) => {
      console.log('商品列表:', products);
    },
    fail: (err) => {
      console.error('查询失败:', err.errMsg);
    }
    });
    
    类型定义
    
    // 交易信息
    interface IAPTransaction {
    transactionId: string;
    productId: string;
    jwsToken: string;      // 用于后端校验
    purchaseDate: string;
    expirationDate?: string;
    receiptData?: string;  // StoreKit 1 时有值
    }
    
    // 商品信息
    interface IAPProduct {
    productId: string;
    displayName: string;
    productDescription: string;
    displayPrice: string;
    price: number;
    currencyCode: string;
    }
    
    后端校验
    
    使用 jwsToken 调用 App Store Server API 校验:
    
    // 调用后端接口校验
    const response = await fetch('/payment/iapJwsToken', {
    method: 'POST',
    body: JSON.stringify({
      order_id: orderId,          // 普通购买时使用
      transaction_id: transactionId, // 恢复购买时使用
      product_id: productId,
      device_uuid: deviceUuid,
      jwsToken: transaction.jwsToken
    })
    });
    
    注意事项
    
    1. 需要 iOS 15.0+ 才能使用 StoreKit 2
    2. 普通购买返回的 jwsToken 用于后端校验,校验成功后必须调用 finishTransaction 关闭交易
    3. 恢复购买返回 JSON 字符串,需要前端层 JSON.parse() 解析
    4. 必须云打包才能使用 UTS 插件
    
    错误码
    
    ┌────────┬──────────────────────────┐
    │ 错误码 │           说明           │
    ├────────┼──────────────────────────┤
    │ 1003   │ 商品不存在               │
    ├────────┼──────────────────────────┤
    │ 1004   │ 用户取消购买             │
    ├────────┼──────────────────────────┤
    │ 1005   │ 购买待处理(家长控制等) │
    ├────────┼──────────────────────────┤
    │ 1008   │ nonce 格式错误           │
    ├────────┼──────────────────────────┤
    │ 9999   │ 其他错误                 │
    └────────┴──────────────────────────┘
    
    ---

1.0.0(2026-05-06)

基于 StoreKit 2 的 Apple IAP 原生 UTS 插件,支持普通订阅购买和 Promotional Offer(促销优惠),兼容 StoreKit 1(iOS 12.2+)。


平台兼容性

uni-app(4.52)

Vue2 Vue3 Vue3插件版本 Chrome Safari app-vue app-nvue app-nvue插件版本 Android iOS iOS插件版本 鸿蒙
- 1.0.0 × × 1.0.0 × 12 1.0.0 ×
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
× × × × × × - - - - - -

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。