Edits history of script submission #20632 for ' CreateTerminalAction (square)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Square = {
      token: string;
    };
    /**
     * CreateTerminalAction
     * Creates a Terminal action request and sends it to the specified device.
     */
    export async function main(
      auth: Square,
      body: {
        idempotency_key: string;
        action: {
          id?: string;
          device_id?: string;
          deadline_duration?: string;
          status?: string;
          cancel_reason?: "BUYER_CANCELED" | "SELLER_CANCELED" | "TIMED_OUT";
          created_at?: string;
          updated_at?: string;
          app_id?: string;
          location_id?: string;
          type?:
            | "QR_CODE"
            | "PING"
            | "SAVE_CARD"
            | "SIGNATURE"
            | "CONFIRMATION"
            | "RECEIPT"
            | "DATA_COLLECTION"
            | "SELECT";
          qr_code_options?: {
            title: string;
            body: string;
            barcode_contents: string;
          };
          save_card_options?: {
            customer_id: string;
            card_id?: string;
            reference_id?: string;
          };
          signature_options?: {
            title: string;
            body: string;
            signature?: { image_type?: string; data?: string }[];
          };
          confirmation_options?: {
            title: string;
            body: string;
            agree_button_text: string;
            disagree_button_text?: string;
            decision?: { has_agreed?: false | true };
          };
          receipt_options?: {
            payment_id: string;
            print_only?: false | true;
            is_duplicate?: false | true;
          };
          data_collection_options?: {
            title: string;
            body: string;
            input_type: "EMAIL" | "PHONE_NUMBER";
            collected_data?: { input_text?: string };
          };
          select_options?: {
            title: string;
            body: string;
            options: { reference_id: string; title: string }[];
            selected_option?: { reference_id: string; title: string };
          };
          device_metadata?: {
            battery_percentage?: string;
            charging_state?: string;
            location_id?: string;
            merchant_id?: string;
            network_connection_type?: string;
            payment_region?: string;
            serial_number?: string;
            os_version?: string;
            app_version?: string;
            wifi_network_name?: string;
            wifi_network_strength?: string;
            ip_address?: string;
          };
          await_next_action?: false | true;
          await_next_action_duration?: string;
        };
      },
    ) {
      const url = new URL(`https://connect.squareup.com/v2/terminals/actions`);
    
      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