| 1 |
<?php |
| 2 |
|
| 3 |
class Mbe_Shipping_Helper_Ups_Uap { |
| 4 |
|
| 5 |
const MBE_UPS_ACCESS_LICENSE_NUMBER = "6CB804D87F868625"; |
| 6 |
const MBE_UPS_USER_ID = "ITMBE0001Z"; |
| 7 |
const MBE_UPS_PASSWORD = "1tMB3#ooo1"; |
| 8 |
const MBE_UPS_URI_TEST = "https://wwwcie.ups.com/ups.app/xml/Locator"; |
| 9 |
const MBE_UPS_URI_PROD = "https://onlinetools.ups.com/ups.app/xml/Locator"; |
| 10 |
const MBE_UPS_UAP_MAXIMUM_LIST_ITEM_10 = "10"; |
| 11 |
const MBE_UPS_UAP_SEARCH_RADIUS_20 = "20"; |
| 12 |
const MBE_UPS_UAP_UNIT_OF_MEASUREMENT_KM ="KM"; |
| 13 |
const MBE_UPS_OPTION_DROP_LOCATIONS_AND_WILL_CALL_LOCATIONS = 1; |
| 14 |
const MBE_UPS_OPTION_ALL_AVAILABLE_ADDITIONAL_SERVICES = 8; |
| 15 |
const MBE_UPS_OPTION_ALL_AVAILABLE_PROGRAM_TYPES = 16; |
| 16 |
const MBE_UPS_OPTION_ALL_AVAILABLE_ADDITIONAL_SERVICES_AND_PROGRAM_TYPES = 24; |
| 17 |
const MBE_UPS_OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS = 32; |
| 18 |
const MBE_UPS_OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_ADDITIONAL_SERVICES = 40; |
| 19 |
const MBE_UPS_OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_PROGRAM_TYPES = 48; |
| 20 |
const MBE_UPS_OPTION_ALL_AVAILABLE_RETAIL_LOCATIONS_AND_ADDITIONAL_SERVICES_AND_PROGRAM_TYPES = 56; |
| 21 |
const MBE_UPS_OPTION_UPS_ACCESS_POINT_LOCATIONS = 64; |
| 22 |
|
| 23 |
/** |
| 24 |
* @throws Exception |
| 25 |
*/ |
| 26 |
public static function getUapList($filter, $simplyfied = true, $userId = null, $password = null, $accessLicenseNumber = null, $test = false) |
| 27 |
{ |
| 28 |
$accessLicenseNumber = $accessLicenseNumber?:self::MBE_UPS_ACCESS_LICENSE_NUMBER; |
| 29 |
$userId = $userId?:self::MBE_UPS_USER_ID; |
| 30 |
$password = $password?:self::MBE_UPS_PASSWORD; |
| 31 |
|
| 32 |
$endpointurl = self::MBE_UPS_URI_PROD; |
| 33 |
if ($test) { |
| 34 |
$endpointurl = self::MBE_UPS_URI_TEST; |
| 35 |
} |
| 36 |
|
| 37 |
// try { |
| 38 |
$accessRequestXML = new SimpleXMLElement("<AccessRequest></AccessRequest>"); |
| 39 |
$locatorRequestXML = new SimpleXMLElement("<LocatorRequest ></LocatorRequest >"); |
| 40 |
|
| 41 |
$accessRequestXML->addChild("AccessLicenseNumber", $accessLicenseNumber); |
| 42 |
$accessRequestXML->addChild("UserId", $userId); |
| 43 |
$accessRequestXML->addChild("Password", $password); |
| 44 |
|
| 45 |
$request = $locatorRequestXML->addChild('Request'); |
| 46 |
$request->addChild("RequestAction", "Locator"); |
| 47 |
$request->addChild("RequestOption", $filter["RequestOption"] ?? self::MBE_UPS_OPTION_DROP_LOCATIONS_AND_WILL_CALL_LOCATIONS ); |
| 48 |
|
| 49 |
$translate = $locatorRequestXML->addChild('Translate'); |
| 50 |
$translate->addChild("LanguageCode", $filter["language"]??'EN'); |
| 51 |
|
| 52 |
// if(!empty($filter["LocationID"])) { |
| 53 |
// $locatorRequestXML->addChild ( "LocationID", $filter["LocationID"]); |
| 54 |
// } else { |
| 55 |
$originAddress = $locatorRequestXML->addChild( 'OriginAddress' ); |
| 56 |
$addressKeyFormat = $originAddress->addChild( 'AddressKeyFormat' ); |
| 57 |
$addressKeyFormat->addChild( "AddressLine", $filter["AddressLine1"]?:'' ); |
| 58 |
$addressKeyFormat->addChild( "PostcodePrimaryLow", $filter["PostcodePrimaryLow"]?:''); |
| 59 |
$addressKeyFormat->addChild( "PoliticalDivision2", $filter["PoliticalDivision2"]?:'' ); |
| 60 |
$addressKeyFormat->addChild( "PoliticalDivision1", $filter["PoliticalDivision1"]?:'' ); |
| 61 |
$addressKeyFormat->addChild( "CountryCode", $filter["CountryCode"]?:'' ); |
| 62 |
|
| 63 |
|
| 64 |
$unitOfMeasurement = $locatorRequestXML->addChild( 'UnitOfMeasurement' ); |
| 65 |
$unitOfMeasurement->addChild( "Code", isset($filter["UnitOfMeasurement"])?$filter["UnitOfMeasurement"]: self::MBE_UPS_UAP_UNIT_OF_MEASUREMENT_KM ); |
| 66 |
|
| 67 |
$LocationSearchCriteria = $locatorRequestXML->addChild( 'LocationSearchCriteria' ); |
| 68 |
$LocationSearchCriteria->addChild( "MaximumListSize", $filter["MaximumListSize"] ?: self::MBE_UPS_UAP_MAXIMUM_LIST_ITEM_10 ); |
| 69 |
$LocationSearchCriteria->addChild( "SearchRadius", $filter["SearchRadius"] ?: self::MBE_UPS_UAP_SEARCH_RADIUS_20 ); |
| 70 |
|
| 71 |
$SortCriteria = $locatorRequestXML->addChild( 'SortCriteria' ); |
| 72 |
$SortCriteria->addChild( 'SortType', "01" ); |
| 73 |
// } |
| 74 |
|
| 75 |
$requestXML = $accessRequestXML->asXML() . $locatorRequestXML->asXML(); |
| 76 |
|
| 77 |
$form = array( |
| 78 |
'http' => array( |
| 79 |
'method' => 'POST', |
| 80 |
'header' => 'Content-type: application/x-www-form-urlencoded', |
| 81 |
'content' => "$requestXML" |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
$request = stream_context_create($form); |
| 86 |
$browser = fopen($endpointurl, 'rb', false, $request); |
| 87 |
if (!$browser) { |
| 88 |
throw new Exception("Connection failed."); |
| 89 |
} |
| 90 |
|
| 91 |
// get response |
| 92 |
$response = stream_get_contents($browser); |
| 93 |
fclose($browser); |
| 94 |
|
| 95 |
$upsUapList = []; |
| 96 |
|
| 97 |
if ( ! $response ) { |
| 98 |
throw new Exception("getUapList: Response Error."); |
| 99 |
} else { |
| 100 |
$xmlResponse = simplexml_load_string($response); |
| 101 |
unset($response); |
| 102 |
unset($xmlResponse->Response); |
| 103 |
$xmlResponse = json_decode(json_encode($xmlResponse), true); |
| 104 |
if ($simplyfied && !empty($xmlResponse)) { |
| 105 |
$dropLocation = $xmlResponse['SearchResults']['DropLocation']; |
| 106 |
if(isset($dropLocation['LocationID']) && isset($dropLocation['AccessPointInformation'])) { |
| 107 |
$upsUapList[] = self::getUapItem($dropLocation); |
| 108 |
} else { |
| 109 |
foreach ( $dropLocation as $item ) { |
| 110 |
if(isset($item['AccessPointInformation'])) { |
| 111 |
$upsUapList[] = self::getUapItem($item); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
return $upsUapList; |
| 116 |
} |
| 117 |
return $xmlResponse; |
| 118 |
} |
| 119 |
// } catch (Exception $ex) { |
| 120 |
// return $ex; |
| 121 |
// } |
| 122 |
} |
| 123 |
|
| 124 |
private static function getUapItem( $item ) |
| 125 |
{ |
| 126 |
return $item['AddressKeyFormat'] + [ 'LocationID' => $item['LocationID'] ] + [ 'PublicAccesPointID' => $item['AccessPointInformation']['PublicAccessPointID'] ] + [ 'Distance' => $item['Distance']['Value'] . ' ' . $item['Distance']['UnitOfMeasurement']['Code'] ] + [ 'StandardHoursOfOperation' => $item['StandardHoursOfOperation'] ]; |
| 127 |
} |
| 128 |
|
| 129 |
} |