release data

This commit is contained in:
Kai Ton 2024-05-17 12:45:43 +00:00
parent 251d054100
commit 9b0043949a
10 changed files with 94 additions and 57 deletions

0
app/code/IpSupply/SyncOrder/Config/Getter.php Normal file → Executable file
View File

0
app/code/IpSupply/SyncOrder/Config/Setter.php Normal file → Executable file
View File

View File

@ -30,6 +30,7 @@ class CheckOrderStatus implements ObserverInterface
$currentContent .= date("Y-m-d H:i:s") . ':';
$currentContent .= $content;
$currentContent .= "\n";
$currentContent = str_replace(array("\r", "\n"), '', 500);
file_put_contents($file, $currentContent);
}
@ -37,9 +38,9 @@ class CheckOrderStatus implements ObserverInterface
{
$order = $observer->getEvent()->getOrder();
$order = [
'customer_id' => $order->getCustomerId(),
'status' => $order->getStatus(),
];
$sync = new \IpSupply\SyncOrder\Sync\Index;
$content = $sync->syncOrder($order);
$this->_saveLog($content);
}
}

View File

21
app/code/IpSupply/SyncOrder/Sync/Buyer.php Normal file → Executable file
View File

@ -1,14 +1,25 @@
<?php
namespace IpSupply\SyncOrder\Sync;
class Buyer implements \JsonSerializable
{
public $username = '';
public $fullName = '';
public $primaryPhone = '';
public $email = '';
public $username = null;
public $fullName = null;
public $primaryPhone = null;
public $email = null;
public function jsonSerialize()
function __construct(\Magento\Sales\Model\Order $order)
{
$this->username = $order->getCustomerEmail() ?? null;
$this->fullName = $order->getCustomerName() ?? null;
$this->primaryPhone = $order->getBillingAddress()?->getTelephone() ?? $order->getShippingAddress()->getTelephone();
$this->email =
$order->getBillingAddress()?->getEmail()
?? $order->getShippingAddress()->getEmail();
}
public function jsonSerialize(): mixed
{
return get_object_vars($this);
}

26
app/code/IpSupply/SyncOrder/Sync/Detail.php Normal file → Executable file
View File

@ -1,21 +1,33 @@
<?php
namespace IpSupply\SyncOrder\Sync;
class Detail implements \JsonSerializable {
class Detail implements \JsonSerializable
{
public $productId = 0;
public $title = '';
public $sku = '';
public $condition = '';
public $title = null;
public $sku = null;
public $condition = null;
public $qty = 0;
public $price = 0;
public $amount = 0;
function __construct() {
function __construct(\Magento\Sales\Model\Order\Item $item)
{
$this->productId = $item->getProductId();
$this->title = $item->getProduct()->getName();
$this->sku = $item->getSku();
$this->qty = $item->getQtyOrdered();
$this->price = $item->getPrice();
$this->amount = $item->getQtyOrdered() * $item->getPrice();
if ($condition = $item->getProductOptionByCode()) {
$this->condition = $condition['attributes_info'][0]['value'] ?? null;
}
}
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return get_object_vars($this);
return (array) get_object_vars($this);
}
}

27
app/code/IpSupply/SyncOrder/Sync/Index.php Normal file → Executable file
View File

@ -1,4 +1,5 @@
<?php
namespace IpSupply\SyncOrder\Sync;
use GuzzleHttp\Client;
@ -11,17 +12,18 @@ final class Index
function __construct()
{
$this->_config = Getter::get('config');
$this->_config = json_decode(Getter::get('config'), true);
}
protected function _headers($headers = array())
{
return array_merge([
'Authorization' => 'Bearer ' . ''
'Authorization' => 'Bearer ' . null
], $headers);
}
protected function _client() {
protected function _client()
{
$client = new Client([
'headers' => $this->_headers(),
'base_url' => $this->_config['url'],
@ -30,17 +32,26 @@ final class Index
return $client;
}
public function syncOrder($order): bool
public function syncOrder($order)
{
if ($order instanceof \Magento\Sales\Model\Order\Interceptor) {
if ($order instanceof \Magento\Sales\Model\Order) {
$orderDetail = new Order($order);
$shippingAddress = new ShippingAddress($order->getShippingAddress());
$buyer = new Buyer($order);
$payload = [
...$orderDetail->jsonSerialize(),
'url' => $this->_config['url'],
'source' => 'magento',
...(new Order($order))->jsonSerialize()
'details' => [],
'buyer' => $buyer->jsonSerialize(),
'shippingAddress' => $shippingAddress->jsonSerialize(),
];
foreach ($order->getAllVisibleItems() as $item) {
$payload['details'][] = (new Detail($item))->jsonSerialize();
}
return true;
return json_encode($payload);
} else {
throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor');
return false;

21
app/code/IpSupply/SyncOrder/Sync/Order.php Normal file → Executable file
View File

@ -1,28 +1,29 @@
<?php
namespace IpSupply\SyncOrder\Sync;
class Order implements \JsonSerializable
{
public $orderId = 0;
public $statusPayment = '';
public $orderDate = '';
public $subTotal = '';
public $total = '';
public $shippingPrice = '';
public $currency = '';
public $statusPayment = null;
public $orderDate = null;
public $subTotal = null;
public $total = null;
public $shippingPrice = null;
public $currency = null;
function __construct(\Magento\Sales\Model\Order\Interceptor $order)
function __construct(\Magento\Sales\Model\Order $order)
{
$this->orderId = $order->getId();
$this->statusPayment = $order->getStatus();
$this->orderDate = $order->getUpdatedAt();
$this->subTotal = $order->getSubtotal();
$this->total = $order->getTotalInvoiced();
$this->shippingPrice = $order->getShippingInvoiced();
$this->total = $order->getGrandTotal();
$this->shippingPrice = $order->getShippingAmount();
$this->currency = $order->getOrderCurrencyCode();
}
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return get_object_vars($this);
}

47
app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php Normal file → Executable file
View File

@ -1,37 +1,38 @@
<?php
namespace IpSupply\SyncOrder\Sync;
class ShippingAddress implements \JsonSerializable
{
public $name = '';
public $street1 = '';
public $street2 = '';
public $name = null;
public $street1 = null;
public $street2 = null;
public $cityName = 0;
public $stateOrProvince = '';
public $country = '';
public $countryName = '';
public $phone = '';
public $email = '';
public $postalCode = '';
public $stateOrProvince = null;
public $country = null;
public $countryName = null;
public $phone = null;
public $email = null;
public $postalCode = null;
function __construct($address)
function __construct(\Magento\Sales\Model\Order\Address|null $address)
{
if ($address instanceof \Magento\Sales\Model\Order\Address) {
$this->email = $address->getEmail();
$this->name = $address->getName();
$this->street1 = $address->getStreetLine(1);
$this->street2 = $address->getStreetLine(2);
$this->country = $address->getCountryId();
$this->cityName = $address->getCity();
$this->phone = $address->getTelephone();
$this->postalCode = $address->getPostcode();
$this->countryName = $address->getCountryId();
} else {
// do something else
if (!$address) {
return;
}
$this->email = $address->getEmail();
$this->name = $address->getName();
$this->street1 = $address->getStreetLine(1);
$this->street2 = $address->getStreetLine(2);
$this->country = $address->getCountryId();
$this->cityName = $address->getCity();
$this->stateOrProvince = $address->getRegion();
$this->phone = $address->getTelephone();
$this->postalCode = $address->getPostcode();
$this->countryName = $address->getCountryId();
}
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return get_object_vars($this);
}

View File