From f71e2099f3d51a31f2de799afce7624206f7cb4b Mon Sep 17 00:00:00 2001 From: Waad Mawlood Date: Thu, 23 Nov 2023 11:54:12 +0300 Subject: [PATCH] added verifySsl for laravel 8.x --- config/zaincash.php | 12 ++++++++++++ readme.md | 3 +++ src/BaseZainCash.php | 5 ++++- src/Services/HttpClient.php | 5 ++++- src/Traits/HttpClientRequests.php | 15 ++++++++++----- src/Traits/Initialable.php | 5 +++++ src/Traits/getSetAttributes.php | 18 ++++++++++++++++++ 7 files changed, 56 insertions(+), 7 deletions(-) diff --git a/config/zaincash.php b/config/zaincash.php index 8e64051..21b514f 100644 --- a/config/zaincash.php +++ b/config/zaincash.php @@ -105,4 +105,16 @@ | 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 41432d8..7669dc5 100644 --- a/readme.md +++ b/readme.md @@ -69,6 +69,7 @@ update config zaincash in `config/zaincash.php` or from `.env` file | 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. |
@@ -86,6 +87,7 @@ 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 ``` @@ -213,6 +215,7 @@ class PaymentController extends Controller | 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 2d2574c..e3e0365 100644 --- a/src/BaseZainCash.php +++ b/src/BaseZainCash.php @@ -32,6 +32,7 @@ abstract class BaseZainCash protected $transactionID; protected $isReturnArray = false; protected $timeout; + protected $verifySsl; public function __construct( $amount = null, @@ -43,7 +44,8 @@ public function __construct( $isTest = null, $language = null, $baseUrl = null, - $timeout = null + $timeout = null, + $verifySsl = null ) { $this->amount = $amount; $this->serviceType = $serviceType; @@ -59,6 +61,7 @@ public function __construct( $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 674715b..1046a3c 100644 --- a/src/Services/HttpClient.php +++ b/src/Services/HttpClient.php @@ -11,9 +11,11 @@ class HttpClient * @param string $url * @param array $data * @param array $headers + * @param int $timeout + * @param bool $verify * @return \Psr\Http\Message\ResponseInterface|\Illuminate\Http\Client\Response|array */ - public function httpPost(string $url, array $data = [], array $headers = [], int $timeout = 10) + public function httpPost(string $url, array $data = [], array $headers = [], int $timeout = 10, bool $verify = false) { set_time_limit($timeout); @@ -21,6 +23,7 @@ public function httpPost(string $url, array $data = [], array $headers = [], int $client = new Client([ 'timeout' => $timeout, + 'verify' => $verify, ]); $response = $client->post($url, [ diff --git a/src/Traits/HttpClientRequests.php b/src/Traits/HttpClientRequests.php index ce380ce..c74360e 100644 --- a/src/Traits/HttpClientRequests.php +++ b/src/Traits/HttpClientRequests.php @@ -19,7 +19,8 @@ protected function createTransactionHttpClient(string $token) [ 'Content-Type' => 'application/x-www-form-urlencoded' ], - $this->getTimeout() + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -37,7 +38,8 @@ protected function sendRequestCheckTransaction(string $token) [ 'Content-Type' => 'application/x-www-form-urlencoded' ], - $this->getTimeout() + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -59,7 +61,8 @@ protected function sendRequestProcessingTransaction(string $phonenumber, string [ 'Content-Type' => 'application/x-www-form-urlencoded' ], - $this->getTimeout() + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -83,7 +86,8 @@ protected function sendRequestPayTransaction(string $phonenumber, string $pin, s [ 'Content-Type' => 'application/x-www-form-urlencoded' ], - $this->getTimeout() + $this->getTimeout(), + $this->getVerifySsl() ); } @@ -102,7 +106,8 @@ protected function sendRequestCancelTransaction() [ 'Content-Type' => 'application/x-www-form-urlencoded' ], - $this->getTimeout() + $this->getTimeout(), + $this->getVerifySsl() ); } } diff --git a/src/Traits/Initialable.php b/src/Traits/Initialable.php index 8663a4b..d5a98a5 100644 --- a/src/Traits/Initialable.php +++ b/src/Traits/Initialable.php @@ -51,6 +51,11 @@ protected function initial() $this->setTimeout($this->getConfig("timeout")); } + // Set the verify SSL. + if (is_null($this->getVerifySsl())) { + $this->setVerifySsl($this->getConfig("verify_ssl")); + } + // Set the URLs. $this->initailUrls(); } diff --git a/src/Traits/getSetAttributes.php b/src/Traits/getSetAttributes.php index b3a2aab..8ed9bac 100644 --- a/src/Traits/getSetAttributes.php +++ b/src/Traits/getSetAttributes.php @@ -364,4 +364,22 @@ 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; + } }