Skip to content

Commit

Permalink
Merge pull request vindi#37 from cedran/master
Browse files Browse the repository at this point in the history
fix: correcting bill sending via webhook
  • Loading branch information
contardi authored Jun 7, 2024
2 parents 114de36 + d697398 commit 657508f
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 97 deletions.
1 change: 0 additions & 1 deletion Api/Data/SubscriptionOrderSearchResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/**
* Interface SubscriptionOrderSearchResultInterface
* @package Vindi\Payment\Api\Data
* @author Iago Cedran <iago@bizcommerce.com.br>
*/
interface SubscriptionOrderSearchResultInterface extends SearchResultsInterface
{
Expand Down
1 change: 0 additions & 1 deletion Api/SubscriptionOrderRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/**
* Interface SubscriptionOrderRepositoryInterface
* @package Vindi\Payment\Api
* @author Iago Cedran <iago@bizcommerce.com.br>
*/
interface SubscriptionOrderRepositoryInterface
{
Expand Down
8 changes: 4 additions & 4 deletions Controller/Index/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Psr\Log\LoggerInterface;
use Vindi\Payment\Logger\Logger;
use Vindi\Payment\Helper\Api;
use Vindi\Payment\Helper\Data;
use Vindi\Payment\Helper\WebhookHandler;
Expand All @@ -23,7 +23,7 @@ class Webhook extends Action
*/
private $api;
/**
* @var LoggerInterface
* @var Logger
*/
private $logger;
/**
Expand All @@ -34,15 +34,15 @@ class Webhook extends Action
/**
* Webhook constructor.
* @param Api $api
* @param LoggerInterface $logger
* @param Logger $logger
* @param WebhookHandler $webhookHandler
* @param Data $helperData
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(
Api $api,
LoggerInterface $logger,
Logger $logger,
WebhookHandler $webhookHandler,
Data $helperData,
Context $context,
Expand Down
23 changes: 22 additions & 1 deletion Cron/ProcessOrderCreationQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Vindi\Payment\Api\OrderCreationQueueRepositoryInterface;
use Vindi\Payment\Helper\WebHookHandlers\OrderCreator;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Lock\LockManagerInterface;

class ProcessOrderCreationQueue
{
Expand All @@ -24,28 +25,46 @@ class ProcessOrderCreationQueue
*/
private $orderCreator;

/**
* @var LockManagerInterface
*/
private $lockManager;

/**
* Lock name for this cron job
*/
private const LOCK_NAME = 'vindi_payment_process_order_creation_queue';

/**
* Constructor
*
* @param LoggerInterface $logger
* @param OrderCreationQueueRepositoryInterface $orderCreationQueueRepository
* @param OrderCreator $orderCreator
* @param LockManagerInterface $lockManager
*/
public function __construct(
LoggerInterface $logger,
OrderCreationQueueRepositoryInterface $orderCreationQueueRepository,
OrderCreator $orderCreator
OrderCreator $orderCreator,
LockManagerInterface $lockManager
) {
$this->logger = $logger;
$this->orderCreationQueueRepository = $orderCreationQueueRepository;
$this->orderCreator = $orderCreator;
$this->lockManager = $lockManager;
}

/**
* Process the oldest pending order creation request.
*/
public function execute()
{
if (!$this->lockManager->lock(self::LOCK_NAME)) {
$this->logger->info(__('The job is already running.'));
return;
}

try {
$queueItem = $this->orderCreationQueueRepository->getOldestPending();
if (!$queueItem) {
Expand Down Expand Up @@ -77,6 +96,8 @@ public function execute()
$this->logger->error(__('Error processing order creation queue: %1', $e->getMessage()));
} catch (\Exception $e) {
$this->logger->error(__('Unexpected error processing order creation queue: %1', $e->getMessage()));
} finally {
$this->lockManager->unlock(self::LOCK_NAME);
}
}
}
16 changes: 8 additions & 8 deletions Helper/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public function request($endpoint, $method = 'POST', $data = [], $dataToLog = nu
}

$url = $this->base_path . $endpoint;
$body = !empty($data) ? json_encode($data) : '';
$requestBody = !empty($data) ? json_encode($data) : '';

$requestId = number_format(microtime(true), 2, '', '');
$dataToLog = null !== $dataToLog ? json_encode($dataToLog) : $body;
$dataToLog = null !== $dataToLog ? json_encode($dataToLog) : $requestBody;

$this->logger->info(__(sprintf(
'[Request #%s]: New Api Request.\n%s %s\n%s',
Expand All @@ -125,8 +125,8 @@ public function request($endpoint, $method = 'POST', $data = [], $dataToLog = nu
CURLOPT_CUSTOMREQUEST => $method
];

if (!empty($body)) {
$ch_options[CURLOPT_POSTFIELDS] = $body;
if (!empty($requestBody)) {
$ch_options[CURLOPT_POSTFIELDS] = $requestBody;
}

curl_setopt_array($ch, $ch_options);
Expand All @@ -141,7 +141,7 @@ public function request($endpoint, $method = 'POST', $data = [], $dataToLog = nu
__(sprintf('[Request #%s]: Error while executing request!\n%s', $requestId, print_r($response, true)))
);
curl_close($ch);
$this->logApiRequest($endpoint, $method, $body, $response, $statusCode, 'Error while executing request');
$this->logApiRequest($endpoint, $method, $requestBody, $response, $statusCode, 'Error while executing request');
return false;
}

Expand All @@ -159,17 +159,17 @@ public function request($endpoint, $method = 'POST', $data = [], $dataToLog = nu
print_r($body, true)
)));

$this->logApiRequest($endpoint, $method, $body, $response, $statusCode, 'Error while recovering request body');
$this->logApiRequest($endpoint, $method, $requestBody, $response, $statusCode, 'Error while recovering request body');

return false;
}

if (!$this->checkResponse($responseBody, $endpoint)) {
$this->logApiRequest($endpoint, $method, $body, json_encode($responseBody), $statusCode, 'API response error');
$this->logApiRequest($endpoint, $method, $requestBody, json_encode($responseBody), $statusCode, 'API response error');
return false;
}

$this->logApiRequest($endpoint, $method, $body, json_encode($responseBody), $statusCode, 'Success');
$this->logApiRequest($endpoint, $method, $requestBody, json_encode($responseBody), $statusCode, 'Success');

return $responseBody;
}
Expand Down
49 changes: 46 additions & 3 deletions Helper/WebHookHandlers/BillCanceled.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ class BillCanceled
*/
protected $orderRepository;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

public function __construct(
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Vindi\Payment\Model\Payment\Bill $bill,
\Vindi\Payment\Helper\WebHookHandlers\Order $order,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->bill = $bill;
$this->order = $order;
$this->logger = $logger;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
Expand All @@ -51,8 +58,18 @@ public function billCanceled($data)
return false;
}

/** @var \Magento\Sales\Model\Order $order */
if (!($order = $this->getOrderFromBill($bill['id']))) {
$isSubscription = isset($bill['subscription']['id']);
$order = null;

if ($isSubscription) {
$order = $this->getOrderByVindiBillAndSubscriptionId($bill['id'], $bill['subscription']['id']);
}

if (!$order) {
$order = $this->getOrderFromBill($bill['id']);
}

if (!$order) {
$this->logger->warning(__('Order not found'));
return false;
}
Expand Down Expand Up @@ -82,4 +99,30 @@ private function getOrderFromBill($billId)

return $this->order->getOrder(compact('bill'));
}

/**
* @param $vindiBillId
* @param $subscriptionId
* @return bool|\Magento\Sales\Api\Data\OrderInterface
*/
private function getOrderByVindiBillAndSubscriptionId($vindiBillId, $subscriptionId)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('vindi_bill_id', $vindiBillId, 'eq')
->addFilter('vindi_subscription_id', $subscriptionId, 'eq')
->create();

$orderList = $this->orderRepository
->getList($searchCriteria)
->getItems();

try {
return reset($orderList);
} catch (\Exception $e) {
$this->logger->error(__('Order with Vindi Bill ID #%1 and Subscription ID #%2 not found', $vindiBillId, $subscriptionId));
$this->logger->error($e->getMessage());
}

return false;
}
}
Loading

0 comments on commit 657508f

Please sign in to comment.