So here I have developed a new module which is Wishusucess Checkout REST API to communicate at the time of checkout. If you also looking to develop a mobile application using Magento 2 then this Module will help you to build your application for a mobile shopping cart using Wishusucess_Checkout keys that will help you to execute the API key successfully.
Endpoint:
POST <host>/rest/<store_code>/V1/carts/mine/estimate-shipping-methods
Headers:
Content-Type: application/json
Authorization: Bearer <customer token>
Payload:
So below is the syntax to get the shipping address.
{ "address": { "region": "New Delhi", "region_id": 43, "region_code": "NY", "country_id": "India", "street": [ "Noida Sector 62" ], "postcode": "201303", "city": "New Delhi", "firstname": "Hemant", "lastname": "Singh", "customer_id": 4, "email": "cs.hemantsingh@gmail.com", "telephone": "7992293862", "same_as_billing": 1 } }
Response:
Now you will get the response something like the below data.
[ { "carrier_code": "flatrate", "method_code": "flatrate", "carrier_title": "Flat Rate", "method_title": "Fixed", "amount": 15, "base_amount": 15, "available": true, "error_message": "", "price_excl_tax": 15, "price_incl_tax": 15 }, { "carrier_code": "tablerate", "method_code": "bestway", "carrier_title": "Best Way", "method_title": "Table Rate", "amount": 5, "base_amount": 5, "available": true, "error_message": "", "price_excl_tax": 5, "price_incl_tax": 5 } ]
Checkout REST API Key Magento 2 Extension
Now let's start a custom module to communicate your Magento store with some other technology like ReactJS or NodeJS store.
Using this module you will get a checkout API key that will help you to communicate on another side.
Checkout API Keys Module Basic File
app/code/wishusucess/Checkout/registration.php
app/code/wishusucess/Checkout/etc/module.xml
app/code/wishusucess/Checkout/etc/di.xml
app/code/wishusucess/Checkout/etc/webapi.xml
app/code/wishusucess/Checkout/Model/CompositeConfigProvider.php
app/code/wishusucess/Checkout/Model/ConfigProviderInterface.php
app/code/wishusucess/Checkout/Model/StoreConfig.php
app/code/wishusucess/Checkout/Payment/ConfigProvider.php
app/code/wishusucess/Checkout/Api/StoreConfigInterface.php
Step 1: Register Your Extension
app/code/wishusucess/Checkout/registration.php
<?php /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'wishusucess_Checkout', __DIR__ );
Step 2: Give Module Basic Information
app/code/wishusucess/Checkout/etc/module.xml
<?xml version="1.0"?> /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Wishusucess_Checkout" setup_version="1.0.0"> <sequence> <module name="Magento_Checkout" /> </sequence> </module> </config>
Define Dependency of REST API Keys
app/code/wishusucess/Checkout/etc/di.xml
<?xml version="1.0"?> /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Wishusucess\Checkout\Api\StoreConfigInterface" type="Wishusucess\Checkout\Model\StoreConfig"/> <type name="Wishusucess\Checkout\Model\CompositeConfigProvider"> <arguments> <argument name="configProviders" xsi:type="array"> <item name="payments_config_provider" xsi:type="object">Wishusucess\Checkout\Model\Payment\ConfigProvider</item> </argument> </arguments> </type> </config>
Step 4: Define Route ID in Your Web API File
So this is the only responsible file to generate your API keys URL address. In this file, you have to mention the route URL and the method like what kind of action you are going to perform on the particular route id. So here we are performing the POST method on the below route URL:
/V1/wishusucess/storeConfig/:cartId
app/code/wishusucess/Checkout/etc/webapi.xml
<?xml version="1.0"?> /** * Developer: Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> <route url="/V1/wishusucess/storeConfig/:cartId" method="GET"> <service class="Wishusucess\Checkout\Api\StoreConfigInterface" method="getStoreConfigsFromCart"/> <resources> <resource ref="anonymous" /> </resources> </route> </routes>
Step 5: Composite Config Provider
app/code/wishusucess/Checkout/Model/CompositeConfigProvider.php
<?php /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ declare(strict_types=1); namespace Wishusucess\Checkout\Model; use Magento\Store\Model\App\Emulation; class CompositeConfigProvider implements ConfigProviderInterface { /** * @var ConfigProviderInterface[] */ private $configProviders; /** * @var Emulation */ private $appEmulation; /** * @param Emulation $emulation * @param ConfigProviderInterface[] $configProviders */ public function __construct( Emulation $emulation, array $configProviders ) { $this->appEmulation = $emulation; $this->configProviders = $configProviders; } /** * {@inheritdoc} */ public function getConfig($cartId = null) { $config = []; foreach ($this->configProviders as $configProvider) { $config = array_merge_recursive($config, $configProvider->getConfig($cartId)); } return [$config]; } }
Create Model For Checkout API Config Provider Interface
app/code/wishusucess/Checkout/Model/ConfigProviderInterface.php
<?php /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ namespace Wishusucess\Checkout\Model; interface ConfigProviderInterface { /** * Retrieve assoc array of checkout configuration * @param int $cartId * @return string[] */ public function getConfig($cartId = null); }
Step 7: Model for Store Config
app/code/wishusucess/Checkout/Model/StoreConfig.php
<?php /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ declare(strict_types=1); namespace Wishusucess\Checkout\Model; use Wishusucess\Checkout\Api\StoreConfigInterface; class StoreConfig implements StoreConfigInterface { /** * @var CompositeConfigProviderFactory */ protected $configProviderFactory; /** * StoreConfig constructor. * @param CompositeConfigProviderFactory $compositeConfigProviderFactory */ public function __construct( CompositeConfigProviderFactory $compositeConfigProviderFactory ) { $this->configProviderFactory = $compositeConfigProviderFactory; } /** * @inheritDoc */ public function getStoreConfigsFromCart($cartId) { return $this->configProviderFactory->create()->getConfig($cartId); } }
Magento 2 REST API Keys For Payment
app/code/wishusucess/Checkout/Payment/ConfigProvider.php
<?php /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ declare(strict_types=1); namespace Wishusucess\Checkout\Model\Payment; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Payment\Model\CcConfig; use Magento\Quote\Api\PaymentMethodManagementInterface; use Magento\Quote\Model\QuoteRepository; use Wishusucess\Checkout\Model\ConfigProviderInterface; class ConfigProvider implements ConfigProviderInterface { /** * @var QuoteRepository */ private $quoteRepository; /** * @var PaymentMethodManagementInterface */ protected $paymentMethodManagement; /** * @var ScopeConfigInterface */ protected $scopeConfig; /** * @var CcConfig */ private $ccConfig; /** * ConfigProvider constructor. * @param QuoteRepository $quoteRepository * @param PaymentMethodManagementInterface $paymentMethodManagement * @param ScopeConfigInterface $scopeConfig * @param CcConfig $ccConfig */ public function __construct( QuoteRepository $quoteRepository, PaymentMethodManagementInterface $paymentMethodManagement, ScopeConfigInterface $scopeConfig, CcConfig $ccConfig ) { $this->quoteRepository = $quoteRepository; $this->paymentMethodManagement = $paymentMethodManagement; $this->scopeConfig = $scopeConfig; $this->ccConfig = $ccConfig; } /** * @inheritDoc */ public function getConfig($cartId = null) { if (null === $cartId) { return ['payment' => []]; } $paymentMethods = []; try { $quote = $this->quoteRepository->getActive($cartId); foreach ($this->paymentMethodManagement->getList($quote->getId()) as $paymentMethod) { $code = $paymentMethod->getCode(); $paymentMethods[$code] = [ 'code' => $code, 'title' => $paymentMethod->getTitle(), 'instructions' => $paymentMethod->getConfigData('instructions') ]; } } catch (NoSuchEntityException $noSuchEntityException) { return ['payment' => []]; } return ['payment' => $paymentMethods]; } }
Magento 2 Create REST API Keys For Checkout
Now here we have to define one function getStoreConfigsFromCart($cartId) so this method will have the parameter of cart id so when we give the cart id in the REST API URL keys then that particular cart id values will show with the all details. And this method will return the value as a string which we have to define also otherwise it will through some errors.
app/code/wishusucess/Checkout/Api/StoreConfigInterface.php
<?php /** * Developer:Wishusucess Magento 2x Developer Team * Module: Wishusucess_Checkout * Author: Hemant Singh Magento 2X Developer * Website: http://www.wishusucess.com/ * */ namespace Wishusucess\Checkout\Api; /** * Interface StoreConfigInterface * @api */ interface StoreConfigInterface { /** * @param int $cartId * @return string[] */ public function getStoreConfigsFromCart($cartId); }
Hire Magento 2 Expert Developer to Develop Your Store
Related Post:
Custom Shipping Text Filed: Show Custom Text Magento 2
Search AutoComplete: Magento 2 Module Add All Category for Search
Share Product on WhatsApp: With Products Image And URL in Magento 2
Recommended Post:
Magento 2.4 Installation Guide: How to Install Magento 2.4.2
Magento Store: Best 36 Magento Websites Example in The World
SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing