Skip to content

Commit

Permalink
refactor - InterPos use http_build_query in Serializer instead of for…
Browse files Browse the repository at this point in the history
…m_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
  • Loading branch information
mustapayev committed Feb 18, 2025
1 parent 65844b0 commit 2695310
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/Gateways/InterPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,17 @@ public function get3DFormData(array $order, string $paymentModel, string $txType
protected function send($contents, string $txType, string $paymentModel, string $url): array
{
$this->logger->debug('sending request', ['url' => $url]);
if (!\is_array($contents)) {
throw new InvalidArgumentException(\sprintf('Argument type must be array, %s provided.', \gettype($contents)));
if (!\is_string($contents)) {
throw new InvalidArgumentException(\sprintf('Argument type must be string, %s provided.', \gettype($contents)));
}

$response = $this->client->post($url, ['form_params' => $contents]);
$response = $this->client->post($url, [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => $contents,
]);

$this->logger->debug('request completed', ['status_code' => $response->getStatusCode()]);

return $this->data = $this->serializer->decode($response->getBody()->getContents(), $txType);
Expand Down
6 changes: 3 additions & 3 deletions src/Serializer/InterPosSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public static function supports(string $gatewayClass): bool
/**
* @inheritDoc
*
* @return array<string, mixed>
* @return string
*/
public function encode(array $data, ?string $txType = null): array
public function encode(array $data, ?string $txType = null): string
{
return $data;
return \http_build_query($data);
}

/**
Expand Down
21 changes: 12 additions & 9 deletions tests/Unit/Gateways/InterPosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function testMake3DPayment(
$txType,
$this->config['gateway_endpoints']['payment_api'],
$create3DPaymentRequestData,
['request-body'],
'request-body',
'response-body',
$paymentResponse,
$order,
Expand Down Expand Up @@ -386,7 +386,7 @@ public function testMakeRegularPayment(array $order, string $txType, string $api
$txType,
$apiUrl,
$requestData,
['request-body'],
'request-body',
'response-body',
$decodedResponse,
$order,
Expand Down Expand Up @@ -420,7 +420,7 @@ public function testMakeRegularPostAuthPayment(array $order, string $apiUrl): vo
$txType,
$apiUrl,
$requestData,
['request-body'],
'request-body',
'response-body',
$decodedResponse,
$order,
Expand Down Expand Up @@ -455,7 +455,7 @@ public function testStatusRequest(array $order, string $apiUrl): void
$txType,
$apiUrl,
$requestData,
['request-body'],
'request-body',
'response-body',
$decodedResponse,
$order,
Expand Down Expand Up @@ -489,7 +489,7 @@ public function testCancelRequest(array $order, string $apiUrl): void
$txType,
$apiUrl,
$requestData,
['request-body'],
'request-body',
'response-body',
$decodedResponse,
$order,
Expand Down Expand Up @@ -523,7 +523,7 @@ public function testRefundRequest(array $order, string $apiUrl): void
$txType,
$apiUrl,
$requestData,
['request-body'],
'request-body',
'response-body',
$decodedResponse,
$order,
Expand Down Expand Up @@ -558,7 +558,7 @@ public function testCustomQueryRequest(array $requestData, ?string $apiUrl, stri
$txType,
$expectedApiUrl,
$updatedRequestData,
$updatedRequestData,
'$updatedRequestData',
'response-body',
['decodedResponse'],
$requestData,
Expand Down Expand Up @@ -729,7 +729,7 @@ private function configureClientResponse(
string $txType,
string $apiUrl,
array $requestData,
array $encodedRequestData,
string $encodedRequestData,
string $responseContent,
array $decodedResponse,
array $order,
Expand All @@ -752,7 +752,10 @@ private function configureClientResponse(
$responseContent,
$apiUrl,
[
'form_params' => $encodedRequestData,
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => $encodedRequestData,
],
);

Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/Serializer/InterPosSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public function testSupports(): void

public function testEncode(): void
{
$data = ['abc' => '1'];
$data = [
'abc' => '1',
'sa' => 'aa',
];
$result = $this->serializer->encode($data);

$this->assertSame($data, $result);
$this->assertSame('abc=1&sa=aa', $result);
}

/**
Expand Down

0 comments on commit 2695310

Please sign in to comment.