Edits history of script submission #20831 for ' UpdateSubscription (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * UpdateSubscription
     * Updates a subscription by modifying or clearing `subscription` field values.
    To clear a field, set its value to `null`.
     */
    export async function main(
      auth: Square,
      subscription_id: string,
      body: {
        subscription?: {
          id?: string;
          location_id?: string;
          plan_variation_id?: string;
          customer_id?: string;
          start_date?: string;
          canceled_date?: string;
          charged_through_date?: string;
          status?: "PENDING" | "ACTIVE" | "CANCELED" | "DEACTIVATED" | "PAUSED";
          tax_percentage?: string;
          invoice_ids?: string[];
          price_override_money?: {
            amount?: number;
            currency?:
              | "UNKNOWN_CURRENCY"
              | "AED"
              | "AFN"
              | "ALL"
              | "AMD"
              | "ANG"
              | "AOA"
              | "ARS"
              | "AUD"
              | "AWG"
              | "AZN"
              | "BAM"
              | "BBD"
              | "BDT"
              | "BGN"
              | "BHD"
              | "BIF"
              | "BMD"
              | "BND"
              | "BOB"
              | "BOV"
              | "BRL"
              | "BSD"
              | "BTN"
              | "BWP"
              | "BYR"
              | "BZD"
              | "CAD"
              | "CDF"
              | "CHE"
              | "CHF"
              | "CHW"
              | "CLF"
              | "CLP"
              | "CNY"
              | "COP"
              | "COU"
              | "CRC"
              | "CUC"
              | "CUP"
              | "CVE"
              | "CZK"
              | "DJF"
              | "DKK"
              | "DOP"
              | "DZD"
              | "EGP"
              | "ERN"
              | "ETB"
              | "EUR"
              | "FJD"
              | "FKP"
              | "GBP"
              | "GEL"
              | "GHS"
              | "GIP"
              | "GMD"
              | "GNF"
              | "GTQ"
              | "GYD"
              | "HKD"
              | "HNL"
              | "HRK"
              | "HTG"
              | "HUF"
              | "IDR"
              | "ILS"
              | "INR"
              | "IQD"
              | "IRR"
              | "ISK"
              | "JMD"
              | "JOD"
              | "JPY"
              | "KES"
              | "KGS"
              | "KHR"
              | "KMF"
              | "KPW"
              | "KRW"
              | "KWD"
              | "KYD"
              | "KZT"
              | "LAK"
              | "LBP"
              | "LKR"
              | "LRD"
              | "LSL"
              | "LTL"
              | "LVL"
              | "LYD"
              | "MAD"
              | "MDL"
              | "MGA"
              | "MKD"
              | "MMK"
              | "MNT"
              | "MOP"
              | "MRO"
              | "MUR"
              | "MVR"
              | "MWK"
              | "MXN"
              | "MXV"
              | "MYR"
              | "MZN"
              | "NAD"
              | "NGN"
              | "NIO"
              | "NOK"
              | "NPR"
              | "NZD"
              | "OMR"
              | "PAB"
              | "PEN"
              | "PGK"
              | "PHP"
              | "PKR"
              | "PLN"
              | "PYG"
              | "QAR"
              | "RON"
              | "RSD"
              | "RUB"
              | "RWF"
              | "SAR"
              | "SBD"
              | "SCR"
              | "SDG"
              | "SEK"
              | "SGD"
              | "SHP"
              | "SLL"
              | "SLE"
              | "SOS"
              | "SRD"
              | "SSP"
              | "STD"
              | "SVC"
              | "SYP"
              | "SZL"
              | "THB"
              | "TJS"
              | "TMT"
              | "TND"
              | "TOP"
              | "TRY"
              | "TTD"
              | "TWD"
              | "TZS"
              | "UAH"
              | "UGX"
              | "USD"
              | "USN"
              | "USS"
              | "UYI"
              | "UYU"
              | "UZS"
              | "VEF"
              | "VND"
              | "VUV"
              | "WST"
              | "XAF"
              | "XAG"
              | "XAU"
              | "XBA"
              | "XBB"
              | "XBC"
              | "XBD"
              | "XCD"
              | "XDR"
              | "XOF"
              | "XPD"
              | "XPF"
              | "XPT"
              | "XTS"
              | "XXX"
              | "YER"
              | "ZAR"
              | "ZMK"
              | "ZMW"
              | "BTC"
              | "XUS";
          };
          version?: number;
          created_at?: string;
          card_id?: string;
          timezone?: string;
          source?: { name?: string };
          actions?: {
            id?: string;
            type?:
              | "CANCEL"
              | "PAUSE"
              | "RESUME"
              | "SWAP_PLAN"
              | "CHANGE_BILLING_ANCHOR_DATE";
            effective_date?: string;
            monthly_billing_anchor_date?: number;
            phases?: {
              uid?: string;
              ordinal?: number;
              order_template_id?: string;
              plan_phase_uid?: string;
            }[];
            new_plan_variation_id?: string;
          }[];
          monthly_billing_anchor_date?: number;
          phases?: {
            uid?: string;
            ordinal?: number;
            order_template_id?: string;
            plan_phase_uid?: string;
          }[];
        };
      },
    ) {
      const url = new URL(
        `https://connect.squareup.com/v2/subscriptions/${subscription_id}`,
      );
    
      const response = await fetch(url, {
        method: "PUT",
        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