Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ composer require paysgator/paysgator-php

### Configuration

For most requests, you simply need to provide your API Key.
For most requests, you simply need to provide your API Key using environment variables.

```php
require 'vendor/autoload.php';

use Paysgator\PaysgatorClient;

$client = new PaysgatorClient([
'api_key' => 'YOUR_API_KEY',
'api_key' => getenv('PAYSGATOR_API_KEY'),
]);
```
> **Security Warning:** Do not hardcode API keys. Store them in environment variables.

### Create Payment

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
"guzzlehttp/guzzle": "^7.8"
},
"autoload": {
Expand Down
11 changes: 9 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
'base_url' => 'https://paysgator.com/api/v1/', // Optional, defaults to production
]);

$apiKey="";
$walletId="";
$apiKey = getenv('PAYSGATOR_API_KEY') ?: '';
$walletId = getenv('PAYSGATOR_WALLET_ID') ?: '';

// Authenticate to get a new token (automatically sets it on the client)
try {

$response = $client->auth()->authenticate($apiKey, $walletId);
$token = $response['accessToken'];

Expand All @@ -31,4 +33,9 @@
]);

print_r($directCharge);
} catch (\Exception $e) {
error_log($e->getMessage());
echo 'An error occurred: ' . $e->getMessage();
}


28 changes: 17 additions & 11 deletions src/PaysgatorClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
namespace Paysgator;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Paysgator\Resources\Payments;
use Paysgator\Resources\Subscriptions;
use Paysgator\Resources\Transactions;
use Paysgator\Resources\Wallet;

class PaysgatorClient
{
private $client;
private $apiKey;
private $baseUrl = 'https://paysgator.com/api/v1/';
private Client $client;
private ?string $apiKey = null;
private string $baseUrl = 'https://paysgator.com/api/v1/';

public function __construct(array $config = [])
{
$this->baseUrl = $config['base_url'] ?? $this->baseUrl;
$this->apiKey = $config['api_key'] ?? null;
$apiKey = $config['api_key'] ?? null;
if ($apiKey !== null && !is_string($apiKey)) {
throw new \InvalidArgumentException('API Key must be a string');
}
$this->apiKey = $apiKey;

$guzzleConfig = [
'base_uri' => $this->baseUrl,
Expand All @@ -35,35 +38,38 @@ public function __construct(array $config = [])
$this->client = new Client($guzzleConfig);
}

public function setApiKey($key)
public function setApiKey(string $key)
{
if ($key === '') {
throw new \InvalidArgumentException('API Key must be a non-empty string');
}
$this->apiKey = $key;
$config = $this->client->getConfig();
$config['headers']['X-Api-Key'] = $key;
$this->client = new Client($config);
}

public function getHttpClient()
public function getHttpClient(): Client
{
return $this->client;
}

public function payments()
public function payments(): Payments
{
return new Payments($this);
}

public function subscriptions()
public function subscriptions(): Subscriptions
{
return new Subscriptions($this);
}

public function transactions()
public function transactions(): Transactions
{
return new Transactions($this);
}

public function wallet()
public function wallet(): Wallet
{
return new Wallet($this);
}
Expand Down
39 changes: 27 additions & 12 deletions src/Resources/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Paysgator\Resources;

use Paysgator\PaysgatorClient;

class Payments
{
private $client;

public function __construct(PaysgatorClient $client)
public function __construct($client)
{
$this->client = $client;
}
Expand All @@ -21,11 +20,19 @@ public function __construct(PaysgatorClient $client)
*/
public function create(array $data)
{
$response = $this->client->getHttpClient()->post('payment/create', [
'json' => $data,
]);

return json_decode($response->getBody()->getContents(), true);
try {
$response = $this->client->getHttpClient()->post('payment/create', [
'json' => $data,
]);

if ($response->getStatusCode() >= 400) {
throw new \Exception('API Error: ' . $response->getStatusCode());
}

return json_decode($response->getBody()->getContents(), true);
} catch (\Exception $e) {
throw $e;
}
}

/**
Expand All @@ -36,10 +43,18 @@ public function create(array $data)
*/
public function confirm(array $data)
{
$response = $this->client->getHttpClient()->post('payment/confirm', [
'json' => $data,
]);

return json_decode($response->getBody()->getContents(), true);
try {
$response = $this->client->getHttpClient()->post('payment/confirm', [
'json' => $data,
]);

if ($response->getStatusCode() >= 400) {
throw new \Exception('API Error: ' . $response->getStatusCode());
}

return json_decode($response->getBody()->getContents(), true);
} catch (\Exception $e) {
throw $e;
}
}
}
19 changes: 14 additions & 5 deletions src/Resources/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Paysgator\Resources;

use Paysgator\PaysgatorClient;
use GuzzleHttp\Exception\GuzzleException;

class Subscriptions
{
private $client;

public function __construct(PaysgatorClient $client)
public function __construct($client)
{
$this->client = $client;
}
Expand All @@ -22,10 +23,18 @@ public function __construct(PaysgatorClient $client)
*/
public function update($id, $action)
{
$response = $this->client->getHttpClient()->patch("subscriptions/{$id}", [
'json' => ['action' => $action],
]);
try {
$response = $this->client->getHttpClient()->patch("subscriptions/{$id}", [
'json' => ['action' => $action],
]);

return json_decode($response->getBody()->getContents(), true);
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
throw new \RuntimeException('Subscription update failed with status code: ' . $response->getStatusCode());
}

return json_decode($response->getBody()->getContents(), true);
} catch (GuzzleException $e) {
throw new \RuntimeException('HTTP request failed: ' . $e->getMessage(), $e->getCode(), $e);
}
}
}
14 changes: 12 additions & 2 deletions src/Resources/Transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Paysgator\Resources;

use Paysgator\PaysgatorClient;
use GuzzleHttp\Exception\RequestException;


class Transactions
{
Expand All @@ -21,8 +23,16 @@ public function __construct(PaysgatorClient $client)
*/
public function get($id)
{
$response = $this->client->getHttpClient()->get("transactions/{$id}");
try {
$response = $this->client->getHttpClient()->get("transactions/{$id}");

if ($response->getStatusCode() >= 400) {
throw new RequestException('API Error', $response->getRequest(), $response);
}

return json_decode($response->getBody()->getContents(), true);
return json_decode($response->getBody()->getContents(), true);
} catch (RequestException $e) {
throw $e;
}
}
}
14 changes: 12 additions & 2 deletions src/Resources/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Paysgator\Resources;

use Paysgator\PaysgatorClient;
use GuzzleHttp\Exception\GuzzleException;


class Wallet
{
Expand All @@ -20,8 +22,16 @@ public function __construct(PaysgatorClient $client)
*/
public function getBalance()
{
$response = $this->client->getHttpClient()->get('wallet/balance');
try {
$response = $this->client->getHttpClient()->get('wallet/balance');

if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Failed to get wallet balance: ' . $response->getReasonPhrase());
}

return json_decode($response->getBody()->getContents(), true);
return json_decode($response->getBody()->getContents(), true);
} catch (GuzzleException $e) {
throw new \RuntimeException('Wallet API error: ' . $e->getMessage(), $e->getCode(), $e);
}
}
}