1

I have a payment module that is working on Magento 1. I wanted to make it work on Magento 2.(bold is because someone commented i should mention what i want to achieve in the question)

So, I have convert my Magento 1 payment module using a converter. I guess I need to modify the codes so that it works Magento 2. And I am stuck at the checkout process, redirecting to the Payment gateway.

Below is my error when magento is trying to redirect to http://localhost:8083/mm/pdcptb/pdcptb/redirect/

There has been an error processing your request

Exception printing is disabled by default for security reasons.

Error log record number: 979987430

The console in the firebug shows:

"NetworkError: 503 Service Unavailable - http://localhost:8083/mm/pdcptb/pdcptb/redirect/"

and below is the error log file:

a:4:{i:0;s:20:"Invalid block type: ";i:1;s:11118:"#0 C:\Apache24\htdocs\mm\vendor\magento\framework\View\Layout\Generator\Block.php(237): Magento\Framework\View\Layout\Generator\Block->getBlockInstance('', Array)
#1 C:\Apache24\htdocs\mm\vendor\magento\framework\View\Layout.php(760): Magento\Framework\View\Layout\Generator\Block->createBlock('', '_0', Array)
#2 C:\Apache24\htdocs\mm\vendor\magento\framework\View\Layout.php(743): Magento\Framework\View\Layout->_createBlock('', '_0', Array)
#3 C:\Apache24\htdocs\mm\var\generation\Magento\Framework\View\Layout\Interceptor.php(349): Magento\Framework\View\Layout->createBlock('', '', Array)
#4 C:\Apache24\htdocs\mm\app\code\Asiapay\Pdcptb\Controller\Pdcptb\Redirect.php(39): Magento\Framework\View\Layout\Interceptor->createBlock('')
#5 C:\Apache24\htdocs\mm\var\generation\Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor.php(24): Asiapay\Pdcptb\Controller\Pdcptb\Redirect->execute()
#6 C:\Apache24\htdocs\mm\vendor\magento\framework\App\Action\Action.php(102): Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor->execute()
#7 [internal function]: Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#8 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Interceptor.php(74): call_user_func_array(Array, Array)
#9 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(70): Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor->___callParent('dispatch', Array)
#10 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Asiapay\\Pdcptb\\...', 'dispatch', Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Array, 'designLoader')

#11 C:\Apache24\htdocs\mm\vendor\magento\framework\App\Action\Plugin\Design.php(39): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#12 [internal function]: Magento\Framework\App\Action\Plugin\Design->aroundDispatch(Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#13 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#14 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Asiapay\\Pdcptb\\...', 'dispatch', Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Array, 'contextPlugin')
#15 C:\Apache24\htdocs\mm\vendor\magento\module-store\App\Action\Plugin\Context.php(106): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#16 [internal function]: Magento\Store\App\Action\Plugin\Context->aroundDispatch(Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#17 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#18 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Asiapay\\Pdcptb\\...', 'dispatch', Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Array, 'storeCheck')
#19 C:\Apache24\htdocs\mm\vendor\magento\module-store\App\Action\Plugin\StoreCheck.php(44): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#20 [internal function]: Magento\Store\App\Action\Plugin\StoreCheck->aroundDispatch(Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))

#21 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68):call_user_func_array(Array, Array)
#22 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Asiapay\\Pdcptb\\...', 'dispatch', Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Array, 'customer-app-ac...')
#23 C:\Apache24\htdocs\mm\vendor\magento\module-customer\Model\App\Action\ContextPlugin.php(61): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#24 [internal function]: Magento\Customer\Model\App\Action\ContextPlugin->aroundDispatch(Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#25 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#26 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Asiapay\\Pdcptb\\...', 'dispatch', Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Array, 'tax-app-action-...')
#27 C:\Apache24\htdocs\mm\vendor\magento\module-tax\Model\App\Action\ContextPlugin.php(91): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#28 [internal function]: Magento\Tax\Model\App\Action\ContextPlugin->aroundDispatch(Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#29 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#30 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Interceptor.php(136): Magento\Framework\Interception\Chain\Chain->invokeNext('Asiapay\\Pdcptb\\...', 'dispatch', Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Array, 'weee-app-action...')

#31 C:\Apache24\htdocs\mm\vendor\magento\module-weee\Model\App\Action\ContextPlugin.php(112): Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#32 [internal function]: Magento\Weee\Model\App\Action\ContextPlugin->aroundDispatch(Object(Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#33 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Interceptor.php(141): call_user_func_array(Array, Array)
#34 C:\Apache24\htdocs\mm\var\generation\Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor.php(39): Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor->___callPlugins('dispatch', Array, Array)
#35 C:\Apache24\htdocs\mm\vendor\magento\framework\App\FrontController.php(55): Asiapay\Pdcptb\Controller\Pdcptb\Redirect\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#36 [internal function]: Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#37 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Interceptor.php(74): call_user_func_array(Array, Array)
#38 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#39 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'requestPreproce...')
#40 C:\Apache24\htdocs\mm\vendor\magento\module-store\App\FrontController\Plugin\RequestPreprocessor.php(89): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))

#41 [internal function]: Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#42 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#43 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#44 C:\Apache24\htdocs\mm\vendor\magento\framework\Module\Plugin\DbStatusValidator.php(69): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#45 [internal function]: Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#46 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#47 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#48 C:\Apache24\htdocs\mm\vendor\magento\module-page-cache\Model\App\FrontController\BuiltinPlugin.php(73): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#49 [internal function]: Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))

#50 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Chain\Chain.php(68): call_user_func_array(Array, Array)
#51 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Interceptor.php(136): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#52 C:\Apache24\htdocs\mm\vendor\magento\module-page-cache\Model\App\FrontController\VarnishPlugin.php(55): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#53 [internal function]: Magento\PageCache\Model\App\FrontController\VarnishPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#54 C:\Apache24\htdocs\mm\vendor\magento\framework\Interception\Interceptor.php(141): call_user_func_array(Array, Array)
#55 C:\Apache24\htdocs\mm\var\generation\Magento\Framework\App\FrontController\Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#56 C:\Apache24\htdocs\mm\vendor\magento\framework\App\Http.php(115): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#57 C:\Apache24\htdocs\mm\vendor\magento\framework\App\Bootstrap.php(258): Magento\Framework\App\Http->launch()
#58 C:\Apache24\htdocs\mm\index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#59 {main}";s:3:"url";s:27:"/mm/pdcptb/pdcptb/redirect/";s:11:"script_name";s:13:"/mm/index.php";}

Below is the code of module/name/Controller/Pdcptb/Redirect.php

<?php

namespace Asiapay\Pdcptb\Controller\Pdcptb;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\View\LayoutFactory;

class Redirect extends AbstractPdcptb
{
/**
 * @var RawFactory
 */
protected $_resultRawFactory;

/**
 * @var LayoutFactory
 */
protected $_viewLayoutFactory;

public function __construct(Context $context, 
    RawFactory $resultRawFactory, 
    LayoutFactory $viewLayoutFactory)
{
    $this->_resultRawFactory = $resultRawFactory;
    $this->_viewLayoutFactory = $viewLayoutFactory;

    parent::__construct($context);
}

/**
 * When a customer chooses Pdcptb on Checkout/Payment page
 */
public function execute()
{
    $session = ObjectManager::getInstance()->get('Magento\Checkout\Model\Session');
    $session->setPdcptbQuoteId($session->getQuoteId());
    $this->_resultRawFactory->create()->setContents($this->_viewLayoutFactory->create()->createBlock('')->toHtml());
    $session->unsQuoteId();
}
}

I guess the error is caused by my modules files. But i'm not sure why they have error. Can anybody guide me to solve this issue? I am not really strong at OO.

5
  • If answer is working for you, you should accept this answer.
    – Sohel Rana
    Commented Jul 12, 2016 at 11:19
  • no, it is not working, it redirects me to the shopping cart if i put pdcptb/redirect.
    – Tine
    Commented Jul 13, 2016 at 2:15
  • You should decide first what do you want, and then write a question here. Make sense?
    – Sohel Rana
    Commented Jul 13, 2016 at 4:43
  • yes i guess i will update it soon. U voted my question down just because i didnt accept your answer?
    – Tine
    Commented Jul 13, 2016 at 8:12
  • Hey, by mistake it's click down vote from phone. Answer accept depends on up to you. But it's really helpful for future if you accept answer not only me. I just says you another too.
    – Sohel Rana
    Commented Jul 13, 2016 at 8:30

2 Answers 2

2

Check following line:

$this->_resultRawFactory->create()->setContents($this->_viewLayoutFactory->create()->createBlock('')->toHtml());

Here createBlock('') need a param which is block object. This block is your custom or magento 2 default. For example:

$this->_resultRawFactory->create()->setContents($this->_viewLayoutFactory->create()->createBlock('\Magento\Framework\View\Element\Template')->toHtml());

[Updated]

Check following function.

public function execute()
{
    try {
        $this->getResponse()->setRedirect($url);
        return;
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        $this->messageManager->addExceptionMessage($e, $e->getMessage());
    } catch (\Exception $e) {

    }

    $this->_redirect('checkout/cart');
}
6
  • hi there, i have tried to put some block into it, it does not generate error anymore. But do u know which block should i put?
    – Tine
    Commented Jul 12, 2016 at 8:08
  • What exactly you need now?
    – Sohel Rana
    Commented Jul 12, 2016 at 8:15
  • Sohel Rana, i need to redirect to a payment gateway. It works when Magento 1 run http://localhost:8083/mm/pdcptb/pdcptb/redirect/, and redirect to the payment gateway. But it Magento 2, it has many error. I need to fix them.
    – Tine
    Commented Jul 12, 2016 at 9:07
  • check updated code
    – Sohel Rana
    Commented Jul 12, 2016 at 9:21
  • ohhh, it redirects me to the shopping cart >< should i replace the $url with my payment gateway URL?
    – Tine
    Commented Jul 12, 2016 at 9:56
0

In magento2 to simply redirect to payment gateway url on click place order you can add following code to your js file(view/payment/method-renderer/yourjs.js)

    placeOrder: function (data, event) {  
        if (event) {  
            event.preventDefault();  
        }  
        var self = this,  
            placeOrder,  
            emailValidationResult = customer.isLoggedIn(),  
            loginFormSelector = 'form[data-role=email-   with-possible-          login]';  
        if (!customer.isLoggedIn()) {   
            $(loginFormSelector).validation();  
            emailValidationResult = Boolean($(loginFormSelector + '   input[name=username]').valid());   
        }  
        if (emailValidationResult && this.validate() &&   additionalValidators.validate()) {  
            this.isPlaceOrderActionAllowed(false);  
            placeOrder = placeOrderAction(this.getData(), false, this.messageContainer);  

            $.when(placeOrder).fail(function () {  
                self.isPlaceOrderActionAllowed(true);  
            }).done(this.afterPlaceOrder.bind(this));  
            return true;  
        }  
        return false;  
    },  

    selectPaymentMethod: function(){  
        selectPaymentMethodAction(this.getData());  
        checkoutData.setSelectedPaymentMethod(this.item.method);  
        return true;  
    },  

    afterPlaceOrder: function () {  
        window.location.replace(url.build('frontname/Pdcptb/redirect'));  
    },  

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.