0

Create order refund

by
Published Apr 8, 2025

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

Script mollie Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Mollie = {
3
  token: string;
4
};
5
/**
6
 * Create order refund
7
 * When using the Orders API, refunds should be made for a specific order.
8

9
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.
10

11
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.
12

13
> 🔑 Access with
14
>
15
> API key
16
>
17
> Access token with **refunds.write**
18
 */
19
export async function main(
20
  auth: Mollie,
21
  orderId: string,
22
  body: {
23
    resource?: string;
24
    id?: string;
25
    mode?: string;
26
    description?: string;
27
    amount?: { currency: string; value: string };
28
    settlementAmount?: { currency: string; value: string };
29
    metadata?: string | {} | string[];
30
    orderId?: string;
31
    settlementId?: string;
32
    status?: string;
33
    createdAt?: string;
34
    externalReference?: { type?: string; id?: string };
35
    testmode?: false | true;
36
    lines: {
37
      resource?: string;
38
      id?: string;
39
      orderId?: string;
40
      name?: string;
41
      sku?: string;
42
      type?:
43
        | "physical"
44
        | "digital"
45
        | "discount"
46
        | "shipping_fee"
47
        | "store_credit"
48
        | "gift_card"
49
        | "surcharge";
50
      status?:
51
        | "created"
52
        | "authorized"
53
        | "paid"
54
        | "canceled"
55
        | "shipping"
56
        | "completed";
57
      metadata?: string | string[] | {};
58
      isCancelable?: false | true;
59
      quantity?: number;
60
      quantityShipped?: number;
61
      amountShipped?: { currency: string; value: string };
62
      quantityRefunded?: number;
63
      amountRefunded?: { currency: string; value: string };
64
      quantityCanceled?: number;
65
      amountCanceled?: { currency: string; value: string };
66
      amount?: { currency: string; value: string };
67
      shippableQuantity?: number;
68
      refundableQuantity?: number;
69
      cancelableQuantity?: number;
70
      unitPrice?: { currency: string; value: string };
71
      totalAmount?: { currency: string; value: string };
72
      vatRate?: string;
73
      vatAmount?: { currency: string; value: string };
74
      createdAt?: string;
75
      discountedAmount?: { currency: string; value: string };
76
    }[];
77
    _links?: {
78
      self?: { href?: string; type?: string };
79
      order?: { href?: string; type?: string };
80
      settlement?: { href?: string; type?: string };
81
      documentation?: { href?: string; type?: string };
82
    };
83
  },
84
) {
85
  const url = new URL(`https://api.mollie.com/v2/orders/${orderId}/refunds`);
86

87
  const response = await fetch(url, {
88
    method: "POST",
89
    headers: {
90
      "Content-Type": "application/json",
91
      Authorization: "Bearer " + auth.token,
92
    },
93
    body: JSON.stringify(body),
94
  });
95
  if (!response.ok) {
96
    const text = await response.text();
97
    throw new Error(`${response.status} ${text}`);
98
  }
99
  return await response.text();
100
}
101