Skip to content

Commit

Permalink
Merge pull request #88 from mewebstudio/psr-logger-integration
Browse files Browse the repository at this point in the history
PSR-18 HTTP Client | PSR-3 logger support
  • Loading branch information
nuryagdym authored Sep 3, 2022
2 parents 852f41c + e286d0e commit cecb30d
Show file tree
Hide file tree
Showing 49 changed files with 747 additions and 826 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.3]
php: ['7.2', '8.0']
dependency-version: [prefer-stable]
steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
composer.lock
/vendor
/.idea

/var/
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Bu paket ile amaçlanan; ortak bir arayüz sınıfı ile, tüm Türk banka sanal
- Sipariş/Ödeme geçmişi sorgulama (`AbstractGateway::TX_HISTORY`)
- Sipariş/Para iadesi yapma (`AbstractGateway::TX_REFUND`)
- Sipariş iptal etme (`AbstractGateway::TX_CANCEL`)
- [PSR-3](https://www.php-fig.org/psr/psr-3/) logger desteği
- [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP Client desteği

#### Farkli Gateway'ler Tek islem akisi
* Farklı bankaya geçiş yapmak için sadece doğru `AccountFactory` method'u kullanarak account degistirmek yeterli.
Expand All @@ -59,14 +61,21 @@ Son yapılan değişiklikler için [`CHANGELOG`](./docs/CHANGELOG.md).
- ext-json
- ext-openssl
- ext-SimpleXML

- PSR-18 HTTP Client

### Kurulum
Test sunucunuz üzerinde;
```sh
$ mkdir pos-test && cd pos-test
$ composer require mews/pos
```
Kütüphane belli bir HTTP Client'ile zorunlu bağımlılığı yoktur.
PSR-18 HTTP Client standarta uyan herhangi bir kütüphane kullanılabilinir.
Projenizde zaten kurulu PSR-18 uygulaması varsa otomatik onu kullanır.

Veya hızlı başlangıç için:
```sh
$ composer require php-http/curl-client nyholm/psr7 mews/pos
```
Diğer PSR-18 uygulamasını sağlayan kütühaneler: https://packagist.org/providers/psr/http-client-implementation

### Unit testler çalıştırma
Projenin root klasoründe bu satırı çalıştırmanız gerekiyor
Expand All @@ -82,6 +91,7 @@ $ ./vendor/bin/phpunit tests
require './vendor/autoload.php';

// API kullanıcı bilgileri
// AccountFactory kullanılacak method Gateway'e göre değişir. Örnek kodlara bakınız.
$account = \Mews\Pos\Factory\AccountFactory::createEstPosAccount(
'akbank', //pos config'deki ayarın index name'i
'yourClientID',
Expand Down Expand Up @@ -258,6 +268,20 @@ Shared hosting'lerde Cpanel'de gördüğünüz IP'den farklı olarak fiziksel su
O IP adres Cpanel'de gözükmez, hosting firmanızdan sorup öğrenmeniz gerekmekte.
Bu hatayı alırsanız hosting firmanın verdiği IP adrese'de banka gateway'i tarafından izin verilmesini sağlayın.

### Debugging
Kütühane [PSR-3](https://www.php-fig.org/psr/psr-3/) standarta uygun logger uygulamayı destekler.
Örnekler: https://packagist.org/providers/psr/log-implementation .

Monolog logger kullanım örnegi:
```shell
composer require monolog/monolog
```
```php
$handler = new \Monolog\Handler\StreamHandler(__DIR__.'/../var/log/pos.log', \Psr\Log\LogLevel::DEBUG);
$logger = new \Monolog\Logger('pos', [$handler]);
$pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account, null, null, $logger);
```

## Genel Kultur
### NonSecure, 3D Secure, 3DPay ve 3DHost ödeme modeller arasındaki farklar
- **3D** - Bankaya göre farklı isimler verilebilir, örn. 3D Full. Gateway'den (3D şifre girdiginiz sayfadan) döndükten sonra ödemeyi tamamlamak için banka gateway'ne 1 istek daha (_provizyon_ isteği) gönderir.
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
],
"require": {
"php": ">=7.2.5",
"ext-SimpleXML": "*",
"ext-dom": "*",
"ext-json": "*",
"ext-openssl": "*",
"ext-SimpleXML": "*",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"php-http/discovery": "^1.14",
"psr/http-client-implementation": "*",
"psr/log": "^1.1",
"symfony/http-foundation": "^5.0",
"symfony/serializer": "^5.0"
},
Expand All @@ -31,9 +33,12 @@
}
},
"require-dev": {
"escapestudios/symfony2-coding-standard": "^3.11",
"monolog/monolog": "^2.8",
"php-http/curl-client": "^2.2",
"phpunit/phpunit": "^8.3",
"slim/psr7": "^1.4",
"squizlabs/php_codesniffer": "^3.5",
"escapestudios/symfony2-coding-standard": "^3.11",
"symfony/var-dumper": "^5.1"
},
"config": {
Expand Down
28 changes: 28 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# Changelog

## [0.9.0] - 2022-09-03
### Changed
- Eski Gateway'e özel (orn. CreditCardEstPos) Kredi Kart sınıfları kaldırıldı.
`0.6.0` versiyonda tanıtılan `Mews\Pos\Entity\Card\CreditCard` kullanılacak.
### New Features
- `guzzlehttp/guzzle` hard coupling kaldırıldı.
Artık herhangi bir [PSR-18 HTTP Client](https://packagist.org/providers/psr/http-client-implementation) kullanılabılınır.
Bu degisiklikle beraber PSR-18 ve PSR-7 client kütüphaneleri kendiniz composer require ile yüklemeniz gerekiyor.

Örneğin:
```shell
composer require php-http/curl-client nyholm/psr7 mews/pos
```
Eğer projenizde zaten PSR-18 ve PSR-7 kütüphaneleri yüklü ise, otomatik onları bulur ve kullanır.
Kodda bir degişiklik gerektirmez.

- Gateway sınıflara **PSR-3** logger desteği eklendi.

Monolog logger kullanım örnegi:
```shell
composer require monolog/monolog
```
```php
$handler = new \Monolog\Handler\StreamHandler(__DIR__.'/../var/log/pos.log', \Psr\Log\LogLevel::DEBUG);
$logger = new \Monolog\Logger('pos', [$handler]);
$pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account, null, null, $logger);
```

## [0.7.0] - 2022-05-18
### Changed
- `\Mews\Pos\PosInterface::prepare()` method artık sipariş verilerini (_currency, id, amount, installment, transaction type_) değiştirmez/formatlamaz.
Expand Down
11 changes: 10 additions & 1 deletion examples/_main_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
function getGateway(\Mews\Pos\Entity\Account\AbstractPosAccount $account): ?\Mews\Pos\PosInterface
{
try {
$pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account);
$handler = new \Monolog\Handler\StreamHandler(__DIR__.'/../var/log/pos.log', \Psr\Log\LogLevel::DEBUG);
$logger = new \Monolog\Logger('pos', [$handler]);

/* $client = new HttpClient(
new \Http\Client\Curl\Client(),
new \Slim\Psr7\Factory\RequestFactory(),
new \Slim\Psr7\Factory\StreamFactory()
);*/

$pos = \Mews\Pos\Factory\PosFactory::createPosGateway($account, null, null, $logger);
$pos->setTestMode(true);

return $pos;
Expand Down
4 changes: 2 additions & 2 deletions examples/template/_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
</div>
<nav class="collapse navbar-collapse" id="bs-navbar">
<ul class="nav navbar-nav">
<li> <a href="<?= $hostUrl ?>/interpos/index.php">InterPos (Deniz bank)</a></li>
<li> <a href="<?= $hostUrl ?>/vakifbank/index.php">VPOS (VakifBank bank)</a></li>
<li> <a href="<?= $hostUrl ?>/akbank/index.php">EST POS</a></li>
<li> <a href="<?= $hostUrl ?>/finansbank-payfor/index.php">PayFor (Finansbank)</a></li>
<li> <a href="<?= $hostUrl ?>/garanti/index.php">Garanti POS</a></li>
<li> <a href="<?= $hostUrl ?>/interpos/index.php">InterPos (Deniz bank)</a></li>
<li> <a href="<?= $hostUrl ?>/vakifbank/index.php">VPOS (VakifBank bank)</a></li>
<li> <a href="<?= $hostUrl ?>/ykb/index.php">PosNet (YKB)</a></li>
<li> <a href="<?= $hostUrl ?>/kuveytpos/index.php">KuveytPOS</a></li>
</ul>
Expand Down
10 changes: 1 addition & 9 deletions examples/vakifbank/3d/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
require './_config.php';

$savedCard = $session->get('card');

$card = new \Mews\Pos\Entity\Card\CreditCardVakifBank(
$savedCard['number'],
$savedCard['year'],
$savedCard['month'],
$savedCard['cvv'],
$savedCard['name'],
$savedCard['type']
);
$card = createCard($pos, $savedCard);

require '../../template/_payment_response.php';
78 changes: 78 additions & 0 deletions src/Client/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Mews\Pos\Client;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use function http_build_query;

/**
* PSR18 HTTP Client wrapper
*/
class HttpClient
{
/** @var ClientInterface */
protected $client;

/** @var RequestFactoryInterface */
protected $requestFactory;

/** @var StreamFactoryInterface */
protected $streamFactory;

/**
* @param ClientInterface $client
* @param RequestFactoryInterface $requestFactory
* @param StreamFactoryInterface $streamFactory
*/
public function __construct(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory
) {
$this->client = $client;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
}

public function post(string $path, ?array $payload = []): ResponseInterface
{
return $this->send('POST', $path, $payload);
}

private function send(string $method, $path, ?array $payload = []): ResponseInterface
{
$request = $this->createRequest($method, $path, $payload);

return $this->client->sendRequest($request);
}

private function createRequest(string $method, string $url, ?array $payload = []): RequestInterface
{
$request = $this->requestFactory->createRequest($method, $url);

if ('POST' == $method) {
$body = null;
if (isset($payload['form_params'])) {
$request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded');
$payload['body'] = http_build_query($payload['form_params']);
}
if (isset($payload['body'])) {
$body = $this->streamFactory->createStream($payload['body']);
}
$request = $request->withBody($body);
}


if (isset($payload['headers'])) {
foreach ($payload['headers'] as $key => $value) {
$request = $request->withHeader($key, $value);
}
}

return $request;
}
}
71 changes: 32 additions & 39 deletions src/Entity/Card/AbstractCreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mews\Pos\Entity\Card;

use DateTimeImmutable;

/**
* Class AbstractCreditCard
*/
Expand All @@ -18,24 +20,13 @@ abstract class AbstractCreditCard
*/
protected $number;

/**
* @var \DateTimeImmutable
*/
protected $expireYear;

/**
* @var \DateTimeImmutable
*/
protected $expireMonth;
/** @var DateTimeImmutable */
protected $expDate;

/**
* @var string
*/
/** @var string */
protected $cvv;

/**
* @var string|null
*/
/** @var string|null */
protected $holderName;

/**
Expand All @@ -47,27 +38,17 @@ abstract class AbstractCreditCard
/**
* AbstractCreditCard constructor.
*
* @param string $number credit card number with or without spaces
* @param string $expireYear accepts year in 1, 2 and 4 digit format. accepted year formats '1' (2001), '02'
* (2002), '20' (2020), '2024' (2024)
* @param string $expireMonth single digit or double digit month values are accepted
* @param string $cvv
* @param string|null $cardHolderName
* @param string|null $cardType examples values: 'visa', 'master', '1', '2'
* @param string $number credit card number with or without spaces
* @param DateTimeImmutable $expDate
* @param string $cvv
* @param string|null $cardHolderName
* @param string|null $cardType examples values: 'visa', 'master', '1', '2'
*
* @throws \DomainException
*/
public function __construct(string $number, string $expireYear, string $expireMonth, string $cvv, ?string $cardHolderName = null, ?string $cardType = null)
public function __construct(string $number, DateTimeImmutable $expDate, string $cvv, ?string $cardHolderName = null, ?string $cardType = null)
{
$this->number = preg_replace('/\s+/', '', $number);

$yearFormat = 4 === strlen($expireYear) ? 'Y' : 'y';
$this->expireYear = \DateTimeImmutable::createFromFormat($yearFormat, $expireYear);
$this->expireMonth = \DateTimeImmutable::createFromFormat('m', $expireMonth);
if (!$this->expireYear || !$this->expireMonth) {
throw new \DomainException('INVALID DATE FORMAT');
}

$this->number = preg_replace('/\s+/', '', $number);
$this->expDate = $expDate;
$this->cvv = $cvv;
$this->holderName = $cardHolderName;
$this->type = $cardType;
Expand All @@ -84,27 +65,39 @@ public function getNumber(): string

/**
* returns exp year in 2 digit format
*
* @param string $format
*
* @return string
*/
public function getExpireYear(): string
public function getExpireYear(string $format = 'y'): string
{
return $this->expireYear->format('y');
return $this->expDate->format($format);
}

/**
* returns exp year in 2 digit format. i.e '01' '02' '12'
*
* @param string $format
*
* @return string
*/
public function getExpireMonth(): string
public function getExpireMonth(string $format = 'm'): string
{
return $this->expireMonth->format('m');
return $this->expDate->format($format);
}

/**
* returns card exp date month and year combined.
*
* @param string $format
*
* @return string
*/
abstract public function getExpirationDate(): string;
public function getExpirationDate(string $format = 'ym'): string
{
return $this->expDate->format($format);
}

/**
* @return string
Expand Down Expand Up @@ -133,7 +126,7 @@ public function setHolderName(?string $name)
/**
* @return string|null
*/
public function getType()
public function getType(): ?string
{
return $this->type;
}
Expand Down
Loading

0 comments on commit cecb30d

Please sign in to comment.