Edits history of script submission #2784 for ' Post plans (stripe)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Stripe = {
      token: string;
    };
    /**
     * Post plans
     * You can now model subscriptions more flexibly using the Prices API. It replaces the Plans API and is backwards compatible to simplify your migration.
     */
    export async function main(
      auth: Stripe,
      body: {
        active?: boolean;
        aggregate_usage?: "last_during_period" | "last_ever" | "max" | "sum";
        amount?: number;
        amount_decimal?: string;
        billing_scheme?: "per_unit" | "tiered";
        currency: string;
        expand?: string[];
        id?: string;
        interval: "day" | "month" | "week" | "year";
        interval_count?: number;
        metadata?: { [k: string]: string } | "";
        nickname?: string;
        product?:
          | {
              active?: boolean;
              id?: string;
              metadata?: { [k: string]: string };
              name: string;
              statement_descriptor?: string;
              tax_code?: string;
              unit_label?: string;
              [k: string]: unknown;
            }
          | string;
        tiers?: {
          flat_amount?: number;
          flat_amount_decimal?: string;
          unit_amount?: number;
          unit_amount_decimal?: string;
          up_to: "inf" | number;
          [k: string]: unknown;
        }[];
        tiers_mode?: "graduated" | "volume";
        transform_usage?: {
          divide_by: number;
          round: "down" | "up";
          [k: string]: unknown;
        };
        trial_period_days?: number;
        usage_type?: "licensed" | "metered";
      }
    ) {
      const url = new URL(`https://api.stripe.com/v1/plans`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
          Authorization: "Bearer " + auth.token,
        },
        body: encodeParams(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    
    function encodeParams(o: any) {
      function iter(o: any, path: string) {
        if (Array.isArray(o)) {
          o.forEach(function (a) {
            iter(a, path + "[]");
          });
          return;
        }
        if (o !== null && typeof o === "object") {
          Object.keys(o).forEach(function (k) {
            iter(o[k], path + "[" + k + "]");
          });
          return;
        }
        data.push(path + "=" + o);
      }
      const data: string[] = [];
      Object.keys(o).forEach(function (k) {
        if (o[k] !== undefined) {
          iter(o[k], k);
        }
      });
      return new URLSearchParams(data.join("&"));
    }
    

    Submitted by hugo697 368 days ago

  • nativets
    type Stripe = {
      token: string;
    };
    /**
     * Post plans
     * You can now model subscriptions more flexibly using the Prices API. It replaces the Plans API and is backwards compatible to simplify your migration.
     */
    export async function main(
      auth: Stripe,
      body: {
        active?: boolean;
        aggregate_usage?: "last_during_period" | "last_ever" | "max" | "sum";
        amount?: number;
        amount_decimal?: string;
        billing_scheme?: "per_unit" | "tiered";
        currency: string;
        expand?: string[];
        id?: string;
        interval: "day" | "month" | "week" | "year";
        interval_count?: number;
        metadata?: { [k: string]: string } | "";
        nickname?: string;
        product?:
          | {
              active?: boolean;
              id?: string;
              metadata?: { [k: string]: string };
              name: string;
              statement_descriptor?: string;
              tax_code?: string;
              unit_label?: string;
              [k: string]: unknown;
            }
          | string;
        tiers?: {
          flat_amount?: number;
          flat_amount_decimal?: string;
          unit_amount?: number;
          unit_amount_decimal?: string;
          up_to: "inf" | number;
          [k: string]: unknown;
        }[];
        tiers_mode?: "graduated" | "volume";
        transform_usage?: {
          divide_by: number;
          round: "down" | "up";
          [k: string]: unknown;
        };
        trial_period_days?: number;
        usage_type?: "licensed" | "metered";
      }
    ) {
      const url = new URL(`https://api.stripe.com/v1/plans`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
          Authorization: "Bearer " + auth.token,
        },
        body: encodeParams(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    
    function encodeParams(o: any) {
      function iter(o: any, path: string) {
        if (Array.isArray(o)) {
          o.forEach(function (a) {
            iter(a, path + "[]");
          });
          return;
        }
        if (o !== null && typeof o === "object") {
          Object.keys(o).forEach(function (k) {
            iter(o[k], path + "[" + k + "]");
          });
          return;
        }
        data.push(path + "=" + o);
      }
      const data: string[] = [];
      Object.keys(o).forEach(function (k) {
        if (o[k] !== undefined) {
          iter(o[k], k);
        }
      });
      return new URLSearchParams(data.join("&"));
    }
    

    Submitted by hugo697 795 days ago

  • nativets
    type Stripe = {
      token: string;
    };
    /**
     * Post plans
     * <p>You can now model subscriptions more flexibly using the <a href="#prices">Prices API</a>. It replaces the Plans API and is backwards compatible to simplify your migration.</p>
     */
    export async function main(
      auth: Stripe,
      body: {
        active?: boolean;
        aggregate_usage?: "last_during_period" | "last_ever" | "max" | "sum";
        amount?: number;
        amount_decimal?: string;
        billing_scheme?: "per_unit" | "tiered";
        currency: string;
        expand?: string[];
        id?: string;
        interval: "day" | "month" | "week" | "year";
        interval_count?: number;
        metadata?: { [k: string]: string } | "";
        nickname?: string;
        product?:
          | {
              active?: boolean;
              id?: string;
              metadata?: { [k: string]: string };
              name: string;
              statement_descriptor?: string;
              tax_code?: string;
              unit_label?: string;
              [k: string]: unknown;
            }
          | string;
        tiers?: {
          flat_amount?: number;
          flat_amount_decimal?: string;
          unit_amount?: number;
          unit_amount_decimal?: string;
          up_to: "inf" | number;
          [k: string]: unknown;
        }[];
        tiers_mode?: "graduated" | "volume";
        transform_usage?: {
          divide_by: number;
          round: "down" | "up";
          [k: string]: unknown;
        };
        trial_period_days?: number;
        usage_type?: "licensed" | "metered";
      }
    ) {
      const url = new URL(`https://api.stripe.com/v1/plans`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
          Authorization: "Bearer " + auth.token,
        },
        body: encodeParams(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    
    function encodeParams(o: any) {
      function iter(o: any, path: string) {
        if (Array.isArray(o)) {
          o.forEach(function (a) {
            iter(a, path + "[]");
          });
          return;
        }
        if (o !== null && typeof o === "object") {
          Object.keys(o).forEach(function (k) {
            iter(o[k], path + "[" + k + "]");
          });
          return;
        }
        data.push(path + "=" + o);
      }
      const data: string[] = [];
      Object.keys(o).forEach(function (k) {
        if (o[k] !== undefined) {
          iter(o[k], k);
        }
      });
      return new URLSearchParams(data.join("&"));
    }
    

    Submitted by hugo697 922 days ago