Edits history of script submission #20630 for ' CreateSubscription (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * CreateSubscription
     * Enrolls a customer in a subscription.
     */
    export async function main(
      auth: Square,
      body: {
        idempotency_key?: string;
        location_id: string;
        plan_variation_id?: string;
        customer_id: string;
        start_date?: string;
        canceled_date?: string;
        tax_percentage?: 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";
        };
        card_id?: string;
        timezone?: string;
        source?: { name?: 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`);
    
      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