Skip to content

Commit

Permalink
added timeout and verifySsl for laravel 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
waadmawlood committed Nov 23, 2023
1 parent 550d4de commit e18b992
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 8 deletions.
22 changes: 22 additions & 0 deletions config/zaincash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |


<br>
Expand All @@ -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
```


Expand Down Expand Up @@ -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`.

Expand Down
8 changes: 7 additions & 1 deletion src/BaseZainCash.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ abstract class BaseZainCash
protected $cancelUrl;
protected $transactionID;
protected $isReturnArray = false;
protected $timeout;
protected $verifySsl;

public function __construct(
$amount = null,
Expand All @@ -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;
Expand All @@ -56,6 +60,8 @@ public function __construct(
$this->isTest = $isTest;
$this->language = $language;
$this->baseUrl = $baseUrl;
$this->timeout = $timeout;
$this->verifySsl = $verifySsl;

$this->initial();
}
Expand Down
11 changes: 9 additions & 2 deletions src/Services/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions src/Traits/HttpClientRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ protected function createTransactionHttpClient(string $token)
$body,
[
'Content-Type' => 'application/x-www-form-urlencoded'
]
],
$this->getTimeout(),
$this->getVerifySsl()
);
}

Expand All @@ -35,7 +37,9 @@ protected function sendRequestCheckTransaction(string $token)
$body,
[
'Content-Type' => 'application/x-www-form-urlencoded'
]
],
$this->getTimeout(),
$this->getVerifySsl()
);
}

Expand All @@ -56,7 +60,9 @@ protected function sendRequestProcessingTransaction(string $phonenumber, string
],
[
'Content-Type' => 'application/x-www-form-urlencoded'
]
],
$this->getTimeout(),
$this->getVerifySsl()
);
}

Expand All @@ -79,7 +85,9 @@ protected function sendRequestPayTransaction(string $phonenumber, string $pin, s
],
[
'Content-Type' => 'application/x-www-form-urlencoded'
]
],
$this->getTimeout(),
$this->getVerifySsl()
);
}

Expand All @@ -97,7 +105,9 @@ protected function sendRequestCancelTransaction()
],
[
'Content-Type' => 'application/x-www-form-urlencoded'
]
],
$this->getTimeout(),
$this->getVerifySsl()
);
}
}
10 changes: 10 additions & 0 deletions src/Traits/Initialable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
36 changes: 36 additions & 0 deletions src/Traits/getSetAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e18b992

Please sign in to comment.