<?php declare(strict_types=1);
namespace AMG\IamapprovedPlugin\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
class CheckoutCompleteSubscriber implements EventSubscriberInterface
{
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// Username + Password
curl_setopt($curl, CURLOPT_USERPWD, "shop:c4a7b55e-b40d-4dc5-b7ec-c07b019b93d7");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
];
}
public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
/* $event->getContext()->addAssociation('manufacturer.media');*/
error_log("Checkout 1", 0);
//Datenabruf zu Bestellung und Kunde
$order = $event->getOrder();
$customer = $order->getOrderCustomer();
$addresses = $order->getAddresses();
//Bestelldaten
$order_number = $order->getOrderNumber();
$order_date = $order->getOrderDateTime();
$amount_total = $order->getAmountTotal(); // not needed
$amount_net = $order->getAmountNet(); // not needed
$order_id = $order->getId();
$products = $order->getLineItems()->filterByOrderId($order_id); //->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE)->getReferenceIds();
//Kundendaten
$first_name = $customer->getFirstName();
$last_name = $customer->getLastName();
$salutation = $customer->getSalutation()->getDisplayName();
$customer_number = $customer->getCustomerNumber();
$company = $customer->getCompany();
$email = $customer->getEmail();
//$ActiveShippingAddress = $customer->getActiveShippingAddress();
// error_log("Checkout 2", 0);
//
// //// PARTS
// $parts = array();
// foreach($products as $product ){
//
//
// error_log(json_encode($product));
// /* $customFields = $product->getProduct()->getCustomFields();
// // loop through each product's custom fields
// foreach($customFields as $name => $value) {
// if ($name !== 'custom_linked_product' || empty($value)) {
// return;
// }
//
// // resolve the $value here
// }
// $product->setCustomFields($customFields);*/
//
// /*$manufacturerId = $product->getPayload();*/
// array_push($parts, array(
// "id"=>$product->getIdentifier(),
// "type"=>$product->getType(),
// "quantity"=>$product->getQuantity(),
// "label"=>$product->getLabel(),
// "weight"=>0.2,
// "create_at"=>$product->getCreatedAt()->format("d-M-Y h:m:s"),
// /*"article_number"=>$product->getPayload()["productNumber"],*/
// "order_number"=>$order_number,
// "payload"=>$product->getPayload()
// ));
// }
// /// END PARTS
//
// // Shipping address
// $address=$addresses->last();
//
// $country = array(
// "name"=>$address->getCountry()->getName(),
// "iso"=>$address->getCountry()->getIso(),
// "iso3"=>$address->getCountry()->getIso3()
// );
//
// $order_address = array(
// "first_name"=>$address->getFirstName(),
// "last_name"=>$address->getLastName(),
// "street"=>$address->getStreet(),
// "zip"=>$address->getZipcode(),
// "city"=>$address->getCity(),
// "company"=>$address->getCompany(),
// "department"=>$address->getDepartment(),
// "country"=>$country
// );
//
// $customer_obj = array(
// "id"=>$customer->getCustomerId(),
// "first_name"=>$first_name,
// "last_name"=>$last_name,
// "salutation"=>$salutation,
// "customer_number"=>$customer_number,
// "company"=>$company,
// "email"=>$email,
// "address"=>$order_address
// );
// // Order-JSON
$arr = array(
"id"=>$order_id,
"number"=>$order_number,
"date"=>$order_date->format("d-M-Y"), // h:m:s
// "partArray"=>$parts,
// "customer"=>$customer_obj
);
//json_encode($arr); host.docker.internal
//Produktversion
// $this->CallAPI("POST", "https://amcc.iam-approved.com/Controller-2.4.5/api/v1/order", json_encode($arr));
// $this->CallAPI("POST", "https://amcc.iam-approved.com/api/v1/order", json_encode($arr));
// $this->CallAPI("POST", "http://localhost:2701/api/v1/order", json_encode($arr));
$this->CallAPI("POST", "https://iam-controller.additive-marking.de/api/test/v1/order", json_encode($arr));
error_log(json_encode($arr), 0);
//Testcall
//$this->CallAPI("GET", "https://amcc.iam-approved.com/api/test", json_encode($output)); //testAll
//error_log($output, 0);
// error_log("Hallo Welt! Ich habe fertig!");
}
}