Skip to content

Commit e18b992

Browse files
committed
added timeout and verifySsl for laravel 6.x
1 parent 550d4de commit e18b992

File tree

7 files changed

+105
-8
lines changed

7 files changed

+105
-8
lines changed

config/zaincash.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,26 @@
9595
*/
9696
'min_amount' => env('ZAINCASH_MIN_AMOUNT', 1000),
9797

98+
/*
99+
|--------------------------------------------------------------------------
100+
| timeout Request (in seconds)
101+
|--------------------------------------------------------------------------
102+
|
103+
| Set the timeout for the request to ZainCash's API.
104+
| The default value is 10 seconds.
105+
| make it 0 (zero) for unlimited timeout (not recommended).
106+
*/
107+
'timeout' => env('ZAINCASH_TIMEOUT', 10),
108+
109+
/*
110+
|--------------------------------------------------------------------------
111+
| Verify SSL
112+
|--------------------------------------------------------------------------
113+
|
114+
| Set the verify SSL for the request to ZainCash's API.
115+
| The default value is true.
116+
| make it false for disable verify SSL (not recommended).
117+
| if it is true and you used the `http` protocol so will get an error. so make it false.
118+
*/
119+
'verify_ssl' => env('ZAINCASH_VERIFY_SSL', true),
98120
];

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ update config zaincash in `config/zaincash.php` or from `.env` file
6868
| prefix_order_id | string | wa3d_ | Prefix for the order ID. |
6969
| 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. |
7070
| 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. |
71+
| timeout | int | 10 | Set the timeout for the request in seconds. |
72+
| verify_ssl | bool | true | Set the verify SSL for the request to ZainCash's API. |
7173

7274

