custom/plugins/AMGIamapprovedPlugin/src/Subscriber/CheckoutCompleteSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace AMG\IamapprovedPlugin\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. class CheckoutCompleteSubscriber implements EventSubscriberInterface
  10. {
  11.     function CallAPI($method$url$data false)
  12.     {
  13.         $curl curl_init();
  14.         switch ($method)
  15.         {
  16.             case "POST":
  17.                 curl_setopt($curlCURLOPT_POST1);
  18.                 if ($data)
  19.                     curl_setopt($curlCURLOPT_POSTFIELDS$data);
  20.                 break;
  21.             case "PUT":
  22.                 curl_setopt($curlCURLOPT_PUT1);
  23.                 break;
  24.             default:
  25.                 if ($data)
  26.                     $url sprintf("%s?%s"$urlhttp_build_query($data));
  27.         }
  28.         // Optional Authentication:
  29.         curl_setopt($curlCURLOPT_HTTPAUTHCURLAUTH_BASIC);
  30.         // Username + Password
  31.         curl_setopt($curlCURLOPT_USERPWD"shop:c4a7b55e-b40d-4dc5-b7ec-c07b019b93d7");
  32.         curl_setopt($curlCURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  33.         curl_setopt($curlCURLOPT_URL$url);
  34.         curl_setopt($curlCURLOPT_RETURNTRANSFER1);
  35.         $result curl_exec($curl);
  36.         curl_close($curl);
  37.         return $result;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  43.         ];
  44.     }
  45.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  46.     {
  47.         /*        $event->getContext()->addAssociation('manufacturer.media');*/
  48.         error_log("Checkout 1"0);
  49.         //Datenabruf zu Bestellung und Kunde
  50.         $order $event->getOrder();
  51.         $customer $order->getOrderCustomer();
  52.         $addresses $order->getAddresses();
  53.         //Bestelldaten
  54.         $order_number $order->getOrderNumber();
  55.         $order_date $order->getOrderDateTime();
  56.         $amount_total $order->getAmountTotal();                   // not needed
  57.         $amount_net $order->getAmountNet();                       // not needed
  58.         $order_id $order->getId();
  59.         $products $order->getLineItems()->filterByOrderId($order_id); //->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE)->getReferenceIds();
  60.         //Kundendaten
  61.         $first_name $customer->getFirstName();
  62.         $last_name $customer->getLastName();
  63.         $salutation $customer->getSalutation()->getDisplayName();
  64.         $customer_number $customer->getCustomerNumber();
  65.         $company $customer->getCompany();
  66.         $email $customer->getEmail();
  67.         //$ActiveShippingAddress = $customer->getActiveShippingAddress();
  68. //         error_log("Checkout 2", 0);
  69. //
  70. //         //// PARTS
  71. //         $parts = array();
  72. //         foreach($products as $product ){
  73. //
  74. //
  75. //             error_log(json_encode($product));
  76. //             /*            $customFields = $product->getProduct()->getCustomFields();
  77. //                         // loop through each product's custom fields
  78. //                         foreach($customFields as $name => $value) {
  79. //                             if ($name !== 'custom_linked_product' || empty($value)) {
  80. //                                return;
  81. //                             }
  82. //
  83. //                             // resolve the $value here
  84. //                         }
  85. //                         $product->setCustomFields($customFields);*/
  86. //
  87. //             /*$manufacturerId = $product->getPayload();*/
  88. //             array_push($parts, array(
  89. //                 "id"=>$product->getIdentifier(),
  90. //                 "type"=>$product->getType(),
  91. //                 "quantity"=>$product->getQuantity(),
  92. //                 "label"=>$product->getLabel(),
  93. //                 "weight"=>0.2,
  94. //                 "create_at"=>$product->getCreatedAt()->format("d-M-Y h:m:s"),
  95. //                 /*"article_number"=>$product->getPayload()["productNumber"],*/
  96. //                 "order_number"=>$order_number,
  97. //                 "payload"=>$product->getPayload()
  98. //             ));
  99. //         }
  100. //         /// END PARTS
  101. //
  102. //         // Shipping address
  103. //         $address=$addresses->last();
  104. //
  105. //         $country = array(
  106. //             "name"=>$address->getCountry()->getName(),
  107. //             "iso"=>$address->getCountry()->getIso(),
  108. //             "iso3"=>$address->getCountry()->getIso3()
  109. //         );
  110. //
  111. //         $order_address = array(
  112. //             "first_name"=>$address->getFirstName(),
  113. //             "last_name"=>$address->getLastName(),
  114. //             "street"=>$address->getStreet(),
  115. //             "zip"=>$address->getZipcode(),
  116. //             "city"=>$address->getCity(),
  117. //             "company"=>$address->getCompany(),
  118. //             "department"=>$address->getDepartment(),
  119. //             "country"=>$country
  120. //         );
  121. //
  122. //         $customer_obj = array(
  123. //             "id"=>$customer->getCustomerId(),
  124. //             "first_name"=>$first_name,
  125. //             "last_name"=>$last_name,
  126. //             "salutation"=>$salutation,
  127. //             "customer_number"=>$customer_number,
  128. //             "company"=>$company,
  129. //             "email"=>$email,
  130. //             "address"=>$order_address
  131. //         );
  132. //         // Order-JSON
  133.         $arr = array(
  134.             "id"=>$order_id,
  135.             "number"=>$order_number,
  136.             "date"=>$order_date->format("d-M-Y"), // h:m:s
  137. //             "partArray"=>$parts,
  138. //             "customer"=>$customer_obj
  139.         );
  140.         //json_encode($arr); host.docker.internal
  141.         //Produktversion
  142.        // $this->CallAPI("POST", "https://amcc.iam-approved.com/Controller-2.4.5/api/v1/order", json_encode($arr));
  143. //         $this->CallAPI("POST", "https://amcc.iam-approved.com/api/v1/order", json_encode($arr));
  144. //         $this->CallAPI("POST", "http://localhost:2701/api/v1/order", json_encode($arr));
  145.         $this->CallAPI("POST""https://iam-controller.additive-marking.de/api/test/v1/order"json_encode($arr));
  146.         error_log(json_encode($arr), 0);
  147.         //Testcall
  148.         //$this->CallAPI("GET", "https://amcc.iam-approved.com/api/test", json_encode($output));       //testAll
  149.         //error_log($output, 0);
  150. //         error_log("Hallo Welt! Ich habe fertig!");
  151.     }
  152. }