diff --git a/README.md b/README.md index 0b064df..c08100c 100644 --- a/README.md +++ b/README.md @@ -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'), ]); ``` diff --git a/composer.json b/composer.json index 8c0d054..3cce55f 100644 --- a/composer.json +++ b/composer.json @@ -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": [ @@ -11,7 +11,7 @@ } ], "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "guzzlehttp/guzzle": "^7.8" }, "autoload": { diff --git a/index.php b/index.php index 970f7d0..d7649dd 100644 --- a/index.php +++ b/index.php @@ -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); @@ -31,4 +38,9 @@ ]); print_r($directCharge); +} catch (\Exception $e) { + error_log('Payment error: ' . $e->getMessage()); + echo 'An error occurred: ' . $e->getMessage(); +} + diff --git a/src/PaysgatorClient.php b/src/PaysgatorClient.php index ae8682b..cbca606 100644 --- a/src/PaysgatorClient.php +++ b/src/PaysgatorClient.php @@ -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; @@ -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); diff --git a/src/Resources/Payments.php b/src/Resources/Payments.php index 38ddcbb..3901f98 100644 --- a/src/Resources/Payments.php +++ b/src/Resources/Payments.php @@ -3,6 +3,7 @@ namespace Paysgator\Resources; use Paysgator\PaysgatorClient; +use Exception; class Payments { @@ -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); + } } /** @@ -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); + } } } diff --git a/src/Resources/Subscriptions.php b/src/Resources/Subscriptions.php index 0ffd103..999e438 100644 --- a/src/Resources/Subscriptions.php +++ b/src/Resources/Subscriptions.php @@ -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); + } } } diff --git a/src/Resources/Transactions.php b/src/Resources/Transactions.php index 3455b47..0c96f44 100644 --- a/src/Resources/Transactions.php +++ b/src/Resources/Transactions.php @@ -3,6 +3,10 @@ namespace Paysgator\Resources; use Paysgator\PaysgatorClient; +use Exception; +use InvalidArgumentException; +use RuntimeException; + class Transactions { @@ -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); } diff --git a/src/Resources/Wallet.php b/src/Resources/Wallet.php index f51805b..2a938ac 100644 --- a/src/Resources/Wallet.php +++ b/src/Resources/Wallet.php @@ -3,6 +3,8 @@ namespace Paysgator\Resources; use Paysgator\PaysgatorClient; +use GuzzleHttp\Exception\RequestException; + class Wallet { @@ -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); + } } }