From 65288e43fa64ccc2b8bceb06331e09391f66badf Mon Sep 17 00:00:00 2001 From: Blum Date: Mon, 5 Jul 2021 19:44:46 +0300 Subject: [PATCH 1/3] Reversal functionality added --- src/Enums/TransactionType.php | 1 + src/ReversalRequest.php | 215 +++++++++++++++++++++++++++++++++ tests/ReversaslRequestTest.php | 67 ++++++++++ 3 files changed, 283 insertions(+) create mode 100644 src/ReversalRequest.php create mode 100644 tests/ReversaslRequestTest.php diff --git a/src/Enums/TransactionType.php b/src/Enums/TransactionType.php index b81bddf..46a0557 100644 --- a/src/Enums/TransactionType.php +++ b/src/Enums/TransactionType.php @@ -22,4 +22,5 @@ class TransactionType extends Enum const REVERSAL_REQUEST = 22; const REVERSAL_ADVICE = 24; const TRANSACTION_STATUS_CHECK = 90; + const REVERSAL = 24; } diff --git a/src/ReversalRequest.php b/src/ReversalRequest.php new file mode 100644 index 0000000..c0ed908 --- /dev/null +++ b/src/ReversalRequest.php @@ -0,0 +1,215 @@ +setTransactionType(TransactionType::REVERSAL()); + } + + /** + * Send data to borica + * + * @return StatusCheckResponse + * @throws Exceptions\SignatureException|ParameterValidationException|SendingException + */ + public function send() + { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $this->getEnvironmentUrl()); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->getData())); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + + $response = curl_exec($ch); + if (curl_error($ch)) { + throw new SendingException(curl_error($ch)); + } + curl_close($ch); + + return (new StatusCheckResponse()) + ->setResponseData(json_decode($response, true)) + ->setPublicKey($this->getPublicKey()); + } + + /** + * @return array + * @throws Exceptions\SignatureException + * @throws ParameterValidationException + */ + public function getData() + { + return [ + 'TERMINAL' => $this->getTerminalID(), + 'TRTYPE' => $this->getTransactionType()->getValue(), + 'AMOUNT' => $this->getAmount(), + 'CURRENCY' => $this->getCurrency(), + 'ORDER' => $this->getOrder(), + 'DESC' => $this->getDescription(), + 'MERCHANT' => $this->getMerchantId(), + 'MERCH_NAME' => $this->getMerchantName(), + 'RRN' => $this->getRrn(), + 'INT_REF' => $this->getIntRef(), + 'TIMESTAMP' => $this->getSignatureTimestamp(), + 'NONCE' => $this->getNonce(), + 'P_SIGN' => $this->generateSignature(), + ]; + } + + /** + * @return string + */ + public function getRrn() + { + return $this->rrn; + } + + /** + * @return string + */ + public function getIntRef() + { + return $this->intRef; + } + + /** + * @return string + * @throws Exceptions\SignatureException + * @throws ParameterValidationException + */ + public function generateSignature() + { + $this->validateRequiredParameters(); + return $this->getPrivateSignature([ + $this->getTerminalID(), + $this->getTransactionType()->getValue(), + $this->getAmount(), + $this->getCurrency(), + $this->getOrder(), + $this->getMerchantId(), + $this->getSignatureTimestamp(), + $this->getNonce() + ]); + } + + /** + * @return void + * @throws ParameterValidationException + */ + public function validateRequiredParameters() + { + if (empty($this->getTransactionType())) { + throw new ParameterValidationException('Transaction type is empty!'); + } + + if (empty($this->getOrder())) { + throw new ParameterValidationException('Order is empty!'); + } + + if (empty($this->getPublicKey())) { + throw new ParameterValidationException('Please set public key for validation response!'); + } + + if (empty($this->getTerminalID())) { + throw new ParameterValidationException('TerminalID is empty!'); + } + } + + /** + * @return mixed|void + * @throws Exceptions\SignatureException + * @throws ParameterValidationException + */ + public function generateForm() + { + return $this->getData(); + } + + /** + * Set transaction reference. + * + * @param string $rrn Референция на транзакцията. + * + * @return ReversalRequest + */ + public function setRrn($rrn) + { + $this->rrn = $rrn; + return $this; + } + + /** + * Set transaction internal reference. + * + * @param string $intRef Вътрешна референция на транзакцията. + * + * @return ReversalRequest + */ + public function setIntRef($intRef) + { + $this->intRef = $intRef; + return $this; + } + + /** + * @return string + */ + public function getMerchantName() + { + return $this->merchantName; + } + + /** + * @param string $merchantName Merchant name. + * + * @return ReversalRequest + */ + public function setMerchantName($merchantName) + { + $this->merchantName = $merchantName; + return $this; + } +} diff --git a/tests/ReversaslRequestTest.php b/tests/ReversaslRequestTest.php new file mode 100644 index 0000000..2b262cc --- /dev/null +++ b/tests/ReversaslRequestTest.php @@ -0,0 +1,67 @@ +setAmount(1) + ->setCurrency('BGN') + ->setOrder(145659) + ->setDescription('Детайли плащане.') + ->setTerminalID(self::TERMINAL_ID) + ->setMerchantId(self::MERCHANT_ID) + ->setMerchantName('Some Company Ltd.') + ->setPrivateKey(__DIR__ . '/certificates/test.key') + ->setPrivateKeyPassword('test') + ->setSignatureTimestamp('20201013115715') + ->setRrn('118601289138') + ->setIntRef('CEAD95D777876F81') + ->setNonce('FC8AC36A9FDADCB6127D273CD15DAEC3') + ->setPublicKey(__DIR__ . '/certificates/public.cer') + ->setSigningSchemaMacExtended() + ->getData(); + + $this->assertEquals([ + 'TRTYPE' => 24, + 'CURRENCY' => 'BGN', + 'ORDER' => '145659', + 'AMOUNT' => '1.00', + 'DESC' => 'Детайли плащане.', + 'TIMESTAMP' => '20201013115715', + 'TERMINAL' => self::TERMINAL_ID, + 'MERCH_NAME' => 'Some Company Ltd.', + 'RRN' => '118601289138', + 'INT_REF' => 'CEAD95D777876F81', + 'NONCE' => 'FC8AC36A9FDADCB6127D273CD15DAEC3', + 'MERCHANT' => self::MERCHANT_ID, + 'P_SIGN' => '765C034CC1AC9213B59AE0D251C48B3883AA44DF73A57E4E12F73786B4847F42D40AF5CAFC33F2A065DF5EF28C095FBA373F4A9D08CD27CCEA9447CFB49A80404A1CB3067223C8AF0964C6A7E7960C43CB4B3C75FF9063C8931C0457CB6B43D9F536DD32950AF90E05A887079149415D1FFA1017ED716696D256FE9DBF69D6C4CB10AB71F60C7405A90E4E111CE37C62901A02188A74D7BA84FABB02E0D877DA998E29DEF0057CD5EAF5CAD0C6132EDB2A9CAE0556FD360F26BD47869802B0EB3C40D1205524AEA0FF5CF413A887F66DD032FCD09C5281C834B072BAEDF92950F4BCE2DA32A4A6E9392B46F9FA55BD08E64F16EE096CCD43DA4F077FFC5A9700' + ], $saleData); + } + + /** + * @return void + * @throws ParameterValidationException|SignatureException + */ + public function testBackRefValidation() + { + $this->expectException(ParameterValidationException::class); + + (new SaleRequest()) + ->setBackRefUrl('wrong url value'); + } +} From 594dfc48acbc7c0823d53f3a89b7d669b5f2a3af Mon Sep 17 00:00:00 2001 From: Blum Date: Tue, 6 Jul 2021 09:44:31 +0300 Subject: [PATCH 2/3] Updated README to include Reversal instructions. --- README.md | 28 ++++++++++++++++++++++++++++ src/ReversalRequest.php | 15 ++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index aaf70d5..52ebb48 100644 --- a/README.md +++ b/README.md @@ -140,8 +140,36 @@ $verifiedResponseData = $statusCheckResponse->getResponseData(); // get field from borica response $statusCheckResponse->getVerifiedData('inDevelopment() + ->setPrivateKey('\', '') + ->setPublicKey('') + ->setTerminalID('') + ->setAmount(123.32) + ->setOrder(123456) + ->setDescription('test reversal') + ->setMerchantId('') + ->setRrn('') + ->setIntRef('') + ; + +//send reversal request to borica +$reversalRequestResponse = $reversalRequest->send(); + +// get data from borica reversal response +$verifiedResponseData = $reversalRequestResponse->getResponseData(); + +// get field from borica reversal response +$reversalRequestResponse->getVerifiedData('STATUSMSG') +``` + + ### Methods #### Set environments diff --git a/src/ReversalRequest.php b/src/ReversalRequest.php index c0ed908..3159fa1 100644 --- a/src/ReversalRequest.php +++ b/src/ReversalRequest.php @@ -12,13 +12,6 @@ class ReversalRequest extends Request implements RequestInterface { - /** - * Original transaction type / TRAN_TRTYPE - * - * @var TransactionType - */ - private $originalTransactionType; - /** * @var array */ @@ -156,6 +149,14 @@ public function validateRequiredParameters() if (empty($this->getTerminalID())) { throw new ParameterValidationException('TerminalID is empty!'); } + + if (empty($this->getIntRef())) { + throw new ParameterValidationException('Internal reference is empty!'); + } + + if (empty($this->getRrn())) { + throw new ParameterValidationException('Payment reference is empty!'); + } } /** From 601630715bf20fd82f0a75f6d024ba30c69a05a4 Mon Sep 17 00:00:00 2001 From: Blum Date: Tue, 6 Jul 2021 09:54:36 +0300 Subject: [PATCH 3/3] Cleaning test alredy executed. --- tests/ReversaslRequestTest.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/ReversaslRequestTest.php b/tests/ReversaslRequestTest.php index 2b262cc..b74c289 100644 --- a/tests/ReversaslRequestTest.php +++ b/tests/ReversaslRequestTest.php @@ -6,7 +6,6 @@ namespace VenelinIliev\Borica3ds\Tests; -use VenelinIliev\Borica3ds\SaleRequest; use VenelinIliev\Borica3ds\ReversalRequest; use VenelinIliev\Borica3ds\Exceptions\SignatureException; use VenelinIliev\Borica3ds\Exceptions\ParameterValidationException; @@ -52,16 +51,4 @@ public function testData() 'P_SIGN' => '765C034CC1AC9213B59AE0D251C48B3883AA44DF73A57E4E12F73786B4847F42D40AF5CAFC33F2A065DF5EF28C095FBA373F4A9D08CD27CCEA9447CFB49A80404A1CB3067223C8AF0964C6A7E7960C43CB4B3C75FF9063C8931C0457CB6B43D9F536DD32950AF90E05A887079149415D1FFA1017ED716696D256FE9DBF69D6C4CB10AB71F60C7405A90E4E111CE37C62901A02188A74D7BA84FABB02E0D877DA998E29DEF0057CD5EAF5CAD0C6132EDB2A9CAE0556FD360F26BD47869802B0EB3C40D1205524AEA0FF5CF413A887F66DD032FCD09C5281C834B072BAEDF92950F4BCE2DA32A4A6E9392B46F9FA55BD08E64F16EE096CCD43DA4F077FFC5A9700' ], $saleData); } - - /** - * @return void - * @throws ParameterValidationException|SignatureException - */ - public function testBackRefValidation() - { - $this->expectException(ParameterValidationException::class); - - (new SaleRequest()) - ->setBackRefUrl('wrong url value'); - } }