Skip to content

Commit

Permalink
Merge branch 'Blum-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
veneliniliev committed Jul 6, 2021
2 parents 1abc48d + 6016307 commit 38d4cbc
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,36 @@ $verifiedResponseData = $statusCheckResponse->getResponseData();

// get field from borica response
$statusCheckResponse->getVerifiedData('<field from response. ex: ACTION');
```
### Reversal request
```php
$reversalRequest = (new ReversalRequest())
//->inDevelopment()
->setPrivateKey('\<path to certificate.key>', '<password / or use method from bottom>')
->setPublicKey('<path to public certificate.cer>')
->setTerminalID('<TID - V*******>')
->setAmount(123.32)
->setOrder(123456)
->setDescription('test reversal')
->setMerchantId('<MID - 15 chars>')
->setRrn('<RRN - Original transaction reference (From the sale response data)>')
->setIntRef('<INT_REF - Internal reference (From the sale response data)>')
;

//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
Expand Down
1 change: 1 addition & 0 deletions src/Enums/TransactionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ class TransactionType extends Enum
const REVERSAL_REQUEST = 22;
const REVERSAL_ADVICE = 24;
const TRANSACTION_STATUS_CHECK = 90;
const REVERSAL = 24;
}
216 changes: 216 additions & 0 deletions src/ReversalRequest.php
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;
}
}
54 changes: 54 additions & 0 deletions tests/ReversaslRequestTest.php
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);
}
}

0 comments on commit 38d4cbc

Please sign in to comment.