Edits history of script submission #12801 for ' Update or cancel tracking information for a PayPal order (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;
    }
    /**
     * Update or cancel tracking information for a PayPal order
     * Updates or cancels the tracking information for a PayPal order, by ID. Updatable attributes or objects:AttributeOpNotesitemsreplaceUsing replace op for items will replace the entire items object with the value sent in request.notify_payerreplace, addstatusreplaceOnly patching status to CANCELLED is currently supported.
     */
    export async function main(
      auth: Paypal,
      id: string,
      tracker_id: string,
      body: {
        op: "add" | "remove" | "replace" | "move" | "copy" | "test";
        path?: string;
        value?: {};
        from?: string;
      }[],
    ) {
      const token = await getToken(auth);
      const url = new URL(
        `https://api-m.paypal.com/v2/checkout/orders/${id}/trackers/${tracker_id}`,
      );
    
      const response = await fetch(url, {
        method: "PATCH",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + 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 428 days ago

  • bun
    //native
    type Paypal = {
      token: string;
    };
    /**
     * Update or cancel tracking information for a PayPal order
     * Updates or cancels the tracking information for a PayPal order, by ID. Updatable attributes or objects:AttributeOpNotesitemsreplaceUsing replace op for items will replace the entire items object with the value sent in request.notify_payerreplace, addstatusreplaceOnly patching status to CANCELLED is currently supported.
     */
    export async function main(
      auth: Paypal,
      id: string,
      tracker_id: string,
      body: {
        op: "add" | "remove" | "replace" | "move" | "copy" | "test";
        path?: string;
        value?: {};
        from?: string;
      }[],
    ) {
      const url = new URL(
        `https://api-m.paypal.com/v2/checkout/orders/${id}/trackers/${tracker_id}`,
      );
    
      const response = await fetch(url, {
        method: "PATCH",
        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 428 days ago