Skip to content

Commit

Permalink
implemented request value formatters and mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Oct 13, 2024
1 parent 6cd8a98 commit b664c4f
Show file tree
Hide file tree
Showing 124 changed files with 5,312 additions and 2,606 deletions.
4 changes: 2 additions & 2 deletions examples/_templates/_credit_card_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<select name="month" id="month" class="form-select input-lg">
<option value="">Month</option>
<?php for ($i = 1; $i <= 12; $i++) : ?>
<option value="<?= $i; ?>" <?= $i == $card->getExpireMonth() ? 'selected': null ?>><?= str_pad($i, 2, 0, STR_PAD_LEFT); ?></option>
<option value="<?= $i; ?>" <?= $i == $card->getExpirationDate()->format('m') ? 'selected': null ?>><?= str_pad($i, 2, 0, STR_PAD_LEFT); ?></option>
<?php endfor; ?>
</select>
</div>
Expand All @@ -36,7 +36,7 @@
<select name="year" id="year" class="form-select input-lg">
<option value="">Year</option>
<?php for ($i = date('Y'); $i <= date('Y') + 20; $i++) : ?>
<option value="<?= $i; ?>" <?= $i == $card->getExpireYear('Y') ? 'selected': null ?>><?= $i; ?></option>
<option value="<?= $i; ?>" <?= $i == $card->getExpirationDate()->format('Y') ? 'selected': null ?>><?= $i; ?></option>
<?php endfor; ?>
</select>
</div>
Expand Down
41 changes: 13 additions & 28 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ parameters:
path: src/Client/HttpClient.php

-
message: "#^Parameter \\#1 \\$currency of method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\AkbankPosRequestDataMapper\\:\\:mapCurrency\\(\\) expects 'EUR'\\|'GBP'\\|'JPY'\\|'RUB'\\|'TRY'\\|'USD', string given\\.$#"
count: 5
path: src/DataMapper/RequestDataMapper/AkbankPosRequestDataMapper.php
message: "#^Parameter \\#1 \\$amount of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueFormatter\\\\RequestValueFormatterInterface\\:\\:formatAmount\\(\\) expects float, float\\|int\\|string\\|null given\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php

-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
message: "#^Parameter \\#1 \\$currency of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueMapper\\\\RequestValueMapperInterface\\:\\:mapCurrency\\(\\) expects 'EUR'\\|'GBP'\\|'JPY'\\|'RUB'\\|'TRY'\\|'USD', float\\|int\\|string\\|null given\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php

-
message: "#^Parameter \\#1 \\$currency of method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\EstPosRequestDataMapper\\:\\:mapCurrency\\(\\) expects 'EUR'\\|'GBP'\\|'JPY'\\|'RUB'\\|'TRY'\\|'USD', string given\\.$#"
message: "#^Parameter \\#1 \\$installment of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueFormatter\\\\RequestValueFormatterInterface\\:\\:formatInstallment\\(\\) expects int\\<0, max\\>, float\\|int\\|string\\|null given\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/EstPosRequestDataMapper.php

-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/GarantiPosRequestDataMapper.php
message: "#^Parameter \\#1 \\$cardType of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueMapper\\\\RequestValueMapperInterface\\:\\:mapCardType\\(\\) expects 'amex'\\|'master'\\|'troy'\\|'visa', 'amex'\\|'master'\\|'troy'\\|'visa'\\|null given\\.$#"
count: 2
path: src/DataMapper/RequestDataMapper/InterPosRequestDataMapper.php

-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
message: "#^Parameter \\#1 \\$cardType of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueMapper\\\\RequestValueMapperInterface\\:\\:mapCardType\\(\\) expects 'amex'\\|'master'\\|'troy'\\|'visa', 'amex'\\|'master'\\|'troy'\\|'visa'\\|null given\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/InterPosRequestDataMapper.php
path: src/DataMapper/RequestDataMapper/KuveytPosRequestDataMapper.php

