From 0917189d419158b2011a5ed73ba03fa2b1699e34 Mon Sep 17 00:00:00 2001 From: ragod123 <35573000+ragod123@users.noreply.github.com> Date: Mon, 6 May 2024 17:49:07 +0530 Subject: [PATCH 1/5] Update Auth.php --- src/Auth.php | 89 ++++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 62 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index 7d5c93f..c5318e2 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -2,44 +2,36 @@ namespace RahulGodiyal\PhpUpsApiWrapper; +use RahulGodiyal\PhpUpsApiWrapper\Utils\HttpClient; + class Auth { - private $_mode; - private $_dev_api_base_url; - private $_prod_api_base_url; + private const DEV_API_BASE_URL = 'https://wwwcie.ups.com'; + private const PROD_API_BASE_URL = 'https://onlinetools.ups.com'; + + private string $mode = 'DEV'; - public function __construct() + public function setMode(string $mode): self { - $this->_mode = 'DEV'; - $this->_dev_api_base_url = 'https://wwwcie.ups.com'; - $this->_prod_api_base_url = 'https://onlinetools.ups.com'; + if ($mode === 'PROD') { + $this->mode = $mode; + } + + return $this; } - /** - * Authenticate User - * @param string $client_id - * @param string $client_secret - * @return string $access_token - */ - public function authenticate(String $client_id, String $client_secret) + public function authenticate(string $client_id, string $client_secret): array { - $curl = curl_init(); - - curl_setopt_array($curl, [ - CURLOPT_HTTPHEADER => [ - "Content-Type: application/x-www-form-urlencoded", - "x-merchant-id: string", - "Authorization: Basic " . base64_encode($client_id . ":" . $client_secret) - ], - CURLOPT_POSTFIELDS => $this->_getPayload(), - CURLOPT_URL => $this->_getAPIBaseURL() . "/security/v1/oauth/token", - CURLOPT_RETURNTRANSFER => true, - CURLOPT_CUSTOMREQUEST => "POST", + $httpClient = new HttpClient(); + $httpClient->setHeader([ + "Content-Type: application/x-www-form-urlencoded", + "x-merchant-id: string", + "Authorization: Basic " . base64_encode($client_id . ":" . $client_secret) ]); - - $response = curl_exec($curl); - curl_close($curl); - $res = json_decode($response); + $httpClient->setPayload("grant_type=client_credentials"); + $httpClient->setUrl($this->_getAPIBaseURL() . "/security/v1/oauth/token"); + $httpClient->setMethod("POST"); + $res = $httpClient->fetch(); if (!isset($res->access_token)) { if (isset($res->response)) { @@ -51,43 +43,16 @@ public function authenticate(String $client_id, String $client_secret) } $access_token = $res->access_token; - return ['status' => 'success', 'access_token' => $access_token]; - } - /** - * Get Payload - * @return string - */ - private function _getPayload() - { - return "grant_type=client_credentials"; - } - - /** - * Set Mode - * @param string DEV|PROD - */ - public function setMode(String $mode) - { - if ($mode === 'PROD') { - $this->_mode = $mode; - return $mode; - } - - $this->_mode = 'DEV'; - return $mode; + return ['status' => 'success', 'access_token' => $access_token]; } - /** - * Get Api Base URL - * @return string url - */ - protected function _getAPIBaseURL() + protected function _getAPIBaseURL(): string { - if ($this->_mode === 'PROD') { - return $this->_prod_api_base_url; + if ($this->mode === 'PROD') { + return self::PROD_API_BASE_URL; } - return $this->_dev_api_base_url; + return self::DEV_API_BASE_URL; } } From ddec19918b2a5c7dbb40dce8beabdce02153c6fd Mon Sep 17 00:00:00 2001 From: ragod123 <35573000+ragod123@users.noreply.github.com> Date: Mon, 6 May 2024 17:49:48 +0530 Subject: [PATCH 2/5] Update AddressValidation.php --- src/AddressValidation.php | 88 ++++++++++++--------------------------- 1 file changed, 26 insertions(+), 62 deletions(-) diff --git a/src/AddressValidation.php b/src/AddressValidation.php index 16288f6..8711599 100644 --- a/src/AddressValidation.php +++ b/src/AddressValidation.php @@ -3,43 +3,26 @@ namespace RahulGodiyal\PhpUpsApiWrapper; use RahulGodiyal\PhpUpsApiWrapper\Auth; +use RahulGodiyal\PhpUpsApiWrapper\Utils\HttpClient; class AddressValidation extends Auth { - private static $_address; - private $_request_option; - private $_version; - private $_query; + private const REQUEST_OPTION = "3"; + private const VERSION = "v2"; - public function __construct() - { - parent::__construct(); - $this->_request_option = "3"; - $this->_version = "v2"; - $this->_query = [ - "regionalrequestindicator" => "string", - "maximumcandidatelistsize" => "1" - ]; - } + private static array $address; + private array $query = [ + "regionalrequestindicator" => "string", + "maximumcandidatelistsize" => "1" + ]; - /** - * Set Address to validate - * @param array $address - * @return self - */ - public static function setAddress(array $address) + public static function setAddress(array $address): self { - self::$_address = $address; + self::$address = $address; return new self; } - /** - * Validate the address - * @param string $client_id - * @param string $client_secret - * @return array of validated address - */ - public function validate(String $client_id, String $client_secret) + public function validate(string $client_id, string $client_secret): array { $auth = $this->authenticate($client_id, $client_secret); @@ -48,23 +31,17 @@ public function validate(String $client_id, String $client_secret) } $access_token = $auth['access_token']; - $curl = curl_init(); - - curl_setopt_array($curl, [ - CURLOPT_HTTPHEADER => [ - "Authorization: Bearer $access_token", - "Content-Type: application/json" - ], - CURLOPT_POSTFIELDS => json_encode($this->_payload()), - CURLOPT_URL => $this->_getAPIBaseURL() . "/api/addressvalidation/" . $this->_version . "/" . $this->_request_option . "?" . http_build_query($this->_query), - CURLOPT_RETURNTRANSFER => true, - CURLOPT_CUSTOMREQUEST => "POST", + + $httpClient = new HttpClient(); + $httpClient->setHeader([ + "Authorization: Bearer $access_token", + "Content-Type: application/json" ]); - - $response = curl_exec($curl); - curl_close($curl); - $res = json_decode($response); - + $httpClient->setPayload(json_encode($this->getPayLoad())); + $httpClient->setUrl($this->_getAPIBaseURL() . "/api/addressvalidation/" . self::VERSION . "/" . self::REQUEST_OPTION . "?" . http_build_query($this->query)); + $httpClient->setMethod("POST"); + $res = $httpClient->fetch(); + if (!isset($res->XAVResponse)) { if (isset($res->response)) { $error = $res->response->errors[0]->message; @@ -78,29 +55,20 @@ public function validate(String $client_id, String $client_secret) return ['status' => 'fail', 'msg' => "Invalid Address."]; } - $addresses = $this->_getAddresses($res->XAVResponse->Candidate); + $addresses = $this->getAddresses($res->XAVResponse->Candidate); return ['status' => 'success', 'addresses' => $addresses]; } - /** - * Get Payload - * @return array $payload - */ - private function _payload() + private function getPayLoad(): array { return [ "XAVRequest" => [ - "AddressKeyFormat" => self::$_address + "AddressKeyFormat" => self::$address ] ]; } - /** - * Get Addresses - * @param array of objects - * @return array of addresses - */ - private function _getAddresses(Array $candidates) + private function getAddresses(array $candidates): array { $addresses = []; foreach ($candidates as $candObj) { @@ -119,11 +87,7 @@ private function _getAddresses(Array $candidates) return $addresses; } - /** - * Set Mode - * @param string DEV|PROD - */ - public function setMode(String $mode) + public function setMode(string $mode): self { parent::setMode($mode); return $this; From 9db5b30b0616f3585134d781a55a33ce647058a6 Mon Sep 17 00:00:00 2001 From: ragod123 <35573000+ragod123@users.noreply.github.com> Date: Mon, 6 May 2024 17:51:30 +0530 Subject: [PATCH 3/5] Add shipping label functionality --- src/Entity/Address.php | 96 +++++++++++++++++++++ src/Entity/BillShipper.php | 35 ++++++++ src/Entity/Dimensions.php | 78 +++++++++++++++++ src/Entity/LabelImageFormat.php | 53 ++++++++++++ src/Entity/LabelSpecification.php | 57 +++++++++++++ src/Entity/LabelStockSize.php | 51 +++++++++++ src/Entity/Package.php | 76 +++++++++++++++++ src/Entity/PackageWeight.php | 39 +++++++++ src/Entity/Packaging.php | 72 ++++++++++++++++ src/Entity/PaymentInformation.php | 36 ++++++++ src/Entity/Phone.php | 44 ++++++++++ src/Entity/Query.php | 30 +++++++ src/Entity/Request.php | 42 +++++++++ src/Entity/ReturnService.php | 76 +++++++++++++++++ src/Entity/Service.php | 81 ++++++++++++++++++ src/Entity/ShipFrom.php | 89 +++++++++++++++++++ src/Entity/ShipTo.php | 89 +++++++++++++++++++ src/Entity/Shipment.php | 137 ++++++++++++++++++++++++++++++ src/Entity/ShipmentCharge.php | 44 ++++++++++ src/Entity/ShipmentRequest.php | 57 +++++++++++++ src/Entity/Shipper.php | 118 +++++++++++++++++++++++++ src/Entity/UnitOfMeasurement.php | 56 ++++++++++++ src/Ship.php | 117 +++++++++++++++++++++++++ src/Utils/HttpClient.php | 57 +++++++++++++ 24 files changed, 1630 insertions(+) create mode 100644 src/Entity/Address.php create mode 100644 src/Entity/BillShipper.php create mode 100644 src/Entity/Dimensions.php create mode 100644 src/Entity/LabelImageFormat.php create mode 100644 src/Entity/LabelSpecification.php create mode 100644 src/Entity/LabelStockSize.php create mode 100644 src/Entity/Package.php create mode 100644 src/Entity/PackageWeight.php create mode 100644 src/Entity/Packaging.php create mode 100644 src/Entity/PaymentInformation.php create mode 100644 src/Entity/Phone.php create mode 100644 src/Entity/Query.php create mode 100644 src/Entity/Request.php create mode 100644 src/Entity/ReturnService.php create mode 100644 src/Entity/Service.php create mode 100644 src/Entity/ShipFrom.php create mode 100644 src/Entity/ShipTo.php create mode 100644 src/Entity/Shipment.php create mode 100644 src/Entity/ShipmentCharge.php create mode 100644 src/Entity/ShipmentRequest.php create mode 100644 src/Entity/Shipper.php create mode 100644 src/Entity/UnitOfMeasurement.php create mode 100644 src/Ship.php create mode 100644 src/Utils/HttpClient.php diff --git a/src/Entity/Address.php b/src/Entity/Address.php new file mode 100644 index 0000000..012d386 --- /dev/null +++ b/src/Entity/Address.php @@ -0,0 +1,96 @@ +addressLine1 = $addressLine1; + return $this; + } + + public function getAddressLine1(): string | null + { + return $this->addressLine1; + } + + public function setAddressLine2(string $addressLine2): self + { + $this->addressLine2 = $addressLine2; + return $this; + } + + public function getAddressLine2(): string | null + { + return $this->addressLine2; + } + + public function setCity(string $city): self + { + $this->city = $city; + return $this; + } + + public function getCity(): string | null + { + return $this->city; + } + + public function setStateProvinceCode(string $state): self + { + $this->stateProvinceCode = $state; + return $this; + } + + public function getStateProvinceCode(): string | null + { + return $this->stateProvinceCode; + } + + public function setPostalCode(string $postal_code): self + { + $this->postalCode = $postal_code; + return $this; + } + + public function getPostalCode(): string | null + { + return $this->postalCode; + } + + public function setCountryCode(string $country_code): self + { + $this->countryCode = $country_code; + return $this; + } + + public function getCountryCode(): string | null + { + return $this->countryCode; + } + + public function toArray(): array + { + $address_lines = [$this->addressLine1]; + + if ($this->addressLine2) { + array_push($address_lines, $this->addressLine2); + } + + return [ + "AddressLine" => $address_lines, + "City" => $this->city, + "StateProvinceCode" => $this->stateProvinceCode, + "PostalCode" => $this->postalCode, + "CountryCode" => $this->countryCode + ]; + } +} diff --git a/src/Entity/BillShipper.php b/src/Entity/BillShipper.php new file mode 100644 index 0000000..d7db872 --- /dev/null +++ b/src/Entity/BillShipper.php @@ -0,0 +1,35 @@ +accountNumber)) { + return true; + } + + return false; + } + + public function setAccountNumber(string $accountNumber): self + { + $this->accountNumber = $accountNumber; + return $this; + } + + public function getAccountNumber(): string + { + return $this->accountNumber; + } + + public function toArray(): array + { + return [ + "AccountNumber" => $this->accountNumber + ]; + } +} \ No newline at end of file diff --git a/src/Entity/Dimensions.php b/src/Entity/Dimensions.php new file mode 100644 index 0000000..7dfed16 --- /dev/null +++ b/src/Entity/Dimensions.php @@ -0,0 +1,78 @@ +unitOfMeasurement = $unitOfMeasurement; + return $this; + } + + public function getUnitOfMeasurement(): UnitOfMeasurement + { + return $this->unitOfMeasurement; + } + + public function setLength(string $length): self + { + if ($this->unitOfMeasurement->getCode() == 'IN') { + if ($length >= 0 && $length <= 108) { + $this->length = $length; + return $this; + } + } + + if ($this->unitOfMeasurement->getCode() == 'CM') { + if ($length >= 0 && $length <= 270) { + $this->length = $length; + return $this; + } + } + + throw new \Exception("Length value is not valid."); + } + + public function getLength(): string + { + return $this->length; + } + + public function setWidth(string $width): self + { + $this->width = $width; + return $this; + } + + public function getWidth(): string + { + return $this->width; + } + + public function setHeight(string $height): self + { + $this->height = $height; + return $this; + } + + public function getHeight(): string + { + return $this->height; + } + + public function toArray(): array + { + return [ + "UnitOfMeasurement" => $this->unitOfMeasurement->toArray(), + "Length" => $this->length, + "Width" => $this->width, + "Height" => $this->height + ]; + } +} diff --git a/src/Entity/LabelImageFormat.php b/src/Entity/LabelImageFormat.php new file mode 100644 index 0000000..c6660c7 --- /dev/null +++ b/src/Entity/LabelImageFormat.php @@ -0,0 +1,53 @@ +code = $code; + return $this; + } + + throw new \Exception("LabelImageFormat code doesn't exists."); + } + + public function getCode(): string + { + return $this->code; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function toArray(): array + { + $labelImageFormat = [ + "Code" => $this->code + ]; + + if ($this->description) { + $labelImageFormat["Description"] = $this->description; + } + + return $labelImageFormat; + } +} \ No newline at end of file diff --git a/src/Entity/LabelSpecification.php b/src/Entity/LabelSpecification.php new file mode 100644 index 0000000..7ee71bb --- /dev/null +++ b/src/Entity/LabelSpecification.php @@ -0,0 +1,57 @@ +labelImageFormat = $labelImageFormat; + return $this; + } + + public function getLabelImageFormat(): LabelImageFormat + { + return $this->labelImageFormat; + } + + public function setLabelStockSize(LabelStockSize $labelStockSize): self + { + $this->labelStockSize = $labelStockSize; + return $this; + } + + public function getLabelStockSize(): LabelStockSize + { + return $this->labelStockSize; + } + + public function setHttpUserAgent(string $httpUserAgent): self + { + $this->httpUserAgent = $httpUserAgent; + return $this; + } + + public function getHttpUserAgent(): string + { + return $this->httpUserAgent; + } + + public function toArray(): array + { + $labelSpecification = [ + "LabelImageFormat" => $this->labelImageFormat->toArray(), + "LabelStockSize" => $this->labelStockSize->toArray() + ]; + + if ($this->httpUserAgent) { + $labelSpecification["HTTPUserAgent"] = $this->httpUserAgent; + } + + return $labelSpecification; + } +} diff --git a/src/Entity/LabelStockSize.php b/src/Entity/LabelStockSize.php new file mode 100644 index 0000000..386a59f --- /dev/null +++ b/src/Entity/LabelStockSize.php @@ -0,0 +1,51 @@ +height = $height; + return $this; + } + + throw new \Exception("LabelStockSize height can be only 6 or 8"); + } + + public function getHeight(): string + { + return $this->height; + } + + public function setWidth(string $width): self + { + if ($width == self::W_4) { + $this->width = $width; + return $this; + } + + throw new \Exception("LabelStockSize width can be only 4"); + } + + public function getWidth(): string + { + return $this->width; + } + + public function toArray(): array + { + return [ + "Height" => $this->height, + "Width" => $this->width + ]; + } +} \ No newline at end of file diff --git a/src/Entity/Package.php b/src/Entity/Package.php new file mode 100644 index 0000000..72f4e41 --- /dev/null +++ b/src/Entity/Package.php @@ -0,0 +1,76 @@ +description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function setPackaging(Packaging $packaging): self + { + $this->packaging = $packaging; + return $this; + } + + public function getPackaging(): Packaging + { + return $this->packaging; + } + + public function setDimensions(Dimensions $dimensions): self + { + $this->dimensions = $dimensions; + return $this; + } + + public function getDimensions(): Dimensions + { + return $this->dimensions; + } + + public function setPackageWeight(PackageWeight $packageWeight): self + { + $this->packageWeight = $packageWeight; + return $this; + } + + public function getPackageWeight(): PackageWeight + { + return $this->packageWeight; + } + + public function toArray(): array + { + $package = [ + "Packaging" => $this->packaging->toArray() + ]; + + if ($this->description) { + $package["Description"] = $this->description; + } + + if ($this->dimensions) { + $package["Dimensions"] = $this->dimensions->toArray(); + } + + if ($this->packageWeight) { + $package["PackageWeight"] = $this->packageWeight->toArray(); + } + + return $package; + } +} diff --git a/src/Entity/PackageWeight.php b/src/Entity/PackageWeight.php new file mode 100644 index 0000000..656ee09 --- /dev/null +++ b/src/Entity/PackageWeight.php @@ -0,0 +1,39 @@ +unitOfMeasurement = $unitOfMeasurement; + return $this; + } + + public function getUnitOfMeasurement(): UnitOfMeasurement + { + return $this->unitOfMeasurement; + } + + public function setWeight(string $weight): self + { + $this->weight = $weight; + return $this; + } + + public function getWeight(): string + { + return $this->weight; + } + + public function toArray(): array + { + return [ + "UnitOfMeasurement" => $this->unitOfMeasurement->toArray(), + "Weight" => $this->weight + ]; + } +} diff --git a/src/Entity/Packaging.php b/src/Entity/Packaging.php new file mode 100644 index 0000000..463a5b7 --- /dev/null +++ b/src/Entity/Packaging.php @@ -0,0 +1,72 @@ +code = $code; + return $this; + } + + throw new \Exception("Packaging code doesn't exists."); + } + + public function getCode(): string + { + return $this->code; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function toArray(): array + { + $packaging = [ + "Code" => $this->code + ]; + + if ($this->description) { + $packaging["Description"] = $this->description; + } + + return $packaging; + } +} \ No newline at end of file diff --git a/src/Entity/PaymentInformation.php b/src/Entity/PaymentInformation.php new file mode 100644 index 0000000..be23628 --- /dev/null +++ b/src/Entity/PaymentInformation.php @@ -0,0 +1,36 @@ +shipmentCharge->exists(); + } + + public function __construct() + { + $this->shipmentCharge = new ShipmentCharge(); + } + + public function setShipmentCharge(ShipmentCharge $shipmentCharge): self + { + $this->shipmentCharge = $shipmentCharge; + return $this; + } + + public function getShipmentCharge(): ShipmentCharge + { + return $this->shipmentCharge; + } + + public function toArray(): array + { + return [ + "ShipmentCharge" => $this->shipmentCharge->toArray() + ]; + } +} \ No newline at end of file diff --git a/src/Entity/Phone.php b/src/Entity/Phone.php new file mode 100644 index 0000000..be1b1c8 --- /dev/null +++ b/src/Entity/Phone.php @@ -0,0 +1,44 @@ +number = $number; + return $this; + } + + public function getNumber(): string + { + return $this->number; + } + + public function setExtension(string $extension): self + { + $this->extension = $extension; + return $this; + } + + public function getExtension(): string + { + return $this->extension; + } + + public function toArray(): array + { + $phone = [ + "Number" => $this->number + ]; + + if ($this->extension) { + $phone["Extension"] = $this->extension; + } + + return $phone; + } +} \ No newline at end of file diff --git a/src/Entity/Query.php b/src/Entity/Query.php new file mode 100644 index 0000000..6379fb2 --- /dev/null +++ b/src/Entity/Query.php @@ -0,0 +1,30 @@ +additionalAddressValidation = $additionalAddressValidation; + return $this; + } + + public function getAdditionalAddressValidation(): string + { + return $this->additionalAddressValidation; + } + + public function toArray(): array + { + if ($this->additionalAddressValidation) { + return [ + "additionaladdressvalidation" => $this->additionalAddressValidation + ]; + } + + return []; + } +} \ No newline at end of file diff --git a/src/Entity/Request.php b/src/Entity/Request.php new file mode 100644 index 0000000..5f2b6a5 --- /dev/null +++ b/src/Entity/Request.php @@ -0,0 +1,42 @@ +subVersion = ""; + } + + public function setSubVersion(string $subVersion): self + { + if (in_array($subVersion, ["1601", "1607", "1701", "1707", "1801", "1807", "2108", "2205"])) { + $this->subVersion = $subVersion; + return $this; + } + + throw new \Exception("SubVersion value doesn't exists."); + } + + public function getSubVersion(): string + { + return $this->subVersion; + } + + public function toArray(): array + { + $request = [ + "RequestOption" => self::REQUEST_OPTION + ]; + + if ($this->subVersion) { + $request["SubVersion"] = $this->subVersion; + } + + return $request; + } +} diff --git a/src/Entity/ReturnService.php b/src/Entity/ReturnService.php new file mode 100644 index 0000000..7a874d6 --- /dev/null +++ b/src/Entity/ReturnService.php @@ -0,0 +1,76 @@ +code) && !empty($this->code)) { + return true; + } + + return false; + } + + public function setCode(string $code): self + { + if (in_array($code, [ + self::UPS_PRINT_AND_MAIL, self::UPS_RETURN_SERVICE_1_ATTEMPT, self::UPS_RETURN_SERVICE_3_ATTEMPT, self::UPS_ELECTRONIC_RETURN_LABEL, self::UPS_PRINT_RETURN_LABEL, self::UPS_EXCHANGE_PRINT_RETURN_LABEL, self::UPS_PACK_AND_COLLECT_SERVICE_1_ATTEMPT_BOX_1, self::UPS_PACK_AND_COLLECT_SERVICE_1_ATTEMPT_BOX_2, self::UPS_PACK_AND_COLLECT_SERVICE_1_ATTEMPT_BOX_3, self::UPS_PACK_AND_COLLECT_SERVICE_1_ATTEMPT_BOX_4, self::UPS_PACK_AND_COLLECT_SERVICE_1_ATTEMPT_BOX_5, self::UPS_PACK_AND_COLLECT_SERVICE_3_ATTEMPT_BOX_1, self::UPS_PACK_AND_COLLECT_SERVICE_3_ATTEMPT_BOX_2, self::UPS_PACK_AND_COLLECT_SERVICE_3_ATTEMPT_BOX_3, self::UPS_PACK_AND_COLLECT_SERVICE_3_ATTEMPT_BOX_4, self::UPS_PACK_AND_COLLECT_SERVICE_3_ATTEMPT_BOX_5 + ])) { + $this->code = $code; + return $this; + } + + throw new \Exception("ReturnService Code doesn't exists."); + } + + public function getCode(): string + { + return $this->code; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function toArray(): array + { + $service = [ + "Code" => $this->code + ]; + + if ($this->description) { + $service["Description"] = $this->description; + } + + return $service; + } +} diff --git a/src/Entity/Service.php b/src/Entity/Service.php new file mode 100644 index 0000000..2e4714e --- /dev/null +++ b/src/Entity/Service.php @@ -0,0 +1,81 @@ +code = $code; + return $this; + } + + throw new \Exception("Service Code doesn't exists."); + } + + public function getCode(): string + { + return $this->code; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function toArray(): array + { + $service = [ + "Code" => $this->code + ]; + + if ($this->description) { + $service["Description"] = $this->description; + } + + return $service; + } +} \ No newline at end of file diff --git a/src/Entity/ShipFrom.php b/src/Entity/ShipFrom.php new file mode 100644 index 0000000..34c6bfa --- /dev/null +++ b/src/Entity/ShipFrom.php @@ -0,0 +1,89 @@ +name = mb_substr($name, 0, 35); + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setAttentionName(string $attn_name): self + { + $this->attentionName = mb_substr($attn_name, 0, 35); + return $this; + } + + public function getAttentionName(): string + { + return $this->attentionName; + } + + public function setPhone(Phone $phone): self + { + $this->phone = $phone; + return $this; + } + + public function getPhone(): Phone + { + return $this->phone; + } + + public function setFaxNumber(string $fax_number): self + { + $this->faxNumber = $fax_number; + return $this; + } + + public function getFaxNumber(): string + { + return $this->faxNumber; + } + + public function setAddress(Address $address): self + { + $this->address = $address; + return $this; + } + + public function getAddress(): Address | null + { + return $this->address; + } + + public function toArray(): array + { + $shipFrom = [ + "Name" => $this->name, + "Address" => $this->address->toArray() + ]; + + if ($this->attentionName) { + $shipFrom["AttentionName"] = $this->attentionName; + } + + if ($this->phone) { + $shipFrom["Phone"] = $this->phone->toArray(); + } + + if ($this->faxNumber) { + $shipFrom["FaxNumber"] = $this->faxNumber; + } + + return $shipFrom; + } +} diff --git a/src/Entity/ShipTo.php b/src/Entity/ShipTo.php new file mode 100644 index 0000000..b3624c4 --- /dev/null +++ b/src/Entity/ShipTo.php @@ -0,0 +1,89 @@ +name = $name; + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setAttentionName(string $attn_name): self + { + $this->attentionName = $attn_name; + return $this; + } + + public function getAttentionName(): string + { + return $this->attentionName; + } + + public function setPhone(Phone $phone): self + { + $this->phone = $phone; + return $this; + } + + public function getPhone(): Phone + { + return $this->phone; + } + + public function setAddress(Address $address): self + { + $this->address = $address; + return $this; + } + + public function getAddress(): Address | null + { + return $this->address; + } + + public function setResidential(string $residential): self + { + $this->residential = $residential; + return $this; + } + + public function getResidential(): string + { + return $this->residential; + } + + public function toArray(): array + { + $shipTo = [ + "Name" => $this->name, + "Address" => $this->address->toArray() + ]; + + if ($this->attentionName) { + $shipTo["AttentionName"] = $this->attentionName; + } + + if ($this->phone) { + $shipTo["Phone"] = $this->phone->toArray(); + } + + if ($this->residential) { + $shipTo["Residential"] = $this->residential; + } + + return $shipTo; + } +} diff --git a/src/Entity/Shipment.php b/src/Entity/Shipment.php new file mode 100644 index 0000000..a7fa91f --- /dev/null +++ b/src/Entity/Shipment.php @@ -0,0 +1,137 @@ +returnService = new ReturnService; + $this->paymentInformation = new PaymentInformation; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function setReturnService(ReturnService $returnService): self + { + $this->returnService = $returnService; + return $this; + } + + public function getReturnService(): ReturnService + { + return $this->returnService; + } + + public function setShipper(Shipper $shipper): self + { + $this->shipper = $shipper; + return $this; + } + + public function getShipper(): Shipper + { + return $this->shipper; + } + + public function setShipTo(ShipTo $shipTo): self + { + $this->shipTo = $shipTo; + return $this; + } + + public function getShipTo(): ShipTo + { + return $this->shipTo; + } + + public function setShipFrom(ShipFrom $shipFrom): self + { + $this->shipFrom = $shipFrom; + return $this; + } + + public function getShipFrom(): ShipFrom + { + return $this->shipFrom; + } + + public function setPaymentInformation(PaymentInformation $paymentInformation): self + { + $this->paymentInformation = $paymentInformation; + return $this; + } + + public function getPaymentInformation(): PaymentInformation + { + return $this->paymentInformation; + } + + public function setService(Service $service): self + { + $this->service = $service; + return $this; + } + + public function getService(): Service + { + return $this->service; + } + + public function setPackage(Package $package): self + { + $this->package = $package; + return $this; + } + + public function getPackage(): Package + { + return $this->package; + } + + public function toArray(): array + { + $shipment = [ + "Shipper" => $this->shipper->toArray(), + "ShipTo" => $this->shipTo->toArray(), + "Service" => $this->service->toArray(), + "Package" => $this->package->toArray() + ]; + + if ($this->description) { + $shipment['Description'] = $this->description; + } + + if ($this->paymentInformation->exists()) { + $shipment["PaymentInformation"] = $this->paymentInformation->toArray(); + } + + if ($this->shipFrom) { + $shipment["ShipFrom"] = $this->shipFrom->toArray(); + } + + if ($this->returnService->exists()) { + $shipment["ReturnService"] = $this->returnService->toArray(); + } + + return $shipment; + } +} diff --git a/src/Entity/ShipmentCharge.php b/src/Entity/ShipmentCharge.php new file mode 100644 index 0000000..bd919b5 --- /dev/null +++ b/src/Entity/ShipmentCharge.php @@ -0,0 +1,44 @@ +billShipper->exists(); + } + + public function __construct() + { + $this->billShipper = new BillShipper(); + } + + public function setBillShipper(BillShipper $billShipper): self + { + $this->billShipper = $billShipper; + return $this; + } + + public function getBillShipper(): BillShipper + { + return $this->billShipper; + } + + public function toArray(): array + { + $shipmentCharge = [ + "Type" => self::TYPE + ]; + + if ($this->billShipper->exists()) { + $shipmentCharge["BillShipper"] = $this->billShipper->toArray(); + } + + return $shipmentCharge; + } +} \ No newline at end of file diff --git a/src/Entity/ShipmentRequest.php b/src/Entity/ShipmentRequest.php new file mode 100644 index 0000000..c18bbe4 --- /dev/null +++ b/src/Entity/ShipmentRequest.php @@ -0,0 +1,57 @@ +request = $request; + return $this; + } + + public function getRequest(): Request + { + return $this->request; + } + + public function setShipment(Shipment $shipment): self + { + $this->shipment = $shipment; + return $this; + } + + public function getShipment(): Shipment + { + return $this->shipment; + } + + public function setLabelSpecification(LabelSpecification $labelSpecification): self + { + $this->labelSpecification = $labelSpecification; + return $this; + } + + public function getLabelSpecification(): LabelSpecification + { + return $this->labelSpecification; + } + + public function toArray(): array + { + $shipmentRequest = [ + "Request" => $this->request->toArray(), + "Shipment" => $this->shipment->toArray() + ]; + + if ($this->labelSpecification) { + $shipmentRequest["LabelSpecification"] = $this->labelSpecification->toArray(); + } + + return $shipmentRequest; + } +} diff --git a/src/Entity/Shipper.php b/src/Entity/Shipper.php new file mode 100644 index 0000000..f4dbfc4 --- /dev/null +++ b/src/Entity/Shipper.php @@ -0,0 +1,118 @@ +name = mb_substr($name, 0, 35); + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setAttentionName(string $attn_name): self + { + $this->attentionName = mb_substr($attn_name, 0, 35); + return $this; + } + + public function getAttentionName(): string + { + return $this->attentionName; + } + + public function setTaxIdentificationNumber(string $tax_identification_number): self + { + $this->taxIdentificationNumber = $tax_identification_number; + return $this; + } + + public function getTaxIdentificationNumber(): string + { + return $this->taxIdentificationNumber; + } + + public function setPhone(Phone $phone): self + { + $this->phone = $phone; + return $this; + } + + public function getPhone(): Phone + { + return $this->phone; + } + + public function setShippingNumber(string $shipper_number): self + { + $this->shipperNumber = $shipper_number; + return $this; + } + + public function getShippingNumber(): string + { + return $this->shipperNumber; + } + + public function setFaxNumber(string $fax_number): self + { + $this->faxNumber = $fax_number; + return $this; + } + + public function getFaxNumber(): string + { + return $this->faxNumber; + } + + public function setAddress(Address $address): self + { + $this->address = $address; + return $this; + } + + public function getAddress(): Address | null + { + return $this->address; + } + + public function toArray(): array + { + $shipper = [ + "Name" => $this->name, + "ShipperNumber" => $this->shipperNumber, + "Address" => $this->address->toArray() + ]; + + if ($this->attentionName) { + $shipper["AttentionName"] = $this->attentionName; + } + + if ($this->taxIdentificationNumber) { + $shipper["TaxIdentificationNumber"] = $this->taxIdentificationNumber; + } + + if ($this->phone) { + $shipper["Phone"] = $this->phone->toArray(); + } + + if ($this->faxNumber) { + $shipper["FaxNumber"] = $this->faxNumber; + } + + return $shipper; + } +} diff --git a/src/Entity/UnitOfMeasurement.php b/src/Entity/UnitOfMeasurement.php new file mode 100644 index 0000000..93bea3d --- /dev/null +++ b/src/Entity/UnitOfMeasurement.php @@ -0,0 +1,56 @@ +code = $code; + return $this; + } + + throw new \Exception("Unit of Measurement code doesn't exists."); + } + + public function getCode(): string + { + return $this->code; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function toArray(): array + { + $measurement = [ + "Code" => $this->code + ]; + + if ($this->description) { + $measurement["Description"] = $this->description; + } + + return $measurement; + } +} diff --git a/src/Ship.php b/src/Ship.php new file mode 100644 index 0000000..210b05b --- /dev/null +++ b/src/Ship.php @@ -0,0 +1,117 @@ +query = new Query(); + $this->onlyLabel = false; + } + + public function setQuery(Query $query): self + { + $this->query = $query; + return $this; + } + + public function getQuery(): Query + { + return $this->query; + } + + public function setShipmentRequest(ShipmentRequest $shipmentRequest): self + { + $this->shipmentRequest = $shipmentRequest; + return $this; + } + + public function getShipmentRequest(): ShipmentRequest + { + return $this->shipmentRequest; + } + + public function createShipment(string $client_id, string $client_secret): array + { + $auth = $this->authenticate($client_id, $client_secret); + + if ($auth['status'] == 'fail') { + return $auth; + } + + $access_token = $auth['access_token']; + + $httpClient = new HttpClient(); + $httpClient->setHeader([ + "Authorization: Bearer $access_token", + "Content-Type: application/json", + "transId: string", + "transactionSrc: testing" + ]); + $httpClient->setPayload(json_encode($this->getPayload())); + $httpClient->setUrl($this->_getAPIBaseURL() . "/api/shipments/" . self::VERSION . "/ship?" . http_build_query($this->query->toArray())); + $httpClient->setMethod("POST"); + $res = $httpClient->fetch(); + + if (!isset($res->ShipmentResponse)) { + if (isset($res->response)) { + $error = $res->response->errors[0]->message; + } else { + $error = "Shipment creation failed! Please try again."; + } + return ['status' => 'fail', 'msg' => $error]; + } + + if (!isset($res->ShipmentResponse->ShipmentResults)) { + return ['status' => 'fail', 'msg' => "Invalid Request."]; + } + + if ($this->onlyLabel) { + $shipmentRes = $res->ShipmentResponse->ShipmentResults; + if (!isset($shipmentRes->PackageResults)) { + return ['status' => 'fail', 'msg' => "Label data is not present in response."]; + } + + $packageRes = $shipmentRes->PackageResults; + if (!isset($packageRes[0]->ShippingLabel)) { + return ['status' => 'fail', 'msg' => "Label data is not present in response."]; + } + + return ['status' => 'success', 'data' => $packageRes[0]->ShippingLabel]; + } + + + return ['status' => 'success', 'data' => $res]; + } + + private function getPayload(): array + { + return [ + "ShipmentRequest" => $this->shipmentRequest->toArray() + ]; + } + + public function setMode(string $mode): self + { + parent::setMode($mode); + return $this; + } + + public function setOnlyLabel(bool $onlyLabel): self + { + $this->onlyLabel = $onlyLabel; + return $this; + } +} diff --git a/src/Utils/HttpClient.php b/src/Utils/HttpClient.php new file mode 100644 index 0000000..a82ce7d --- /dev/null +++ b/src/Utils/HttpClient.php @@ -0,0 +1,57 @@ +header = $header; + return $this; + } + + public function setPayload(string $payload): self + { + $this->payload = $payload; + return $this; + } + + public function setUrl(string $url): self + { + $this->url = $url; + return $this; + } + + public function setMethod(string $method): self + { + $this->method = $method; + return $this; + } + + public function fetch(): object + { + try { + $curl = curl_init(); + + curl_setopt_array($curl, [ + CURLOPT_HTTPHEADER => $this->header, + CURLOPT_POSTFIELDS => $this->payload, + CURLOPT_URL => $this->url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CUSTOMREQUEST => strtoupper($this->method), + ]); + + $response = curl_exec($curl); + curl_close($curl); + + return json_decode($response); + } catch (\Exception $e) { + throw new \Exception("Curl request Failed. Reason: ". $e->getMessage()); + } + } +} \ No newline at end of file From 4128acbe036c130d2020e4999822bb762193b6d1 Mon Sep 17 00:00:00 2001 From: ragod123 <35573000+ragod123@users.noreply.github.com> Date: Mon, 6 May 2024 17:55:32 +0530 Subject: [PATCH 4/5] create shipping label demo file --- demo/create-label.php | 194 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 demo/create-label.php diff --git a/demo/create-label.php b/demo/create-label.php new file mode 100644 index 0000000..3156862 --- /dev/null +++ b/demo/create-label.php @@ -0,0 +1,194 @@ +setAddressLine1("address line 1"); +$address->setAddressLine2(""); // optional +$address->setCity("Timonium"); +$address->setStateProvinceCode("MD"); +$address->setPostalCode("21093"); +$address->setCountryCode("US"); + +$phone = new Phone(); +$phone->setNumber("1115554758"); +$phone->setExtension(""); // optional + +$shipper = new Shipper(); +$shipper->setName("Shipper Test"); +$shipper->setAttentionName(""); // optional +$shipper->setTaxIdentificationNumber(""); // optional +$shipper->setPhone($phone); +$shipper->setShippingNumber("123456"); +$shipper->setFaxNumber(""); // optional +$shipper->setAddress($address); +/********* End Shipper **********/ + +/************ ShipTo **********/ +$address = new Address(); +$address->setAddressLine1("address line 1"); +$address->setAddressLine2(""); // optional +$address->setCity("Timonium"); +$address->setStateProvinceCode("MD"); +$address->setPostalCode("21093"); +$address->setCountryCode("US"); + +$phone = new Phone(); +$phone->setNumber("1115554758"); + +$shipTo = new ShipTo(); +$shipTo->setName("Shipper Test"); +$shipTo->setAttentionName(""); // optional +$shipTo->setPhone($phone); +$shipTo->setAddress($address); +$shipTo->setResidential(""); // optional +/************ End ShipTo **********/ + +/************ ShipFrom **********/ +$address = new Address(); +$address->setAddressLine1("address line 1"); +$address->setAddressLine2(""); // optional +$address->setCity("Timonium"); +$address->setStateProvinceCode("MD"); +$address->setPostalCode("21093"); +$address->setCountryCode("US"); + +$phone = new Phone(); +$phone->setNumber("1115554758"); + +$shipFrom = new ShipFrom(); +$shipFrom->setName("Shipper Test"); +$shipFrom->setAttentionName(""); // optional +$shipFrom->setPhone($phone); +$shipFrom->setFaxNumber(""); // optional +$shipFrom->setAddress($address); +/************ End ShipFrom **********/ + +/************ PaymentInformation **********/ +$billShipper = new BillShipper(); +$billShipper->setAccountNumber("123456"); + +$shipmentCharge = new ShipmentCharge(); +$shipmentCharge->setBillShipper($billShipper); + +$paymentInformation = new PaymentInformation(); +$paymentInformation->setShipmentCharge($shipmentCharge); +/************ End PaymentInformation **********/ + +/************ Service **********/ +$service = new Service(); +$service->setCode(Service::GROUND); +$service->setDescription("Ground"); // optional +/************ End Service **********/ + +/************ Package **********/ +$packaging = new Packaging(); +$packaging->setCode(Packaging::CUSTOMER_SUPPLIED_PACKAGE); +$packaging->setDescription("Ups Letter"); // optional + +$unitOfMeasurement = new UnitOfMeasurement(); +$unitOfMeasurement->setCode(UnitOfMeasurement::INCHES); +$unitOfMeasurement->setDescription("Inches"); // optional + +$dimensions = new Dimensions(); +$dimensions->setUnitOfMeasurement($unitOfMeasurement); +$dimensions->setLength("10"); +$dimensions->setWidth("30"); +$dimensions->setHeight("45"); + +$unitOfMeasurement = new UnitOfMeasurement(); +$unitOfMeasurement->setCode(UnitOfMeasurement::POUNDS); +$unitOfMeasurement->setDescription("POUNDS"); // optional + +$packageWeight = new PackageWeight(); +$packageWeight->setUnitOfMeasurement($unitOfMeasurement); +$packageWeight->setWeight("5"); + +$package = new Package(); +$package->setDescription(""); // optional +$package->setPackaging($packaging); +$package->setDimensions($dimensions); +$package->setPackageWeight($packageWeight); +/************ End Package **********/ + +/************ Shipment **********/ +$shipment = new Shipment(); +$shipment->setDescription("Ship WS test"); +$shipment->setShipper($shipper); +$shipment->setShipTo($shipTo); +$shipment->setShipFrom($shipFrom); +$shipment->setPaymentInformation($paymentInformation); +$shipment->setService($service); +$shipment->setPackage($package); +/************ End Shipment **********/ + +/************ Label Specification **********/ +$labelImageFormat = new LabelImageFormat(); +$labelImageFormat->setCode(LabelImageFormat::GIF); +$labelImageFormat->setDescription("GIF"); // optional + +$labelStockSize = new LabelStockSize(); +$labelStockSize->setHeight(LabelStockSize::H_8); +$labelStockSize->setWidth(LabelStockSize::W_4); + +$labelSpecification = new LabelSpecification(); +$labelSpecification->setLabelImageFormat($labelImageFormat); +$labelSpecification->setLabelStockSize($labelStockSize); +$labelSpecification->setHttpUserAgent("Mozilla/4.5"); // optional +/************ End Label Specification **********/ + +/************ Shipment Request **********/ +$shipmentRequest = new ShipmentRequest(); +$shipmentRequest->setRequest(new Request); +$shipmentRequest->setShipment($shipment); +$shipmentRequest->setLabelSpecification($labelSpecification); +/************ End Shipment Request **********/ + +/************ Create Ship **********/ +$ship = new Ship(); +$ship->setShipmentRequest($shipmentRequest); +$ship->setOnlyLabel(true); // optional +// $ship->setMode('PROD'); // Optional | only used for prod +$shipRes = $ship->createShipment($client_id, $client_secret); +/************ End Create Ship **********/ + +echo '
'; print_r($shipRes); echo ''; +?> + + + + + +
'; print_r($shipRes); echo ''; +?> + + + + + +