Skip to content

Commit

Permalink
feat: more API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Oct 31, 2021
1 parent d844ec7 commit d6e4f55
Show file tree
Hide file tree
Showing 11 changed files with 677 additions and 5 deletions.
87 changes: 87 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,91 @@ public function fetchAuthTokenRaw(): ResponseInterface
$options
);
}

public function fetchBalanceRaw(string $accountNumber): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/balance', [
'accountNumber' => $accountNumber,
]);
}

public function fetchAccountRaw(string $bankCode, string $accountNumber): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/accountEnquiry', [
'destinationBankCode' => $bankCode,
'accountNumber' => $accountNumber,
]);
}

public function fetchDomesticAccountRaw(string $accountNumber): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/domAccountEnquiry', [
'accountNumber' => $accountNumber,
]);
}

public function fetchDomesticTransactionRaw(string $reference): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/domTransaction', [
'transactionReference' => $reference,
]);
}

public function sendDomesticTransaction(TransactionInterface $transaction): ResponseInterface
{
if ($transaction instanceof SourceModelInterface) {
$this->setSourceModel($transaction);
}

return $this->performRequest(HttpMethodEnum::POST(), 'api/transaction/zenithDomTransfer', [
'transactionReference' => $transaction->getReference(),
'paymentReference' => $transaction->getReference(),
'senderName' => $transaction->getSenderName(),
'beneficiaryName' => $transaction->getRecipientName(),
'crAccount' => $transaction->getRecipientAccount(),
'drAccount' => $transaction->getDebitAccount(),
'amount' => $transaction->getAmount(),
'resend' => $transaction->shouldResend(),
]);
}

public function fetchTransactionRaw(string $reference): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/transaction', [
'transactionReference' => $reference,
]);
}

public function transactionLookupRaw(string $accountNumber, \DateTime $transactionDate): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::POST(), 'api/enquiry/transactionLookup', [
'accountNumber' => $accountNumber,
'transactionDate' => $transactionDate->format('Y-m-d'),
]);
}

/**
* @param HttpMethodEnum $method
* @param string $uri
* @param array<mixed> $data
* @return ResponseInterface
*/
private function performRequest(HttpMethodEnum $method, string $uri, array $data): ResponseInterface
{
$options = [
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . (string) $this->getAuthToken(),
],
\GuzzleHttp\RequestOptions::JSON => $data,
];

if ($this->getSourceModel()) {
$options[\BrokeYourBike\HasSourceModel\Enums\RequestOptions::SOURCE_MODEL] = $this->getSourceModel();
}

$uri = (string) $this->resolveUriFor($this->config->getUrl(), $uri);
return $this->httpClient->request((string) $method, $uri, $options);
}
}
23 changes: 23 additions & 0 deletions src/Interfaces/TransactionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\ZenithBank\Interfaces;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
interface TransactionInterface
{
public function getReference(): string;
public function getSenderName(): string;
public function getRecipientName(): string;
public function getRecipientAccount(): string;
public function getDebitAccount(): string;
public function getAmount(): float;
public function shouldResend(): bool;
}
68 changes: 68 additions & 0 deletions tests/FetchAccountRawTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\ZenithBank\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface;
use BrokeYourBike\ZenithBank\Client;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class FetchAccountRawTest extends TestCase
{
private string $authToken = 'secure-token';
private string $bankCode = '12345';
private string $accountNumber = '654987';

/**
* @test
* @dataProvider isLiveProvider
*/
public function it_can_prepare_request(bool $isLive): void
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('isLive')->willReturn($isLive);
$mockedConfig->method('getUrl')->willReturn('https://api.example/');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/api/enquiry/accountEnquiry',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->authToken}",
],
\GuzzleHttp\RequestOptions::JSON => [
'destinationBankCode' => $this->bankCode,
'accountNumber' => $this->accountNumber,
],
],
])->once();

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
$mockedCache->method('has')->willReturn(true);
$mockedCache->method('get')->willReturn($this->authToken);

/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);

$requestResult = $api->fetchAccountRaw($this->bankCode, $this->accountNumber);

$this->assertInstanceOf(ResponseInterface::class, $requestResult);
}
}
6 changes: 1 addition & 5 deletions tests/FetchAuthTokenRawTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function it_can_prepare_request(bool $isLive): void
$mockedConfig->method('getUsername')->willReturn($this->username);
$mockedConfig->method('getPassword')->willReturn($this->password);

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')->willReturn('{}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
Expand All @@ -52,7 +48,7 @@ public function it_can_prepare_request(bool $isLive): void
'userProtector' => $this->password,
],
],
])->once()->andReturn($mockedResponse);
])->once();

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();

Expand Down
66 changes: 66 additions & 0 deletions tests/FetchBalanceRawTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\ZenithBank\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface;
use BrokeYourBike\ZenithBank\Client;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class FetchBalanceRawTest extends TestCase
{
private string $authToken = 'secure-token';
private string $accountNumber = '654987';

/**
* @test
* @dataProvider isLiveProvider
*/
public function it_can_prepare_request(bool $isLive): void
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('isLive')->willReturn($isLive);
$mockedConfig->method('getUrl')->willReturn('https://api.example/');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/api/enquiry/balance',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->authToken}",
],
\GuzzleHttp\RequestOptions::JSON => [
'accountNumber' => $this->accountNumber,
],
],
])->once();

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
$mockedCache->method('has')->willReturn(true);
$mockedCache->method('get')->willReturn($this->authToken);

/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);

$requestResult = $api->fetchBalanceRaw($this->accountNumber);

$this->assertInstanceOf(ResponseInterface::class, $requestResult);
}
}
66 changes: 66 additions & 0 deletions tests/FetchDomesticAccountRawTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\ZenithBank\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\ZenithBank\Interfaces\ConfigInterface;
use BrokeYourBike\ZenithBank\Client;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class FetchDomesticAccountRawTest extends TestCase
{
private string $authToken = 'secure-token';
private string $accountNumber = '654987';

/**
* @test
* @dataProvider isLiveProvider
*/
public function it_can_prepare_request(bool $isLive): void
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('isLive')->willReturn($isLive);
$mockedConfig->method('getUrl')->willReturn('https://api.example/');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'POST',
'https://api.example/api/enquiry/domAccountEnquiry',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => "Bearer {$this->authToken}",
],
\GuzzleHttp\RequestOptions::JSON => [
'accountNumber' => $this->accountNumber,
],
],
])->once();

$mockedCache = $this->getMockBuilder(CacheInterface::class)->getMock();
$mockedCache->method('has')->willReturn(true);
$mockedCache->method('get')->willReturn($this->authToken);

/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* @var CacheInterface $mockedCache
* */
$api = new Client($mockedConfig, $mockedClient, $mockedCache);

$requestResult = $api->fetchDomesticAccountRaw($this->accountNumber);

$this->assertInstanceOf(ResponseInterface::class, $requestResult);
}
}
Loading

0 comments on commit d6e4f55

Please sign in to comment.