Edits history of script submission #20735 for ' RedeemLoyaltyReward (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * RedeemLoyaltyReward
     * Redeems a loyalty reward.
    
    The endpoint sets the reward to the `REDEEMED` terminal state.
    
    If you are using your own order processing system (not using the
    Orders API), you call this endpoint after the buyer paid for the
    purchase.
    
    After the reward reaches the terminal state, it cannot be deleted.
    In other words, points used for the reward cannot be returned
    to the account.
     */
    export async function main(
      auth: Square,
      reward_id: string,
      body: { idempotency_key: string; location_id: string },
    ) {
      const url = new URL(
        `https://connect.squareup.com/v2/loyalty/rewards/${reward_id}/redeem`,
      );
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago