Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added debug mode for logging. #470

Draft
wants to merge 2 commits into
base: master-2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Controller/Payment/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Webhook extends \Razorpay\Magento\Controller\BaseController
*/
protected const WEBHOOK_NOTIFY_WAIT_TIME = (5 * 60);

/**
* @var \Razorpay\Magento\Model\Util\DebugMode
*/
protected $debug;

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
Expand All @@ -106,7 +111,8 @@ public function __construct(
\Magento\Sales\Model\Service\InvoiceService $invoiceService,
\Magento\Framework\DB\Transaction $transaction,
\Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
\Razorpay\Magento\Model\Util\DebugMode $debug
) {
parent::__construct(
$context,
Expand All @@ -126,6 +132,7 @@ public function __construct(
$this->invoiceSender = $invoiceSender;
$this->orderSender = $orderSender;
$this->orderStatus = static::STATUS_PROCESSING;
$this->debug = $debug;

$this->enableCustomPaidOrderStatus = $this->config->isCustomPaidOrderStatusEnabled();

Expand All @@ -149,6 +156,7 @@ public function execute()

if (json_last_error() !== 0)
{
$this->debug->log("Razorpay Webhook processing stopped due to last json error.");
return;
}

Expand All @@ -157,12 +165,17 @@ public function execute()
if (($this->config->isWebhookEnabled() === true) &&
(isset($post['event']) && empty($post['event']) === false))
{
$this->debug->log("Razorpay Webhook processing, webhook is enabled and event is present in post data");

if (!empty($razorpaySignature) === true)
{
$this->debug->log("Razorpay Webhook processing, razorpay signature is present");

$webhookSecret = $this->config->getWebhookSecret();
// To accept webhooks, the merchant must configure it on the magento backend by setting the secret.
if (empty($webhookSecret) === true)
{
$this->debug->log("Razorpay Webhook processing stopped as webhook secret is not present");
return;
}

Expand Down Expand Up @@ -190,12 +203,15 @@ public function execute()
}

if (isset($post['payload']['payment']['entity']['notes']['merchant_order_id']) === true)
{
{
$orderId = $post['payload']['payment']['entity']['notes']['merchant_order_id'];
$paymentId = $post['payload']['payment']['entity']['id'];
$orderWebhookData = $this->getOrderWebhookData($orderId);
$amountPaid = $post['payload']['payment']['entity']['amount'];

$this->debug->log("Razorpay Webhook processing, webhook data is present. Order Id = " . $orderId . ' Payment Id = ' . $paymentId .
" Order webhook data = " . json_encode($orderWebhookData) . ' Amount Paid = ' . $amountPaid);

if ($post['event'] === 'order.paid')
{
sleep(1);
Expand Down
15 changes: 13 additions & 2 deletions Cron/CancelPendingOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class CancelPendingOrders {

protected $resetCartOrderTimeout;

/**
* @var \Razorpay\Magento\Model\Util\DebugMode
*/
protected $debug;

/**
* CancelOrder constructor.
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
Expand All @@ -70,7 +75,8 @@ public function __construct(
\Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder,
\Magento\Sales\Api\OrderManagementInterface $orderManagement,
\Razorpay\Magento\Model\Config $config,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
\Razorpay\Magento\Model\Util\DebugMode $debug
)
{
$this->orderRepository = $orderRepository;
Expand All @@ -83,6 +89,7 @@ public function __construct(
$this->pendingOrderTimeout = ($this->config->getPendingOrderTimeout() > 0) ? $this->config->getPendingOrderTimeout() : 30;
$this->isCancelResetCartCronEnabled = $this->config->isCancelResetCartOrderCronEnabled();
$this->resetCartOrderTimeout = ($this->config->getResetCartOrderTimeout() > 0) ? $this->config->getResetCartOrderTimeout() : 30;
$this->debug = $debug;
}

public function execute()
Expand Down Expand Up @@ -110,6 +117,8 @@ public function execute()
$orders = $this->orderRepository->getList($searchCriteria);
foreach ($orders->getItems() as $order)
{
$this->debug->log("Cronjob: Magento Order Id = " . $order->getIncrementId() . " picked for cancelation.");

if ($order->getPayment()->getMethod() === 'razorpay') {
$this->cancelOrder($order);
}
Expand Down Expand Up @@ -149,6 +158,8 @@ public function execute()

foreach ($orders->getItems() as $order)
{
$this->debug->log("Cronjob: Magento Order Id = " . $order->getIncrementId() . " picked for cancelation in reset cart cron.");

if ($order->getPayment()->getMethod() === 'razorpay') {
$this->cancelOrder($order);
}
Expand All @@ -166,7 +177,7 @@ private function cancelOrder($order)
if ($order)
{
if ($order->canCancel()) {
$this->logger->info("Cronjob: Cancelling Order ID: " . $order->getEntityId());
$this->logger->info("Cronjob: Cancelling Order ID: " . $order->getIncrementId());

$order->cancel()
->setState(
Expand Down
17 changes: 14 additions & 3 deletions Cron/UpdateOrdersToProcessingV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class UpdateOrdersToProcessingV2 {
*/
protected $captureCommand;

/**
* @var \Razorpay\Magento\Model\Util\DebugMode
*/
protected $debug;

/**
* CancelOrder constructor.
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
Expand All @@ -122,7 +127,8 @@ public function __construct(
\Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
\Razorpay\Magento\Model\Config $config,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
\Razorpay\Magento\Model\Util\DebugMode $debug
)
{
$this->config = $config;
Expand All @@ -139,6 +145,7 @@ public function __construct(
$this->orderSender = $orderSender;
$this->logger = $logger;
$this->orderStatus = static::STATUS_PROCESSING;
$this->debug = $debug;

$this->enableCustomPaidOrderStatus = $this->config->isCustomPaidOrderStatusEnabled();

Expand Down Expand Up @@ -174,6 +181,8 @@ public function execute()
{
foreach ($orderLink as $orderData)
{
$this->debug->log("Cronjob: Magento Order Id = " . $orderData['order_id'] . " picked for updation");

$order = $this->orderRepository->get($orderData['order_id']);
$singleOrderLinkCollection = $objectManagement->get('Razorpay\Magento\Model\OrderLink')
->getCollection()
Expand All @@ -188,6 +197,8 @@ public function execute()
$rzpWebhookData = $orderData['rzp_webhook_data'];
if (empty($rzpWebhookData) === false) // check if webhook cron has run and populated the rzp_webhook_data column
{
$this->debug->log("Cronjob: Webhook data present for Magento Order Id = " . $orderData['order_id']);

$rzpWebhookDataObj = unserialize($rzpWebhookData); // nosemgrep

if (isset($rzpWebhookDataObj[static::ORDER_PAID]) === true)
Expand Down Expand Up @@ -223,7 +234,7 @@ public function execute()
private function updateOrderStatus($order, $event, $rzpWebhookData, $orderLinkCollection)
{
$this->logger->info("Cronjob: Updating to Processing for Order ID: "
. $order->getEntityId()
. $order->getIncrementId()
. " and Event :"
. $event
. " started."
Expand Down Expand Up @@ -355,7 +366,7 @@ private function updateOrderStatus($order, $event, $rzpWebhookData, $orderLinkCo
}

$this->logger->info("Cronjob: Updating to Processing for Order ID: "
. $order->getEntityId()
. $order->getIncrementId()
. " and Event :"
. $event
. " ended."
Expand Down
5 changes: 5 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Config
const ENABLE_CUSTOM_PAID_ORDER_STATUS = 'enable_custom_paid_order_status';
const CUSTOM_PAID_ORDER_STATUS = 'custom_paid_order_status';
const ENABLE_UPDATE_ORDER_CRON_V1 = 'enable_update_order_cron_v1';
const ENABLED_DEBUG_MODE = 'enable_debug_mode';
/**
* @var string
*/
Expand Down Expand Up @@ -132,6 +133,10 @@ public function setStoreId($storeId)
return $this;
}

public function isDebugModeEnabled()
{
return (bool) (int) $this->getConfigData(self::ENABLED_DEBUG_MODE, $this->storeId);
}
/**
* Retrieve information from payment configuration
*
Expand Down
37 changes: 37 additions & 0 deletions Model/Util/DebugMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Razorpay\Magento\Model\Util;

class DebugMode
{
/**
* @var \Razorpay\Magento\Model\Config
*/
protected $config;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

protected $isDebugModeEnabled;

public function __construct(
\Razorpay\Magento\Model\Config $config,
\Psr\Log\LoggerInterface $logger
)
{
$this->config = $config;
$this->logger = $logger;

$this->isDebugModeEnabled = $this->config->isDebugModeEnabled();
}

public function log($message)
{
if($this->isDebugModeEnabled)
{
$this->logger->info($message);
}
}
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/razorpay/enable_update_order_cron_v1</config_path>
</field>
<field id="enable_debug_mode" translate="label" type="select" sortOrder="101" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Debug Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/razorpay/enable_debug_mode</config_path>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<enable_custom_paid_order_status>0</enable_custom_paid_order_status>
<custom_paid_order_status>processing</custom_paid_order_status>
<enable_update_order_cron_v1>1</enable_update_order_cron_v1>
<enable_debug_mode>0</enable_debug_mode>
</razorpay>
</payment>
</default>
Expand Down
5 changes: 5 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@
<argument name="logger" xsi:type="object">RazorpayLogger</argument>
</arguments>
</type>
<type name="Razorpay\Magento\Model\Util\DebugMode">
<arguments>
<argument name="logger" xsi:type="object">RazorpayLogger</argument>
</arguments>
</type>
</config>