Edits history of script submission #12149 for ' Get payments (figma)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Figma = {
      token: string;
    };
    /**
     * Get payments
     * There are two methods to query for a user's payment information on a plugin, widget, or Community file. The first method, using plugin payment tokens, is typically used when making queries from a plugin's or widget's code. The second method, providing a user ID and resource ID, is typically used when making queries from anywhere else.
    
    Note that you can only query for resources that you own. In most cases, this means that you can only query resources that you originally created.
     */
    export async function main(
      auth: Figma,
      plugin_payment_token: string | undefined,
      user_id: string | undefined,
      community_file_id: string | undefined,
      plugin_id: string | undefined,
      widget_id: string | undefined,
    ) {
      const url = new URL(`https://api.figma.com/v1/payments`);
      for (const [k, v] of [
        ["plugin_payment_token", plugin_payment_token],
        ["user_id", user_id],
        ["community_file_id", community_file_id],
        ["plugin_id", plugin_id],
        ["widget_id", widget_id],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago