Skip to content

Commit

Permalink
Merge pull request #124 from alma/feature/ecom-1781-php-client-update…
Browse files Browse the repository at this point in the history
…-php-client-to-be-able-to-update-orders

feature: refactor order and add updateTracking put endpoint
  • Loading branch information
Francois-Gomis authored Jul 16, 2024
2 parents d808828 + 0f96923 commit 66ed59c
Show file tree
Hide file tree
Showing 9 changed files with 752 additions and 280 deletions.
37 changes: 28 additions & 9 deletions src/Endpoints/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ public function update($orderId, $orderData)
return new Order($response->json);
}

/**
* @param string $orderId
* @param string|null $carrier
* @param string|null $trackingNumber
* @param string|null $trackingUrl
* @return Order
* @throws AlmaException
*/
public function updateTracking($orderId, $carrier = null, $trackingNumber = null, $trackingUrl = null)
{
$trackingData = array_filter([
'carrier' => $carrier,
'tracking_number' => $trackingNumber,
'tracking_url' => $trackingUrl
]);
$response = $this->request(self::ORDERS_PATH . "/{$orderId}")->setRequestBody($trackingData)->put();
return new Order($response->json);
}

/**
* @param int $limit
* @param string|null $startingAfter
Expand Down Expand Up @@ -91,7 +110,7 @@ public function fetchAll($limit = 20, $startingAfter = null, $filters = array())
$response = $this->request(self::ORDERS_PATH)->setQueryParams($args)->get();
return new PaginatedResults(
$response,
function($startingAfter) use ($limit, $filters) {
function ($startingAfter) use ($limit, $filters) {
return $this->fetchAll($limit, $startingAfter, $filters);
}
);
Expand Down Expand Up @@ -123,11 +142,11 @@ public function sendStatus($orderExternalId, $orderData = array())

try {
$response = $this->request(self::ORDERS_PATH_V2 . "/{$orderExternalId}/status")->setRequestBody(array(
'status' => $label,
'status' => $label,
'is_shipped' => $orderData['is_shipped'],
))->post();
}catch (AlmaException $e) {
$this->logger->error('Error sending status');
} catch (AlmaException $e) {
$this->logger->error('Error sending status');
throw new RequestException('Error sending status', $e);
}

Expand All @@ -143,21 +162,21 @@ public function sendStatus($orderExternalId, $orderData = array())
*/
public function validateStatusData($orderData = array())
{
if(count($orderData) == 0) {
if (count($orderData) == 0) {
throw new ParametersException('Missing in the required parameters (status, is_shipped) when calling orders.sendStatus');
}

try {
$this->arrayUtils->checkMandatoryKeys(['status', 'is_shipped'], $orderData);
} catch (MissingKeyException $e ) {
throw new ParametersException('Error in the required parameters (status, is_shipped) when calling orders.sendStatus',0, $e);
} catch (MissingKeyException $e) {
throw new ParametersException('Error in the required parameters (status, is_shipped) when calling orders.sendStatus', 0, $e);
}

if(!is_bool($orderData['is_shipped'])) {
if (!is_bool($orderData['is_shipped'])) {
throw new ParametersException('Parameter "is_shipped" must be a boolean');
}

if(!$orderData['status']) {
if (!$orderData['status']) {
throw new ParametersException('Missing the required parameter "status" when calling orders.sendStatus');
}
}
Expand Down
192 changes: 187 additions & 5 deletions src/Entities/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,199 @@

namespace Alma\API\Entities;

class Order extends Base
class Order
{
/** @var string ID of the Payment owning this Order */
/** @var string | null Order carrier */
private $carrier;

/** @var string | null Order carrier tracking number */
private $trackingNumber;

/** @var string | null Order carrier tracking URL */
private $trackingUrl;

/** @var string ID of the Payment owning this Order
* @deprecated
*/
public $payment;

/** @var string Order reference from the merchant's platform */
/** @var string ID of the Payment owning this Order */
private $paymentId;

/**
* @var string | null Order reference from the merchant's platform
* @deprecated
*/
public $merchant_reference;

/** @var string URL to the merchant's backoffice for that Order */
/**
* @var string | null Order reference from the merchant's platform
*/
private $merchantReference;

/**
* @var string | null URL to the merchant's backoffice for that Order
* @deprecated
*/
public $merchant_url;

/** @var array Free-form custom data */
/** @var string | null URL to the merchant's backoffice for that Order */
private $merchantUrl;

/**
* @var array Free-form custom data
* @deprecated
* */
public $data;

/** @var array Free-form custom data */
private $orderData;

/**
* @var string | null Order comment
*/
private $comment;
/**
* @var int Order creation timestamp
*/
private $createdAt;

/**
* @var string | null Customer URL
*/
private $customerUrl;

/**
* @var string Order external ID
*/
private $externalId;

/**
* @var string Order updated timestamp
*/
private $updatedAt;
/**
* @var string Order ID
* @deprecated
*/
public $id;


public function __construct($orderDataArray)
{
$this->carrier = $orderDataArray['carrier'];
$this->comment = $orderDataArray['comment'];
$this->createdAt = $orderDataArray['created'];
$this->customerUrl = $orderDataArray['customer_url'];
$this->data = $orderDataArray['data'];
$this->orderData = $orderDataArray['data'];
$this->id = $orderDataArray['id'];
$this->externalId = $orderDataArray['id'];
$this->merchant_reference = $orderDataArray['merchant_reference'];
$this->merchantReference = $orderDataArray['merchant_reference'];
$this->merchant_url = $orderDataArray['merchant_url'];
$this->merchantUrl = $orderDataArray['merchant_url'];
$this->payment = $orderDataArray['payment'];
$this->paymentId = $orderDataArray['payment'];
$this->trackingNumber = $orderDataArray['tracking_number'];
$this->trackingUrl = $orderDataArray['tracking_url'];
$this->updatedAt = isset($orderDataArray['updated']) ? $orderDataArray['updated'] : null;
}

/**
* @return string|null
*/
public function getCarrier()
{
return $this->carrier;
}

/**
* @return string|null
*/
public function getTrackingNumber()
{
return $this->trackingNumber;
}

/**
* @return string|null
*/
public function getTrackingUrl()
{
return $this->trackingUrl;
}

/**
* @return string
*/
public function getPaymentId()
{
return $this->paymentId;
}

/**
* @return string|null
*/
public function getMerchantReference()
{
return $this->merchantReference;
}

/**
* @return string|null
*/
public function getMerchantUrl()
{
return $this->merchantUrl;
}

/**
* @return string
*/
public function getExternalId()
{
return $this->externalId;
}

/**
* @return string|null
*/
public function getComment()
{
return $this->comment;
}

/**
* @return int
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* @return string|null
*/
public function getCustomerUrl()
{
return $this->customerUrl;
}

/**
* @return int
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
* @return array
*/
public function getOrderData()
{
return $this->orderData;
}

}
59 changes: 59 additions & 0 deletions tests/Integration/Endpoints/OrdersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Alma\API\Tests\Integration\Endpoints;

use Alma\API\Entities\Order;
use Alma\API\Tests\Integration\TestHelpers\ClientTestHelper;
use Alma\API\Tests\Integration\TestHelpers\PaymentTestHelper;
use PHPUnit\Framework\TestCase;

class OrdersTest extends TestCase
{
protected static $almaClient;
protected static $payment;

public static function setUpBeforeClass(): void
{
OrdersTest::$almaClient = ClientTestHelper::getAlmaClient();
OrdersTest::$payment = PaymentTestHelper::createPayment(26500, 3);
}

public function testCanCreateANewOrder()
{
$payment = OrdersTest::$payment;
$order = $payment->orders[0];
$this->assertInstanceOf(Order::class, $order);
$this->assertEquals('ABC-123', $order->getMerchantReference());
$newOrder = OrdersTest::$almaClient->payments->addOrder($payment->id, ['merchant_reference' => 'ABC-123-NEW']);
$this->assertInstanceOf(Order::class, $newOrder);
$this->assertEquals('ABC-123-NEW', $newOrder->getMerchantReference());
}

public function testCanUpdateOrderTracking()
{
$payment = OrdersTest::$payment;
$order = $payment->orders[0];
$this->assertInstanceOf(Order::class, $order);
$this->assertNull($order->getCarrier());
$this->assertNull($order->getTrackingUrl());
$this->assertNull($order->getTrackingNumber());

$updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), null,null , 'https://tracking.com');
$this->assertInstanceOf(Order::class, $updatedOrder);
$this->assertNull($order->getCarrier());
$this->assertNull($updatedOrder->getTrackingNumber());
$this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl());

$updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'UPS');
$this->assertInstanceOf(Order::class, $updatedOrder);
$this->assertEquals('UPS', $updatedOrder->getCarrier());
$this->assertNull($updatedOrder->getTrackingNumber());
$this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl());

$updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'LAPOSTE','123456789' , 'https://laposte.com');
$this->assertInstanceOf(Order::class, $updatedOrder);
$this->assertEquals('LAPOSTE', $updatedOrder->getCarrier());
$this->assertEquals('123456789', $updatedOrder->getTrackingNumber());
$this->assertEquals('https://laposte.com', $updatedOrder->getTrackingUrl());
}
}
Loading

0 comments on commit 66ed59c

Please sign in to comment.