Skip to content

Commit 2695310

Browse files
committed
refactor - InterPos use http_build_query in Serializer instead of form_params when sending request
Eventually, I will remove form_params option from HttpClient everywhere. Currently, form_params is only used PayFlexV4Pos, but replacing form_params will take big changes, which is planned in v2
1 parent 65844b0 commit 2695310

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

src/Gateways/InterPos.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,17 @@ public function get3DFormData(array $order, string $paymentModel, string $txType
191191
protected function send($contents, string $txType, string $paymentModel, string $url): array
192192
{
193193
$this->logger->debug('sending request', ['url' => $url]);
194-
if (!\is_array($contents)) {
195-
throw new InvalidArgumentException(\sprintf('Argument type must be array, %s provided.', \gettype($contents)));
194+
if (!\is_string($contents)) {
195+
throw new InvalidArgumentException(\sprintf('Argument type must be string, %s provided.', \gettype($contents)));
196196
}
197197

198-
$response = $this->client->post($url, ['form_params' => $contents]);
198+
$response = $this->client->post($url, [
199+
'headers' => [
200+
'Content-Type' => 'application/x-www-form-urlencoded',
201+
],
202+
'body' => $contents,
203+
]);
204+
199205
$this->logger->debug('request completed', ['status_code' => $response->getStatusCode()]);
200206

201207
return $this->data = $this->serializer->decode($response->getBody()->getContents(), $txType);

src/Serializer/InterPosSerializer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public static function supports(string $gatewayClass): bool
2222
/**
2323
* @inheritDoc
2424
*
25-
* @return array<string, mixed>
25+
* @return string
2626
*/
27-
public function encode(array $data, ?string $txType = null): array
27+
public function encode(array $data, ?string $txType = null): string
2828
{
29-
return $data;
29+
return \http_build_query($data);
3030
}
3131

3232
/**

tests/Unit/Gateways/InterPosTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function testMake3DPayment(
241241
$txType,
242242
$this->config['gateway_endpoints']['payment_api'],
243243
$create3DPaymentRequestData,
244-
['request-body'],
244+
'request-body',
245245
'response-body',
246246
$paymentResponse,
247247
$order,
@@ -386,7 +386,7 @@ public function testMakeRegularPayment(array $order, string $txType, string $api
386386
$txType,
387387
$apiUrl,
388388
$requestData,
389-
['request-body'],
389+
'request-body',
390390
'response-body',
391391
$decodedResponse,
392392
$order,
@@ -420,7 +420,7 @@ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): vo
420420
$txType,
421421
$apiUrl,
422422
$requestData,
423-
['request-body'],
423+
'request-body',
424424
'response-body',
425425
$decodedResponse,
426426
$order,
@@ -455,7 +455,7 @@ public function testStatusRequest(array $order, string $apiUrl): void
455455
$txType,
456456
$apiUrl,
457457
$requestData,
458-
['request-body'],
458+
'request-body',
459459
'response-body',
460460
$decodedResponse,
461461
$order,
@@ -489,7 +489,7 @@ public function testCancelRequest(array $order, string $apiUrl): void
489489
$txType,
490490
$apiUrl,
491491
$requestData,
492-
['request-body'],
492+
'request-body',
493493
'response-body',
494494
$decodedResponse,
495495
$order,
@@ -523,7 +523,7 @@ public function testRefundRequest(array $order, string $apiUrl): void
523523
$txType,
524524
$apiUrl,
525525
$requestData,
526-
['request-body'],
526+
'request-body',
527527
'response-body',
528528
$decodedResponse,
529529
$order,
@@ -558,7 +558,7 @@ public function testCustomQueryRequest(array $requestData, ?string $apiUrl, stri
558558
$txType,
559559
$expectedApiUrl,
560560
$updatedRequestData,
561-
$updatedRequestData,
561+
'$updatedRequestData',
562562
'response-body',
563563
['decodedResponse'],
564564
$requestData,
@@ -729,7 +729,7 @@ private function configureClientResponse(
729729
string $txType,
730730
string $apiUrl,
731731
array $requestData,
732-
array $encodedRequestData,
732+
string $encodedRequestData,
733733
string $responseContent,
734734
array $decodedResponse,
735735
array $order,
@@ -752,7 +752,10 @@ private function configureClientResponse(
752752
$responseContent,
753753
$apiUrl,
754754
[
755-
'form_params' => $encodedRequestData,
755+
'headers' => [
756+
'Content-Type' => 'application/x-www-form-urlencoded',
757+
],
758+
'body' => $encodedRequestData,
756759
],
757760
);
758761

tests/Unit/Serializer/InterPosSerializerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ public function testSupports(): void
3333

3434
public function testEncode(): void
3535
{
36-
$data = ['abc' => '1'];
36+
$data = [
37+
'abc' => '1',
38+
'sa' => 'aa',
39+
];
3740
$result = $this->serializer->encode($data);
3841

39-
$this->assertSame($data, $result);
42+
$this->assertSame('abc=1&sa=aa', $result);
4043
}
4144

4245
/**

0 commit comments

Comments
 (0)