From 6ec41e04d265ca91503262dd7cdbd240a4ef529e Mon Sep 17 00:00:00 2001 From: david Date: Sat, 26 Aug 2023 22:05:04 +0300 Subject: [PATCH] use curl for making calls to Donmo API --- composer.json | 5 ++-- lib/ApiService.php | 67 +++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index e260d76..3f35bd3 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,4 @@ - { +{ "name": "donmo-roundup/module-donations", "type": "magento2-module", "keywords": [ @@ -12,7 +12,8 @@ "homepage": "https://www.donmo.org", "description": "Donmo Roundup Magento Plugin for collecting donations for Ukraine on the payment page", "require": { - "php": ">=7.4" + "php": ">=7.4", + "ext-curl": "*" }, "autoload": { diff --git a/lib/ApiService.php b/lib/ApiService.php index 7b9a496..6bcbb74 100644 --- a/lib/ApiService.php +++ b/lib/ApiService.php @@ -7,22 +7,15 @@ use Donmo\Roundup\Logger\Logger; use Donmo\Roundup\Model\Config as DonmoConfig; -use Magento\Framework\HTTP\ZendClient; -use Magento\Framework\HTTP\ZendClientFactory; -use Zend_Http_Client; - class ApiService { - private ZendClient $client; private Logger $logger; private DonmoConfig $donmoConfig; public function __construct( - ZendClientFactory $httpClientFactory, Logger $logger, DonmoConfig $donmoConfig ) { - $this->client = $httpClientFactory->create(); $this->logger = $logger; $this->donmoConfig = $donmoConfig; } @@ -48,50 +41,70 @@ private function generatePayload(array $donations): array * @param $mode * @param DonationInterface[] $donations * @return int - * @throws \Zend_Http_Client_Exception */ public function createAndConfirmDonations($mode, array $donations): int { $sk = $this->donmoConfig->getSecretKey($mode); $url = Donmo::$apiBase . '/donations/confirm'; - $this->client->setUri($url); - $this->client->setMethod(Zend_Http_Client::POST); - $this->client->setHeaders('sk', $sk); + + $ch = curl_init(); + $headers = array( + 'Content-Type: application/json', + "sk: $sk" + ); $payload = $this->generatePayload($donations); + $body = json_encode(['donations' => $payload]); + + curl_setopt($ch, CURLOPT_URL, $url); // URL to request + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string + curl_setopt($ch, CURLOPT_POST, true); // Set the request method to POST - $json = json_encode(['donations' => $payload]); - $result = $this->client->setRawData($json, 'application/json')->request(); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - $status = $result->getStatus(); - $body = $result->getBody(); + $response = curl_exec($ch); + $this->logger->info('response is' . $response); + + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $this->logger->info('status is' . $status); if ($status == 200) { - $this->logger->info("Donmo CreateAndConfirmDonations API Request Successful: \n" . $body); + $this->logger->info("Donmo CreateAndConfirmDonations API Request Successful: \n" . $response); } else { - $this->logger->error("Unsuccessful Donmo CreateAndConfirmDonations API Request: \n" . $body); + $this->logger->error("Unsuccessful Donmo CreateAndConfirmDonations API Request: \n" . $response); } return $status; } public function deleteDonation($donationMode, $id): int { - $sk = $this->donmoConfig->getSecretKey($donationMode); + $sk = $this->donmoConfig->getSecretKey($donationMode); + + $url = Donmo::$apiBase . "/donations/{$id}"; + + $ch = curl_init(); + $headers = array( + 'Content-Type: application/json', + "sk: $sk" + ); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $response = curl_exec($ch); - $url = Donmo::$apiBase . "/donations/{$id}"; - $this->client->setUri($url); - $this->client->setMethod(Zend_Http_Client::DELETE); - $this->client->setHeaders('sk', $sk); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $result = $this->client->request(); - $status = $result->getStatus(); - $body = $result->getBody(); if ($status == 200) { - $this->logger->info("Donmo DeleteDonation API Request Successful: \n" . $body); + $this->logger->info("Donmo DeleteDonation API Request Successful: \n" . $response); } else { - $this->logger->error("Unsuccessful Delete Donation API request: \n" . $body); + $this->logger->error("Unsuccessful Delete Donation API request: \n" . $response); } return $status;