Edits history of script submission #12718 for ' Acknowledge returned item (paypal)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Paypal = {
      clientId: string;
      clientSecret: string;
    };
    
    async function getToken(auth: Paypal): Promise<string> {
      const url = new URL(`https://api-m.paypal.com/v1/oauth2/token`);
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Basic ${btoa(`${auth.clientId}:${auth.clientSecret}`)}`,
        },
        body: new URLSearchParams({
          grant_type: "client_credentials",
        }),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`Could not get token: ${response.status} ${text}`);
      }
      const json = await response.json();
      return json.access_token;
    }
    type Base64 = string;
    /**
     * Acknowledge returned item
     * Acknowledges that the customer returned an item for a dispute, by ID. A merchant can make this request for disputes with the `MERCHANDISE_OR_SERVICE_NOT_AS_DESCRIBED` reason. Allowed acknowledgement_type values for the request is available in dispute details allowed response options object. For constraints and rules regarding documents, see documents.
     */
    export async function main(
      auth: Paypal,
      id: string,
      body: {
        acknowledgement_document?: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
      },
    ) {
      const token = await getToken(auth);
      const url = new URL(
        `https://api-m.paypal.com/v1/customer/disputes/${id}/acknowledge-return-item`,
      );
    
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined) {
          if (["acknowledgement_document"].includes(k)) {
            const { base64, type, name } = v as {
              base64: Base64;
              type: string;
              name: string;
            };
            formData.append(
              k,
              new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
                type,
              }),
              name,
            );
          } else {
            formData.append(k, String(v));
          }
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + token,
        },
        body: formData,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago

  • bun
    //native
    type Paypal = {
      token: string;
    };
    type Base64 = string;
    /**
     * Acknowledge returned item
     * Acknowledges that the customer returned an item for a dispute, by ID. A merchant can make this request for disputes with the `MERCHANDISE_OR_SERVICE_NOT_AS_DESCRIBED` reason. Allowed acknowledgement_type values for the request is available in dispute details allowed response options object. For constraints and rules regarding documents, see documents.
     */
    export async function main(
      auth: Paypal,
      id: string,
      body: {
        acknowledgement_document?: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
      },
    ) {
      const url = new URL(
        `https://api-m.paypal.com/v1/customer/disputes/${id}/acknowledge-return-item`,
      );
    
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined) {
          if (["acknowledgement_document"].includes(k)) {
            const { base64, type, name } = v as {
              base64: Base64;
              type: string;
              name: string;
            };
            formData.append(
              k,
              new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
                type,
              }),
              name,
            );
          } else {
            formData.append(k, String(v));
          }
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: formData,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago