Skip to content

Commit 3a05f7c

Browse files
authored
Merge pull request #5 from proclame/add-label-void
Add Void Shipment functionality with associated classes and documentation
2 parents 9506695 + ff04f93 commit 3a05f7c

File tree

4 files changed

+197
-4
lines changed

4 files changed

+197
-4
lines changed

demo/void-shipment.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use RahulGodiyal\PhpUpsApiWrapper\Entity\VoidShipmentQuery;
4+
use RahulGodiyal\PhpUpsApiWrapper\VoidShipment;
5+
6+
require_once('./vendor/autoload.php');
7+
8+
$client_id = "xxxxxxxxxxxxxxxx"; // UPS Client ID
9+
$client_secret = "xxxxxxxxxxxxxxx"; // UPS Client Secret
10+
11+
// Shipment identification number to void
12+
$shipmentIdentificationNumber = "1Zxxxxxxxxxxxxxxxx"; // Replace with actual shipment ID
13+
14+
/********* Void Shipment Query *********/
15+
$query = new VoidShipmentQuery(); // optional
16+
$query->setTrackingNumber(""); // optional
17+
/********* End Void Shipment Query *********/
18+
19+
/************ Void Shipment **********/
20+
$voidShipment = new VoidShipment();
21+
$voidShipmentRes = $voidShipment
22+
->setQuery($query) // optional
23+
->setShipmentIdentificationNumber($shipmentIdentificationNumber)
24+
// ->setMode('PROD') // optional - uncomment for production
25+
->voidShipment($client_id, $client_secret);
26+
/************ End Void Shipment **********/
27+
28+
echo '<pre>'; print_r($voidShipmentRes); echo '</pre>';

