Skip to content

Commit

Permalink
added some logs in UpdateOrdersToProcessing cron
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsharma1100 committed Jul 20, 2022
1 parent 7b5f35a commit 4b2a2a9
Showing 1 changed file with 105 additions and 95 deletions.
200 changes: 105 additions & 95 deletions Cron/UpdateOrdersToProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function __construct(

public function execute()
{

$this->logger->info("Cronjob: Update Orders To Processing Cron started.");

$dateTimeCheck = date('Y-m-d H:i:s', strtotime('-' . static::PROCESS_ORDER_WAIT_TIME . ' minutes'));
Expand All @@ -126,7 +125,8 @@ public function execute()

foreach ($orders->getItems() as $order)
{
if ($order->getPayment()->getMethod() === 'razorpay') {
if ((empty($order) === false) && ($order->getPayment()->getMethod() === 'razorpay'))
{
$rzpWebhookData = $order->getRzpWebhookData();
if (empty($rzpWebhookData) === false) // check if webhook cron has run and populated the rzp_webhook_data column
{
Expand All @@ -136,124 +136,134 @@ public function execute()
{
$this->updateOrderStatus($order, $rzpWebhookDataObj);
}
}
else
{
$this->logger->info('Razorpay Webhook code not triggered yet. \'rzp_webhook_data\' is empty');
}
}
}
}

private function updateOrderStatus($order, $rzpWebhookData)
{
if ($order)
$this->logger->info("Cronjob: Updating to Processing for Order ID: "
. $order->getEntityId()
. " and Event :"
. $rzpWebhookData['event']
. " started."
);

$payment = $order->getPayment();
$paymentId = $rzpWebhookData['payment_id'];
$rzpOrderAmount = $rzpWebhookData['amount'];
$event = $rzpWebhookData['event'];

$payment->setLastTransId($paymentId)
->setTransactionId($paymentId)
->setIsTransactionClosed(true)
->setShouldCloseParentTransaction(true);

$payment->setParentTransactionId($payment->getTransactionId());

if ($event === 'payment.authorized')
{
$this->logger->info("Cronjob: Updating to Processing for Order ID: "
. $order->getEntityId()
. " and Event :"
. $rzpWebhookData['event']);

$payment = $order->getPayment();
$paymentId = $rzpWebhookData['payment_id'];
$rzpOrderAmount = $rzpWebhookData['amount'];
$event = $rzpWebhookData['event'];

$payment->setLastTransId($paymentId)
->setTransactionId($paymentId)
->setIsTransactionClosed(true)
->setShouldCloseParentTransaction(true);

$payment->setParentTransactionId($payment->getTransactionId());

if ($event === 'payment.authorized')
{
$payment->addTransactionCommentsToOrder(
"$paymentId",
(new AuthorizeCommand())->execute(
$payment,
$order->getGrandTotal(),
$order
),
""
);
}
else
{
$payment->addTransactionCommentsToOrder(
"$paymentId",
(new CaptureCommand())->execute(
$payment,
$order->getGrandTotal(),
$order
),
""
);
}
$payment->addTransactionCommentsToOrder(
"$paymentId",
(new AuthorizeCommand())->execute(
$payment,
$order->getGrandTotal(),
$order
),
""
);
}
else
{
$payment->addTransactionCommentsToOrder(
"$paymentId",
(new CaptureCommand())->execute(
$payment,
$order->getGrandTotal(),
$order
),
""
);
}

$transaction = $payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_AUTH, null, true, "");
$transaction = $payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_AUTH, null, true, "");

$transaction->setIsClosed(true);
$transaction->setIsClosed(true);

$transaction->save();
$transaction->save();

$amountPaid = number_format($rzpOrderAmount / 100, 2, ".", "");
$amountPaid = number_format($rzpOrderAmount / 100, 2, ".", "");

$order->setState(static::STATUS_PROCESSING)->setStatus(static::STATUS_PROCESSING);
$order->setState(static::STATUS_PROCESSING)->setStatus(static::STATUS_PROCESSING);

$order->addStatusHistoryComment(
__(
'Actual Amount %1 of %2, with Razorpay Offer/Fee applied.',
"Authroized",
$order->getBaseCurrency()->formatTxt($amountPaid)
)
);
$order->addStatusHistoryComment(
__(
'Actual Amount %1 of %2, with Razorpay Offer/Fee applied.',
"Authroized",
$order->getBaseCurrency()->formatTxt($amountPaid)
)
);

//update/disable the quote
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quote = $objectManager->get('Magento\Quote\Model\Quote')->load($order->getQuoteId());
$quote->setIsActive(false)->save();
//update/disable the quote
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quote = $objectManager->get('Magento\Quote\Model\Quote')->load($order->getQuoteId());
$quote->setIsActive(false)->save();

if ($event === 'order.paid')
if ($event === 'order.paid')
{
if ($order->canInvoice() && $this->config->canAutoGenerateInvoice())
{
if ($order->canInvoice() && $this->config->canAutoGenerateInvoice())
{
$invoice = $this->invoiceService->prepareInvoice($order);
$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
$invoice->setTransactionId($paymentId);
$invoice->register();
$invoice->save();
$invoice = $this->invoiceService->prepareInvoice($order);
$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE);
$invoice->setTransactionId($paymentId);
$invoice->register();
$invoice->save();

$transactionSave = $this->transaction
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$transactionSave = $this->transaction
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();

$this->invoiceSender->send($invoice);
$this->invoiceSender->send($invoice);

//send notification code
$order->setState(static::STATUS_PROCESSING)->setStatus(static::STATUS_PROCESSING);
//send notification code
$order->setState(static::STATUS_PROCESSING)->setStatus(static::STATUS_PROCESSING);

$order->addStatusHistoryComment(
__('Notified customer about invoice #%1.', $invoice->getId())
)->setIsCustomerNotified(true);
$order->addStatusHistoryComment(
__('Notified customer about invoice #%1.', $invoice->getId())
)->setIsCustomerNotified(true);

//send Order email, after successfull payment
try
{
$this->checkoutSession->setRazorpayMailSentOnSuccess(true);
$this->orderSender->send($order);
$this->checkoutSession->unsRazorpayMailSentOnSuccess();
}
catch (\Magento\Framework\Exception\MailException $e)
{
$this->logger->critical($e);
}
catch (\Exception $e)
{
$this->logger->critical($e);
}
//send Order email, after successfull payment
try
{
$this->checkoutSession->setRazorpayMailSentOnSuccess(true);
$this->orderSender->send($order);
$this->checkoutSession->unsRazorpayMailSentOnSuccess();
}
catch (\Magento\Framework\Exception\MailException $e)
{
$this->logger->critical($e);
}
catch (\Exception $e)
{
$this->logger->critical($e);
}
}

$order->save();
}

$order->save();

$this->logger->info("Cronjob: Updating to Processing for Order ID: "
. $order->getEntityId()
. " and Event :"
. $rzpWebhookData['event']
. " ended."
);
}

// /**
Expand Down

0 comments on commit 4b2a2a9

Please sign in to comment.