FeaturesAffiliate

Invite & Affiliate

ShipAny implements an invite and affiliate system. You can use this system to incentivize users to invite new users.

How to use

  1. Refer to this commit to create the necessary data tables and business logic.

If it’s a new project, it’s already integrated by default.

  1. User self-defined invite link

User enters: http://localhost:3000/my-invites page, can generate their own invite link.

my-invites

  1. User’s share invite link is like this

http://localhost:3000/i/idoubi

  1. New user enters the system through the sharer’s invite link, and will be automatically associated with the sharer.

  2. Sharer can view invite records and invite rewards on /my-invites page

  3. Admin can view invite records and invite rewards in the background

Currently does not support online automatic withdrawal, need to contact users manually to 发放奖励。

admin-affiliate

How to customize reward amount and ratio

The default reward rules are:

  • New user registers through the invite link, only records the status, does not 发放奖励
  • User 每次支付,给邀请人发放 $50 固定金额奖励

You can modify the reward rules according to your business needs:

  1. When a new user registers, it will request the /api/update-invite interface

You can modify the logic of this interface to change the reward rules for the new user registration scenario.

/app/api/update-invite/route.ts
await insertAffiliate({
  user_uuid: user_uuid,
  invited_by: inviteUser.uuid,
  created_at: getIsoTimestr(),
  status: AffiliateStatus.Pending,
  paid_order_no: "",
  paid_amount: 0,
  reward_percent: AffiliateRewardPercent.Invited,
  reward_amount: AffiliateRewardAmount.Invited,
});

The default values of AffiliateRewardPercent.Invited and AffiliateRewardAmount.Invited are 0.

You can modify the corresponding values according to your business needs.

  1. When a user pays successfully, it will callback the updateAffiliateForOrder method after updating the order status

You can modify the logic inside this method to change the reward for the invited user

services/affiliate.ts
import { findAffiliateByOrderNo, insertAffiliate } from "@/models/affiliate";
 
import { AffiliateRewardAmount } from "./constant";
import { AffiliateRewardPercent } from "./constant";
import { AffiliateStatus } from "./constant";
import { Order } from "@/types/order";
import { findUserByUuid } from "@/models/user";
import { getIsoTimestr } from "@/lib/time";
 
export async function updateAffiliateForOrder(order: Order) {
  try {
    const user = await findUserByUuid(order.user_uuid);
    if (user && user.uuid && user.invited_by && user.invited_by !== user.uuid) {
      const affiliate = await findAffiliateByOrderNo(order.order_no);
      if (affiliate) {
        return;
      }
 
      await insertAffiliate({
        user_uuid: user.uuid,
        invited_by: user.invited_by,
        created_at: getIsoTimestr(),
        status: AffiliateStatus.Completed,
        paid_order_no: order.order_no,
        paid_amount: order.amount,
        reward_percent: AffiliateRewardPercent.Paied,
        reward_amount: AffiliateRewardAmount.Paied,
      });
    }
  } catch (e) {
    console.log("update affiliate for order failed: ", e);
    throw e;
  }
}

The default value of AffiliateRewardPercent.Paied is 20, representing a 20% commission rate

The default value of AffiliateRewardAmount.Paied is 50, representing a $50 commission amount

You can modify the corresponding values according to your business needs.