-
message: "#^Default value of the parameter \\#7 \\$extraData \\(array\\{\\}\\) of method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\PayFlexCPV4PosRequestDataMapper\\:\\:create3DFormData\\(\\) is incompatible with type array\\{CommonPaymentUrl\\: string, PaymentToken\\: string\\}\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/PayFlexCPV4PosRequestDataMapper.php

-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
message: "#^Parameter \\#1 \\$cardType of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueMapper\\\\RequestValueMapperInterface\\:\\:mapCardType\\(\\) expects 'amex'\\|'master'\\|'troy'\\|'visa', 'amex'\\|'master'\\|'troy'\\|'visa'\\|null given\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/PayFlexCPV4PosRequestDataMapper.php

Expand All @@ -51,12 +51,12 @@ parameters:
path: src/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapper.php

-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
message: "#^Method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\PayFlexV4PosRequestDataMapper\\:\\:create3DEnrollmentCheckRequestData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapper.php

-
message: "#^Method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\PayFlexV4PosRequestDataMapper\\:\\:create3DEnrollmentCheckRequestData\\(\\) return type has no value type specified in iterable type array\\.$#"
message: "#^Parameter \\#1 \\$cardType of method Mews\\\\Pos\\\\DataMapper\\\\RequestValueMapper\\\\RequestValueMapperInterface\\:\\:mapCardType\\(\\) expects 'amex'\\|'master'\\|'troy'\\|'visa', 'amex'\\|'master'\\|'troy'\\|'visa'\\|null given\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/PayFlexV4PosRequestDataMapper.php

Expand All @@ -65,11 +65,6 @@ parameters:
count: 1
path: src/DataMapper/RequestDataMapper/PayForPosRequestDataMapper.php

-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/PayForPosRequestDataMapper.php

-
message: "#^Cannot assign offset 'authCode' to array\\<string, string\\>\\|string\\.$#"
count: 1
Expand All @@ -95,11 +90,6 @@ parameters:
count: 1
path: src/DataMapper/RequestDataMapper/PosNetRequestDataMapper.php

-
message: "#^Method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\PosNetRequestDataMapper\\:\\:create3DEnrollmentCheckRequestData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/DataMapper/RequestDataMapper/PosNetRequestDataMapper.php

-
message: "#^Method Mews\\\\Pos\\\\DataMapper\\\\RequestDataMapper\\\\PosNetV1PosRequestDataMapper\\:\\:create3DFormData\\(\\) has parameter \\$extraData with no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -140,11 +130,6 @@ parameters:
count: 1
path: src/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapper.php

-
message: "#^Property Mews\\\\Pos\\\\DataMapper\\\\ResponseDataMapper\\\\AbstractResponseDataMapper\\:\\:\\$currencyMappings \\(array\\<string, 'EUR'\\|'GBP'\\|'JPY'\\|'RUB'\\|'TRY'\\|'USD'\\>\\) does not accept non\\-empty\\-array\\<392\\|643\\|826\\|840\\|949\\|978\\|string, 'EUR'\\|'GBP'\\|'JPY'\\|'RUB'\\|'TRY'\\|'USD'\\>\\.$#"
count: 1
path: src/DataMapper/ResponseDataMapper/PosNetV1PosResponseDataMapper.php

