Skip to content

Commit e4b8321

Browse files
author
Tamara
committed
Add SDK
0 parents  commit e4b8321

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3741
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
.idea

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Tamara PHP SDK
2+
3+
Tamara PHP SDK is a wrapper for the Tamara API.
4+
5+
# Installation
6+
7+
Update `composer.json`
8+
9+
```yaml
10+
"require": {
11+
"tamara-solution/php-sdk": "1.0.0"
12+
}
13+
```
14+
15+
# Usage
16+
17+
```php
18+
$configuration = Configuration::create($apiUrl, $apiToken, $apiRequestTimeout, $transport);
19+
$client = Client::create($configuration);
20+
21+
$response = $client->getPaymentTypes('SA');
22+
23+
if ($response->isSuccess()) {
24+
var_dump($response->getPaymentTypes());
25+
}
26+
```
27+
28+
### Notification Service
29+
```php
30+
$notification = \Tamara\Notification\NotificationService::create('token-key');
31+
$message = $notification->processAuthoriseNotification();
32+
33+
var_dump($message->getOrderId());
34+
var_dump($message->getOrderStatus());
35+
var_dump($message->getData());
36+
```
37+
38+
##### Symfony DI
39+
```yaml
40+
tamarapay.configuration:
41+
factory: ['Tamara\Configuration', create]
42+
arguments:
43+
- https://api.tamarapay.com
44+
- test_token
45+
46+
tamarapay.client:
47+
factory: ['Tamara\Client', create]
48+
arguments: ['@tamarapay.configuration']
49+
```
50+
51+
# Notes
52+
- We use Guzzlehttp library for the http client transport
53+
- You can use your own transport service and just need to implement the `Psr\Http\Client\ClientInterface`

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "tamara-solution/php-sdk",
3+
"license": "MIT",
4+
"description": "Tamara PHP Client Library",
5+
"keywords": [
6+
"tamara",
7+
"tamara php sdk",
8+
"buy now, pay later",
9+
"installment"
10+
],
11+
"homepage": "https://github.com/tamara-solution/php-sdk",
12+
"authors": [
13+
{
14+
"name": "Tamara Team",
15+
"email": "tech@tamara.co"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.1.0",
20+
"psr/http-client": "^1.0",
21+
"psr/log": "^1.0",
22+
"ext-json": "*",
23+
"symfony/http-foundation": "^4.1",
24+
"firebase/php-jwt": "^5.1"
25+
},
26+
"suggest": {
27+
"guzzlehttp/guzzle": "^6.0"
28+
},
29+
"require-dev": {
30+
"guzzlehttp/guzzle": "^6.0",
31+
"phpunit/phpunit": "^7",
32+
"roave/security-advisories": "dev-master"
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"Tamara\\": "src/Tamara"
37+
}
38+
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"Tamara\\Tests\\": "tests/"
42+
}
43+
}
44+
}

src/Tamara/Client.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Tamara;
4+
5+
use Tamara\HttpClient\HttpClient;
6+
use Tamara\Request\Order\AuthoriseOrderRequest;
7+
use Tamara\Request\Checkout\CreateCheckoutRequest;
8+
use Tamara\Request\Checkout\GetPaymentTypesRequest;
9+
use Tamara\Request\Order\CancelOrderRequest;
10+
use Tamara\Request\Payment\CaptureRequest;
11+
use Tamara\Request\Payment\RefundRequest;
12+
use Tamara\Request\RequestDispatcher;
13+
use Tamara\Response\ClientResponse;
14+
use Tamara\Response\Order\AuthoriseOrderResponse;
15+
use Tamara\Response\Checkout\GetPaymentTypesResponse;
16+
use Tamara\Response\Checkout\CreateCheckoutResponse;
17+
use Tamara\Response\Payment\CancelResponse;
18+
use Tamara\Response\Payment\CaptureResponse;
19+
use Tamara\Response\Payment\RefundResponse;
20+
21+
class Client
22+
{
23+
/**
24+
* @var string
25+
*/
26+
public const VERSION = '1.0.0';
27+
28+
/**
29+
* @var HttpClient
30+
*/
31+
protected $httpClient;
32+
33+
/**
34+
* @var RequestDispatcher
35+
*/
36+
private $requestDispatcher;
37+
38+
/**
39+
* @param Configuration $configuration
40+
*
41+
* @return Client
42+
*/
43+
public static function create(Configuration $configuration): Client
44+
{
45+
return new static($configuration->createHttpClient());
46+
}
47+
48+
/**
49+
* @param HttpClient $httpClient
50+
*/
51+
public function __construct(HttpClient $httpClient)
52+
{
53+
$this->httpClient = $httpClient;
54+
$this->requestDispatcher = new RequestDispatcher($httpClient);
55+
}
56+
57+
/**
58+
* @param string $countryCode
59+
*
60+
* @return GetPaymentTypesResponse
61+
*
62+
* @throws Exception\RequestDispatcherException
63+
*/
64+
public function getPaymentTypes(string $countryCode): GetPaymentTypesResponse
65+
{
66+
return $this->requestDispatcher->dispatch(new GetPaymentTypesRequest($countryCode));
67+
}
68+
69+
/**
70+
* @param CreateCheckoutRequest $createCheckoutRequest
71+
*
72+
* @return CreateCheckoutResponse
73+
*
74+
* @throws Exception\RequestDispatcherException
75+
*/
76+
public function createCheckout(CreateCheckoutRequest $createCheckoutRequest): CreateCheckoutResponse
77+
{
78+
return $this->requestDispatcher->dispatch($createCheckoutRequest);
79+
}
80+
81+
/**
82+
* @param AuthoriseOrderRequest $authoriseOrderRequest
83+
*
84+
* @return AuthoriseOrderResponse
85+
*
86+
* @throws Exception\RequestDispatcherException
87+
*/
88+
public function authoriseOrder(AuthoriseOrderRequest $authoriseOrderRequest): AuthoriseOrderResponse
89+
{
90+
return $this->requestDispatcher->dispatch($authoriseOrderRequest);
91+
}
92+
93+
/**
94+
* @param CancelOrderRequest $cancelOrderRequest
95+
*
96+
* @return CancelResponse
97+
*
98+
* @throws Exception\RequestDispatcherException
99+
*/
100+
public function cancelOrder(CancelOrderRequest $cancelOrderRequest): CancelResponse
101+
{
102+
return $this->requestDispatcher->dispatch($cancelOrderRequest);
103+
}
104+
105+
/**
106+
* @param CaptureRequest $captureRequest
107+
*
108+
* @return CaptureResponse
109+
*
110+
* @throws Exception\RequestDispatcherException
111+
*/
112+
public function capture(CaptureRequest $captureRequest): CaptureResponse
113+
{
114+
return $this->requestDispatcher->dispatch($captureRequest);
115+
}
116+
117+
/**
118+
* @param RefundRequest $refundRequest
119+
*
120+
* @return RefundResponse
121+
*
122+
* @throws Exception\RequestDispatcherException
123+
*/
124+
public function refund(RefundRequest $refundRequest): RefundResponse
125+
{
126+
return $this->requestDispatcher->dispatch($refundRequest);
127+
}
128+
}

