Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ FedEx Rest API documentation https://developer.fedex.com/api/en-us/get-started.h
- [ ] Postal Code Validation API
- [x] Rate Quotes API
- [ ] Service Availability API
- [ ] Trade Documents Upload API

### Other
- [x] [oAuth authorization](#authorization)
Expand Down
56 changes: 56 additions & 0 deletions src/FedexRest/Services/TradeDocument/Entity/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace FedexRest\Services\TradeDocument\Entity;

class Document
{
public string $workflowName;
public string $name;
public string $contentType;
public Meta $meta;
public ?string $carrierCode = null;

public function setWorkflowName(string $workflowName): Document
{
$this->workflowName = $workflowName;
return $this;
}

public function setName(string $name): Document
{
$this->name = $name;
return $this;
}

public function setContentType(string $contentType): Document
{
$this->contentType = $contentType;
return $this;
}

public function setMeta(Meta $meta): Document
{
$this->meta = $meta;
return $this;
}

public function setCarrierCode(string $carrierCode): Document
{
$this->carrierCode = $carrierCode;
return $this;
}

public function prepare(): array
{
$data = [
'workflowName' => $this->workflowName,
'name' => $this->name,
'contentType' => $this->contentType,
'meta' => $this->meta->prepare(),
];
if (!empty($this->carrierCode)) {
$data['carrierCode'] = $this->carrierCode;
}
return $data;
}
}
44 changes: 44 additions & 0 deletions src/FedexRest/Services/TradeDocument/Entity/ImageDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace FedexRest\Services\TradeDocument\Entity;

class ImageDocument
{
public string $referenceId;
public string $name;
public string $contentType;
public ImageMeta $meta;

public function setReferenceId(string $referenceId): ImageDocument
{
$this->referenceId = $referenceId;
return $this;
}
public function setName(string $name): ImageDocument
{
$this->name = $name;
return $this;
}

public function setContentType(string $contentType): ImageDocument
{
$this->contentType = $contentType;
return $this;
}

public function setMeta(ImageMeta $meta): ImageDocument
{
$this->meta = $meta;
return $this;
}

public function prepare(): array
{
return [
'referenceId' => $this->referenceId,
'name' => $this->name,
'contentType' => $this->contentType,
'meta' => $this->meta->prepare(),
];
}
}
30 changes: 30 additions & 0 deletions src/FedexRest/Services/TradeDocument/Entity/ImageMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace FedexRest\Services\TradeDocument\Entity;

class ImageMeta
{

public string $imageType;
public string $imageIndex;

public function setImageType(string $imageType): ImageMeta
{
$this->imageType = $imageType;
return $this;
}

public function setImageIndex(string $imageIndex): ImageMeta
{
$this->imageIndex = $imageIndex;
return $this;
}

public function prepare(): array
{
return [
'imageType' => $this->imageType,
'imageIndex' => $this->imageIndex,
];
}
}
88 changes: 88 additions & 0 deletions src/FedexRest/Services/TradeDocument/Entity/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace FedexRest\Services\TradeDocument\Entity;

class Meta
{
public string $shipDocumentType;
public string $originCountryCode;
public string $destinationCountryCode;
public ?string $formCode = null;
public ?string $trackingNumber = null;
public ?string $shipmentDate = null;
public ?string $originLocationCode = null;
public ?string $destinationLocationCode = null;

public function setShipDocumentType(string $shipDocumentType): Meta
{
$this->shipDocumentType = $shipDocumentType;
return $this;
}

public function setOriginCountryCode(string $originCountryCode): Meta
{
$this->originCountryCode = $originCountryCode;
return $this;
}

public function setDestinationCountryCode(string $destinationCountryCode): Meta
{
$this->destinationCountryCode = $destinationCountryCode;
return $this;
}

public function setFormCode(string $formCode): Meta
{
$this->formCode = $formCode;
return $this;
}

public function setTrackingNumber(string $trackingNumber): Meta
{
$this->trackingNumber = $trackingNumber;
return $this;
}

public function setShipmentDate(string $shipmentDate): Meta
{
$this->shipmentDate = $shipmentDate;
return $this;
}

public function setOriginLocationCode(string $originLocationCode): Meta
{
$this->originLocationCode = $originLocationCode;
return $this;
}

public function setDestinationLocationCode(string $destinationLocationCode): Meta
{
$this->destinationLocationCode = $destinationLocationCode;
return $this;
}

public function prepare(): array
{
$data = [
'shipDocumentType' => $this->shipDocumentType,
'originCountryCode' => $this->originCountryCode,
'destinationCountryCode' => $this->destinationCountryCode,
];
if (!empty($this->formCode)) {
$data['formCode'] = $this->formCode;
}
if (!empty($this->trackingNumber)) {
$data['trackingNumber'] = $this->trackingNumber;
}
if (!empty($this->shipmentDate)) {
$data['shipmentDate'] = $this->shipmentDate;
}
if (!empty($this->originLocationCode)) {
$data['originLocationCode'] = $this->originLocationCode;
}
if (!empty($this->destinationLocationCode)) {
$data['destinationLocationCode'] = $this->destinationLocationCode;
}
return $data;
}
}
21 changes: 21 additions & 0 deletions src/FedexRest/Services/TradeDocument/Entity/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace FedexRest\Services\TradeDocument\Entity;

class Rule
{
public string $workflowName;

public function setWorkflowName(string $workflowName): Rule
{
$this->workflowName = $workflowName;
return $this;
}

public function prepare(): array
{
return [
'workflowName' => $this->workflowName,
];
}
}
68 changes: 68 additions & 0 deletions src/FedexRest/Services/TradeDocument/UploadDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace FedexRest\Services\TradeDocument;

use FedexRest\Exceptions\MissingAccessTokenException;
use FedexRest\Services\AbstractRequest;
use FedexRest\Services\TradeDocument\Entity\Document;
use GuzzleHttp\Exception\GuzzleException;

class UploadDocument extends AbstractRequest
{

protected string $production_url = 'https://documentapi.prod.fedex.com';
protected string $testing_url = 'https://documentapitest.prod.fedex.com/sandbox';
public string $attachment;
public Document $document;

public function setAttachment(string $attachment): UploadDocument
{
$this->attachment = $attachment;
return $this;
}

public function setDocument(Document $document): UploadDocument
{
$this->document = $document;
return $this;
}

public function prepare(): array {
return [
[
'name' => 'attachment',
'contents' => $this->attachment,
'filename' => $this->document->name,
'headers' => [
'Content-Type' => $this->document->contentType
]
],
[
'name' => 'document',
'contents' => json_encode($this->document->prepare())
]
];
}

public function setApiEndpoint()
{
return '/documents/v1/etds/upload';
}

/**
* @throws MissingAccessTokenException
* @throws GuzzleException
*/
public function request() {
parent::request();
try {
$query = $this->http_client->post($this->getApiUri($this->api_endpoint), [
'multipart' => $this->prepare(),
'http_errors' => FALSE,
]);
return ($this->raw === true) ? $query : json_decode($query->getBody()->getContents());
} catch (\Exception $e) {
return $e->getMessage();
}
}
}
Loading