-
message: "#^Method Mews\\\\Pos\\\\Factory\\\\PosFactory\\:\\:createPosGateway\\(\\) should return Mews\\\\Pos\\\\PosInterface but returns object\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions src/Crypt/CryptInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function check3DHash(AbstractPosAccount $posAccount, array $data): bool;
/**
* creates hash for 3D secure payments
*
* @param AbstractPosAccount $posAccount
* @param array<string, string> $requestData
* @param AbstractPosAccount $posAccount
* @param array<string, string|int|float> $requestData
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Crypt/GarantiPosCrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function create3DHash(AbstractPosAccount $posAccount, array $requestData)
$requestData['txntype'],
$requestData['txninstallmentcount'],
$posAccount->getStoreKey(),
$this->createSecurityData($posAccount, $requestData['txntype']),
$this->createSecurityData($posAccount, (string) $requestData['txntype']),
];

return $this->hashStringUpperCase(implode(static::HASH_SEPARATOR, $map), self::HASH_ALGORITHM);
Expand Down
211 changes: 21 additions & 190 deletions src/DataMapper/RequestDataMapper/AbstractRequestDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
namespace Mews\Pos\DataMapper\RequestDataMapper;

use Mews\Pos\Crypt\CryptInterface;
use Mews\Pos\DataMapper\RequestValueFormatter\RequestValueFormatterInterface;
use Mews\Pos\DataMapper\RequestValueMapper\RequestValueMapperInterface;
use Mews\Pos\Entity\Account\AbstractPosAccount;
use Mews\Pos\Entity\Card\CreditCardInterface;
use Mews\Pos\Exceptions\UnsupportedTransactionTypeException;
use Mews\Pos\PosInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -18,64 +17,29 @@
abstract class AbstractRequestDataMapper implements RequestDataMapperInterface
{
protected EventDispatcherInterface $eventDispatcher;

/** @var array<PosInterface::MODEL_*, string> */
protected array $secureTypeMappings = [];

/**
* Transaction Types
*
* @var array<PosInterface::TX_TYPE_*, string|array<PosInterface::MODEL_*, string>>
*/
protected array $txTypeMappings = [];

/** @var array<CreditCardInterface::CARD_TYPE_*, string> */
protected array $cardTypeMapping = [];

/** @var array<PosInterface::LANG_*, string> */
protected array $langMappings = [
PosInterface::LANG_TR => 'tr',
PosInterface::LANG_EN => 'en',
];

/**
* default olarak ISO 4217 kodlar tanimliyoruz.
* fakat bazi banklar ISO standarti kullanmiyorlar.
* Currency mapping
*
* @var non-empty-array<PosInterface::CURRENCY_*, string|int>
*/
protected array $currencyMappings = [
PosInterface::CURRENCY_TRY => '949',
PosInterface::CURRENCY_USD => '840',
PosInterface::CURRENCY_EUR => '978',
PosInterface::CURRENCY_GBP => '826',
PosInterface::CURRENCY_JPY => '392',
PosInterface::CURRENCY_RUB => '643',
];

/**
* period mapping for recurring orders
* @var array<'DAY'|'WEEK'|'MONTH'|'YEAR', string>
*/
protected array $recurringOrderFrequencyMapping = [];
protected RequestValueMapperInterface $valueMapper;
protected RequestValueFormatterInterface $valueFormatter;
protected CryptInterface $crypt;

protected bool $testMode = false;

protected CryptInterface $crypt;

/**
* @param EventDispatcherInterface $eventDispatcher
* @param CryptInterface $crypt
* @param array<PosInterface::CURRENCY_*, string|int> $currencyMappings
* @param RequestValueMapperInterface $valueMapper
* @param RequestValueFormatterInterface $valueFormatter
* @param EventDispatcherInterface $eventDispatcher
* @param CryptInterface $crypt
*/
public function __construct(EventDispatcherInterface $eventDispatcher, CryptInterface $crypt, array $currencyMappings = [])
public function __construct(
RequestValueMapperInterface $valueMapper,
RequestValueFormatterInterface $valueFormatter,
EventDispatcherInterface $eventDispatcher,
CryptInterface $crypt
)
{
$this->valueMapper = $valueMapper;
$this->valueFormatter = $valueFormatter;
$this->eventDispatcher = $eventDispatcher;
$this->crypt = $crypt;
if ([] !== $currencyMappings) {
$this->currencyMappings = $currencyMappings;
}
}

