Skip to content

Commit

Permalink
added changed to reset cart and invoice generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ram Prakash Singh committed Sep 24, 2020
1 parent 79046ff commit 880b0e6
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 25 deletions.
15 changes: 11 additions & 4 deletions Controller/Payment/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

class Order extends \Razorpay\Magento\Controller\BaseController
{
protected $quote;
protected $quote;

protected $checkoutSession;
protected $checkoutSession;

protected $_currency = PaymentMethod::CURRENCY;
/**
protected $_currency = PaymentMethod::CURRENCY;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $checkoutSession
Expand Down Expand Up @@ -52,6 +52,13 @@ public function execute()
$maze_version = $this->_objectManager->get('Magento\Framework\App\ProductMetadataInterface')->getVersion();
$module_version = $this->_objectManager->get('Magento\Framework\Module\ModuleList')->getOne('Razorpay_Magento')['setup_version'];


//if already order from same session , let make it's to pending state
$orderModel = $this->_objectManager->get('Magento\Sales\Model\Order')->load($mazeOrder->getEntityId());
$orderModel->setState('new')
->setStatus('pending');
$orderModel->save();

if ($payment_action === 'authorize')
{
$payment_capture = 0;
Expand Down
89 changes: 89 additions & 0 deletions Controller/Payment/ResetCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Razorpay\Magento\Controller\Payment;

use Razorpay\Magento\Model\PaymentMethod;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\ObjectManager;

class ResetCart extends \Razorpay\Magento\Controller\BaseController
{
protected $quote;

protected $checkoutSession;

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Razorpay\Model\CheckoutFactory $checkoutFactory
* @param \Magento\Razorpay\Model\Config\Payment $razorpayConfig
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Razorpay\Magento\Model\CheckoutFactory $checkoutFactory,
\Razorpay\Magento\Model\Config $config,
\Magento\Catalog\Model\Session $catalogSession
) {
parent::__construct(
$context,
$customerSession,
$checkoutSession,
$config
);

$this->checkoutFactory = $checkoutFactory;
$this->catalogSession = $catalogSession;
$this->config = $config;
}

public function execute()
{
$lastQuoteId = $this->checkoutSession->getLastQuoteId();
$lastOrderId = $this->checkoutSession->getLastRealOrder();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();



if ($lastQuoteId && $lastOrderId) {
$orderModel = $objectManager->get('Magento\Sales\Model\Order')->load($lastOrderId->getEntityId());

if($orderModel->canCancel()) {

$quote = $objectManager->get('Magento\Quote\Model\Quote')->load($lastQuoteId);
$quote->setIsActive(true)->save();

//not canceling order as cancled order can't be used again for order processing.
//$orderModel->cancel();
$orderModel->setStatus('canceled');
$orderModel->save();
$this->checkoutSession->setFirstTimeChk('0');

$responseContent = [
'success' => true,
'redirect_url' => 'checkout/#payment'
];
}
}

if (!$lastQuoteId || !$lastOrderId) {
$responseContent = [
'success' => true,
'redirect_url' => 'checkout/cart'
];
}

$this->messageManager->addError(__('Payment Failed or Payment closed'));

$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$response->setData($responseContent);
$response->setHttpResponseCode(200);

return $response;

}

}
94 changes: 83 additions & 11 deletions Controller/Payment/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ class Validate extends \Razorpay\Magento\Controller\BaseController implements Cs
const STATUS_APPROVED = 'APPROVED';
const STATUS_PROCESSING = 'processing';

/**
* @var \Magento\Sales\Model\Service\InvoiceService
*/
protected $_invoiceService;
protected $_invoiceSender;

/**
* @var \Magento\Framework\DB\Transaction
*/
protected $_transaction;


/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Customer\Model\Session $customerSession
Expand All @@ -49,6 +61,9 @@ public function __construct(
\Razorpay\Magento\Model\Config $config,
\Magento\Catalog\Model\Session $catalogSession,
\Magento\Sales\Api\Data\OrderInterface $order,
\Magento\Sales\Model\Service\InvoiceService $invoiceService,
\Magento\Framework\DB\Transaction $transaction,
\Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,
OrderRepositoryInterface $orderRepository,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
)
Expand All @@ -65,12 +80,16 @@ public function __construct(

$this->api = new Api($keyId, $keySecret);
$this->order = $order;

$this->config = $config;

$this->objectManagement = \Magento\Framework\App\ObjectManager::getInstance();
$this->catalogSession = $catalogSession;
$this->orderRepository = $orderRepository;
$this->orderSender = $orderSender;

$this->_invoiceService = $invoiceService;
$this->_invoiceSender = $invoiceSender;
$this->_transaction = $transaction;
}

/**
Expand Down Expand Up @@ -102,11 +121,18 @@ public function execute()
return;
}

$order = $this->checkoutSession->getLastRealOrder();

$responseContent = [
'success' => false,
'redirect_url' => 'checkout/#payment',
'parameters' => []
];

try
{
$this->validateSignature($post);

$order = $this->checkoutSession->getLastRealOrder();
$orderId = $order->getIncrementId();
$order->setState(static::STATUS_PROCESSING)->setStatus(static::STATUS_PROCESSING);

Expand All @@ -120,45 +146,85 @@ public function execute()
->setIsTransactionClosed(true)
->setShouldCloseParentTransaction(true);

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

$payment->addTransactionCommentsToOrder(
$paymentId,
""
);

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

$order->save();

$this->orderRepository->save($order);

//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($order->canInvoice() and
($this->config->getPaymentAction() === \Razorpay\Magento\Model\PaymentMethod::ACTION_AUTHORIZE_CAPTURE) and
$this->config->canAutoGenerateInvoice())
{
$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();

$this->_invoiceSender->send($invoice);
//send notification code
$order->setState(static::STATUS_PROCESSING)->setStatus(static::STATUS_PROCESSING);
$order->addStatusHistoryComment(
__('Notified customer about invoice #%1.', $invoice->getId())
)
->setIsCustomerNotified(true)
->save();
}

//send Order email, after successfull payment
$this->checkoutSession->setRazorpayMailSentOnSuccess(true);
$this->orderSender->send($order, true);
$this->checkoutSession->unsRazorpayMailSentOnSuccess();

$responseContent = [
'success' => true,
'redirect_url' => 'onepage/success/',
'redirect_url' => 'checkout/onepage/success/',
'order_id' => $orderId,
];

$code = 200;

$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$response->setData($responseContent);
$response->setHttpResponseCode($code);
return $response;

}
catch(\Razorpay\Api\Errors\Error $e)
{
$responseContent = [
'message' => $e->getMessage(),
'parameters' => []
];
$responseContent['message'] = $e->getMessage();

$code = $e->getCode();
}
catch(\Exception $e)
{
$responseContent = [
'message' => $e->getMessage(),
'parameters' => []
];
$responseContent['message'] = $e->getMessage();

$code = $e->getCode();
}

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

$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$response->setData($responseContent);
Expand All @@ -169,6 +235,12 @@ public function execute()

protected function validateSignature($request)
{
if(empty($request['error']) === false)
{
$this->messageManager->addError(__('Payment Failed'));
throw new \Exception("Payment Failed or error from gateway");
}

$attributes = array(
'razorpay_payment_id' => $request['razorpay_payment_id'],
'razorpay_order_id' => $this->catalogSession->getRazorpayOrderID(),
Expand Down
11 changes: 10 additions & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Config
const KEY_PUBLIC_KEY = 'key_id';
const KEY_PRIVATE_KEY = 'key_secret';
const KEY_MERCHANT_NAME_OVERRIDE = 'merchant_name_override';
const KEY_PAYMENT_ACTION = 'payment_action';
const KEY_PAYMENT_ACTION = 'rzp_payment_action';
const KEY_AUTO_INVOICE = 'auto_invoice';

/**
* @var string
Expand Down Expand Up @@ -94,6 +95,14 @@ public function isActive()
return (bool) (int) $this->getConfigData(self::KEY_ACTIVE, $this->storeId);
}

/**
* @return bool
*/
public function canAutoGenerateInvoice()
{
return (bool) (int) $this->getConfigData(self::KEY_AUTO_INVOICE, $this->storeId);
}

/**
* To check billing country is allowed for the payment method
*
Expand Down
9 changes: 8 additions & 1 deletion Model/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod
/**
* @var bool
*/
protected $_canUseInternal = false; //Disable module for Magento Admin Order
protected $_canUseInternal = true; //Disable module for Magento Admin Order

/**
* @var bool
Expand Down Expand Up @@ -95,6 +95,8 @@ class PaymentMethod extends \Magento\Payment\Model\Method\AbstractMethod
*/
protected $orderRepository;

//protected $_isOffline = true;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -204,6 +206,11 @@ public function refund(InfoInterface $payment, $amount)
return $this;
}

public function capture(InfoInterface $payment, $amount)
{
return $this;
}

/**
* Format param "channel" for transaction
*
Expand Down
11 changes: 8 additions & 3 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/razorpay/active</config_path>
</field>
<field id="payment_action" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="rzp_payment_action" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" >
<label>Payment Action</label>
<source_model>Razorpay\Magento\Model\PaymentAction</source_model>
<config_path>payment/razorpay/payment_action</config_path>
<config_path>payment/razorpay/rzp_payment_action</config_path>
</field>
<field id="title" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Title</label>
Expand All @@ -36,9 +36,14 @@
</field>
<field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
<label>New Order Status</label>
<source_model>Magento\Sales\Model\Config\Source\Order\Status\Processing</source_model>
<source_model>Magento\Sales\Model\Config\Source\Order\Status\NewStatus</source_model>
<config_path>payment/razorpay/order_status</config_path>
</field>
<field id="auto_invoice" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Auto Generate Invoice</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/razorpay/auto_invoice</config_path>
</field>
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Payment from Applicable Countries</label>
<source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
Expand Down
Loading

0 comments on commit 880b0e6

Please sign in to comment.