From 4671b6ffdc9a7755a29459dea1f665fa4a5b99b8 Mon Sep 17 00:00:00 2001 From: Gundo Sifhufhi <sifhufhisg@gmail.com> Date: Tue, 3 Jan 2023 22:07:34 +0200 Subject: [PATCH] URL CONSTANTS (TRACKING & RATES_ENDPOINT) Refactoring (Removed redundant fields from system.xml --- .idea/csv-editor.xml | 7 +++ uafrica/Customshipping/Helper/Data.php | 21 +++++---- .../Model/Carrier/Customshipping.php | 45 ++++--------------- .../Customshipping/etc/adminhtml/system.xml | 14 ++++++ 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/.idea/csv-editor.xml b/.idea/csv-editor.xml index 24b6446..aa50a8a 100644 --- a/.idea/csv-editor.xml +++ b/.idea/csv-editor.xml @@ -10,6 +10,13 @@ </Attribute> </value> </entry> + <entry key="/src/app/code/uafrica/Customshipping/i18n/en_US.csv"> + <value> + <Attribute> + <option name="separator" value="," /> + </Attribute> + </value> + </entry> </map> </option> </component> diff --git a/uafrica/Customshipping/Helper/Data.php b/uafrica/Customshipping/Helper/Data.php index 60754bd..edaa3a3 100644 --- a/uafrica/Customshipping/Helper/Data.php +++ b/uafrica/Customshipping/Helper/Data.php @@ -1,13 +1,18 @@ <?php namespace uafrica\Customshipping\Helper; +use Magento\Framework\App\Helper\AbstractHelper; +use Magento\Framework\App\Helper\Context; +use Magento\Framework\Module\ModuleListInterface; +use Magento\Store\Model\ScopeInterface; + /** * @category uafrica * @package uafrica_customshipping * @author uafrica * @website https://www.bob.co.za */ -class Data extends \Magento\Framework\App\Helper\AbstractHelper +class Data extends AbstractHelper { const XML_PATH_ENABLED = 'uafrica_customshipping/general/enabled'; @@ -19,17 +24,17 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper protected $_logger; /** - * @var \Magento\Framework\Module\ModuleListInterface + * @var ModuleListInterface */ protected $_moduleList; /** - * @param \Magento\Framework\App\Helper\Context $context - * @param \Magento\Framework\Module\ModuleListInterface $moduleList + * @param Context $context + * @param ModuleListInterface $moduleList */ public function __construct( - \Magento\Framework\App\Helper\Context $context, - \Magento\Framework\Module\ModuleListInterface $moduleList + Context $context, + ModuleListInterface $moduleList ) { $this->_logger = $context->getLogger(); $this->_moduleList = $moduleList; @@ -46,7 +51,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper { return $this->scopeConfig->getValue( self::XML_PATH_ENABLED, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ScopeInterface::SCOPE_STORE ); } @@ -54,7 +59,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper { return $this->scopeConfig->getValue( self::XML_PATH_DEBUG, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ScopeInterface::SCOPE_STORE ); } diff --git a/uafrica/Customshipping/Model/Carrier/Customshipping.php b/uafrica/Customshipping/Model/Carrier/Customshipping.php index 5349387..ce5e20f 100644 --- a/uafrica/Customshipping/Model/Carrier/Customshipping.php +++ b/uafrica/Customshipping/Model/Carrier/Customshipping.php @@ -23,6 +23,11 @@ use Magento\Framework\HTTP\Client\CurlFactory; */ class Customshipping extends AbstractCarrier implements CarrierInterface { + /** Tracking Endpoint */ + const TRACKING = 'https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference='; + /*** RATES API Endpoint*/ + const RATES_ENDPOINT = 'https://8390956f-c00b-497d-8742-87b1d6305bd2.mock.pstmn.io/putrates'; + /** * Carrier's code * @@ -47,9 +52,6 @@ class Customshipping extends AbstractCarrier implements CarrierInterface */ protected $_rateMethodFactory; - - - //TODO: REFACTOR THIS /** * @var \Magento\Framework\Controller\Result\JsonFactory */ @@ -281,14 +283,14 @@ class Customshipping extends AbstractCarrier implements CarrierInterface // $result = $this->_trackFactory->create(); $result = new DataObject(); //Insert tracking code dynamically here - $result->setUrl('https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference='.$tracking); + $result->setUrl(self::TRACKING.$tracking); $result->setTracking($tracking); $result->setCarrierTitle($this->getConfigData('title')); //Perform curl request to get tracking info from uAfrica API //Inject tracking code into the url dynamically - $this->curl->get('https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference='.$tracking); + $this->curl->get(self::TRACKING.$tracking); $response = $this->curl->getBody(); @@ -374,13 +376,8 @@ class Customshipping extends AbstractCarrier implements CarrierInterface */ private function getApiUrl(): string { - // return 'https://api.dev.ship.uafrica.com'; - //Used for testing purposes only on localhost, since there is not endpoint to get magento rates to work with - //Basically, I am using the structure of the response from the uAfrica API to test the functionality of the module - // return $this->getConfigData('rates_endpoint'); - - return 'https://8390956f-c00b-497d-8742-87b1d6305bd2.mock.pstmn.io/putrates'; + return self::RATES_ENDPOINT; } /** @@ -436,30 +433,6 @@ class Customshipping extends AbstractCarrier implements CarrierInterface //} //] //} - //Mock URL FOR NOW, will be replaced with the actual API URL - //Like; $this->getConfigData('api_url') - //Fill in the payload with the data from the request - - - //TODO: Get the rates from the API and append them to the result object - //JSON data fed to post man mock server as a response to the request for rates - //{ - //"rates": [ - //{ - //"id": "113810", - //"description": "Standard Economy Shipping Rate", - //"service_name": "Courier Door to Door Delivery - Economy", - //"service_code": "113810_23", - //"total_price": 7500, - //"currency": "ZAR", - //"min_delivery_date": "2022-12-29", - //"max_delivery_date": "2023-01-03" - //} - //] - //} - //Mock URL FOR NOW, will be replaced with the actual API URL - //Like; $this->getConfigData('api_url') - //GET ALL DESTINATION DATA FROM THE REQUEST $destination = $request->getDestPostcode(); $destCountry = $request->getDestCountryId(); $destRegion = $request->getDestRegionCode(); @@ -566,8 +539,6 @@ class Customshipping extends AbstractCarrier implements CarrierInterface 'method' => 'uafrica', ]; - //TODO: Get the allowed methods from the API and return them - //Get shipping methods dynamically from the API // $arr = []; // $rates = $this->getRates(); diff --git a/uafrica/Customshipping/etc/adminhtml/system.xml b/uafrica/Customshipping/etc/adminhtml/system.xml index b27a096..034de7f 100644 --- a/uafrica/Customshipping/etc/adminhtml/system.xml +++ b/uafrica/Customshipping/etc/adminhtml/system.xml @@ -58,6 +58,20 @@ <label>Residential Delivery</label> <source_model>Magento\Config\Model\Config\Source\Yesno</source_model> </field> +<!-- <field id="production_webservices_url" translate="label" type="text" sortOrder="90" showInDefault="0" showInWebsite="1" canRestore="1">--> +<!-- <label>Get Rates(uAfrica)</label>--> +<!-- <depends>--> +<!-- <field id="sandbox_mode">0</field>--> +<!-- </depends>--> +<!--<!–- Not Functional at this stage–>--> +<!-- <comment>URL To Get Shipping Rates</comment>--> +<!-- </field>--> + +<!-- <field id="price" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0">--> +<!-- <label>Shipping Cost</label>--> +<!-- <validate>validate-number validate-zero-or-greater</validate>--> +<!-- </field>--> + <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Displayed Error Message</label> </field> -- GitLab