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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ composer require paysgator/paysgator-php

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

> **Security:** Never hardcode API keys. Use environment variables.
> **Error Handling:** All API calls throw exceptions. Wrap requests in try-catch blocks.


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

use Paysgator\PaysgatorClient;

$client = new PaysgatorClient([
'api_key' => 'YOUR_API_KEY',
'api_key' => getenv('PAYSGATOR_API_KEY'),
]);
```

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "paysgator/paysgator-php",
"description": "Paysgator API Client for PHP",
"version": "1.0.0",
"version": "2.0.0",
"type": "library",
"license": "MIT",
"authors": [
Expand All @@ -11,7 +11,7 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"guzzlehttp/guzzle": "^7.8"
},
"autoload": {
Expand Down
16 changes: 14 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
'base_url' => 'https://paysgator.com/api/v1/', // Optional, defaults to production
]);

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

if (empty($apiKey) || empty($walletId)) {
die('Error: Missing credentials. Set PAYSGATOR_API_KEY and PAYSGATOR_WALLET_ID environment variables.');
}


try {

// Authenticate to get a new token (automatically sets it on the client)
$response = $client->auth()->authenticate($apiKey, $walletId);
Expand All @@ -31,4 +38,9 @@
]);

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


5 changes: 4 additions & 1 deletion src/PaysgatorClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Paysgator;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Paysgator\Resources\Payments;
use Paysgator\Resources\Subscriptions;
use Paysgator\Resources\Transactions;
Expand Down Expand Up @@ -37,7 +36,11 @@ public function __construct(array $config = [])

public function setApiKey($key)
{
if ($this->apiKey === $key) {
return;
}
$this->apiKey = $key;

$config = $this->client->getConfig();
$config['headers']['X-Api-Key'] = $key;
$this->client = new Client($config);
Expand Down
35 changes: 25 additions & 10 deletions src/Resources/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Paysgator\Resources;

use Paysgator\PaysgatorClient;
use Exception;

class Payments
{
Expand All @@ -21,11 +22,18 @@ 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);
if (empty($data)) {
throw new Exception('Payment data cannot be empty');
}
try {
$response = $this->client->getHttpClient()->post('payment/create', [
'json' => $data,
]);

return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
throw new Exception('Failed to create payment: ' . $e->getMessage(), $e->getCode(), $e);
}
}

/**
Expand All @@ -36,10 +44,17 @@ 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);
if (empty($data)) {
throw new Exception('Payment data cannot be empty');
}
try {
$response = $this->client->getHttpClient()->post('payment/confirm', [
'json' => $data,
]);

return json_decode($response->getBody()->getContents(), true);
} catch (Exception $e) {
throw new Exception('Failed to confirm payment: ' . $e->getMessage(), $e->getCode(), $e);
}
}
}
23 changes: 19 additions & 4 deletions src/Resources/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@ public function __construct(PaysgatorClient $client)
*/
public function update($id, $action)
{
$response = $this->client->getHttpClient()->patch("subscriptions/{$id}", [
'json' => ['action' => $action],
]);
if (empty($id) || !is_string($id)) {
throw new \InvalidArgumentException('Subscription ID is required.');
}
if (empty($action) || !is_string($action)) {
throw new \InvalidArgumentException('Action is required.');
}

return json_decode($response->getBody()->getContents(), true);
try {
$response = $this->client->getHttpClient()->patch("subscriptions/{$id}", [
'json' => ['action' => $action],
]);

if ($response->getStatusCode() >= 400) {
throw new \RuntimeException('API request failed: ' . $response->getReasonPhrase());
}

return json_decode($response->getBody()->getContents(), true);
} catch (\Exception $e) {
throw new \RuntimeException('Failed to update subscription: ' . $e->getMessage(), 0, $e);
}
}
}
14 changes: 13 additions & 1 deletion src/Resources/Transactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Paysgator\Resources;

use Paysgator\PaysgatorClient;
use Exception;
use InvalidArgumentException;
use RuntimeException;


class Transactions
{
Expand All @@ -21,7 +25,15 @@ public function __construct(PaysgatorClient $client)
*/
public function get($id)
{
$response = $this->client->getHttpClient()->get("transactions/{$id}");
if (empty($id) || !is_string($id)) {
throw new InvalidArgumentException('Transaction ID must be a non-empty string');
}

try {
$response = $this->client->getHttpClient()->get("transactions/{$id}");
} catch (Exception $e) {
throw new RuntimeException('Failed to fetch transaction: ' . $e->getMessage(), 0, $e);
}

return json_decode($response->getBody()->getContents(), true);
}
Expand Down
17 changes: 14 additions & 3 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\RequestException;


class Wallet
{
Expand All @@ -18,10 +20,19 @@ public function __construct(PaysgatorClient $client)
*
* @return array
*/
public function getBalance()
public function getBalance(): array
{
$response = $this->client->getHttpClient()->get('wallet/balance');
try {
$response = $this->client->getHttpClient()->get('wallet/balance');
$data = json_decode($response->getBody()->getContents(), true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid JSON response from API');
}

return json_decode($response->getBody()->getContents(), true);
return $data;
} catch (RequestException $e) {
throw new \RuntimeException('API Request failed: ' . $e->getMessage(), $e->getCode(), $e);
}
}
}