Skip to content

Commit

Permalink
feat: client
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed May 14, 2023
1 parent 13ee9a8 commit 58b917e
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 3 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ jobs:

steps:
- uses: actions/checkout@v3

- uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php-version }}"
coverage: pcov

- run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- nrun: vendor/bin/phpunit --coverage-clover=coverage.xml

- uses: paambaati/codeclimate-action@v4
Expand Down
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "brokeyourbike/parallex-bank-api-client",
"description": "Parallex Bank API Client for PHP",
"keywords": [
"api",
"api-client",
"parallex-bank",
"brokeyourbike"
],
"type": "library",
"license": "MPL-2.0",
"autoload": {
"psr-4": {
"BrokeYourBike\\ParallexBank\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BrokeYourBike\\ParallexBank\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Ivan Stasiuk",
"email": "[email protected]",
"homepage": "https://stasi.uk"
}
],
"minimum-stability": "stable",
"require": {
"php": "^8.1",
"brokeyourbike/http-client": "^1.0",
"brokeyourbike/resolve-uri": "^1.0",
"brokeyourbike/http-enums": "^2.0",
"brokeyourbike/has-source-model": "^2.0",
"brokeyourbike/data-transfer-object": "^0.2.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"phpunit/phpunit": "^9",
"mockery/mockery": "^1.4",
"nesbot/carbon": "^2.53"
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
}
}
79 changes: 79 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank;

use GuzzleHttp\ClientInterface;
use BrokeYourBike\ResolveUri\ResolveUriTrait;
use BrokeYourBike\ParallexBank\Models\TransactionResponse;
use BrokeYourBike\ParallexBank\Interfaces\TransactionInterface;
use BrokeYourBike\ParallexBank\Interfaces\ConfigInterface;
use BrokeYourBike\HttpEnums\HttpMethodEnum;
use BrokeYourBike\HttpClient\HttpClientTrait;
use BrokeYourBike\HttpClient\HttpClientInterface;
use BrokeYourBike\HasSourceModel\SourceModelInterface;
use BrokeYourBike\HasSourceModel\HasSourceModelTrait;

/**
* @author Ivan Stasiuk <[email protected]>
*/
class Client implements HttpClientInterface
{
use HttpClientTrait;
use ResolveUriTrait;
use HasSourceModelTrait;

private ConfigInterface $config;

public function __construct(ConfigInterface $config, ClientInterface $httpClient)
{
$this->config = $config;
$this->httpClient = $httpClient;
}

public function getConfig(): ConfigInterface
{
return $this->config;
}

public function postTransaction(string $requestId, TransactionInterface $transaction): TransactionResponse
{
$options = [
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Client-Id' => $this->config->getClientId(),
'Client-Key' => $this->config->getClientSecret(),
],
\GuzzleHttp\RequestOptions::JSON => [
'BankId' => $transaction->getBankCode(),
'TrnType' => 'T',
'TrnSubType' => 'CI',
'RequestID' => $requestId,
'PartTrnRec' => [
[
'AcctId' => $transaction->getBankAccount(),
'CreditDebitFlg' => 'D',
'TrnAmt' => $transaction->getAmount(),
'CurrencyCode' => $transaction->getCurrencyCode(),
'TrnParticulars' => $transaction->getReference(),
'ValueDt' => $transaction->getValueDate()->format('Y-m-d\TH:i:s.uP'),
]
],
],
];

if ($transaction instanceof SourceModelInterface){
$options[\BrokeYourBike\HasSourceModel\Enums\RequestOptions::SOURCE_MODEL] = $transaction;
}

$uri = (string) $this->resolveUriFor($this->config->getUrl(), '/coreapi/api/finacle/PostingTransaction');
$response = $this->httpClient->request(HttpMethodEnum::POST->value, $uri, $options);

return new TransactionResponse($response);
}
}
15 changes: 15 additions & 0 deletions src/Enums/ErrorCodeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank\Enums;

/**
* @author Ivan Stasiuk <[email protected]>
*/
enum ErrorCodeEnum: string
{}
19 changes: 19 additions & 0 deletions src/Interfaces/ConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank\Interfaces;

/**
* @author Ivan Stasiuk <[email protected]>
*/
interface ConfigInterface
{
public function getUrl(): string;
public function getClientId(): string;
public function getClientSecret(): string;
}
24 changes: 24 additions & 0 deletions src/Interfaces/TransactionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank\Interfaces;

use Carbon\CarbonImmutable;

/**
* @author Ivan Stasiuk <[email protected]>
*/
interface TransactionInterface
{
public function getReference(): string;
public function getAmount(): float;
public function getCurrencyCode(): string;
public function getBankCode(): string;
public function getBankAccount(): string;
public function getValueDate(): CarbonImmutable;
}
19 changes: 19 additions & 0 deletions src/Models/TransactionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank\Models;

use Spatie\DataTransferObject\Attributes\MapFrom;
use BrokeYourBike\DataTransferObject\JsonResponse;

/**
* @author Ivan Stasiuk <[email protected]>
*/
class TransactionResponse extends JsonResponse
{}

61 changes: 61 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank\Tests;

use BrokeYourBike\ResolveUri\ResolveUriTrait;
use BrokeYourBike\ParallexBank\Interfaces\ConfigInterface;
use BrokeYourBike\ParallexBank\Client;
use BrokeYourBike\HttpClient\HttpClientTrait;
use BrokeYourBike\HttpClient\HttpClientInterface;
use BrokeYourBike\HasSourceModel\HasSourceModelTrait;

/**
* @author Ivan Stasiuk <[email protected]>
*/
class ClientTest extends TestCase
{
/** @test */
public function it_implemets_http_client_interface(): void
{
/** @var ConfigInterface */
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();

/** @var \GuzzleHttp\ClientInterface */
$mockedHttpClient = $this->getMockBuilder(\GuzzleHttp\ClientInterface::class)->getMock();

$api = new Client($mockedConfig, $mockedHttpClient);

$this->assertInstanceOf(HttpClientInterface::class, $api);
$this->assertSame($mockedConfig, $api->getConfig());
}

/** @test */
public function it_uses_http_client_trait(): void
{
$usedTraits = class_uses(Client::class);

$this->assertArrayHasKey(HttpClientTrait::class, $usedTraits);
}

/** @test */
public function it_uses_resolve_uri_trait(): void
{
$usedTraits = class_uses(Client::class);

$this->assertArrayHasKey(ResolveUriTrait::class, $usedTraits);
}

/** @test */
public function it_uses_has_source_model_trait(): void
{
$usedTraits = class_uses(Client::class);

$this->assertArrayHasKey(HasSourceModelTrait::class, $usedTraits);
}
}
21 changes: 21 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

// Copyright (C) 2023 Ivan Stasiuk <[email protected]>.
//
// 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\ParallexBank\Tests;

/**
* @author Ivan Stasiuk <[email protected]>
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
protected function tearDown(): void
{
parent::tearDown();
\Mockery::close();
}
}

0 comments on commit 58b917e

Please sign in to comment.