Skip to content

Commit baf8d79

Browse files
committed
added timeout of request
1 parent f389b93 commit baf8d79

File tree

7 files changed

+52
-16
lines changed

7 files changed

+52
-16
lines changed

config/zaincash.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,13 @@
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+
*/
106+
'timeout' => env('ZAINCASH_TIMEOUT', 10),
98107
];

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ 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. |
7172

7273

7374
<br>
@@ -84,6 +85,7 @@ ZAINCASH_IS_REDIRECT=false # optional default false
8485
ZAINCASH_MIN_AMOUNT=1000 # optional default 1000
8586
ZAINCASH_TEST_URL=https://test.zaincash.iq/ # optional
8687
ZAINCASH_LIVE_URL=https://api.zaincash.iq/ # optional
88+
ZAINCASH_TIMEOUT=10 # optional
8789
```
8890

8991

@@ -210,6 +212,7 @@ class PaymentController extends Controller
210212
| processingUrl |🔴| string-null | `getProcessingUrl()` | `setProcessingUrl($processingUrl)` | - |
211213
| processingOtpUrl |🔴| string-null | `getProcessingOtpUrl()` | `setProcessingOtpUrl($processingOtpUrl)` | - |
212214
| cancelUrl |🔴| string-null | `getCancelUrl()` | `setCancelUrl($cancelUrl)` | - |
215+
| timeout |🔴| int-null | `getTimeout()` | `setTimeout($timeout)` | - |
213216

214217
⚠️ `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`.
215218

src/BaseZainCash.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public function __construct(
3131
protected $processingOtpUrl = null,
3232
protected $cancelUrl = null,
3333
protected $transactionID = null,
34-
protected $isReturnArray = false
34+
protected $isReturnArray = false,
35+
protected $timeout = null
3536
) {
3637
if ($orderId) {
3738
$this->orderId = $this->getConfig("prefix_order_id") . $orderId;

src/Services/HttpClient.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ class HttpClient
1010
/**
1111
* @return \Psr\Http\Message\ResponseInterface|\Illuminate\Http\Client\Response|array
1212
*/
13-
public function httpPost(string $url, array $data = [], array $headers = []): mixed
13+
public function httpPost(string $url, array $data = [], array $headers = [], $timeout = 10): mixed
1414
{
15+
set_time_limit($timeout);
16+
1517
try {
16-
$response = Http::timeout(10)->withHeaders($headers)->asForm()->post($url, $data);
18+
$response = Http::timeout($timeout)->withHeaders($headers)->asForm()->post($url, $data);
1719
return $response;
1820
} catch (RequestException $e) {
1921
return [

src/Traits/HttpClientRequests.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ protected function createTransactionHttpClient(string $token)
1414

1515
return app(\Waad\ZainCash\Services\HttpClient::class)
1616
->httpPost(
17-
$this->getTUrl(),
18-
$body
17+
url: $this->getTUrl(),
18+
data: $body,
19+
timeout: $this->getTimeout()
1920
);
2021
}
2122

@@ -28,8 +29,9 @@ protected function sendRequestCheckTransaction(string $token)
2829
$body = $this->bodyPostRequestCheckTransaction($token, $this->getMerchantId());
2930
return app(\Waad\ZainCash\Services\HttpClient::class)
3031
->httpPost(
31-
$this->getCUrl(),
32-
$body
32+
url: $this->getCUrl(),
33+
data: $body,
34+
timeout: $this->getTimeout()
3335
);
3436
}
3537

@@ -42,12 +44,13 @@ protected function sendRequestProcessingTransaction(string $phonenumber, string
4244
{
4345
return app(\Waad\ZainCash\Services\HttpClient::class)
4446
->httpPost(
45-
$this->getProcessingUrl(),
46-
[
47+
url: $this->getProcessingUrl(),
48+
data: [
4749
'id' => $this->getTransactionID(),
4850
'phonenumber' => $phonenumber,
4951
'pin' => $pin,
50-
]
52+
],
53+
timeout: $this->getTimeout()
5154
);
5255
}
5356

@@ -61,13 +64,14 @@ protected function sendRequestPayTransaction(string $phonenumber, string $pin, s
6164
{
6265
return app(\Waad\ZainCash\Services\HttpClient::class)
6366
->httpPost(
64-
$this->getProcessingOtpUrl(),
65-
[
67+
url: $this->getProcessingOtpUrl(),
68+
data: [
6669
'id' => $this->getTransactionID(),
6770
'phonenumber' => $phonenumber,
6871
'pin' => $pin,
6972
'otp' => $otp,
70-
]
73+
],
74+
timeout: $this->getTimeout()
7175
);
7276
}
7377

@@ -78,11 +82,12 @@ protected function sendRequestCancelTransaction()
7882
{
7983
return app(\Waad\ZainCash\Services\HttpClient::class)
8084
->httpPost(
81-
$this->getCancelUrl(),
82-
[
85+
url: $this->getCancelUrl(),
86+
data: [
8387
'id' => $this->getTransactionID(),
8488
'type' => 'MERCHANT_PAYMENT'
85-
]
89+
],
90+
timeout: $this->getTimeout()
8691
);
8792
}
8893
}

src/Traits/Initialable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ protected function initial(): void
4444
$this->setIsRedirect($this->getConfig("is_redirect"));
4545
}
4646

47+
// Set the timeout request.
48+
if (is_null($this->getTimeout())) {
49+
$this->setTimeout($this->getConfig("timeout"));
50+
}
51+
4752
// Set the URLs.
4853
$this->initailUrls();
4954
}

src/Traits/getSetAttributes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,15 @@ public function setIsReturnArray(bool $isReturnArray = false): self
215215
$this->isReturnArray = $isReturnArray;
216216
return $this;
217217
}
218+
219+
public function getTimeout(): int|null
220+
{
221+
return $this->timeout;
222+
}
223+
224+
public function setTimeout(int $timeout): self
225+
{
226+
$this->timeout = $timeout;
227+
return $this;
228+
}
218229
}

0 commit comments

Comments
 (0)