Skip to content

用户付费

utools.isPurchasedUser()

用户付费状态

类型定义

ts
function isPurchasedUser(): boolean|string

示例代码

js
const purchase = utools.isPurchasedUser();
if (typeof purchase === "boolean") {
  // 付费买断
} else {
  // 按时长订阅
  // purchase 的格式为 yyyy-mm-dd hh:mm:ss
}

utools.openPurchase(options, callback)

打开付费页面

类型定义

ts
function openPurchase(options: OpenPurchaseOptions,callback: PurchaseCallback): void
OpenPurchaseOptions 类型定义
ts
interface OpenPurchaseOptions {
  goodsId: string;
  outOrderId?: string;
  attach?: string;
}

字段说明

  • goodsId
    • 商品 ID,在 “ uTools 开发者工具” 插件应用中创建
  • outOrderId
    • 第三方服务生成的订单号(6 - 64 字符)
  • attach
    • 第三方服务附加数据,在查询 API 和支付通知中原样返回,可作为自定义参数使用(最多 256 字符)
PurchaseCallback 类型定义
ts
PurchaseCallback(): void

示例代码

js
utools.openPurchase(
  {
    goodsId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // 商品 ID
  },
  () => {
    console.log("支付成功");
  }
);

utools.openPayment(options, callback)

打开付费页面

类型定义

ts
function openPayment(options: OpenPaymentOptions,callback: PaymentCallback): void
OpenPaymentOptions 类型定义
ts
interface OpenPurchaseOptions {
  goodsId: string;
  outOrderId?: string;
  attach?: string;
}

字段说明

  • goodsId
    • 商品 ID,在 “ uTools 开发者工具” 插件应用中创建
  • outOrderId
    • 第三方服务生成的订单号(6 - 64 字符)
  • attach
    • 第三方服务附加数据,在查询 API 和支付通知中原样返回,可作为自定义参数使用(最多 256 字符)
PaymentCallback 类型定义
ts
PurchaseCallback(): void

示例代码

js
utools.openPayment(
  {
    goodsId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // 商品 ID
  },
  () => {
    console.log("购买成功");
  }
);

utools.fetchUserPayments()

获取用户支付记录

类型定义

ts
function fetchUserPayments(): Promise<Payment[]>
Payment 类型定义
ts
interface Payment {
  order_id: string;
  out_order_id: string;
  open_id: string;
  pay_fee: number;
  body: string;
  attach: string;
  goods_id: string;
  paid_at: string;
  created_at: string;
}

字段说明

  • order_id
    • uTools 订单 ID
  • out_order_id
    • 外部或第三方服务生成的订单号
  • open_id
    • uTools 用户 ID
  • pay_fee
    • 支付金额,单位为分
  • body
    • 商品描述
  • attach
    • 附加数据
  • goods_id
    • 商品 ID
  • paid_at
    • 支付时间
  • created_at
    • 订单生成时间

示例代码

js
utools.fetchUserPayments().then((payments) => {
  console.log(payments);
});