-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2021. Venelin Iliev. | ||
* https://veneliniliev.com | ||
*/ | ||
|
||
namespace VenelinIliev\Borica3ds; | ||
|
||
use VenelinIliev\Borica3ds\Enums\TransactionType; | ||
use VenelinIliev\Borica3ds\Exceptions\ParameterValidationException; | ||
use VenelinIliev\Borica3ds\Exceptions\SendingException; | ||
|
||
class ReversalRequest extends Request implements RequestInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $sendResponse; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $rrn; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $intRef; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $merchantName; | ||
|
||
/** | ||
* StatusCheckRequest constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/* | ||
* Copyright (c) 2021. Venelin Iliev. | ||
* https://veneliniliev.com | ||
*/ | ||
|
||
namespace VenelinIliev\Borica3ds\Tests; | ||
|
||
use VenelinIliev\Borica3ds\ReversalRequest; | ||
use VenelinIliev\Borica3ds\Exceptions\SignatureException; | ||
use VenelinIliev\Borica3ds\Exceptions\ParameterValidationException; | ||
|
||
class ReversaslRequestTest extends TestCase | ||
{ | ||
/** | ||
* @throws ParameterValidationException|SignatureException | ||
*/ | ||
public function testData() | ||
{ | ||
$saleData = (new ReversalRequest()) | ||
->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); | ||
} | ||
} |