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/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..3159fa1 --- /dev/null +++ b/src/ReversalRequest.php @@ -0,0 +1,216 @@ +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!'); + } + + if (empty($this->getIntRef())) { + throw new ParameterValidationException('Internal reference is empty!'); + } + + if (empty($this->getRrn())) { + throw new ParameterValidationException('Payment reference 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..b74c289 --- /dev/null +++ b/tests/ReversaslRequestTest.php @@ -0,0 +1,54 @@ +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); + } +}