Skip to content

Commit

Permalink
Merge pull request #26 from donmo-roundup/refactor-api-service-with-curl
Browse files Browse the repository at this point in the history
use curl for making calls to Donmo API
  • Loading branch information
DavidVin357 committed Aug 26, 2023
2 parents 649b0b9 + 6ec41e0 commit 0e7bcf9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"name": "donmo-roundup/module-donations",
"type": "magento2-module",
"keywords": [
Expand All @@ -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": {
Expand Down
67 changes: 40 additions & 27 deletions lib/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 0e7bcf9

Please sign in to comment.