Skip to content

Commit

Permalink
feat: add client
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Mar 23, 2022
1 parent 013f388 commit 5590379
Show file tree
Hide file tree
Showing 20 changed files with 816 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
12 changes: 12 additions & 0 deletions .gitattributes
@@ -0,0 +1,12 @@
# Auto detect text files and perform LF normalization
* text=auto

.gitattributes export-ignore
.github export-ignore
.gitignore export-ignore
.github/ export-ignore
tests/ export-ignore
phpunit.xml export-ignore
phpstan-baseline.neon export-ignore
phpstan.neon export-ignore
psalm.xml export-ignore
27 changes: 27 additions & 0 deletions .gitignore
@@ -0,0 +1,27 @@
/vendor/
/vendor-bin/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
.env
Homestead.yaml
Homestead.json
/.vagrant
.phpunit.result.cache
composer.lock

*.p12
49 changes: 49 additions & 0 deletions composer.json
@@ -0,0 +1,49 @@
{
"name": "brokeyourbike/tingg-api-client",
"description": "Tingg API Client for PHP",
"type": "library",
"license": "MPL-2.0",
"keywords": [
"api",
"api-client",
"tingg",
"brokeyourbike"
],
"autoload": {
"psr-4": {
"BrokeYourBike\\Tingg\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BrokeYourBike\\Tingg\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Ivan Stasiuk",
"email": "ivan@stasi.uk",
"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/data-transfer-object": "^0.2"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"phpunit/phpunit": "^9.5",
"mockery/mockery": "^1.4",
"nesbot/carbon": "^2",
"larapack/dd": "^1.1"
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
}
}
6 changes: 6 additions & 0 deletions phpstan.neon
@@ -0,0 +1,6 @@
parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
level: max
paths:
- src
19 changes: 19 additions & 0 deletions phpunit.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
colors="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Tests">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
110 changes: 110 additions & 0 deletions src/Client.php
@@ -0,0 +1,110 @@
<?php

// Copyright (C) 2022 Ivan Stasiuk <ivan@stasi.uk>.
//
// 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\Tingg;

use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\ClientInterface;
use BrokeYourBike\Tingg\Models\PaymentsResponse;
use BrokeYourBike\Tingg\Interfaces\PaymentInterface;
use BrokeYourBike\Tingg\Interfaces\ConfigInterface;
use BrokeYourBike\ResolveUri\ResolveUriTrait;
use BrokeYourBike\HttpEnums\HttpMethodEnum;
use BrokeYourBike\HttpClient\HttpClientTrait;
use BrokeYourBike\HttpClient\HttpClientInterface;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
class Client implements HttpClientInterface
{
use HttpClientTrait;
use ResolveUriTrait;

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 postPayment(PaymentInterface $payment): PaymentsResponse
{
$extraData = [];

if ($this->config->getCallbackUrl()) {
$extraData['callBackUrl'] = $this->config->getCallbackUrl();
}

if ($payment->getProductCode()) {
$extraData['productCode'] = $payment->getProductCode();
}

$response = $this->performRequest(HttpMethodEnum::POST, '/', [
'countryCode' => $payment->getCountryCodeAlpha2(),
'function' => 'BEEP.postPayment',
'payload' => [
'credentials' => [
'username' => $this->config->getUsername(),
'password' => $this->config->getPassword(),
],
'packet' => [
[
'serviceCode' => $payment->getServiceCode(),
'MSISDN' => $payment->getSenderPhoneNumber(),
'invoiceNumber' => '',
'customerNames' => $payment->getSenderName(),
'accountNumber' => $payment->getRecipientPhoneNumber(),
'payerTransactionID' => $payment->getReference(),
'amount' => $payment->getAmount(),
'hubID' => '',
'narration' => $payment->getReference(),
'datePaymentReceived' => $payment->getDate()->format('Y-m-d H:i:s'),
'currencyCode' => $payment->getCurrencyCode(),
'extraData' => \json_encode($extraData),
],
],
],
]);
return new PaymentsResponse($response);
}

/**
* @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::HEADERS => [
'Accept' => 'application/json',
],
];

$option = match ($method) {
HttpMethodEnum::GET => \GuzzleHttp\RequestOptions::QUERY,
default => \GuzzleHttp\RequestOptions::JSON,
};

$options[$option] = $data;

// dd($options);

$uri = (string) $this->resolveUriFor($this->config->getUrl(), $uri);
// dd($uri);
return $this->httpClient->request($method->value, $uri, $options);
}
}
45 changes: 45 additions & 0 deletions src/Enums/AccountValidationStatusCodeEnum.php
@@ -0,0 +1,45 @@
<?php

// Copyright (C) 2022 Ivan Stasiuk <ivan@stasi.uk>.
//
// 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\Tingg\Enums;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
enum AccountValidationStatusCodeEnum: string
{
/**
* Invalid serviceID
*/
case INVALID_SERVICEID = '167';

/**
* Action on client profile account was successful
*/
case SUCCESS = '200';

/**
* Validation feature not available
*/
case VALIDATION_FEATURE_NOT_AVAILABLE = '301';

/**
* Invalid Account Number
*/
case INVALID_ACCOUNT_NUMBER = '306';

/**
* Account number provided is valid
*/
case ACCOUNT_NUMBER_IS_VALID = '307';

/**
* Bill info available
*/
case BILL_INFO_AVAILABLE = '308';
}
30 changes: 30 additions & 0 deletions src/Enums/AuthCodeEnum.php
@@ -0,0 +1,30 @@
<?php

// Copyright (C) 2022 Ivan Stasiuk <ivan@stasi.uk>.
//
// 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\Tingg\Enums;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
enum AuthCodeEnum: string
{
/**
* Client authenticated successfully
*/
case AUTH_SUCCESS = '131';

/**
* Client authentication failed
*/
case AUTH_FAILED = '132';

/**
* Generic failure status code with matching appropriate description
*/
case GENERIC_FAILURE = '174';
}
45 changes: 45 additions & 0 deletions src/Enums/BalanceStatusCodeEnum.php
@@ -0,0 +1,45 @@
<?php

// Copyright (C) 2022 Ivan Stasiuk <ivan@stasi.uk>.
//
// 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\Tingg\Enums;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
enum BalanceStatusCodeEnum: string
{
/**
* Generic exception occurred with matching appropriate description
*/
case GENERIC_EXCEPTION = '104';

/**
* Inactive service
*/
case INACTIVE_SERVICE = '106';

/**
* Invalid serviceID
*/
case INVALID_SERVICEID = '167';

/**
* Generic failure occurred with appropriate status description
*/
case GENERIC_FAILURE = '174';

/**
* Successfully able to query the float balance
*/
case SUCCESS = '200';

/**
* Failed to fetch the float balance
*/
case FAILED = '302';
}
31 changes: 31 additions & 0 deletions src/Enums/BillStatusCodeEnum.php
@@ -0,0 +1,31 @@
<?php

// Copyright (C) 2022 Ivan Stasiuk <ivan@stasi.uk>.
//
// 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\Tingg\Enums;

/**
* @author Ivan Stasiuk <ivan@stasi.uk>
*/
enum BillStatusCodeEnum: string
{
/**
* Invalid account number
*/
case INVALID_ACCOUNT_NUMBER = '306';

/**
* Bill information is available.
* The merchant has a bill for the account.
*/
case BILL_INFORMATION_IS_AVAILABLE = '308';

/**
* Bill information not available
*/
case BILL_INFORMATION_NOT_AVAILABLE = '309';
}

0 comments on commit 5590379

Please sign in to comment.