I'm new in magento 2, my question is how to send parameters with results responses to another url whenever i click "Place Order" and before proceeding to success page. Using event and Observer. Please help. Thank you in advance.
2 Answers
To send parameters with the result response to another URL before proceeding to the success page, you can create an observer for the sales_order_place_after event.
First, create a file named events.xml in your module's etc directory with the following content:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="your_module_observer" instance="Your\Module\Observer\PlaceOrderAfter" />
</event>
</config>
Next, create the PlaceOrderAfter.php observer class in the Observer directory of your module with the following content:
<?php
namespace Your\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class PlaceOrderAfter implements ObserverInterface
{
protected $_redirect;
protected $_response;
protected $_urlInterface;
public function __construct(
\Magento\Framework\App\Response\Http $response,
\Magento\Framework\App\ActionFlag $redirect,
\Magento\Framework\UrlInterface $urlInterface
) {
$this->_response = $response;
$this->_redirect = $redirect;
$this->_urlInterface = $urlInterface;
}
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
// Set parameters you want to pass in a variable
$params = ['param1' => 'value1', 'param2' => 'value2'];
// Redirect to the URL along with the parameters
$this->_response->setRedirect($this->_urlInterface->getUrl('your/url/path', ['_query' => $params]));
$this->_redirect->set('', 'redirect', true);
}
}
In the above code, you can set the parameters you want to pass in a variable named $params and redirect to the URL along with the parameters using the setRedirect function of the \Magento\Framework\App\Response\Http class.
Make sure to replace Your\Module with the namespace of your module and your/url/path with the URL you want to redirect to along with the parameters.
I'm having error it says "Something went wrong with your request. Please try again later. i see this error on the inspect jquery.js:10109 POST http://clickstore.magento.com/rest/default/V1/carts/mine/payment-information 500 (Internal Server Error)
here's my event.xml
<event name="sales_order_place_after">
<observer name="sample_payment_gateway_place_order_after" instance="SampleModule\SamplePaymentGateway\Observer\PlaceOrderAfter" />
</event>
Here's my observer OrderPlaceAfter.php
public function __construct(
\Magento\Framework\App\Response\Http $response,
\Magento\Framework\App\ActionFlag $redirect,
\Magento\Framework\UrlInterface $urlInterface
) {
$this->_response = $response;
$this->_redirect = $redirect;
$this->_urlInterface = $urlInterface;
}
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
// Set parameters you want to pass in a variable
$params = array(
'merchantid' => MERCHANT_ID,
'txnid' => '001',
'amount' => 100,
'ccy' => 'PHP',
'description' => 'My order description.',
'email' => '[email protected]'
);
// Redirect to the URL along with the parameters
$this->_response->setRedirect($this->_urlInterface->getUrl('https://gw.dragonpay.ph/Pay.aspx?procid=GCSH&', ['_query' => $params]));
$this->_redirect->set('', 'redirect', true);
}
}