Edits history of script submission #12550 for ' Cancel payment (mollie)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Mollie = {
      token: string;
    };
    /**
     * Cancel payment
     * Depending on the payment method, you may be able to cancel a payment for a certain amount of time — usually until the next business day or as long as the payment status is open.
    
    Payments may also be canceled manually from the Mollie Dashboard.
    
    The `isCancelable` property on the [Payment object](get-payment) will indicate if the payment can be canceled.
    
    > 🔑 Access with
    >
    > API key
    >
    > Access token with **payments.write**
     */
    export async function main(
      auth: Mollie,
      paymentId: string,
      testmode: string | undefined,
    ) {
      const url = new URL(`https://api.mollie.com/v2/payments/${paymentId}`);
      for (const [k, v] of [["testmode", testmode]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "DELETE",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.text();
    }
    

    Submitted by hugo697 428 days ago