@@ -1638,269 +1638,6 @@ protected function _addRestRate(array $product, array $exchangeRates): self
16381638 return $ this ;
16391639 }
16401640
1641- /**
1642- * DHL REST API for Quote Data
1643- *
1644- * @return Result\ProxyDeferred
1645- * @throws LocalizedException
1646- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1647- */
1648- protected function _getQuotesRest ()
1649- {
1650- $ rawRequest = $ this ->_rawRequest ;
1651- $ url = $ this ->getGatewayURL ();
1652- $ packageWeightUnit = $ this ->_getRestPackageWeightUnit ();
1653-
1654- /** Dutiable */
1655- $ dutiable = ["isCustomsDeclarable " => false ];
1656- if ($ this ->isDutiable ($ rawRequest ->getOrigCountryId (), $ rawRequest ->getDestCountryId ())) {
1657- $ declaredValue = (int ) $ rawRequest ->getValue ();
1658- $ baseCurrencyCode = $ this ->_storeManager
1659- ->getWebsite ($ this ->_request ->getWebsiteId ())
1660- ->getBaseCurrencyCode ();
1661- $ dutiable = [
1662- "isCustomsDeclarable " => true ,
1663- "monetaryAmount " => [
1664- [
1665- "typeCode " => "declaredValue " ,
1666- "value " => $ declaredValue ,
1667- "currency " => $ baseCurrencyCode
1668- ]
1669- ]
1670- ];
1671- }
1672-
1673- $ rateParams = array_merge ([
1674- "customerDetails " => [
1675- "shipperDetails " => [
1676- "postalCode " => $ rawRequest ->getOrigPostal (),
1677- "cityName " => $ rawRequest ->getOrigCity (),
1678- "countryCode " => $ rawRequest ->getOrigCountryId ()
1679- ],
1680- "receiverDetails " => [
1681- "postalCode " => $ rawRequest ->getDestPostal (),
1682- "cityName " => $ rawRequest ->getDestCity (),
1683- "countryCode " => $ rawRequest ->getDestCountryId ()
1684- ]
1685- ],
1686- "accounts " => [
1687- [
1688- "typeCode " => "shipper " ,
1689- "number " => $ this ->getConfigData ('account ' )
1690- ]
1691- ],
1692- "plannedShippingDateAndTime " => date ('Y-m-d\TH:i:s\Z ' , strtotime ($ this ->_getShipDate ())),
1693- "unitOfMeasurement " => $ packageWeightUnit ,
1694- "getAdditionalInformation " => [
1695- [
1696- "typeCode " => "allValueAddedServices " ,
1697- "isRequested " => true
1698- ]
1699- ],
1700- "packages " => [
1701- [
1702- "typeCode " => "3BX " ,
1703- "weight " => (float ) $ this ->_getWeight ($ rawRequest ->getWeight ()),
1704- "dimensions " => [
1705- // If no value is provided for the dimension, a default size of 3 will be used
1706- "length " => $ this ->_getDimension (max (3 , $ this ->getConfigData ('depth ' ))),
1707- "width " => $ this ->_getDimension (max (3 , $ this ->getConfigData ('width ' ))),
1708- "height " => $ this ->_getDimension (max (3 , $ this ->getConfigData ('height ' )))
1709- ]
1710- ]
1711- ]
1712- ], $ dutiable );
1713-
1714- $ ratePayload = json_encode ($ rateParams , JSON_PRETTY_PRINT );
1715-
1716- $ httpResponse = $ this ->httpClient ->request (
1717- new Request ($ url . '/rates ' , Request::METHOD_POST , $ this ->getRestHeaders (), $ ratePayload )
1718- );
1719- $ debugData ['request ' ] = $ ratePayload ;
1720-
1721- return $ this ->proxyDeferredFactory ->create (
1722- [
1723- 'deferred ' => new CallbackDeferred (
1724- function () use ($ httpResponse , $ debugData ) {
1725- $ responseResult = null ;
1726- $ jsonResponse = '' ;
1727- try {
1728- $ responseResult = $ httpResponse ->get ();
1729- } catch (HttpException $ e ) {
1730- $ debugData ['result ' ] = ['error ' => $ e ->getMessage (), 'code ' => $ e ->getCode ()];
1731- $ this ->_logger ->critical ($ e );
1732- }
1733- if ($ responseResult ) {
1734- $ jsonResponse = $ responseResult ->getStatusCode () >= 400 ? '' : $ responseResult ->getBody ();
1735- }
1736- $ debugData ['result ' ] = $ jsonResponse ;
1737- $ this ->_debug ($ debugData );
1738- return $ this ->_parseRestResponse ($ jsonResponse );
1739- }
1740- )
1741- ]
1742- );
1743- }
1744-
1745- /**
1746- * DHL REST Auth Token
1747- *
1748- * @return string
1749- */
1750- private function getDhlAccessToken () : string
1751- {
1752- $ username = (string ) $ this ->getConfigData ('api_key ' );
1753- $ password = (string ) $ this ->getConfigData ('api_secret ' );
1754- $ access_token = base64_encode ($ username . ": " . $ password );
1755- return $ access_token ;
1756- }
1757-
1758- /**
1759- * Rest API Headers
1760- *
1761- * @return string[]
1762- */
1763- private function getRestHeaders (): array
1764- {
1765- return [
1766- "Authorization " => "Basic " . $ this ->getDhlAccessToken (),
1767- "Content-Type " => "application/json " ,
1768- "x-version " => self ::DHL_REST_API_VERSION
1769- ];
1770- }
1771-
1772- /**
1773- * Parse response from DHL REST API
1774- *
1775- * @param string $rateResponse
1776- * @return Result
1777- * @throws LocalizedException
1778- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
1779- */
1780- protected function _parseRestResponse ($ rateResponse ): Result
1781- {
1782- $ responseError = __ ('The response is in wrong format. ' );
1783- if ($ rateResponse !== null && strlen ($ rateResponse ) > 0 ) {
1784- $ rateResponseData = json_decode ($ rateResponse , true );
1785- if (isset ($ rateResponseData ['products ' ]) && isset ($ rateResponseData ['exchangeRates ' ])) {
1786- foreach ($ rateResponseData ['products ' ] as $ product ) {
1787- $ this ->_addRestRate ($ product , $ rateResponseData ['exchangeRates ' ]);
1788- }
1789- } else {
1790- $ this ->_errors [] = $ responseError ;
1791- }
1792- }
1793-
1794- /* @var $result Result */
1795- $ result = $ this ->_rateFactory ->create ();
1796- if ($ this ->_rates ) {
1797- foreach ($ this ->_rates as $ rate ) {
1798- $ method = $ rate ['service ' ];
1799- $ data = $ rate ['data ' ];
1800- /* @var $rate Method */
1801- $ rate = $ this ->_rateMethodFactory ->create ();
1802- $ rate ->setCarrier (self ::CODE );
1803- $ rate ->setCarrierTitle ($ this ->getConfigData ('title ' ));
1804- $ rate ->setMethod ($ method );
1805- $ rate ->setMethodTitle ($ data ['term ' ]);
1806- $ rate ->setCost ($ data ['price_total ' ]);
1807- $ rate ->setPrice ($ data ['price_total ' ]);
1808- $ result ->append ($ rate );
1809- }
1810- } else {
1811- if (!empty ($ this ->_errors )) {
1812- if ($ this ->_isShippingLabelFlag ) {
1813- throw new LocalizedException ($ responseError );
1814- }
1815- $ this ->debugErrors ($ this ->_errors );
1816- }
1817- $ result ->append ($ this ->getErrorMessage ());
1818- }
1819-
1820- return $ result ;
1821- }
1822-
1823- /**
1824- * DHL Quote Data calculating rates
1825- *
1826- * @param array $product
1827- * @param array $exchangeRates
1828- * @return $this
1829- * @throws LocalizedException
1830- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
1831- */
1832- protected function _addRestRate (array $ product , array $ exchangeRates ): self
1833- {
1834- if (isset ($ product ['productName ' ])
1835- && isset ($ product ['productCode ' ])
1836- && isset ($ exchangeRates [0 ]['currency ' ])
1837- && array_key_exists ((string )$ product ['productCode ' ], $ this ->getAllowedMethods ())
1838- ) {
1839- // DHL product code, e.g. '3', 'A', 'N', etc.
1840- $ dhlProduct = (string )$ product ['productCode ' ];
1841- $ totalPrice = $ product ['totalPrice ' ];
1842- $ billic_price = array_column (
1843- array_filter ($ totalPrice , fn ($ price ) => $ price ['currencyType ' ] === 'BILLC ' ),
1844- 'price '
1845- );
1846-
1847- $ totalEstimate = (float )(string ) $ billic_price [0 ] ?? null ;
1848- $ currencyCode = (string )$ exchangeRates [0 ]['currency ' ];
1849- $ baseCurrencyCode = $ this ->_storeManager ->getWebsite ($ this ->_request ->getWebsiteId ())
1850- ->getBaseCurrencyCode ();
1851- $ dhlProductDescription = $ this ->getDhlProductTitle ($ dhlProduct );
1852-
1853- if ($ currencyCode != $ baseCurrencyCode ) {
1854- /* @var $currency Currency */
1855- $ currency = $ this ->_currencyFactory ->create ();
1856- $ rates = $ currency ->getCurrencyRates ($ currencyCode , [$ baseCurrencyCode ]);
1857- if (!empty ($ rates ) && isset ($ rates [$ baseCurrencyCode ])) {
1858- // Convert to store display currency using store exchange rate
1859- $ totalEstimate = $ totalEstimate * $ rates [$ baseCurrencyCode ];
1860- } else {
1861- $ rates = $ currency ->getCurrencyRates ($ baseCurrencyCode , [$ currencyCode ]);
1862- if (!empty ($ rates ) && isset ($ rates [$ currencyCode ])) {
1863- $ totalEstimate = $ totalEstimate / $ rates [$ currencyCode ];
1864- }
1865- if (!isset ($ rates [$ currencyCode ]) || !$ totalEstimate ) {
1866- $ totalEstimate = false ;
1867- $ this ->_errors [] = __ (
1868- 'We had to skip DHL method %1 because we couldn \'t find exchange rate %2 (Base Currency). ' ,
1869- $ currencyCode ,
1870- $ baseCurrencyCode
1871- );
1872- }
1873- }
1874- }
1875- if ($ totalEstimate ) {
1876- $ data = [
1877- 'term ' => $ dhlProductDescription ,
1878- 'price_total ' => $ this ->getMethodPrice ($ totalEstimate , $ dhlProduct ),
1879- ];
1880- if (!empty ($ this ->_rates )) {
1881- foreach ($ this ->_rates as $ product ) {
1882- if ($ product ['data ' ]['term ' ] == $ data ['term ' ]
1883- && $ product ['data ' ]['price_total ' ] == $ data ['price_total ' ]
1884- ) {
1885- return $ this ;
1886- }
1887- }
1888- }
1889- $ this ->_rates [] = ['service ' => $ dhlProduct , 'data ' => $ data ];
1890- } else {
1891- $ this ->_errors [] = __ ("Zero shipping charge for '%1' " , $ dhlProductDescription );
1892- }
1893- } else {
1894- $ dhlProductDescription = false ;
1895- if (isset ($ product ['productCode ' ])) {
1896- $ dhlProductDescription = $ this ->getDhlProductTitle ((string )$ product ['productCode ' ]);
1897- }
1898- $ dhlProductDescription = $ dhlProductDescription ? $ dhlProductDescription : __ ("DHL " );
1899- $ this ->_errors [] = __ ("Zero shipping charge for '%1' " , $ dhlProductDescription );
1900- }
1901- return $ this ;
1902- }
1903-
19041641 /**
19051642 * Returns dimension unit (cm or inch)
19061643 *
0 commit comments