0

Create payment

by
Published Apr 8, 2025

Payment creation is elemental to the Mollie API: this is where most payment implementations start off.

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 payment
7
 * Payment creation is elemental to the Mollie API: this is where most payment implementations start off.
8
 */
9
export async function main(
10
  auth: Mollie,
11
  include: "details.qrCode" | undefined,
12
  body: {
13
    resource?: string;
14
    id?: string;
15
    mode?: string;
16
    description: string;
17
    amount: { currency: string; value: string };
18
    amountRefunded?: { currency: string; value: string };
19
    amountRemaining?: { currency: string; value: string };
20
    amountCaptured?: { currency: string; value: string };
21
    amountChargedBack?: { currency: string; value: string };
22
    settlementAmount?: { currency: string; value: string };
23
    redirectUrl: string;
24
    cancelUrl?: string;
25
    webhookUrl?: string;
26
    lines?: {
27
      type?: string;
28
      description: string;
29
      quantity: number;
30
      quantityUnit?: string;
31
      unitPrice: { currency: string; value: string };
32
      discountAmount?: { currency: string; value: string };
33
      recurring?: {
34
        description?: string;
35
        interval: string;
36
        amount?: { currency: string; value: string };
37
        times?: number;
38
        startDate?: string;
39
      };
40
      totalAmount: { currency: string; value: string };
41
      vatRate?: string;
42
      vatAmount?: { currency: string; value: string };
43
      sku?: string;
44
      categories?: "meal" | "eco" | "gift" | "sport_culture"[];
45
      imageUrl?: string;
46
      productUrl?: string;
47
    }[];
48
    billingAddress?: {
49
      title?: string;
50
      givenName?: string;
51
      familyName?: string;
52
      organizationName?: string;
53
      streetAndNumber?: string;
54
      streetAdditional?: string;
55
      postalCode?: string;
56
      email?: string;
57
      phone?: string;
58
      city?: string;
59
      region?: string;
60
      country?: string;
61
    };
62
    shippingAddress?: {
63
      title?: string;
64
      givenName?: string;
65
      familyName?: string;
66
      organizationName?: string;
67
      streetAndNumber?: string;
68
      streetAdditional?: string;
69
      postalCode?: string;
70
      email?: string;
71
      phone?: string;
72
      city?: string;
73
      region?: string;
74
      country?: string;
75
    };
76
    locale?: string;
77
    countryCode?: string;
78
    method?: string;
79
    issuer?: string;
80
    restrictPaymentMethodsToCountry?: string;
81
    metadata?: string | {} | string[];
82
    captureMode?: string;
83
    captureDelay?: string;
84
    captureBefore?: string;
85
    applicationFee?: {
86
      amount?: { currency: string; value: string };
87
      description?: string;
88
    };
89
    routing?: {
90
      resource?: string;
91
      id?: string;
92
      mode?: string;
93
      amount?: { currency: string; value: string };
94
      destination?: { type?: string; organizationId?: string };
95
      releaseDate?: string;
96
      _links?: {
97
        self?: { href?: string; type?: string };
98
        payment?: { href?: string; type?: string };
99
      };
100
    }[];
101
    sequenceType?: string;
102
    subscriptionId?: string;
103
    mandateId?: string;
104
    customerId?: string;
105
    profileId?: string;
106
    settlementId?: string;
107
    orderId?: string;
108
    status?: string;
109
    statusReason?: { code: string; message: string };
110
    isCancelable?: false | true;
111
    details?: {};
112
    createdAt?: string;
113
    authorizedAt?: string;
114
    paidAt?: string;
115
    canceledAt?: string;
116
    expiresAt?: string;
117
    expiredAt?: string;
118
    failedAt?: string;
119
    testmode?: false | true;
120
    _links?: {
121
      self?: { href?: string; type?: string };
122
      checkout?: { href?: string; type?: string };
123
      mobileAppCheckout?: { href?: string; type?: string };
124
      changePaymentState?: { href?: string; type?: string };
125
      dashboard?: { href?: string; type?: string };
126
      refunds?: { href?: string; type?: string };
127
      chargebacks?: { href?: string; type?: string };
128
      captures?: { href?: string; type?: string };
129
      settlement?: { href?: string; type?: string };
130
      customer?: { href?: string; type?: string };
131
      mandate?: { href?: string; type?: string };
132
      subscription?: { href?: string; type?: string };
133
      order?: { href?: string; type?: string };
134
      terminal?: { href?: string; type?: string };
135
      documentation?: { href?: string; type?: string };
136
    };
137
  },
138
) {
139
  const url = new URL(`https://api.mollie.com/v2/payments`);
140
  for (const [k, v] of [["include", include]]) {
141
    if (v !== undefined && v !== "" && k !== undefined) {
142
      url.searchParams.append(k, v);
143
    }
144
  }
145
  const response = await fetch(url, {
146
    method: "POST",
147
    headers: {
148
      "Content-Type": "application/json",
149
      Authorization: "Bearer " + auth.token,
150
    },
151
    body: JSON.stringify(body),
152
  });
153
  if (!response.ok) {
154
    const text = await response.text();
155
    throw new Error(`${response.status} ${text}`);
156
  }
157
  return await response.text();
158
}
159