/**
Expand All @@ -94,39 +58,6 @@ public function isTestMode(): bool
return $this->testMode;
}

/**
* @return array<CreditCardInterface::CARD_TYPE_*, string>
*/
public function getCardTypeMapping(): array
{
return $this->cardTypeMapping;
}

/**
* @return array<PosInterface::MODEL_*, string>
*/
public function getSecureTypeMappings(): array
{
return $this->secureTypeMappings;
}

/**
* @return array<PosInterface::TX_TYPE_*, string|array<PosInterface::MODEL_*, string>>
*/
public function getTxTypeMappings(): array
{
return $this->txTypeMappings;
}

/**
* @return non-empty-array<PosInterface::CURRENCY_*, string|int>
*/
public function getCurrencyMappings(): array
{
return $this->currencyMappings;
}


/**
* @inheritDoc
*/
Expand All @@ -136,106 +67,8 @@ public function setTestMode(bool $testMode): void
}

/**
* @phpstan-param PosInterface::TX_TYPE_* $txType
* @phpstan-param PosInterface::MODEL_*|null $paymentModel
*
* @param string $txType
* @param string|null $paymentModel
*
* @return string
*
* @throws UnsupportedTransactionTypeException
*/
public function mapTxType(string $txType, ?string $paymentModel = null): string
{
if (!$this->isSupportedTxType($txType, $paymentModel)) {
throw new UnsupportedTransactionTypeException();
}

if (\is_string($this->txTypeMappings[$txType])) {
return $this->txTypeMappings[$txType];
}

return $this->txTypeMappings[$txType][$paymentModel];
}

/**
* @phpstan-param PosInterface::TX_TYPE_* $txType
* @phpstan-param PosInterface::MODEL_*|null $paymentModel
*
* @param string $txType
* @param string|null $paymentModel
*
* @return bool
*/
public function isSupportedTxType(string $txType, ?string $paymentModel = null): bool
{
if (!isset($this->txTypeMappings[$txType])) {
return false;
}

if (\is_array($this->txTypeMappings[$txType])) {
if (null === $paymentModel) {
return false;
}

return isset($this->txTypeMappings[$txType][$paymentModel]);
}

return true;
}

/**
* @return array<'DAY'|'WEEK'|'MONTH'|'YEAR', string>
*/
public function getRecurringOrderFrequencyMapping(): array
{
return $this->recurringOrderFrequencyMapping;
}

/**
* formats installment
* @param int $installment
*
* @return string|int
*/
abstract protected function mapInstallment(int $installment);

/**
* @phpstan-param PosInterface::CURRENCY_* $currency
*
* @param string $currency
*
* @return string|int currency code that is accepted by bank
*/
protected function mapCurrency(string $currency)
{
return $this->currencyMappings[$currency] ?? $currency;
}

/**
* @param float $amount
*
* @return int|string|float
*/
protected function formatAmount(float $amount)
{
return $amount;
}

/**
* @param string $period
*
* @return string
*/
protected function mapRecurringFrequency(string $period): string
{
return $this->recurringOrderFrequencyMapping[$period] ?? $period;
}

/**
* bank returns error messages for specified language value
* usually accepted values are tr,en
* according to the language value the POS UI will be displayed in the selected language
* and error messages will be returned in the selected language
*
* @param AbstractPosAccount $posAccount
* @param array<string, mixed> $order
Expand All @@ -244,11 +77,9 @@ protected function mapRecurringFrequency(string $period): string
*/
protected function getLang(AbstractPosAccount $posAccount, array $order): string
{
if (isset($order['lang'])) {
return $this->langMappings[$order['lang']];
}
$lang = $order['lang'] ?? $posAccount->getLang();

return $this->langMappings[$posAccount->getLang()];
return $this->valueMapper->mapLang($lang);
}

/**
Expand Down
Loading

0 comments on commit b664c4f

Please sign in to comment.