Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\InventoryItem;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Transaction;
|
||||
|
||||
class InventoryService
|
||||
{
|
||||
/** @var ClientInterface */
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function updateInventory(string $warehouseId, array $items, string $sellingPartyId, bool $isFullUpdate = false)
|
||||
{
|
||||
//First map all InventoryItems to an array
|
||||
$items = array_map(
|
||||
function (InventoryItem $item) {
|
||||
$data = $item->toArray();
|
||||
unset($data['availableQuantity']['unitSize']);
|
||||
|
||||
return $data;
|
||||
},
|
||||
$items
|
||||
);
|
||||
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
"/vendor/directFulfillment/inventory/v1/warehouses/{$warehouseId}/items",
|
||||
[
|
||||
'json' => [
|
||||
'inventory' => [
|
||||
'sellingParty' => [
|
||||
'partyId' => $sellingPartyId
|
||||
],
|
||||
'items' => $items,
|
||||
'isFullUpdate' => $isFullUpdate
|
||||
]
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload'];
|
||||
|
||||
return (new Transaction('inventory_update'))->setExternalId($payload['transactionId']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Invoice;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Transaction;
|
||||
|
||||
class InvoiceService
|
||||
{
|
||||
/** @var ClientInterface */
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function submitInvoice(Invoice $invoice): Transaction
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
'/vendor/directFulfillment/payments/v1/invoices',
|
||||
['json' => [$invoice->toArray()]]
|
||||
);
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload'];
|
||||
|
||||
return (new Transaction('invoice'))->setExternalId($payload['transactionId']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Service;
|
||||
|
||||
use DateTime;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\TransferException as GuzzleTransferException;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\PurchaseOrder;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\PurchaseOrderAcknowledgement;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Transaction;
|
||||
use Xentral\Modules\AmazonVendorDF\Exception\TransferException;
|
||||
|
||||
class PurchaseOrderService
|
||||
{
|
||||
/** @var ClientInterface */
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|null $createdAfter
|
||||
* @param DateTime|null $createdBefore
|
||||
* @param int|null $limit
|
||||
*
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function getPurchaseOrderNumbers(
|
||||
?DateTime $createdAfter = null,
|
||||
?DateTime $createdBefore = null,
|
||||
?int $limit = null
|
||||
): array {
|
||||
$orders = $this->getOrders($createdAfter, $createdBefore, $limit);
|
||||
|
||||
return array_map(
|
||||
function (array $order) {
|
||||
return $order['purchaseOrderNumber'];
|
||||
},
|
||||
$orders
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|null $createdAfter
|
||||
* @param DateTime|null $createdBefore
|
||||
* @param int|null $limit
|
||||
*
|
||||
* @return array|PurchaseOrder[]
|
||||
*/
|
||||
public function getPurchaseOrders(
|
||||
?DateTime $createdAfter = null,
|
||||
?DateTime $createdBefore = null,
|
||||
?int $limit = null
|
||||
): array {
|
||||
$orders = $this->getOrders($createdAfter, $createdBefore, $limit);
|
||||
|
||||
return array_map(
|
||||
function (array $order) {
|
||||
return PurchaseOrder::fromPurchaseOrderResponse($order);
|
||||
},
|
||||
$orders
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime|null $createdAfter
|
||||
* @param DateTime|null $createdBefore
|
||||
* @param int|null $limit
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOrders(
|
||||
?DateTime $createdAfter = null,
|
||||
?DateTime $createdBefore = null,
|
||||
?int $limit = null
|
||||
): array {
|
||||
$finished = false;
|
||||
$nextToken = null;
|
||||
$orders = [];
|
||||
if ($limit !== null) {
|
||||
$response = $this->sendGetOrdersRequest($createdAfter, $createdBefore, null, $limit);
|
||||
$orders = array_merge($orders, $response['payload']['orders']);
|
||||
} else {
|
||||
while (!$finished) {
|
||||
$response = $this->sendGetOrdersRequest($createdAfter, $createdBefore, $nextToken);
|
||||
if ($response['payload']['pagination']['nextToken']) {
|
||||
$nextToken = $response['payload']['pagination']['nextToken'];
|
||||
} else {
|
||||
$finished = true;
|
||||
}
|
||||
$orders = array_merge($orders, $response['payload']['orders']);
|
||||
}
|
||||
}
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
public function getOrder(string $purchaseOrderNumber): PurchaseOrder
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
"/vendor/directFulfillment/orders/v1/purchaseOrders/{$purchaseOrderNumber}"
|
||||
);
|
||||
|
||||
$payload = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
return PurchaseOrder::fromPurchaseOrderResponse($payload);
|
||||
}
|
||||
|
||||
public function submitAcknowledgement(PurchaseOrderAcknowledgement $acknowledgement): Transaction
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
'/vendor/directFulfillment/orders/v1/acknowledgements',
|
||||
[
|
||||
'json' => [
|
||||
'orderAcknowledgements' => [$acknowledgement->toArray()],
|
||||
],
|
||||
]
|
||||
);
|
||||
}catch (GuzzleTransferException $exception){
|
||||
throw new TransferException('Error while submitting acknowledgement',0, $exception);
|
||||
}
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload'];
|
||||
|
||||
return (new Transaction('purchase_order_acknowledgement'))->setExternalId($payload['transactionId']);
|
||||
}
|
||||
|
||||
protected function formatDate(DateTime $date)
|
||||
{
|
||||
return $date->format(DateTime::ISO8601);
|
||||
}
|
||||
|
||||
protected function sendGetOrdersRequest(
|
||||
?DateTime $createdAfter,
|
||||
?DateTime $createdBefore,
|
||||
?string $nextToken = null,
|
||||
?int $limit = null
|
||||
): array {
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
'/vendor/directFulfillment/orders/v1/purchaseOrders',
|
||||
[
|
||||
'query' => array_merge(
|
||||
[
|
||||
'createdAfter' => $this->formatDate($createdAfter ?? new DateTime('-7 days')),
|
||||
'createdBefore' => $this->formatDate($createdBefore ?? new DateTime()),
|
||||
'limit' => $limit ?: 100,
|
||||
'includeDetails' => true,
|
||||
'sortOrder' => 'DESC',
|
||||
],
|
||||
$nextToken !== null ? ['nextToken' => $nextToken] : []
|
||||
),
|
||||
]
|
||||
);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\ShipmentConfirmation;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\ShippingLabel;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\ShippingLabelRequest;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Transaction;
|
||||
|
||||
class ShippingService
|
||||
{
|
||||
/** @var ClientInterface */
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $purchaseOrderNumber
|
||||
*
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @return array|ShippingLabel[]
|
||||
*/
|
||||
public function getShippingLabels(string $purchaseOrderNumber): array
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
"/vendor/directFulfillment/shipping/v1/shippingLabels/{$purchaseOrderNumber}"
|
||||
);
|
||||
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload'];
|
||||
|
||||
return array_map(
|
||||
function (array $data) use ($payload) {
|
||||
$label = new ShippingLabel($payload['purchaseOrderNumber'], $data['content'], $payload['labelFormat']);
|
||||
if (isset($data['trackingNumber']) && $data['trackingNumber'] !== '') {
|
||||
$label->setTrackingNumber($data['trackingNumber']);
|
||||
}
|
||||
|
||||
return $label;
|
||||
},
|
||||
$payload['labelData']
|
||||
);
|
||||
}
|
||||
|
||||
public function submitShippingLabelRequest(ShippingLabelRequest $shippingLabelRequest): Transaction
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
'/vendor/directFulfillment/shipping/v1/shippingLabels',
|
||||
[
|
||||
'json' => [
|
||||
'shippingLabelRequests' => [$shippingLabelRequest->toArray()],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload'];
|
||||
|
||||
return (new Transaction('shipping_label_request'))->setExternalId($payload['transactionId']);
|
||||
}
|
||||
|
||||
public function submitShipmentConfirmation(ShipmentConfirmation $confirmation)
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
'/vendor/directFulfillment/shipping/v1/shipmentConfirmations',
|
||||
[
|
||||
'json' => [
|
||||
'shipmentConfirmations' => [
|
||||
$confirmation->toArray(),
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload'];
|
||||
|
||||
return (new Transaction('shipment_confirmation'))->setExternalId($payload['transactionId']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\BadResponseException;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Token;
|
||||
use Xentral\Modules\AmazonVendorDF\Exception\IssueTokenException;
|
||||
|
||||
class TokenService
|
||||
{
|
||||
/** @var ClientInterface */
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function requestToken(string $refreshToken, string $clientId, string $clientSecret)
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
'https://api.amazon.com/auth/o2/token',
|
||||
[
|
||||
'form_params' => [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $refreshToken,
|
||||
'client_id' => $clientId,
|
||||
'client_secret' => $clientSecret,
|
||||
],
|
||||
]
|
||||
);
|
||||
}catch (BadResponseException $badResponseException){
|
||||
throw IssueTokenException::fromResponse($badResponseException->getResponse());
|
||||
}
|
||||
|
||||
return Token::fromResponse($response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\AmazonVendorDF\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Xentral\Modules\AmazonVendorDF\Data\Transaction;
|
||||
|
||||
class TransactionService
|
||||
{
|
||||
/** @var ClientInterface */
|
||||
private $client;
|
||||
|
||||
public function __construct(ClientInterface $client)
|
||||
{
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function updateTransactionStatus(Transaction $transaction): Transaction
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
"/vendor/directFulfillment/transactions/v1/transactions/{$transaction->getExternalId()}"
|
||||
);
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload']['transactionStatus'];
|
||||
|
||||
$transaction->setStatus($payload['status']);
|
||||
if($transaction->hasFailed()){
|
||||
$transaction->setErrors($payload['errors']);
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
public function getTransactionByTransactionId(string $transactionId): Transaction
|
||||
{
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
"/vendor/directFulfillment/transactions/v1/transactions/{$transactionId}"
|
||||
);
|
||||
|
||||
// The response data is wrapped in a `payload` key
|
||||
$payload = json_decode($response->getBody()->getContents(), true)['payload']['transactionStatus'];
|
||||
|
||||
$transaction = new Transaction();
|
||||
$transaction->setStatus($payload['status']);
|
||||
if ($transaction->hasFailed()) {
|
||||
$transaction->setErrors($payload['errors']);
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user