mirror of
https://github.com/opdavies/glassboxx-sdk-php.git
synced 2025-02-17 22:50:48 +00:00
parent
e83c6e59e4
commit
532dfd43a1
10 changed files with 342 additions and 8 deletions
9
src/Glassboxx/Enum/InteractionType.php
Normal file
9
src/Glassboxx/Enum/InteractionType.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\Glassboxx\Enum;
|
||||
|
||||
final class InteractionType
|
||||
{
|
||||
public const LOAN = 'loan';
|
||||
public const PURCHASE = 'purchase';
|
||||
}
|
62
src/Glassboxx/Request/OrderRequest.php
Normal file
62
src/Glassboxx/Request/OrderRequest.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Opdavies\Glassboxx\Request;
|
||||
|
||||
use Opdavies\Glassboxx\Enum\InteractionType;
|
||||
use Opdavies\Glassboxx\Traits\UsesAuthTokenTrait;
|
||||
use Opdavies\Glassboxx\Traits\UsesCreatedAtTrait;
|
||||
use Opdavies\Glassboxx\ValueObject\OrderInterface;
|
||||
|
||||
class OrderRequest extends AbstractRequest implements OrderRequestInterface
|
||||
{
|
||||
use UsesCreatedAtTrait;
|
||||
use UsesAuthTokenTrait;
|
||||
|
||||
/** @var OrderInterface */
|
||||
private $order;
|
||||
|
||||
public function forOrder(OrderInterface $order): AbstractRequest
|
||||
{
|
||||
$this->order = $order;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function execute(): string
|
||||
{
|
||||
$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,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->client->request(
|
||||
'POST',
|
||||
self::BASE_URL . self::ENDPOINT,
|
||||
[
|
||||
'auth_bearer' => $this->authToken,
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'body' => json_encode($body),
|
||||
]
|
||||
);
|
||||
|
||||
return $response->getContent(false);
|
||||
}
|
||||
}
|
16
src/Glassboxx/Request/OrderRequestInterface.php
Normal file
16
src/Glassboxx/Request/OrderRequestInterface.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Opdavies\Glassboxx\Request;
|
||||
|
||||
use Opdavies\Glassboxx\ValueObject\OrderInterface;
|
||||
|
||||
interface OrderRequestInterface
|
||||
{
|
||||
public const ENDPOINT = '/glassboxxorder/toglassboxx';
|
||||
|
||||
public function forOrder(OrderInterface $order): AbstractRequest;
|
||||
|
||||
public function execute(): string;
|
||||
}
|
25
src/Glassboxx/Traits/UsesCreatedAtTrait.php
Normal file
25
src/Glassboxx/Traits/UsesCreatedAtTrait.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Opdavies\Glassboxx\Traits;
|
||||
|
||||
use DateTime;
|
||||
|
||||
trait UsesCreatedAtTrait
|
||||
{
|
||||
/** @var string */
|
||||
private $time = 'now';
|
||||
|
||||
public function getCreatedAtDate(): string
|
||||
{
|
||||
return (new DateTime($this->time))->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
public function setCreatedDate(string $dateString): self
|
||||
{
|
||||
$this->time = $dateString;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
62
src/Glassboxx/ValueObject/Order.php
Normal file
62
src/Glassboxx/ValueObject/Order.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Opdavies\Glassboxx\ValueObject;
|
||||
|
||||
final class Order implements OrderInterface
|
||||
{
|
||||
/** @var CustomerInterface */
|
||||
private $customer;
|
||||
|
||||
/** @var string */
|
||||
private $sku;
|
||||
|
||||
/** @var string */
|
||||
private $orderNumber;
|
||||
|
||||
/** @var string */
|
||||
private $currencyCode;
|
||||
|
||||
/** @var float */
|
||||
private $price;
|
||||
|
||||
public function __construct(
|
||||
CustomerInterface $customer,
|
||||
string $sku,
|
||||
string $orderNumber,
|
||||
string $currencyCode,
|
||||
float $price
|
||||
) {
|
||||
$this->customer = $customer;
|
||||
$this->sku = $sku;
|
||||
$this->orderNumber = $orderNumber;
|
||||
$this->currencyCode = $currencyCode;
|
||||
$this->price = $price;
|
||||
}
|
||||
|
||||
public function getCurrencyCode(): string
|
||||
{
|
||||
return $this->currencyCode;
|
||||
}
|
||||
|
||||
public function getCustomer(): CustomerInterface
|
||||
{
|
||||
return $this->customer;
|
||||
}
|
||||
|
||||
public function getOrderNumber(): string
|
||||
{
|
||||
return $this->orderNumber;
|
||||
}
|
||||
|
||||
public function getPrice(): float
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
|
||||
public function getSku(): string
|
||||
{
|
||||
return $this->sku;
|
||||
}
|
||||
}
|
24
src/Glassboxx/ValueObject/OrderInterface.php
Normal file
24
src/Glassboxx/ValueObject/OrderInterface.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Opdavies\Glassboxx\ValueObject;
|
||||
|
||||
interface OrderInterface
|
||||
{
|
||||
public function __construct(
|
||||
CustomerInterface $customer,
|
||||
string $sku,
|
||||
string $orderNumber,
|
||||
string $currencyCode,
|
||||
float $price
|
||||
);
|
||||
|
||||
public function getCurrencyCode(): string;
|
||||
|
||||
public function getCustomer(): CustomerInterface;
|
||||
|
||||
public function getOrderNumber(): string;
|
||||
|
||||
public function getPrice(): float;
|
||||
|
||||
public function getSku(): string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue