Edits history of script submission #20807 for ' SwapPlan (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * SwapPlan
     * Schedules a `SWAP_PLAN` action to swap a subscription plan variation in an existing subscription. 
    For more information, see [Swap Subscription Plan Variations](https://developer.squareup.com/docs/subscriptions-api/swap-plan-variations).
     */
    export async function main(
      auth: Square,
      subscription_id: string,
      body: {
        new_plan_variation_id?: string;
        phases?: { ordinal: number; order_template_id?: string }[];
      },
    ) {
      const url = new URL(
        `https://connect.squareup.com/v2/subscriptions/${subscription_id}/swap-plan`,
      );
    
      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