Skip to content

Commit 9ae23ea

Browse files
committed
Enhance request header management by ensuring merchant ID is set for relevant requests and improving type safety for header values
1 parent 924fba1 commit 9ae23ea

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/Client.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ private function setRequestHeader($name, $value)
5050
if (!is_array($this->request_headers)) {
5151
$this->resetRequestHeaders();
5252
}
53-
$name = trim($name);
54-
$value = trim($value);
53+
$name = trim((string) $name);
54+
$value = trim((string) $value);
5555
if ($value === '' || $name === '') {
5656
return;
5757
}
@@ -119,6 +119,7 @@ public function startSolicitation($order)
119119
}
120120

121121
$this->initCurl($this->_endpoint . '/orders');
122+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $order['merchant']['id'] ?? '');
122123
$this->verbThePayload('POST', array('order' => $order));
123124
$this->dealWithResponse();
124125
curl_close($this->ch);
@@ -136,16 +137,24 @@ public function qualifyForSolicitation($order)
136137
return true;
137138
}
138139

139-
public function getIdentificationForm($uri, $options = array())
140+
/**
141+
* Retrieves the identification form for a given URI and options.
142+
*
143+
* @param string $uri The URI to retrieve the identification form from.
144+
* @param array $options An associative array of options for the form.
145+
* @param string $merchant_id The merchant ID to be used in the request.
146+
*
147+
* @return string The HTML content of the identification form.
148+
*/
149+
public function getIdentificationForm($uri, $options = array(), $merchant_id = '')
140150
{
141151
$options["product"] = array_key_exists('product', $options) ? $options["product"] : "i1";
142152
$options["ajax"] = (isset($options["ajax"]) && $options["ajax"]) ? "true" : "false";
143153
$this->initCurl($uri . '/form_v2' . '?' . http_build_query($options));
144154
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
145155

146156
$this->setRequestHeader(self::HEADER_ACCEPT, self::TYPE_HTML);
147-
// TODO: Set merchant ID
148-
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
157+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $merchant_id);
149158

150159
$this->sendRequest();
151160
$this->dealWithResponse();
@@ -159,6 +168,8 @@ public function sendIdentificationForm($uri, $options = array())
159168
$options["product_code"] = $options["product"];
160169
$options["channel"] = array_key_exists('channel', $options) ? $options["channel"] : "sms";
161170
$this->initCurl($uri . '/form_deliveries');
171+
// TODO: Set merchant ID?
172+
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
162173
$this->verbThePayload('POST', $options);
163174
$this->dealWithResponse();
164175
curl_close($this->ch);
@@ -172,6 +183,8 @@ public function startCards($order)
172183
}
173184

174185
$this->initCurl($this->_endpoint . '/cards');
186+
// TODO: Set merchant ID?
187+
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
175188
$this->verbThePayload('POST', array('order' => $order));
176189
$this->dealWithResponse();
177190
curl_close($this->ch);
@@ -183,7 +196,7 @@ public function getCardsForm($uri, $options = array())
183196
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
184197

185198
$this->setRequestHeader(self::HEADER_ACCEPT, self::TYPE_HTML);
186-
// TODO: Set merchant ID
199+
// TODO: Set merchant ID?
187200
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
188201

189202
$this->sendRequest();
@@ -204,21 +217,20 @@ public function qualifyForstartCards($order)
204217

205218
public function getMerchantPaymentMethods($merchant)
206219
{
207-
$this->getPaymentMethods($this->_endpoint . '/merchants/' . $merchant);
220+
$this->getPaymentMethods($this->_endpoint . '/merchants/' . $merchant, array(), $merchant);
208221
}
209222

210-
public function getPaymentMethods($uri, $options = array())
223+
public function getPaymentMethods($uri, $options = array(), $merchant_id = '')
211224
{
212225
if (!preg_match('!^https?://!', $uri)) {
213226
$uri = $this->_endpoint . '/orders/' . $uri;
214227
}
215228
$this->initCurl($uri . '/payment_methods' . (count($options) > 0 ? '?' . http_build_query($options) : ''));
216-
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
217229

218230
$this->setRequestHeader(self::HEADER_ACCEPT, self::TYPE_JSON);
219-
// TODO: Set merchant ID
220-
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
231+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $merchant_id);
221232

233+
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
222234
$this->sendRequest();
223235
$this->dealWithResponse();
224236
curl_close($this->ch);
@@ -230,8 +242,7 @@ public function getAvailableDisbursements($merchant)
230242
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
231243

232244
$this->setRequestHeader(self::HEADER_ACCEPT, self::TYPE_JSON);
233-
// TODO: Set merchant ID
234-
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
245+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $merchant);
235246

236247
$this->sendRequest();
237248
$this->dealWithResponse();
@@ -244,8 +255,10 @@ public function getDisbursementDetails($path)
244255
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
245256

246257
$this->setRequestHeader(self::HEADER_ACCEPT, self::TYPE_JSON);
247-
// TODO: Set merchant ID
248-
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
258+
259+
if (preg_match('~merchants/([^/]+)/disbursements~', $path, $matches)) {
260+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $matches[1]);
261+
}
249262

250263
$this->sendRequest();
251264
$this->dealWithResponse();
@@ -276,6 +289,7 @@ public function updateOrder($uri, $order, $verb = 'PUT')
276289
$uri = $this->_endpoint . '/orders/' . $uri;
277290
}
278291
$this->initCurl($uri);
292+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $order['merchant']['id'] ?? '');
279293
$this->verbThePayload($verb, array('order' => $order));
280294
$this->dealWithResponse();
281295
if ($this->status == 409) {
@@ -287,6 +301,7 @@ public function updateOrder($uri, $order, $verb = 'PUT')
287301
public function sendDeliveryReport($delivery_report)
288302
{
289303
$this->initCurl($this->_endpoint . '/delivery_reports');
304+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $delivery_report['merchant']['id'] ?? '');
290305
$this->verbThePayload('POST', array('delivery_report' => $delivery_report));
291306
$this->dealWithResponse();
292307
curl_close($this->ch);
@@ -298,6 +313,7 @@ public function orderUpdate($order)
298313
'/merchants/' . urlencode($order['merchant']['id']) .
299314
'/orders/' . urlencode($order['merchant_reference']['order_ref_1']);
300315
$this->initCurl($uri);
316+
$this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, $order['merchant']['id'] ?? '');
301317
$this->verbThePayload('PUT', array('order' => $order));
302318
$this->dealWithResponse();
303319
if ($this->status == 409) {
@@ -392,8 +408,6 @@ private function verbThePayload($verb, $payload)
392408
$this->setRequestHeader(self::HEADER_ACCEPT, self::TYPE_JSON);
393409
$this->setRequestHeader(self::HEADER_CONTENT_TYPE, self::TYPE_JSON);
394410
$this->setRequestHeader(self::HEADER_CONTENT_LENGTH, (string) strlen($data_string));
395-
// TODO: Set merchant ID
396-
// $this->setRequestHeader(self::HEADER_SEQURA_MERCHANT_ID, '');
397411

398412
$this->sendRequest();
399413
}

0 commit comments

Comments
 (0)