跳到正文
Longplay
开发文档
章节目录 / 优惠券、奖励与推荐

优惠券、奖励与推荐

兑换、领取和发放优惠券,生成推荐码并兑换奖励。

Markdown
API v1更新于

兑换券码#

用户输入券码后在浏览器调用 redeemCoupon。不可用时返回 reason,如 outside_window、limit_reached、sold_out。

redeem.tsTypeScript
import { createLongplay } from "@tinkerlandai/longplay-sdk"; const longplay = createLongplay({  baseUrl: "https://api.longplayai.com",  scope: { projectId: "<APP_UUID>", environment: "sandbox" },}); type UUID = ReturnType<typeof crypto.randomUUID>; export async function redeem(customerToken: string, code: string, requestId: UUID) {  longplay.identify(customerToken);  const { offer, reason } = await longplay.redeemCoupon(code, requestId);  if (!offer) return { ok: false as const, reason };  const offers = await longplay.listOffers();  return { ok: true as const, offers };}

从后端发券#

grant.tsTypeScript
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server"; const longplay = createLongplayServer({  baseUrl: process.env.LONGPLAY_API_URL!,  apiKey: process.env.LONGPLAY_SERVER_KEY!,  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },}); export async function grantWelcomeCoupon(customerId: string, requestId: string) {  const { offer, reason } = await longplay.grantOffer({    customerId,    couponId: process.env.LONGPLAY_WELCOME_COUPON_ID!,    requestId,  });  return offer ?? reason;}

需要 offers:grant 权限。用户自助领取使用浏览器的 claimCoupon(couponId, requestId)。

在购买中使用优惠#

  • 新购买:把领取到的 offerId 传给 previewAttach。
  • 已有订阅:调用 setBillingCoupons。
  • 在你自己的结账中用券:后端调用 recordMerchantEvent 上报 coupon.redeemed。

推荐码与奖励#

浏览器调用 createReferralCode({ programId, requestId }) 生成推荐码;被推荐的用户调用 redeemReward({ code, requestId }) 兑换奖励。

相关接口#