How can I redirect user to the payment gateway after placing order in magento 2?
-
19-12-2020 - |
Question
I am creating my own payment method which should redirect user to the payment gateway of a bank after placing order. How can I do it?
Solution
This is the observer code to redirect to that payment gateway.
public function execute(\Magento\Framework\Event\Observer $observer)
{
$orderId = $observer->getEvent()->getOrderIds();
$base_url = $this->_storeManager->getStore()->getBaseUrl();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId[0]);
$payment = $order->getPayment();
$method = $payment->getMethodInstance();
$methodTitle = $method->getTitle();
$order_data= $order->getData();
$status = $this->checkoutSession->getLastOrderStatus();
$increment_id = $order_data['increment_id'];
$redirect = $objectManager->get('\Magento\Framework\App\Response\Http');
$redirect->setRedirect($base_url.'custompayment/index/redirect/id/'.$increment_id.'');
return;
}
My Controller having load layout and render layout only please find my phtml file for that controller it'll auto submit and redirect to payment gateway,
<form action="<?php echo $this->getModeurl();?>" id="custom_payment" method="post">
<input type="hidden" name="action" value="pay" />
<input type="hidden" name="currency" value="<?php echo $this->getOrderdetails('order_currency_code');?>" />
<input type="hidden" name="version" value="2.0" />
<input type="hidden" name="item_name_<?php echo $i ?>" value="<?php echo $item['name'] ?>" />
<input type="hidden" name="item_description_<?php echo $i ?>" value="<?php echo $item['description'] ?>" />
<input type="hidden" name="item_quantity_<?php echo $i ?>" value="<?php echo round($item['quantity']) ?>" />
<input type="hidden" name="item_amount_<?php echo $i ?>" value="<?php echo $item['price'] ?>" />
<input type="hidden" name="merchant" value="<?php echo $this->getMerchant();?>" />
<input type="hidden" name="ref_id" value="<?php echo $this->getOrderdetails('increment_id');?>" />
<input type="hidden" name="delivery_charge" value="<?php echo $this->getOrderdetails('shipping_amount');?>" />
<input type="hidden" name="tax_amount" value="<?php echo $this->getOrderdetails('tax_amount');?>" />
<input type="hidden" name="total_amount" value="<?php echo $this->getOrderdetails('grand_total');?>" />
<input type="hidden" name="str_url" value="" />
<input type="hidden" name="success_url" value="http://127.0.0.1/payment/smoovpay/index/response" />
<input type="hidden" name="cancel_url" value="http://YourWebSite" />
<input type="hidden" name="signature" value="<?php echo $this->getSignatureHash();?>"/>
<input type="hidden" name="signature_algorithm" value="sha1" />
<h3>Please wait we are redirecting to Smoovpay.....</h3>
</form>
<script type="text/javascript">
//<![CDATA[
var paymentform = document.getElementById('custom_payment');
window.onload = paymentform.submit();
//]]>
</script>