0

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
  • which type of other URL you are talking about? Commented Mar 31, 2023 at 5:17
  • the 3rd party merchant API url.
    – vinz vinz
    Commented Mar 31, 2023 at 9:26

2 Answers 2

0

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.

1
  • btw sir, i want to pass the parameters of customer order details
    – vinz vinz
    Commented Mar 31, 2023 at 11:44
0

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)

enter image description here

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);
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.