0

Cancel payment

by
Published Apr 8, 2025

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**

Script mollie Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Mollie = {
3
  token: string;
4
};
5
/**
6
 * Cancel payment
7
 * 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.
8

9
Payments may also be canceled manually from the Mollie Dashboard.
10

11
The `isCancelable` property on the [Payment object](get-payment) will indicate if the payment can be canceled.
12

13
> 🔑 Access with
14
>
15
> API key
16
>
17
> Access token with **payments.write**
18
 */
19
export async function main(
20
  auth: Mollie,
21
  paymentId: string,
22
  testmode: string | undefined,
23
) {
24
  const url = new URL(`https://api.mollie.com/v2/payments/${paymentId}`);
25
  for (const [k, v] of [["testmode", testmode]]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "DELETE",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.text();
42
}
43