7375
<br>
@@ -84,6 +86,8 @@ ZAINCASH_IS_REDIRECT=false # optional default false
8486
ZAINCASH_MIN_AMOUNT=1000 # optional default 1000
8587
ZAINCASH_TEST_URL=https://test.zaincash.iq/ # optional
8688
ZAINCASH_LIVE_URL=https://api.zaincash.iq/ # optional
89+
ZAINCASH_TIMEOUT=10 # optional
90+
ZAINCASH_VERIFY_SSL=true # optional
8791
```
8892

8993

@@ -210,6 +214,8 @@ class PaymentController extends Controller
210214
| processingUrl |🔴| string-null | `getProcessingUrl()` | `setProcessingUrl($processingUrl)` | - |
211215
| processingOtpUrl |🔴| string-null | `getProcessingOtpUrl()` | `setProcessingOtpUrl($processingOtpUrl)` | - |
212216
| cancelUrl |🔴| string-null | `getCancelUrl()` | `setCancelUrl($cancelUrl)` | - |
217+
| timeout |🔴| int-null | `getTimeout()` | `setTimeout($timeout)` | - |
218+
| verifySsl |🔴| bool-null | `getVerifySsl()` | `setVerifySsl($verifySsl)` | - |
213219

214220
⚠️ `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`.
215221

src/BaseZainCash.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ abstract class BaseZainCash
3131
protected $cancelUrl;
3232
protected $transactionID;
3333
protected $isReturnArray = false;
34+
protected $timeout;
35+
protected $verifySsl;
3436

3537
public function __construct(
3638
$amount = null,
@@ -41,7 +43,9 @@ public function __construct(
4143
$merchantId = null,
4244
$isTest = null,
4345
$language = null,
44-
$baseUrl = null
46+
$baseUrl = null,
47+
$timeout = null,
48+
$verifySsl = null
4549
) {
4650
$this->amount = $amount;
4751
$this->serviceType = $serviceType;
@@ -56,6 +60,8 @@ public function __construct(
5660
$this->isTest = $isTest;
5761
$this->language = $language;
5862
$this->baseUrl = $baseUrl;
63+
$this->timeout = $timeout;
64+
$this->verifySsl = $verifySsl;
5965

6066
$this->initial();
6167
}

src/Services/HttpClient.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ class HttpClient
1111
* @param string $url
1212
* @param array $data
1313
* @param array $headers
14+
* @param int $timeout
15+
* @param bool $verify
1416
* @return \Psr\Http\Message\ResponseInterface|array
1517
*/
16-
public function httpPost(string $url, array $data = [], array $headers = [])
18+
public function httpPost(string $url, array $data = [], array $headers = [], int $timeout = 10, bool $verify = false)
1719
{
20+
set_time_limit($timeout);
21+
1822
try {
1923

20-
$client = new Client();
24+
$client = new Client([
25+
'timeout' => $timeout,
26+
'verify' => $verify,
27+
]);
2128

2229
$response = $client->post($url, [
2330
'headers' => $headers,

src/Traits/HttpClientRequests.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ protected function createTransactionHttpClient(string $token)
1818
$body,
1919
[
2020
'Content-Type' => 'application/x-www-form-urlencoded'
21-
]
21+
],
22+
$this->getTimeout(),
23+
$this->getVerifySsl()
2224
);
2325
}
2426

@@ -35,7 +37,9 @@ protected function sendRequestCheckTransaction(string $token)
3537
$body,
3638
[
3739
'Content-Type' => 'application/x-www-form-urlencoded'
38-
]
40+
],
41+
$this->getTimeout(),
42+
$this->getVerifySsl()
3943
);
4044
}
4145

@@ -56,7 +60,9 @@ protected function sendRequestProcessingTransaction(string $phonenumber, string
5660
],
5761
[
5862
'Content-Type' => 'application/x-www-form-urlencoded'
59-
]
63+
],
64+
$this->getTimeout(),
65+
$this->getVerifySsl()
6066
);
6167
}
6268

@@ -79,7 +85,9 @@ protected function sendRequestPayTransaction(string $phonenumber, string $pin, s
7985
],
8086
[
8187
'Content-Type' => 'application/x-www-form-urlencoded'
82-
]
88+
],
89+
$this->getTimeout(),
90+
$this->getVerifySsl()
8391
);
8492
}
8593

@@ -97,7 +105,9 @@ protected function sendRequestCancelTransaction()
97105
],
98106
[
99107
'Content-Type' => 'application/x-www-form-urlencoded'
100-
]
108+
],
109+
$this->getTimeout(),
110+
$this->getVerifySsl()
101111
);
102112
}
103113
}

src/Traits/Initialable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ protected function initial()
4646
$this->setIsRedirect($this->getConfig("is_redirect"));
4747
}
4848

49+
// Set the timeout request.
50+
if (is_null($this->getTimeout())) {
51+
$this->setTimeout($this->getConfig("timeout"));
52+
}
53+
54+
// Set the verify SSL.
55+
if (is_null($this->getVerifySsl())) {
56+
$this->setVerifySsl($this->getConfig("verify_ssl"));
57+
}
58+
4959
$this->initailUrls();
5060
}
5161

src/Traits/getSetAttributes.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,40 @@ public function setIsReturnArray($isReturnArray = false)
346346
$this->isReturnArray = $isReturnArray;
347347
return $this;
348348
}
349+
350+
/**
351+
* @return int|null
352+
*/
353+
public function getTimeout()
354+
{
355+
return $this->timeout;
356+
}
357+
358+
/**
359+
* @param int $timeout
360+
* @return self
361+
*/
362+
public function setTimeout(int $timeout)
363+
{
364+
$this->timeout = $timeout;
365+
return $this;
366+
}
367+
368+
/**
369+
* @return bool|null
370+
*/
371+
public function getVerifySsl()
372+
{
373+
return $this->verifySsl;
374+
}
375+
376+
/**
377+
* @param bool $verifySsl
378+
* @return self
379+
*/
380+
public function setVerifySsl(bool $verifySsl)
381+
{
382+
$this->verifySsl = $verifySsl;
383+
return $this;
384+
}
349385
}

0 commit comments

Comments
 (0)