src/Tamara/Configuration.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace Tamara;
4+
5+
use GuzzleHttp\Client;
6+
use Psr\Http\Client\ClientInterface;
7+
use Psr\Log\LoggerInterface;
8+
use Tamara\Factory\RequestFactory;
9+
use Tamara\HttpClient\GuzzleHttpAdapter;
10+
use Tamara\HttpClient\HttpClient;
11+
12+
class Configuration
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $apiUrl;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $apiToken;
23+
24+
/**
25+
* @var int in seconds
26+
*/
27+
protected $apiRequestTimeout = 30;
28+
29+
/**
30+
* @var ClientInterface
31+
*/
32+
protected $transport;
33+
34+
/**
35+
* @var LoggerInterface
36+
*/
37+
protected $logger;
38+
39+
/**
40+
* @param string $apiUrl
41+
* @param string $apiToken
42+
* @param int|null $apiRequestTimeout
43+
* @param LoggerInterface $logger
44+
* @param ClientInterface|null $transport
45+
*
46+
* @return Configuration
47+
*/
48+
public static function create(
49+
string $apiUrl,
50+
string $apiToken,
51+
int $apiRequestTimeout = null,
52+
LoggerInterface $logger = null,
53+
ClientInterface $transport = null
54+
): Configuration {
55+
return new static($apiUrl, $apiToken, $apiRequestTimeout, $logger, $transport);
56+
}
57+
58+
/**
59+
* @param string $apiUrl
60+
* @param string $apiToken
61+
* @param int|null $apiRequestTimeout
62+
* @param LoggerInterface $logger
63+
* @param ClientInterface|null $transport
64+
*/
65+
public function __construct(
66+
string $apiUrl,
67+
string $apiToken,
68+
int $apiRequestTimeout = null,
69+
LoggerInterface $logger = null,
70+
ClientInterface $transport = null
71+
) {
72+
$this->apiUrl = $apiUrl;
73+
$this->apiToken = $apiToken;
74+
75+
if (null !== $apiRequestTimeout) {
76+
$this->apiRequestTimeout = $apiRequestTimeout;
77+
}
78+
79+
$this->logger = $logger;
80+
$this->transport = $transport;
81+
}
82+
83+
/**
84+
* @return HttpClient
85+
*/
86+
public function createHttpClient(): HttpClient
87+
{
88+
$transport = $this->transport ?? $this->createDefaultTransport();
89+
90+
return new HttpClient(
91+
$this->getApiUrl(),
92+
$this->getApiToken(),
93+
$transport,
94+
new RequestFactory()
95+
);
96+
}
97+
98+
/**
99+
* @return string
100+
*/
101+
public function getApiUrl(): string
102+
{
103+
return $this->apiUrl;
104+
}
105+
106+
/**
107+
* @return string
108+
*/
109+
public function getApiToken(): string
110+
{
111+
return $this->apiToken;
112+
}
113+
114+
/**
115+
* @return int
116+
*/
117+
public function getApiRequestTimeout(): int
118+
{
119+
return $this->apiRequestTimeout;
120+
}
121+
122+
/**
123+
* @return LoggerInterface|null
124+
*/
125+
public function getLogger(): ?LoggerInterface
126+
{
127+
return $this->logger;
128+
}
129+
130+
/**
131+
* @return ClientInterface
132+
*/
133+
protected function createDefaultTransport(): ClientInterface
134+
{
135+
return new GuzzleHttpAdapter(new Client(), $this->getApiRequestTimeout(), $this->logger);
136+
}
137+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamara\Exception;
6+
7+
use Exception;
8+
9+
class RequestDispatcherException extends Exception
10+
{
11+
}

0 commit comments

Comments
 (0)