Skip to content

Commit

Permalink
prevent user to provide invalid data to get3DFormData method
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Nov 16, 2024
1 parent 3d546af commit ec4e461
Show file tree
Hide file tree
Showing 31 changed files with 576 additions and 53 deletions.
6 changes: 6 additions & 0 deletions docs/THREED-PAYMENT-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ try {
$transactionType,
$card
);
} catch (\InvalidArgumentException $e) {
// örneğin kart bilgisi sağlanmadığında bu exception'i alırsınız.
var_dump($e);
} catch (\LogicException $e) {
// ödeme modeli veya işlem tipi desteklenmiyorsa bu exception'i alırsınız.
var_dump($e);
} catch (\Exception|\Error $e) {
var_dump($e);
exit;
Expand Down
6 changes: 6 additions & 0 deletions docs/THREED-SECURE-AND-PAY-PAYMENT-IN-MODALBOX-EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ try {
$transactionType,
$card
);
} catch (\InvalidArgumentException $e) {
// örneğin kart bilgisi sağlanmadığında bu exception'i alırsınız.
var_dump($e);
} catch (\LogicException $e) {
// ödeme modeli veya işlem tipi desteklenmiyorsa bu exception'i alırsınız.
var_dump($e);
} catch (\Throwable $e) {
var_dump($e);
exit;
Expand Down
3 changes: 3 additions & 0 deletions examples/_common-codes/3d-host/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

try {
$formData = $pos->get3DFormData($order, PosInterface::MODEL_3D_HOST, $transaction);
} catch (\LogicException $e) {
// ödeme modeli veya işlem tipi desteklenmiyorsa bu exception'i alırsınız.
dd($e);
} catch (\Exception $e) {
dd($e);
}
Expand Down
6 changes: 6 additions & 0 deletions examples/_common-codes/3d/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ function (RequestDataPreparedEvent $requestDataPreparedEvent): void {
try {
$formData = $pos->get3DFormData($order, $paymentModel, $transaction, $card);
//dd($formData);
} catch (\InvalidArgumentException $e) {
// örneğin kart bilgisi sağlanmadığında bu exception'i alırsınız.
dd($e);
} catch (\LogicException $e) {
// ödeme modeli veya işlem tipi desteklenmiyorsa bu exception'i alırsınız.
dd($e);
} catch (\Throwable $e) {
dd($e);
}
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ parameters:
count: 1
path: src/Gateways/PayFlexCPV4Pos.php

-
message: "#^Parameter \\#2 \\$creditCard of method Mews\\\\Pos\\\\Gateways\\\\PayFlexV4Pos\\:\\:sendEnrollmentRequest\\(\\) expects Mews\\\\Pos\\\\Entity\\\\Card\\\\CreditCardInterface, Mews\\\\Pos\\\\Entity\\\\Card\\\\CreditCardInterface\\|null given\\.$#"
count: 1
path: src/Gateways/PayFlexV4Pos.php

-
message: "#^Method Mews\\\\Pos\\\\Gateways\\\\PosNet\\:\\:getOosTransactionData\\(\\) should return array\\{approved\\: string, respCode\\: string, respText\\: string, oosRequestDataResponse\\?\\: array\\{data1\\: string, data2\\: string, sign\\: string\\}\\} but returns array\\<string, mixed\\>\\.$#"
count: 1
Expand All @@ -174,3 +179,8 @@ parameters:
message: "#^Offset 'oosRequestDataRespo…' does not exist on array\\{approved\\: string, respCode\\: string, respText\\: string, oosRequestDataResponse\\?\\: array\\{data1\\: string, data2\\: string, sign\\: string\\}\\}\\.$#"
count: 1
path: src/Gateways/PosNet.php

-
message: "#^Parameter \\#4 \\$creditCard of method Mews\\\\Pos\\\\Gateways\\\\PosNet\\:\\:getOosTransactionData\\(\\) expects Mews\\\\Pos\\\\Entity\\\\Card\\\\CreditCardInterface, Mews\\\\Pos\\\\Entity\\\\Card\\\\CreditCardInterface\\|null given\\.$#"
count: 1
path: src/Gateways/PosNet.php
18 changes: 18 additions & 0 deletions src/Gateways/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,22 @@ protected function is3DAuthSuccess(array $responseData): bool

return false;
}

/**
* @param PosInterface::MODEL_3D_* $paymentModel
* @param PosInterface::TX_TYPE_PAY_AUTH|PosInterface::TX_TYPE_PAY_PRE_AUTH $txType
* @param CreditCardInterface|null $card
*
* @throws \InvalidArgumentException when inputs are not valid
*/
protected function check3DFormInputs(string $paymentModel, string $txType, CreditCardInterface $card = null): void
{
if (!self::isSupportedTransaction($txType, $paymentModel)) {
throw new \LogicException('Bu banka altyapısı sağlanan ödeme modelini ya da işlem tipini desteklenmiyor.');
}

if ((PosInterface::MODEL_3D_SECURE === $paymentModel || PosInterface::MODEL_3D_PAY === $paymentModel) && null === $card) {
throw new \InvalidArgumentException('Bu ödeme modeli için kart bilgileri zorunlu!');
}
}
}
2 changes: 2 additions & 0 deletions src/Gateways/AkbankPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public function make3DHostPayment(Request $request, array $order, string $txType
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

$gatewayUrl = PosInterface::MODEL_3D_HOST === $paymentModel ? $this->get3DHostGatewayURL() : $this->get3DGatewayURL();
Expand Down
2 changes: 2 additions & 0 deletions src/Gateways/EstPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public function make3DHostPayment(Request $request, array $order, string $txType
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

return $this->requestDataMapper->create3DFormData($this->account, $order, $paymentModel, $txType, $this->get3DGatewayURL(), $creditCard);
Expand Down
2 changes: 2 additions & 0 deletions src/Gateways/GarantiPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function make3DPayPayment(Request $request, array $order, string $txType)
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

return $this->requestDataMapper->create3DFormData($this->account, $order, $paymentModel, $txType, $this->get3DGatewayURL(), $creditCard);
Expand Down
8 changes: 3 additions & 5 deletions src/Gateways/InterPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,12 @@ public function orderHistory(array $order): PosInterface
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$gatewayUrl = $this->get3DHostGatewayURL();

if (PosInterface::MODEL_3D_SECURE === $paymentModel || PosInterface::MODEL_3D_PAY === $paymentModel) {
$gatewayUrl = $this->get3DGatewayURL();
}
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

$gatewayUrl = PosInterface::MODEL_3D_HOST === $paymentModel ? $this->get3DHostGatewayURL() : $this->get3DGatewayURL();

return $this->requestDataMapper->create3DFormData($this->account, $order, $paymentModel, $txType, $gatewayUrl, $creditCard);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Gateways/KuveytPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ public function orderHistory(array $order): PosInterface
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$gatewayUrl = $this->get3DGatewayURL();

$this->logger->debug('preparing 3D form data');

return $this->getCommon3DFormData($this->account, $order, $paymentModel, $txType, $gatewayUrl, $creditCard);
Expand Down
2 changes: 2 additions & 0 deletions src/Gateways/PayFlexCPV4Pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public function orderHistory(array $order): PosInterface
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

/** @var array{CommonPaymentUrl: string|null, PaymentToken: string|null, ErrorCode: string|null, ResponseMessage: string|null} $data */
$data = $this->registerPayment($order, $txType, $paymentModel, $creditCard);

Expand Down
5 changes: 1 addition & 4 deletions src/Gateways/PayFlexV4Pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Mews\Pos\Gateways;

use Exception;
use LogicException;
use Mews\Pos\DataMapper\RequestDataMapper\PayFlexV4PosRequestDataMapper;
use Mews\Pos\DataMapper\RequestDataMapper\RequestDataMapperInterface;
use Mews\Pos\DataMapper\ResponseDataMapper\PayFlexV4PosResponseDataMapper;
Expand Down Expand Up @@ -159,9 +158,7 @@ public function orderHistory(array $order): PosInterface
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
if (!$creditCard instanceof CreditCardInterface) {
throw new LogicException('Kredi kartı bilgileri eksik!');
}
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$data = $this->sendEnrollmentRequest($order, $creditCard, $txType, $paymentModel);

Expand Down
7 changes: 3 additions & 4 deletions src/Gateways/PayForPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ public function orderHistory(array $order): PosInterface
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

$gatewayURL = $this->get3DGatewayURL();
if (PosInterface::MODEL_3D_HOST === $paymentModel) {
$gatewayURL = $this->get3DHostGatewayURL();
}
$gatewayURL = PosInterface::MODEL_3D_HOST === $paymentModel ? $this->get3DHostGatewayURL() : $this->get3DGatewayURL();

return $this->requestDataMapper->create3DFormData($this->account, $order, $paymentModel, $txType, $gatewayURL, $creditCard);
}
Expand Down
15 changes: 10 additions & 5 deletions src/Gateways/PosNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Mews\Pos\Gateways;

use InvalidArgumentException;
use LogicException;
use Mews\Pos\DataMapper\RequestDataMapper\PosNetRequestDataMapper;
use Mews\Pos\DataMapper\RequestDataMapper\RequestDataMapperInterface;
use Mews\Pos\DataMapper\ResponseDataMapper\PosNetResponseDataMapper;
Expand Down Expand Up @@ -167,9 +166,7 @@ public function make3DHostPayment(Request $request, array $order, string $txType
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
if (!$creditCard instanceof CreditCardInterface) {
throw new LogicException('Kredi kartı veya sipariş bilgileri eksik!');
}
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$data = $this->getOosTransactionData($order, $txType, $paymentModel, $creditCard);

Expand All @@ -180,7 +177,15 @@ public function get3DFormData(array $order, string $paymentModel, string $txType

$this->logger->debug('preparing 3D form data');

return $this->requestDataMapper->create3DFormData($this->account, $order, $paymentModel, $txType, $this->get3DGatewayURL(), null, $data['oosRequestDataResponse']);
return $this->requestDataMapper->create3DFormData(
$this->account,
$order,
$paymentModel,
$txType,
$this->get3DGatewayURL(),
null,
$data['oosRequestDataResponse']
);
}

/** @return PosNetAccount */
Expand Down
2 changes: 2 additions & 0 deletions src/Gateways/PosNetV1Pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public function make3DHostPayment(Request $request, array $order, string $txType
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

return $this->requestDataMapper->create3DFormData($this->account, $order, $paymentModel, $txType, $this->get3DGatewayURL(), $creditCard);
Expand Down
4 changes: 1 addition & 3 deletions src/Gateways/ToslaPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ public function make3DHostPayment(Request $request, array $order, string $txType
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
if (PosInterface::MODEL_3D_HOST !== $paymentModel && !$creditCard instanceof CreditCardInterface) {
throw new \LogicException('Kredi kart bilgileri eksik!');
}
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$data = $this->registerPayment($order, $paymentModel, $txType);

Expand Down
2 changes: 2 additions & 0 deletions src/Gateways/VakifKatilimPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function make3DHostPayment(Request $request, array $order, string $txType
*/
public function get3DFormData(array $order, string $paymentModel, string $txType, CreditCardInterface $creditCard = null): array
{
$this->check3DFormInputs($paymentModel, $txType, $creditCard);

$this->logger->debug('preparing 3D form data');

if (PosInterface::MODEL_3D_HOST === $paymentModel) {
Expand Down
6 changes: 4 additions & 2 deletions src/PosInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ interface PosInterface
/**
* returns form data, key values, necessary for 3D payment
*
* @phpstan-param PosInterface::MODEL_3D_* $paymentModel
* @phpstan-param PosInterface::MODEL_3D_* $paymentModel
* @phpstan-param PosInterface::TX_TYPE_PAY_AUTH|PosInterface::TX_TYPE_PAY_PRE_AUTH $txType
*
* @param array<string, mixed> $order
Expand All @@ -122,7 +122,9 @@ interface PosInterface
* @return array{gateway: string, method: 'POST'|'GET', inputs: array<string, string>}
*
* @throws \RuntimeException when request to the bank to get 3D form data failed
* @throws \LogicException when card data is not provided when it is required for the given payment model
* @throws ClientExceptionInterface when request to the bank to get 3D form data failed
* @throws \InvalidArgumentException when card data is not provided when it is required for the given payment model
* @throws \LogicException when given payment model or transaction type is not supported
* @throws UnsupportedTransactionTypeException
* @throws ClientExceptionInterface
*/
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Gateways/AkbankPosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,24 @@ public function testGet3DFormData(
$this->assertSame($actual, $formData);
}

/**
* @dataProvider threeDFormDataBadInputsProvider
*/
public function testGet3DFormDataWithBadInputs(
array $order,
string $paymentModel,
string $txType,
bool $isWithCard,
string $expectedExceptionClass
): void
{
$card = $isWithCard ? $this->card : null;

$this->expectException($expectedExceptionClass);

$this->pos->get3DFormData($order, $paymentModel, $txType, $card);
}

/**
* @dataProvider historyRequestDataProvider
*/
Expand Down Expand Up @@ -849,6 +867,33 @@ public static function orderHistoryDataProvider(): iterable
];
}

public static function threeDFormDataBadInputsProvider(): array
{
return [
'3d_secure_without_card' => [
'order' => ['id' => '2020110828BC'],
'paymentModel' => PosInterface::MODEL_3D_SECURE,
'txType' => PosInterface::TX_TYPE_PAY_AUTH,
'isWithCard' => false,
'expectedExceptionClass' => \InvalidArgumentException::class,
],
'3d_pay_without_card' => [
'order' => ['id' => '2020110828BC'],
'paymentModel' => PosInterface::MODEL_3D_PAY,
'txType' => PosInterface::TX_TYPE_PAY_AUTH,
'isWithCard' => false,
'expectedExceptionClass' => \InvalidArgumentException::class,
],
'unsupported_payment_model' => [
'order' => ['id' => '2020110828BC'],
'paymentModel' => PosInterface::MODEL_3D_PAY_HOSTING,
'txType' => PosInterface::TX_TYPE_PAY_AUTH,
'isWithCard' => false,
'expectedExceptionClass' => \LogicException::class,
],
];
}

public static function threeDFormDataProvider(): iterable
{
yield '3d_host' => [
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Gateways/EstPosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ public function testGet3DFormData(
$this->assertSame(['formData'], $actual);
}

/**
* @dataProvider threeDFormDataBadInputsProvider
*/
public function testGet3DFormDataWithBadInputs(
array $order,
string $paymentModel,
string $txType,
bool $isWithCard,
string $expectedExceptionClass
): void
{
$card = $isWithCard ? $this->card : null;

$this->expectException($expectedExceptionClass);

$this->pos->get3DFormData($order, $paymentModel, $txType, $card);
}

/**
* @return void
*/
Expand Down Expand Up @@ -805,6 +823,26 @@ public static function cancelRequestDataProvider(): array
];
}

public static function threeDFormDataBadInputsProvider(): array
{
return [
'3d_secure_without_card' => [
'order' => ['id' => '2020110828BC'],
'paymentModel' => PosInterface::MODEL_3D_SECURE,
'txType' => PosInterface::TX_TYPE_PAY_AUTH,
'isWithCard' => false,
'expectedExceptionClass' => \InvalidArgumentException::class,
],
'3d_pay_without_card' => [
'order' => ['id' => '2020110828BC'],
'paymentModel' => PosInterface::MODEL_3D_PAY,
'txType' => PosInterface::TX_TYPE_PAY_AUTH,
'isWithCard' => false,
'expectedExceptionClass' => \InvalidArgumentException::class,
],
];
}

private function configureClientResponse(
string $txType,
string $apiUrl,
Expand Down
Loading

0 comments on commit ec4e461

Please sign in to comment.