5


The problem actually is defined in two steps:
1. How to define a REST Api which returns an array?
2. How to use that REST Api through javascript?

Description:
What I have done but not achieved results:

etc/frontend/webapi.xml:

<?xml version="1.0"?> <routes
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">

    <route url="/V1/shetab/transaction/refid" method="POST">
        <service class="Amaj\ShetabPayment\Api\ShetabServiceInterface" method="getTransRefId"/>
        <resources>
            <resource ref="anonymous" /><!-- works even if user has been logged of -->
        </resources>
    </route>

</routes>


etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Amaj\ShetabPayment\Api\ShetabServiceInterface"
                type="Amaj\ShetabPayment\Model\ShetabService" />
</config>


Api/ShetabServiceInterface.php:

interface ShetabServiceInterface
{
    /** Returns Transaction Reference ID
     *
     * @param int $quoteId
     * @return string|null
     */
    public function getTransRefId($quoteId);
}


Model/ShetabService.php:

namespace Amaj\ShetabPayment\Model;

interface ShetabService implements \Amaj\ShetabPayment\Api\ShetabServiceInterface
{
    protected $_logger;

    public function __construc(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger;
    }


    /** Returns Transaction Reference ID
     *
     * @param int $quoteId
     * @return string|null
     */
    public function getTransRefId($quoteId)
    {
        $this->_logger->info('getTransRefId is called: ' . $quoteId);
        $result = array(
            'transRefId' => 50
        );
        return json_encode($result);
    }
}


Javascript code:

define(
    [
        'jquery',
        'Magento_Checkout/js/model/url-builder',
        'mage/storage',
        'Magento_Checkout/js/model/error-processor',
        'Magento_Checkout/js/model/full-screen-loader'
    ],
    function (
        $,
        urlBuilder,
        storage,
        errorProcessor,
        fullScreenLoader
    ) {
        'use strict';

        return {
            getTransRefId: function(quoteId, messageContainer) {

                var result = storage.post(
                    urlBuilder.createUrl('/shetab/transaction/refid', {}),
                    JSON.stringify({
                        quoteId: quoteId
                    })

                ).fail(
                    function (response) {
                        errorProcessor.process(response, messageContainer);
                        fullScreenLoader.stopLoader();
                    }
                );
                console.log( JSON.stringify(result.responseText) );
            }
        };


    }
);
3

"Request does not match any route." appears because of incorrect path of webapi.xml.

You must move it to etc/webapi.xml.

Also your file Model/ShetabService.php contains errors:

  • class ShetabService, not interface
  • typo in __construct function name(last t missed)

Here is fixed one:

namespace Amaj\ShetabPayment\Model;

class ShetabService implements \Amaj\ShetabPayment\Api\ShetabServiceInterface
{
    private $_logger;

    public function __construct(\Psr\Log\LoggerInterface $logger) {
        $this->_logger = $logger;
    }


    /** Returns Transaction Reference ID
     *
     * @param int $quoteId
     * @return string|null
     */
    public function getTransRefId($quoteId)
    {
        $this->_logger->info('getTransRefId is called: ' . $quoteId);
        $result = array(
            'transRefId' => 50
        );
        return json_encode($result);
    }
}
2

Your Api method should have the annotations: @api, param and return values, for example:

namespace Amaj\ShetabPayment\Api;

interface ShetabServiceInterface
{

    /**
     * @api
     * @param string $quoteId
     * @return string|null
     */
    public function getTransRefId($quoteId);
}
  1. How to define a REST Api which returns an array?

It should return Json format. Because the response from Web Api already sets the json type for header. Javascript can handle the Json type easily.

  1. How to use that REST Api through javascript

We should take a look: vendor/magento/module-checkout/view/frontend/web/js/action/set-payment-information.js

  • Thanks for your answer. I've updated my post according to your post. In javascript log, it prints that result.responseText is: {"message":"Request does not match any route.","trace":null}. Why it doesn't find the route? – Pro-Magento Feb 8 '17 at 18:45
  • @Pro-Magento updated my code with annotation ` @api` in the interface. – Khoa TruongDinh Feb 9 '17 at 0:39
  • Thank you my friend. Useful points you mentioned! I've up-voted your post, but seems the actual problem was those mentioned by Yaroslav. – Pro-Magento Feb 10 '17 at 16:36
  • @Pro-Magento yeah! Glad to see your module can work. – Khoa TruongDinh Feb 10 '17 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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