Skip to content

Commit

Permalink
PON-521: Added log handler for razorpay logs (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramth05 authored Aug 25, 2020
1 parent ff72152 commit 63bed56
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
11 changes: 7 additions & 4 deletions Controller/Payment/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function __construct(
\Razorpay\Magento\Model\CheckoutFactory $checkoutFactory,
\Magento\Framework\App\CacheInterface $cache,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
\Razorpay\Magento\Model\LogHandler $handler
) {
parent::__construct(
$context,
Expand All @@ -56,6 +57,8 @@ public function __construct(
$this->cache = $cache;
$this->orderRepository = $orderRepository;
$this->logger = $logger;
$this->handler = $handler;
$this->logger->setHandlers ( [$this->handler] );
$this->objectManagement = \Magento\Framework\App\ObjectManager::getInstance();
}

Expand Down Expand Up @@ -91,7 +94,7 @@ public function execute()

if (empty($salesOrder['entity_id']) === false)
{
$this->logger->warning("Razorpay inside order already processed with webhook quoteID:" . $receipt_id
$this->logger->info("Razorpay inside order already processed with webhook quoteID:" . $receipt_id
." and OrderID:".$salesOrder['entity_id']);

$this->checkoutSession
Expand All @@ -115,7 +118,7 @@ public function execute()
//set the chache to stop webhook processing
$this->cache->save("started", "quote_Front_processing_$receipt_id", ["razorpay"], 30);

$this->logger->warning("Razorpay front-end order processing started quoteID:" . $receipt_id);
$this->logger->info("Razorpay front-end order processing started quoteID:" . $receipt_id);

$responseContent = [
'success' => false,
Expand Down Expand Up @@ -153,7 +156,7 @@ public function execute()
{
if(empty($_POST['email']) === true)
{
$this->logger->warning("Email field is required");
$this->logger->info("Email field is required");

$responseContent = [
'message' => "Email field is required",
Expand Down
23 changes: 13 additions & 10 deletions Controller/Payment/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManagement,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Framework\App\CacheInterface $cache,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
\Razorpay\Magento\Model\LogHandler $handler
)
{
parent::__construct(
Expand All @@ -87,6 +88,8 @@ public function __construct(
$this->api = new Api($keyId, $keySecret);
$this->order = $order;
$this->logger = $logger;
$this->handler = $handler;
$this->logger->setHandlers ( [$this->handler] );

$this->objectManagement = \Magento\Framework\App\ObjectManager::getInstance();
$this->quoteManagement = $quoteManagement;
Expand All @@ -110,7 +113,7 @@ public function execute()
return;
}

$this->logger->warning("Razorpay Webhook processing started.");
$this->logger->info("Razorpay Webhook processing started.");

if (($this->config->isWebhookEnabled() === true) &&
(empty($post['event']) === false))
Expand Down Expand Up @@ -160,7 +163,7 @@ public function execute()
}
}

$this->logger->warning("Razorpay Webhook processing completed.");
$this->logger->info("Razorpay Webhook processing completed.");
}

/**
Expand All @@ -174,15 +177,15 @@ protected function orderPaid(array $post)

if (isset($post['payload']['payment']['entity']['notes']['merchant_quote_id']) === false)
{
$this->logger->warning("Razorpay Webhook: Quote ID not set for Razorpay payment_id(:$paymentId)");
$this->logger->info("Razorpay Webhook: Quote ID not set for Razorpay payment_id(:$paymentId)");
return;
}

$quoteId = $post['payload']['payment']['entity']['notes']['merchant_quote_id'];

if (empty($this->cache->load("quote_Front_processing_".$quoteId)) === false)
{
$this->logger->warning("Razorpay Webhook: Order processing is active for quoteID: $quoteId and Razorpay payment_id(:$paymentId)");
$this->logger->info("Razorpay Webhook: Order processing is active for quoteID: $quoteId and Razorpay payment_id(:$paymentId)");
header('Status: 409 Conflict, too early for processing', true, 409);
exit;
}
Expand All @@ -191,7 +194,7 @@ protected function orderPaid(array $post)

$amount = number_format($post['payload']['payment']['entity']['amount']/100, 2, ".", "");

$this->logger->warning("Razorpay Webhook processing started for Razorpay payment_id(:$paymentId)");
$this->logger->info("Razorpay Webhook processing started for Razorpay payment_id(:$paymentId)");

$payment_created_time = $post['payload']['payment']['entity']['created_at'];

Expand All @@ -201,7 +204,7 @@ protected function orderPaid(array $post)
//exit if quote is not active
if (!$quote->getIsActive())
{
$this->logger->warning("Razorpay Webhook: Quote order is inactive for quoteID: $quoteId and Razorpay payment_id(:$paymentId)");
$this->logger->info("Razorpay Webhook: Quote order is inactive for quoteID: $quoteId and Razorpay payment_id(:$paymentId)");
return;
}

Expand All @@ -210,7 +213,7 @@ protected function orderPaid(array $post)

if ($quoteAmount !== $post['payload']['payment']['entity']['amount'])
{
$this->logger->warning("Razorpay Webhook: Amount paid doesn't match with store order amount for Razorpay payment_id(:$paymentId)");
$this->logger->info("Razorpay Webhook: Amount paid doesn't match with store order amount for Razorpay payment_id(:$paymentId)");
return;
}

Expand All @@ -231,7 +234,7 @@ protected function orderPaid(array $post)

if ($orderRzpPaymentId === $paymentId)
{
$this->logger->warning("Razorpay Webhook: Sales Order and payment already exist for Razorpay payment_id(:$paymentId)");
$this->logger->info("Razorpay Webhook: Sales Order and payment already exist for Razorpay payment_id(:$paymentId)");
return;
}
}
Expand All @@ -249,7 +252,7 @@ protected function orderPaid(array $post)
->setShouldCloseParentTransaction(true);
$order->save();

$this->logger->warning("Razorpay Webhook Processed successfully for Razorpay payment_id(:$paymentId): and quoteID(: $quoteId) and OrderID(: ". $order->getEntityId() .")");
$this->logger->info("Razorpay Webhook Processed successfully for Razorpay payment_id(:$paymentId): and quoteID(: $quoteId) and OrderID(: ". $order->getEntityId() .")");
}

protected function getQuoteObject($post, $quoteId)
Expand Down
59 changes: 59 additions & 0 deletions Model/LogHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Razorpay\Magento\Model;

use Magento\Framework\Filesystem\DriverInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* Used to display webhook url link
*/
class LogHandler extends \Magento\Framework\Logger\Handler\Base
{
/**
* Logging level
* @var int
*/
protected $loggerType = Logger::INFO;

/**
* File name
* @var string
*/
public $fileName = '';
/**
* File name
* @var string
*/
public $cutomfileName = 'NO_PATH';
/**
* @var TimezoneInterface
*/
protected $_localeDate;

public function __construct(
DriverInterface $filesystem,
\Magento\Framework\Filesystem $corefilesystem,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
$filePath = null
) {
$this->_localeDate = $localeDate;
$corefilesystem= $corefilesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);
$logpath = $corefilesystem->getAbsolutePath('log/Razorpay/');


// Custom log file name for each day because log will be full for optimization
$filename = 'rzp_'.Date('Y_m_d').'.log';

$filepath = $logpath . $filename;

$this->cutomfileName = $filepath;

parent::__construct(
$filesystem,
$filepath
);

}
}

0 comments on commit 63bed56

Please sign in to comment.