Edits history of script submission #12766 for ' Make offer to resolve dispute (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;
    }
    /**
     * Make offer to resolve dispute
     * Makes an offer to the other party to resolve a dispute, by ID. To make this call, the stage in the dispute lifecycle must be `INQUIRY`. If the customer accepts the offer, PayPal automatically makes a refund. Allowed offer_type values for the request is available in dispute details allowed response options object.
     */
    export async function main(
      auth: Paypal,
      id: string,
      body: {
        note: string;
        offer_amount?: { currency_code: string; value: string };
        return_shipping_address?: {
          address_line_1?: string;
          address_line_2?: string;
          address_line_3?: string;
          admin_area_4?: string;
          admin_area_3?: string;
          admin_area_2?: string;
          admin_area_1?: string;
          postal_code?: string;
          country_code: string;
          address_details?: {
            street_number?: string;
            street_name?: string;
            street_type?: string;
            delivery_service?: string;
            building_name?: string;
            sub_building?: string;
          };
        };
        invoice_id?: string;
        offer_type:
          | "REFUND"
          | "REFUND_WITH_RETURN"
          | "REFUND_WITH_REPLACEMENT"
          | "REPLACEMENT_WITHOUT_REFUND";
      },
    ) {
      const token = await getToken(auth);
      const url = new URL(
        `https://api-m.paypal.com/v1/customer/disputes/${id}/make-offer`,
      );
    
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined && v !== "") {
          formData.append(k, String(v));
        }
      }
      const response = await fetch(url, {
        method: "POST",
        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;
    };
    /**
     * Make offer to resolve dispute
     * Makes an offer to the other party to resolve a dispute, by ID. To make this call, the stage in the dispute lifecycle must be `INQUIRY`. If the customer accepts the offer, PayPal automatically makes a refund. Allowed offer_type values for the request is available in dispute details allowed response options object.
     */
    export async function main(
      auth: Paypal,
      id: string,
      body: {
        note: string;
        offer_amount?: { currency_code: string; value: string };
        return_shipping_address?: {
          address_line_1?: string;
          address_line_2?: string;
          address_line_3?: string;
          admin_area_4?: string;
          admin_area_3?: string;
          admin_area_2?: string;
          admin_area_1?: string;
          postal_code?: string;
          country_code: string;
          address_details?: {
            street_number?: string;
            street_name?: string;
            street_type?: string;
            delivery_service?: string;
            building_name?: string;
            sub_building?: string;
          };
        };
        invoice_id?: string;
        offer_type:
          | "REFUND"
          | "REFUND_WITH_RETURN"
          | "REFUND_WITH_REPLACEMENT"
          | "REPLACEMENT_WITHOUT_REFUND";
      },
    ) {
      const url = new URL(
        `https://api-m.paypal.com/v1/customer/disputes/${id}/make-offer`,
      );
    
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined && v !== "") {
          formData.append(k, String(v));
        }
      }
      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 428 days ago