PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / Services / Mollie / MollieClient.php
ameliabooking / src / Infrastructure / Services / Mollie Last commit date
MollieClient.php 2 months ago MollieResponse.php 2 months ago
MollieClient.php
145 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\Services\Mollie;
9
10 /**
11 * Class MollieClient
12 *
13 * @package AmeliaBooking\Infrastructure\Services\Mollie
14 */
15 class MollieClient
16 {
17 private const BASE_URL = 'https://api.mollie.com/v2';
18
19 /**
20 * @var string
21 */
22 private string $apiKey;
23
24 /**
25 * MollieClient constructor.
26 */
27 public function __construct(string $apiKey)
28 {
29 $this->apiKey = $apiKey;
30 }
31
32 /**
33 * Create a new payment.
34 *
35 * @throws \RuntimeException On cURL or JSON error.
36 */
37 public function createPayment(array $data): array
38 {
39 return $this->request('POST', '/payments', $data);
40 }
41
42 /**
43 * Retrieve an existing payment.
44 *
45 * @throws \RuntimeException On cURL or JSON error.
46 */
47 public function getPayment(string $id): array
48 {
49 return $this->request('GET', '/payments/' . urlencode($id));
50 }
51
52 /**
53 * Create a payment link.
54 *
55 * @throws \RuntimeException On cURL or JSON error.
56 */
57 public function createPaymentLink(array $data): array
58 {
59 return $this->request('POST', '/payment-links', $data);
60 }
61
62 /**
63 * Retrieve an existing payment link.
64 *
65 * @throws \RuntimeException On cURL or JSON error.
66 */
67 public function getPaymentLink(string $id): array
68 {
69 return $this->request('GET', '/payment-links/' . urlencode($id));
70 }
71
72 /**
73 * Create a refund for a payment.
74 *
75 * @throws \RuntimeException On cURL or JSON error.
76 */
77 public function createRefund(string $paymentId, array $data): array
78 {
79 return $this->request('POST', '/payments/' . urlencode($paymentId) . '/refunds', $data);
80 }
81
82 /**
83 * Convenience helper: fetch the `status` string of a payment.
84 */
85 public function getPaymentStatus(string $id): ?string
86 {
87 $response = $this->getPayment($id);
88
89 return $response['status'] ?? null;
90 }
91
92 /**
93 * Send an HTTP request to the Mollie API.
94 *
95 * @throws \RuntimeException On cURL failure or invalid JSON.
96 */
97 private function request(string $method, string $endpoint, array $body = []): array
98 {
99 $ch = curl_init();
100
101 $options = [
102 CURLOPT_URL => self::BASE_URL . $endpoint,
103 CURLOPT_RETURNTRANSFER => true,
104 CURLOPT_ENCODING => '',
105 CURLOPT_MAXREDIRS => 5,
106 CURLOPT_TIMEOUT => 30,
107 CURLOPT_FOLLOWLOCATION => false,
108 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
109 CURLOPT_CUSTOMREQUEST => $method,
110 CURLOPT_SSL_VERIFYPEER => true,
111 CURLOPT_HTTPHEADER => [
112 'Authorization: Bearer ' . $this->apiKey,
113 'Content-Type: application/json',
114 'Accept: application/json',
115 ],
116 ];
117
118 if ($method !== 'GET' && !empty($body)) {
119 $options[CURLOPT_POSTFIELDS] = json_encode($body);
120 }
121
122 curl_setopt_array($ch, $options);
123
124 $raw = curl_exec($ch);
125 $httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
126 $curlError = curl_error($ch);
127
128 if ($curlError) {
129 throw new \RuntimeException('Mollie API cURL error: ' . $curlError);
130 }
131
132 $decoded = json_decode($raw, true);
133
134 if (!is_array($decoded)) {
135 throw new \RuntimeException(
136 'Invalid Mollie API response (HTTP ' . $httpCode . '): ' . substr((string)$raw, 0, 200)
137 );
138 }
139
140 $decoded['_http_code'] = $httpCode;
141
142 return $decoded;
143 }
144 }
145