Skip to content

Commit

Permalink
Switch to cURL for API requests in Zibal driver (#283)
Browse files Browse the repository at this point in the history
* Switch to cURL for API requests in Zibal driver

Replaced the HTTP client code with cURL to handle API requests for the Zibal payment gateway. The change aims to streamline the request process, encapsulating the data and headers setup within the cURL functions.

* Refactor Zibal verification to use cURL instead of client

This change removes the client object and switches to using cURL for handling the payment verification process. The updated implementation uses direct cURL functions to set the request parameters and handle the response, streamlining the verification process. Additionally, the `orderId` variable was removed as it was unused.
  • Loading branch information
aliqasemzadeh authored Dec 12, 2024
1 parent ac3605e commit 24eafe9
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions src/Drivers/Zibal/Zibal.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

class Zibal extends Driver
{
/**
* Zibal Client.
*
* @var object
*/
protected $client;

/**
* Invoice
*
Expand Down Expand Up @@ -69,23 +62,32 @@ public function purchase()
$orderId = $details['order_id'];
}

$data = array(
"merchant"=> $this->settings->merchantId, //required
"callbackUrl"=> $this->settings->callbackUrl, //required
"amount"=> $amount, //required
"orderId"=> $orderId, //optional
);

// Pass current $data array to add existing optional details
$data = $this->checkOptionalDetails($data);

$response = $this->client->request(
'POST',
$this->settings->apiPurchaseUrl,
["json" => $data, "http_errors" => false]
);

$body = json_decode($response->getBody()->getContents(), false);
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => $this->settings->apiPurchaseUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"merchant": "'.$this->settings->merchantId.'",
"amount": "'.$amount.'",
"callbackUrl": "'.$this->settings->callbackUrl.'",
"orderId": "'.$orderId.'"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));

$response = curl_exec($curl);

curl_close($curl);
$body = json_decode($response, false);

if ($body->result != 100) {
// some error has happened
Expand Down Expand Up @@ -126,26 +128,38 @@ public function verify() : ReceiptInterface
{
$successFlag = Request::input('success');
$status = Request::input('status');
$orderId = Request::input('orderId');
$transactionId = $this->invoice->getTransactionId() ?? Request::input('trackId');

if ($successFlag != 1) {
$this->notVerified($this->translateStatus($status), $status);
}

//start verfication
$data = array(
"merchant" => $this->settings->merchantId, //required
"trackId" => $transactionId, //required
);

$response = $this->client->request(
'POST',
$this->settings->apiVerificationUrl,
["json" => $data, "http_errors" => false]
);

$body = json_decode($response->getBody()->getContents(), false);

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => $this->settings->apiVerificationUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"merchant": "'.$this->settings->merchantId.'",
"trackId": "'.$transactionId.'",
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));

$response = curl_exec($curl);

curl_close($curl);
$body = json_decode($response, false);

if ($body->result != 100) {
$this->notVerified($body->message, $body->result);
Expand Down

0 comments on commit 24eafe9

Please sign in to comment.