From d19c429a23355d5f86b2df424f404785d45753a3 Mon Sep 17 00:00:00 2001 From: yanoandri Date: Thu, 19 Dec 2024 11:50:06 +0700 Subject: [PATCH] Fix curl issues (#45) --- modules/gateways/xendit.php | 2 +- modules/gateways/xendit/lib/ActionBase.php | 2 +- modules/gateways/xendit/lib/XenditRequest.php | 23 ++++++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/gateways/xendit.php b/modules/gateways/xendit.php index c57d805..40d035f 100644 --- a/modules/gateways/xendit.php +++ b/modules/gateways/xendit.php @@ -10,7 +10,7 @@ require __DIR__ . '/xendit/autoload.php'; // Module version -const XENDIT_PAYMENT_GATEWAY_VERSION = '2.1.0'; +const XENDIT_PAYMENT_GATEWAY_VERSION = '2.1.1'; use WHMCS\Billing\Invoice; use Xendit\Lib\ActionBase; diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index 4a27d36..bd29cb7 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -393,6 +393,6 @@ public function setTransactionsToExpired($transaction) { * @return boolean */ public function isTransactionsDataValid($transactionsData) { - return !empty($transactionsData) && $transactionsData[0]["transactionid"] !== ""; + return !empty($transactionsData) && !empty($transactionsData[0]["transactionid"]) && $transactionsData[0]["transactionid"] !== ""; } } diff --git a/modules/gateways/xendit/lib/XenditRequest.php b/modules/gateways/xendit/lib/XenditRequest.php index c5e74d7..7574fcc 100644 --- a/modules/gateways/xendit/lib/XenditRequest.php +++ b/modules/gateways/xendit/lib/XenditRequest.php @@ -52,11 +52,11 @@ protected function request(string $endpoint, array $param = [], string $method = $curl = curl_init(); curl_setopt_array($curl, array( - CURLOPT_URL => sprintf("%s/%s", $this->tpi_server_domain, $endpoint), + CURLOPT_URL => sprintf("%s%s", $this->tpi_server_domain, $endpoint), CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, + CURLOPT_TIMEOUT => 60, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, @@ -64,7 +64,24 @@ protected function request(string $endpoint, array $param = [], string $method = CURLOPT_HTTPHEADER => $param["headers"] )); - $response = curl_exec($curl); + // Handle with retry mechanism + $retry = 3; + $response = false; + + while ($retry > 0) { + $response = curl_exec($curl); + + // Check for cURL errors + if (curl_errno($curl)) { + $error_msg = curl_error($curl); + if (strpos($error_msg, 'Could not resolve host') !== false) { + $retry--; + sleep(1 * $retry); // Wait for 1 second before retrying + continue; + } + } + break; + } curl_close($curl); return $response;