cancel
Showing results for 
Search instead for 
Did you mean: 

Call external API on billing step of Checkout

Call external API on billing step of Checkout

I need to make an external API call during checkout, after the shipping step. This call will use data from the cart. When the API is called, the cart data is passed to my app, returned, and displayed in the order summary in the Billing-Step. I'm having a problem however, in that I reference my .js component file in checkout_index_index.xml, inside the sidebar element. This is outside the Steps portion of the xml file. In the .js file, I grab the data I need from the window.checkoutConfig interface. Since the js file is referenced inside the sidebar element and not inside the Billing-Step, the php file where my api call is made is fired at the beginning of the checkout process. I need the order total including shipping in my API call. When the call is made at the beginning of the checkout process, the shipping cost is not included in the order total from the cart. How can I make this call with the full order total including shipping, to display on the Billing-Step of checkout? I'm including my php and xml below.

 

<?php


namespace Vendor\Module\Model;

use Magento\Checkout\Model\ConfigProviderInterface;


class CustomConfigProvider implements ConfigProviderInterface
{       

    public function __construct(        \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {        $this->curlFactory = $curlFactory;        $this->jsonHelper = $jsonHelper;
    }



    public function getConfig()
    {        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();        $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
        $grandTotal = $cart->getQuote()->getGrandTotal();        $url = 'http://localhost:4000/calculate-price'; //pass dynamic url        $requstbody = [
            'order_total'=>$grandTotal,
            'order_currency'=>'ETH',
            'currency'=>'USD'
        ];

    /* Create curl factory */    $httpAdapter = $this->curlFactory->create();
    /* Forth parameter is POST body */    $httpAdapter->write(\Zend_Http_Client::POST, $url, '1.1', ["Content-Type:application/json","Authorization: Bearer EQrmnth2nCLjxrMX10iFs2DAjNmKJan5J5WBqd2gS+wJK5pypZ12QLbzPF0b2pOM"],json_encode($requstbody));    $result = $httpAdapter->read();    $body = \Zend_Http_Response::extractBody($result);
    /* convert JSON to Array */    $response = $this->jsonHelper->jsonDecode($body);    $config = [];    $config['customData'] = $response;
    return $config;

    }
}

 checkout_index_index.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="checkout.root">
        <arguments>
            <argument name="jsLayout" xsi:type="array">
                <item name="components" xsi:type="array">
                    <item name="checkout" xsi:type="array">
                        <item name="children" xsi:type="array">
                            <item name="steps" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="billing-step" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="payment" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="renders" xsi:type="array">
                                                        <!-- merge payment method renders here -->
                                                        <item name="children" xsi:type="array">
                                                            <item name="newpayment-payments" xsi:type="array">
                                                                <item name="component" xsi:type="string">Vendor/Module/js/view/payment/newpayment</item>
                                                                <item name="methods" xsi:type="array">
                                                                    <item name="newpayment" xsi:type="array">
                                                                        <!-- <item name="isBillingAddressRequired" xsi:type="boolean">true</item> -->
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                            <item name="sidebar" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="summary" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="totals" xsi:type="array">
                                                <item name="component" xsi:type="string">Magento_Checkout/js/view/summary/totals</item>
                                                <item name="displayArea" xsi:type="string">totals</item>
                                                <item name="config" xsi:type="array">
                                                    <item name="template" xsi:type="string">Magento_Checkout/summary/totals</item>
                                                </item>
                                                <item name="children" xsi:type="array">
                                                    <!-- sort order for this totals is configured on admin panel-->
                                                    <!-- Stores->Configuration->SALES->Sales->General->Checkout Totals Sort Order -->
                                                    <item name="grand-total" xsi:type="array">
                                                        <item name="component" xsi:type="string">Vendor/Module/js/view/summary/grand-total</item>
                                                        <item name="config" xsi:type="array">
                                                            <item name="title" xsi:type="string" translate="true">Order Total</item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</body>
</page>