readme.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ Latest OAuth 2.0 Rest API Wrapper for UPS web services.
1010
3. [Address Validation](#address-validation)
1111
4. [Create Shipment | Shipping Label](#create-shipment--shipping-label)
1212
5. [Tracking API](#tracking-api)
13-
6. [Documentation](#documentation)
14-
7. [License](#license)
15-
8. [Support the Project](#support-the-project)
13+
6. [Void Shipment](#void-shipment)
14+
7. [Documentation](#documentation)
15+
8. [License](#license)
16+
9. [Support the Project](#support-the-project)
1617

1718
<a name="requirements"></a>
1819
## Requirements
@@ -282,6 +283,37 @@ echo '<pre>'; print_r($shipRes); echo '</pre>';
282283

283284
```
284285

286+
<a name="void-shipment"></a>
287+
## Void Shipment
288+
```php
289+
<?php
290+
291+
use RahulGodiyal\PhpUpsApiWrapper\Entity\VoidShipmentQuery;
292+
use RahulGodiyal\PhpUpsApiWrapper\VoidShipment;
293+
294+
require_once('./vendor/autoload.php');
295+
296+
$client_id = "******************************"; // UPS Client ID
297+
$client_secret = "*****************************************"; // UPS Client Secret
298+
299+
// Shipment identification number to void
300+
$shipmentIdentificationNumber = "1Z12345E0205271688"; // Replace with actual shipment ID
301+
302+
/********* Void Shipment Query *********/
303+
$query = new VoidShipmentQuery(); // optional
304+
$query->setTrackingNumber(""); // optional - if you want to include tracking number in the query
305+
/********* End Void Shipment Query *********/
306+
307+
$voidShipment = new VoidShipment();
308+
$voidShipmentRes = $voidShipment
309+
->setQuery($query) // optional
310+
->setShipmentIdentificationNumber($shipmentIdentificationNumber)
311+
// ->setMode('PROD') // optional - uncomment for production
312+
->voidShipment($client_id, $client_secret);
313+
314+
echo '<pre>'; print_r($voidShipmentRes); echo '</pre>'; die();
315+
```
316+
285317
<a name="tracking-api"></a>
286318
## Tracking API
287319
```php
@@ -328,4 +360,3 @@ PHP UPS API is licensed under [The MIT License (MIT)](LICENSE).
328360
If you enjoy using this package, please consider [buying me a coffee](https://www.buymeacoffee.com/ragod). Your support keeps the development going! ☕
329361

330362
[![Buy Me a Coffee](https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg)](https://www.buymeacoffee.com/ragod)
331-

src/Entity/VoidShipmentQuery.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RahulGodiyal\PhpUpsApiWrapper\Entity;
4+
5+
class VoidShipmentQuery
6+
{
7+
private string $trackingNumber = "";
8+
9+
public function exists()
10+
{
11+
if (!empty($this->trackingNumber)) {
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
18+
public function setTrackingNumber(string $trackingNumber): self
19+
{
20+
$this->trackingNumber = $trackingNumber;
21+
return $this;
22+
}
23+
24+
public function getTrackingNumber(): string
25+
{
26+
return $this->trackingNumber;
27+
}
28+
29+
public function toArray(): array
30+
{
31+
if ($this->trackingNumber) {
32+
return [
33+
"trackingnumber" => $this->trackingNumber
34+
];
35+
}
36+
37+
return [];
38+
}
39+
}

src/VoidShipment.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace RahulGodiyal\PhpUpsApiWrapper;
4+
5+
use RahulGodiyal\PhpUpsApiWrapper\Entity\VoidShipmentQuery;
6+
use RahulGodiyal\PhpUpsApiWrapper\Utils\HttpClient;
7+
8+
class VoidShipment extends Auth
9+
{
10+
private const VERSION = "v2409";
11+
12+
private VoidShipmentQuery $query;
13+
private string $shipmentIdentificationNumber;
14+
private object $apiResponse;
15+
16+
public function __construct()
17+
{
18+
$this->query = new VoidShipmentQuery();
19+
}
20+
21+
public function setQuery(VoidShipmentQuery $query): self
22+
{
23+
$this->query = $query;
24+
return $this;
25+
}
26+
27+
public function getQuery(): VoidShipmentQuery
28+
{
29+
return $this->query;
30+
}
31+
32+
public function setShipmentIdentificationNumber(string $shipmentIdentificationNumber): self
33+
{
34+
$this->shipmentIdentificationNumber = $shipmentIdentificationNumber;
35+
return $this;
36+
}
37+
38+
public function getShipmentIdentificationNumber(): string
39+
{
40+
return $this->shipmentIdentificationNumber;
41+
}
42+
43+
public function voidShipment(string $client_id, string $client_secret): array
44+
{
45+
$auth = $this->authenticate($client_id, $client_secret);
46+
47+
if ($auth['status'] === 'fail') {
48+
return $auth;
49+
}
50+
51+
$access_token = $auth['access_token'];
52+
53+
$queryParams = "";
54+
if ($this->query->exists()) {
55+
$queryParams = "?" . http_build_query($this->query->toArray());
56+
}
57+
58+
$httpClient = new HttpClient();
59+
$httpClient->setHeader([
60+
"Authorization: Bearer $access_token",
61+
"Content-Type: application/json",
62+
"transId: string",
63+
"transactionSrc: testing"
64+
]);
65+
$httpClient->setUrl($this->_getAPIBaseURL() . "/api/shipments/" . self::VERSION . "/void/cancel/" . $this->shipmentIdentificationNumber . $queryParams);
66+
$httpClient->setMethod("DELETE");
67+
$this->apiResponse = $res = $httpClient->fetch();
68+
69+
if (!isset($res->VoidShipmentResponse)) {
70+
if (isset($res->response)) {
71+
$error = $res->response->errors[0]->message;
72+
} else {
73+
$error = "Voiding shipment failed! Please try again.";
74+
}
75+
return ['status' => 'fail', 'msg' => $error];
76+
}
77+
78+
if (!isset($res->VoidShipmentResponse->Status)) {
79+
return ['status' => 'fail', 'msg' => "Invalid Request."];
80+
}
81+
82+
return ['status' => 'success', 'data' => $res->VoidShipmentResponse];
83+
}
84+
85+
public function setMode(string $mode): self
86+
{
87+
parent::setMode($mode);
88+
return $this;
89+
}
90+
91+
public function getResponse(): string
92+
{
93+
return json_encode($this->apiResponse);
94+
}
95+
}

0 commit comments

Comments
 (0)