Allow for easily sending multiple order items

This allows for the sending of multiple items in a single order, by
adding a new `OrderItem` class and moving all of the order item specific
fields from the `Order` to an individual item.

These can then be assigned to an order using the `withOrderItems()`
method as an array of order items.

Fixes #19
This commit is contained in:
Oliver Davies 2020-06-08 19:33:49 +01:00
parent 0ed99797f5
commit ae766763b2
9 changed files with 86 additions and 53 deletions

View file

@ -8,6 +8,7 @@ use Opdavies\Glassboxx\Enum\InteractionType;
use Opdavies\Glassboxx\Traits\UsesAuthTokenTrait;
use Opdavies\Glassboxx\Traits\UsesCreatedAtTrait;
use Opdavies\Glassboxx\ValueObject\OrderInterface;
use Opdavies\Glassboxx\ValueObject\OrderItem;
use RuntimeException;
class OrderRequest extends AbstractRequest implements OrderRequestInterface
@ -18,6 +19,9 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
/** @var OrderInterface|null */
private $order;
/** @var OrderItem[] */
private $orderItems = [];
public function forOrder(OrderInterface $order): AbstractRequest
{
$this->order = $order;
@ -25,6 +29,13 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
return $this;
}
public function withOrderItems(array $orderItems): AbstractRequest
{
$this->orderItems = $orderItems;
return $this;
}
public function execute(): string
{
if (!$this->config) {
@ -39,24 +50,24 @@ class OrderRequest extends AbstractRequest implements OrderRequestInterface
throw new RuntimeException('There is no customer');
}
$body = [
'items' => [
[
'created_at' => $this->getCreatedAtDate(),
'currency_code' => $this->order->getCurrencyCode(),
'customer_email' => $this->order->getCustomer()->getEmailAddress(),
'customer_firstname' => $this->order->getCustomer()->getFirstName(),
'customer_lastname' => $this->order->getCustomer()->getLastName(),
'discount_amount' => 0,
'duration_for_loan' => 0,
'hostname' => $this->config->getVendorId(),
'original_order_number' => $this->order->getOrderNumber(),
'price_incl_tax' => $this->order->getPrice(),
'sku' => $this->order->getSku(),
'type_of_interaction' => InteractionType::PURCHASE,
],
],
];
$body = [];
foreach ($this->orderItems as $orderItem) {
$body['items'][] = [
'created_at' => $this->getCreatedAtDate(),
'currency_code' => $this->order->getCurrencyCode(),
'customer_email' => $this->order->getCustomer()->getEmailAddress(),
'customer_firstname' => $this->order->getCustomer()->getFirstName(),
'customer_lastname' => $this->order->getCustomer()->getLastName(),
'discount_amount' => 0,
'duration_for_loan' => 0,
'hostname' => $this->config->getVendorId(),
'original_order_number' => $this->order->getOrderNumber(),
'price_incl_tax' => $orderItem->getPrice(),
'sku' => $orderItem->getSku(),
'type_of_interaction' => InteractionType::PURCHASE,
];
}
$response = $this->client->request(
'POST',