Edits history of script submission #12561 for ' Create order refund (mollie)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Mollie = {
      token: string;
    };
    /**
     * Create order refund
     * When using the Orders API, refunds should be made for a specific order.
    
    If you want to refund arbitrary amounts, however, you can also use the [Create payment refund endpoint](create-refund) by creating a refund on the payment itself.
    
    If an order line is still in the `authorized` state, it cannot be refunded. You should cancel it instead. Order lines that are `paid`, `shipping` or `completed` can be refunded.
    
    > 🔑 Access with
    >
    > API key
    >
    > Access token with **refunds.write**
     */
    export async function main(
      auth: Mollie,
      orderId: string,
      body: {
        resource?: string;
        id?: string;
        mode?: string;
        description?: string;
        amount?: { currency: string; value: string };
        settlementAmount?: { currency: string; value: string };
        metadata?: string | {} | string[];
        orderId?: string;
        settlementId?: string;
        status?: string;
        createdAt?: string;
        externalReference?: { type?: string; id?: string };
        testmode?: false | true;
        lines: {
          resource?: string;
          id?: string;
          orderId?: string;
          name?: string;
          sku?: string;
          type?:
            | "physical"
            | "digital"
            | "discount"
            | "shipping_fee"
            | "store_credit"
            | "gift_card"
            | "surcharge";
          status?:
            | "created"
            | "authorized"
            | "paid"
            | "canceled"
            | "shipping"
            | "completed";
          metadata?: string | string[] | {};
          isCancelable?: false | true;
          quantity?: number;
          quantityShipped?: number;
          amountShipped?: { currency: string; value: string };
          quantityRefunded?: number;
          amountRefunded?: { currency: string; value: string };
          quantityCanceled?: number;
          amountCanceled?: { currency: string; value: string };
          amount?: { currency: string; value: string };
          shippableQuantity?: number;
          refundableQuantity?: number;
          cancelableQuantity?: number;
          unitPrice?: { currency: string; value: string };
          totalAmount?: { currency: string; value: string };
          vatRate?: string;
          vatAmount?: { currency: string; value: string };
          createdAt?: string;
          discountedAmount?: { currency: string; value: string };
        }[];
        _links?: {
          self?: { href?: string; type?: string };
          order?: { href?: string; type?: string };
          settlement?: { href?: string; type?: string };
          documentation?: { href?: string; type?: string };
        };
      },
    ) {
      const url = new URL(`https://api.mollie.com/v2/orders/${orderId}/refunds`);
    
      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.text();
    }
    

    Submitted by hugo697 428 days ago