From e18b992e692498a774d6e7f65c5159ff968883e7 Mon Sep 17 00:00:00 2001 From: Waad Mawlood Date: Thu, 23 Nov 2023 11:46:42 +0300 Subject: [PATCH] added timeout and verifySsl for laravel 6.x --- config/zaincash.php | 22 +++++++++++++++++++ readme.md | 6 ++++++ src/BaseZainCash.php | 8 ++++++- src/Services/HttpClient.php | 11 ++++++++-- src/Traits/HttpClientRequests.php | 20 ++++++++++++----- src/Traits/Initialable.php | 10 +++++++++ src/Traits/getSetAttributes.php | 36 +++++++++++++++++++++++++++++++ 7 files changed, 105 insertions(+), 8 deletions(-) diff --git a/config/zaincash.php b/config/zaincash.php index 4815ca5..21b514f 100644 --- a/config/zaincash.php +++ b/config/zaincash.php @@ -95,4 +95,26 @@ */ 'min_amount' => env('ZAINCASH_MIN_AMOUNT', 1000), + /* + |-------------------------------------------------------------------------- + | timeout Request (in seconds) + |-------------------------------------------------------------------------- + | + | Set the timeout for the request to ZainCash's API. + | The default value is 10 seconds. + | make it 0 (zero) for unlimited timeout (not recommended). + */ + 'timeout' => env('ZAINCASH_TIMEOUT', 10), + + /* + |-------------------------------------------------------------------------- + | Verify SSL + |-------------------------------------------------------------------------- + | + | Set the verify SSL for the request to ZainCash's API. + | The default value is true. + | make it false for disable verify SSL (not recommended). + | if it is true and you used the `http` protocol so will get an error. so make it false. + */ + 'verify_ssl' => env('ZAINCASH_VERIFY_SSL', true), ]; diff --git a/readme.md b/readme.md index f3a098b..7669dc5 100644 --- a/readme.md +++ b/readme.md @@ -68,6 +68,8 @@ update config zaincash in `config/zaincash.php` or from `.env` file | prefix_order_id | string | wa3d_ | Prefix for the order ID. | | is_redirect | bool | false | Specify whether or not to redirect to the ZainCash payment page. If false, ZainCash returns a Transaction ID to the backend. If true, redirection after the request. | | min_amount | int | 1000 | Set the minimum amount for a valid transaction in Iraqi Dinar (IQD). Transactions with amounts less than this value will be considered invalid. | +| timeout | int | 10 | Set the timeout for the request in seconds. | +| verify_ssl | bool | true | Set the verify SSL for the request to ZainCash's API. |
@@ -84,6 +86,8 @@ ZAINCASH_IS_REDIRECT=false # optional default false ZAINCASH_MIN_AMOUNT=1000 # optional default 1000 ZAINCASH_TEST_URL=https://test.zaincash.iq/ # optional ZAINCASH_LIVE_URL=https://api.zaincash.iq/ # optional +ZAINCASH_TIMEOUT=10 # optional +ZAINCASH_VERIFY_SSL=true # optional ``` @@ -210,6 +214,8 @@ class PaymentController extends Controller | processingUrl |🔴| string-null | `getProcessingUrl()` | `setProcessingUrl($processingUrl)` | - | | processingOtpUrl |🔴| string-null | `getProcessingOtpUrl()` | `setProcessingOtpUrl($processingOtpUrl)` | - | | cancelUrl |🔴| string-null | `getCancelUrl()` | `setCancelUrl($cancelUrl)` | - | +| timeout |🔴| int-null | `getTimeout()` | `setTimeout($timeout)` | - | +| verifySsl |🔴| bool-null | `getVerifySsl()` | `setVerifySsl($verifySsl)` | - | ⚠️ `Important` column means that this attribute is constantly used and has no default value. On the contrary, we can change it, but it will take the default value from `config/zaincash.php`. diff --git a/src/BaseZainCash.php b/src/BaseZainCash.php index ec8e6d1..8fc748b 100644 --- a/src/BaseZainCash.php +++ b/src/BaseZainCash.php @@ -31,6 +31,8 @@ abstract class BaseZainCash protected $cancelUrl; protected $transactionID; protected $isReturnArray = false; + protected $timeout; + protected $verifySsl; public function __construct( $amount = null, @@ -41,7 +43,9 @@ public function __construct( $merchantId = null, $isTest = null, $language = null, - $baseUrl = null + $baseUrl = null, + $timeout = null, + $verifySsl = null ) { $this->amount = $amount; $this->serviceType = $serviceType; @@ -56,6 +60,8 @@ public function __construct( $this->isTest = $isTest; $this->language = $language; $this->baseUrl = $baseUrl; + $this->timeout = $timeout; + $this->verifySsl = $verifySsl; $this->initial(); } diff --git a/src/Services/HttpClient.php b/src/Services/HttpClient.php index f7cec9c..9e2696a 100644 --- a/src/Services/HttpClient.php +++ b/src/Services/HttpClient.php @@ -11,13 +11,20 @@ class HttpClient * @param string $url * @param array $data * @param array $headers + * @param int $timeout + * @param bool $verify * @return \Psr\Http\Message\ResponseInterface|array */ - public function httpPost(string $url, array $data = [], array $headers = []) + public function httpPost(string $url, array $data = [], array $headers = [], int $timeout = 10, bool $verify = false) { + set_time_limit($timeout); + try { - $client = new Client(); + $client = new Client([ + 'timeout' => $timeout, + 'verify' => $verify, + ]); $response = $client->post($url, [ 'headers' => $headers, diff --git a/src/Traits/HttpClientRequests.php b/src/Traits/HttpClientRequests.php index 4fefa07..c74360e 100644 --- a/src/Traits/HttpClientRequests.php +++ b/src/Traits/HttpClientRequests.php @@ -18,7 +18,9 @@ protected function createTransactionHttpClient(string $token) $body, [ 'Content-Type' => 'application/x-www-form-urlencoded' - ] + ], + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -35,7 +37,9 @@ protected function sendRequestCheckTransaction(string $token) $body, [ 'Content-Type' => 'application/x-www-form-urlencoded' - ] + ], + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -56,7 +60,9 @@ protected function sendRequestProcessingTransaction(string $phonenumber, string ], [ 'Content-Type' => 'application/x-www-form-urlencoded' - ] + ], + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -79,7 +85,9 @@ protected function sendRequestPayTransaction(string $phonenumber, string $pin, s ], [ 'Content-Type' => 'application/x-www-form-urlencoded' - ] + ], + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -97,7 +105,9 @@ protected function sendRequestCancelTransaction() ], [ 'Content-Type' => 'application/x-www-form-urlencoded' - ] + ], + $this->getTimeout(), + $this->getVerifySsl() ); } } diff --git a/src/Traits/Initialable.php b/src/Traits/Initialable.php index f30e1a0..0f2dd45 100644 --- a/src/Traits/Initialable.php +++ b/src/Traits/Initialable.php @@ -46,6 +46,16 @@ protected function initial() $this->setIsRedirect($this->getConfig("is_redirect")); } + // Set the timeout request. + if (is_null($this->getTimeout())) { + $this->setTimeout($this->getConfig("timeout")); + } + + // Set the verify SSL. + if (is_null($this->getVerifySsl())) { + $this->setVerifySsl($this->getConfig("verify_ssl")); + } + $this->initailUrls(); } diff --git a/src/Traits/getSetAttributes.php b/src/Traits/getSetAttributes.php index 86ce609..8ed9bac 100644 --- a/src/Traits/getSetAttributes.php +++ b/src/Traits/getSetAttributes.php @@ -346,4 +346,40 @@ public function setIsReturnArray($isReturnArray = false) $this->isReturnArray = $isReturnArray; return $this; } + + /** + * @return int|null + */ + public function getTimeout() + { + return $this->timeout; + } + + /** + * @param int $timeout + * @return self + */ + public function setTimeout(int $timeout) + { + $this->timeout = $timeout; + return $this; + } + + /** + * @return bool|null + */ + public function getVerifySsl() + { + return $this->verifySsl; + } + + /** + * @param bool $verifySsl + * @return self + */ + public function setVerifySsl(bool $verifySsl) + { + $this->verifySsl = $verifySsl; + return $this; + } }