OpenMage / magento-lts

Official OpenMage LTS codebase | Migrate easily from Magento Community Edition in minutes! Download the source code for free or contribute to OpenMage LTS | Security vulnerability patches, bug fixes, performance improvements and more.
https://www.openmage.org
Open Software License 3.0
863 stars 438 forks source link

UPS REST _parseRestResponse doesn't return rates when only one service is available for a destination #4043

Closed iamniels closed 1 week ago

iamniels commented 2 weeks ago

After switching to UPS REST API, no rates were available for Cyprus. For Cyprus only one service is available on our UPS contract. In this case the response from UPS is:

"RatedShipment": {
      "Service": {
        "Code": "65",
        "Description": ""
      },
      "RatedShipmentAlert": {
        "Code": "110971",
        "Description": "..."
      },

Instead of:

"RatedShipment": [
      {
        "Service": {
          "Code": "07",
          "Description": ""
        },

So when there is only one service, an object is returned instead of an array.

UPS::_parseRestResponse doesn't handle this case properly.

Can be fixed like this:

    protected function _parseRestResponse(string $rateResponse)
    {
        $costArr = [];
        $priceArr = [];
        $errorTitle = '';
        if (strlen($rateResponse) > 0) {
            $rateResponseData = json_decode($rateResponse, true);
            if (@$rateResponseData['RateResponse']['Response']['ResponseStatus']['Description'] === 'Success') {
                $arr = $rateResponseData['RateResponse']['RatedShipment'] ?? [];
                if(isset($arr['Service'])){
                    $arr = [$arr];
                }