diff --git a/src/Sermepa/Tpv/Tpv.php b/src/Sermepa/Tpv/Tpv.php index 657df65..39b83bf 100644 --- a/src/Sermepa/Tpv/Tpv.php +++ b/src/Sermepa/Tpv/Tpv.php @@ -13,6 +13,15 @@ class Tpv CONST READ_TIMEOUT = 120; CONST SSLVERSION_TLSv1_2 = 6; + public const INSTALLMENTS = 'I'; + public const RECURRING = 'R'; + public const REAUTHORIZATION = 'H'; + public const RESUBMISSION = 'E'; + public const DELAYED = 'D'; + public const INCREMENTAL = 'M'; + public const NO_SHOW = 'N'; + public const OTHER = 'C'; + protected $_setEnvironment; protected $_setNameForm; protected $_setIdForm; @@ -918,9 +927,8 @@ public function setMerchantCofIni($isFirstTransaction) */ public function setMerchantCofType($value) { - $validOptions = ['I', 'R', 'H', 'E', 'D', 'M', 'N', 'C']; $value = strtoupper($value); - if (!in_array($value, $validOptions, true)) { + if (!self::isValidCofType($value)) { throw new TpvException('Set Merchant COF type'); } $this->_setParameters['DS_MERCHANT_COF_TYPE'] = $value; @@ -928,6 +936,25 @@ public function setMerchantCofType($value) return $this; } + private static function isValidCofType($value): bool + { + return in_array($value, self::getValidCofTypes(), true); + } + + private static function getValidCofTypes(): array + { + return [ + self::INSTALLMENTS, + self::RECURRING, + self::REAUTHORIZATION, + self::RESUBMISSION, + self::DELAYED, + self::INCREMENTAL, + self::NO_SHOW, + self::OTHER, + ]; + } + /** * COF identifier identifier. Optional. This * identifier is returned in the answer of the first diff --git a/tests/TpvTest.php b/tests/TpvTest.php index 8e43cc6..6801fb2 100644 --- a/tests/TpvTest.php +++ b/tests/TpvTest.php @@ -539,4 +539,71 @@ public function set_new_parameters() } + public function validTransactionTypeProvider(): array + { + return [ + 'Installments' => [Tpv::INSTALLMENTS], + 'Recurring' => [Tpv::RECURRING], + 'Reauthorization' => [Tpv::REAUTHORIZATION], + 'Resubmission' => [Tpv::RESUBMISSION], + 'Delayed' => [Tpv::DELAYED], + 'Incremental' => [Tpv::INCREMENTAL], + 'No Show' => [Tpv::NO_SHOW], + 'Otras' => [Tpv::OTHER], + ]; + } + + /** + * @test + * @dataProvider validTransactionTypeProvider + */ + public function transaction_type_is_valid($type): void + { + $redsys = new Tpv(); + $redsys->setMerchantCofType($type); + $ds = $redsys->getParameters(); + $this->assertEquals($type, $ds['DS_MERCHANT_COF_TYPE']); + } + + + + public function invalidTransactionTypeProvider(): array + { + return [ + 'A' => ['A'], + 'B' => ['B'], + 'F' => ['F'], + 'G' => ['G'], + 'J' => ['J'], + 'K' => ['K'], + 'L' => ['L'], + 'O' => ['O'], + 'P' => ['P'], + 'Q' => ['Q'], + 'S' => ['S'], + 'T' => ['T'], + 'U' => ['U'], + 'V' => ['V'], + 'W' => ['W'], + 'X' => ['X'], + 'Y' => ['Y'], + 'Z' => ['Z'], + ]; + } + + /** + * @test + * @dataProvider invalidTransactionTypeProvider + */ + + public function throw_when_transaction_type_is_not_valid($type): void + { + $this->expectExceptionMessage('Set Merchant COF type'); + $this->expectException(\Sermepa\Tpv\TpvException::class); + $redsys = new Tpv(); + $redsys->setMerchantCofType($type); + + $redsys->setParameters($type); + + } }