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

View File

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

@ -1,14 +1,25 @@
<?php <?php
namespace IpSupply\SyncOrder\Sync; namespace IpSupply\SyncOrder\Sync;
class Buyer implements \JsonSerializable class Buyer implements \JsonSerializable
{ {
public $username = ''; public $username = null;
public $fullName = ''; public $fullName = null;
public $primaryPhone = ''; public $primaryPhone = null;
public $email = ''; 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); return get_object_vars($this);
} }

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

@ -1,21 +1,33 @@
<?php <?php
namespace IpSupply\SyncOrder\Sync; namespace IpSupply\SyncOrder\Sync;
class Detail implements \JsonSerializable { class Detail implements \JsonSerializable
{
public $productId = 0; public $productId = 0;
public $title = ''; public $title = null;
public $sku = ''; public $sku = null;
public $condition = ''; public $condition = null;
public $qty = 0; public $qty = 0;
public $price = 0; public $price = 0;
public $amount = 0; public $amount = 0;
function __construct() { function __construct(\Magento\Sales\Model\Order\Item $item)
}
public function jsonSerialize()
{ {
return get_object_vars($this); $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(): mixed
{
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 <?php
namespace IpSupply\SyncOrder\Sync; namespace IpSupply\SyncOrder\Sync;
use GuzzleHttp\Client; use GuzzleHttp\Client;
@ -11,17 +12,18 @@ final class Index
function __construct() function __construct()
{ {
$this->_config = Getter::get('config'); $this->_config = json_decode(Getter::get('config'), true);
} }
protected function _headers($headers = array()) protected function _headers($headers = array())
{ {
return array_merge([ return array_merge([
'Authorization' => 'Bearer ' . '' 'Authorization' => 'Bearer ' . null
], $headers); ], $headers);
} }
protected function _client() { protected function _client()
{
$client = new Client([ $client = new Client([
'headers' => $this->_headers(), 'headers' => $this->_headers(),
'base_url' => $this->_config['url'], 'base_url' => $this->_config['url'],
@ -30,17 +32,26 @@ final class Index
return $client; 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 = [ $payload = [
...$orderDetail->jsonSerialize(),
'url' => $this->_config['url'], 'url' => $this->_config['url'],
'source' => 'magento', '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 json_encode($payload);
return true;
} else { } else {
throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor'); throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor');
return false; return false;

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

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

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

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

View File