Initial commit

This commit is contained in:
2025-04-12 10:34:26 -04:00
parent 06ffcc1400
commit f094a1f705
11 changed files with 315 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
<?php
namespace SoundPress\WHIOrders\Observer\Checkout;
use SoundPress\WHIOrders\Helper\ConfigHelper;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use GuzzleHttp\ClientFactory;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Log\LoggerInterface;
class SendWHIOrder implements ObserverInterface
{
protected $clientFactory;
protected $configHelper;
protected $logger;
public function __construct(
ClientFactory $clientFactory,
ConfigHelper $configHelper,
LoggerInterface $logger
) {
$this->clientFactory = $clientFactory;
$this->configHelper = $configHelper;
$this->logger = $logger;
}
function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$shippingAddress = $order->getShippingAddress();
$billingAddress = $order->getBillingAddress();
$whiOrder = [
'metadata' => [
'platform' => 'Magento',
'plaformOrderId' => $order->getId(),
],
'billingAddress' => [
'firstName' => $billingAddress->getFirstname(),
'lastName' => $billingAddress->getLastname(),
'address1' => $billingAddress->getStreet()[0],
'address2' => count($billingAddress->getStreet()) > 1 ? $billingAddress->getStreet()[1] : null,
'city' => $billingAddress->getCity(),
'state' => $billingAddress->getRegion(),
'zip' => $billingAddress->getPostCode(),
'email' => $billingAddress->getEmail(),
'phone' => $billingAddress->getTelephone()
],
'shippingAddress' => [
'firstName' => $shippingAddress->getFirstname(),
'lastName' => $shippingAddress->getLastname(),
'address1' => $shippingAddress->getStreet()[0],
'address2' => count($shippingAddress->getStreet()) > 1 ? $shippingAddress->getStreet()[1] : null,
'city' => $shippingAddress->getCity(),
'state' => $shippingAddress->getRegion(),
'zip' => $shippingAddress->getPostCode(),
],
'amounts' => [
'shipping' => $order->getShippingAmount(),
'tax' => $order->getTaxAmount(),
'subtotal' => $order->getSubtotal(),
'total' => $order->getGrandTotal(),
],
'parts' => []
];
foreach ($order->getAllItems() as $item) {
$part = [
'sku' => $item->getSku(),
'price' => $item->getPrice(),
'salesTax' => $item->getTaxAmount(),
'discount' => $item->getDiscountAmount(),
'quantity' => $item->getQtyOrdered(),
];
array_push($whiOrder['parts'], $part);
}
$baseUrl = $this->configHelper->getBaseUrl();
$token = $this->configHelper->getApiToken();
$client = $this->clientFactory->create(['config' => [
'base_uri' => $baseUrl,
'headers' => [
'Content-type' => 'application/json',
'Authorization' => "Bearer $token"
]
]]);
try {
$client->request('POST', 'whi/orders', [
'body' => json_encode($whiOrder)
]);
} catch (GuzzleException $exception) {
$this->logger->error('Error sending ' . $order->getId() . ' to WHI for processing. Server returned HTTP response code ' . $exception->getCode());
}
}
}