Beispiel #1
0
 public function processGetOrders(ApiCallEvent $event)
 {
     $api = $event->getApi();
     $response = $api->getResponse();
     if ($response->isInError()) {
         $this->logger->error($response->getFormattedError());
         throw new BadResponseException($response->getFormattedError());
     }
     $dispatcher = $event->getDispatcher();
     $orders = $response->getGroup("Orders");
     $validOrders = [];
     /** @var \Thelia\Model\Country $country */
     $shopCountry = CountryQuery::create()->findOneByShopCountry(true);
     $calculator = new Calculator();
     /**
      * Check if there is only one order: reformat the array in that case
      */
     if (array_key_exists("IdOrder", $orders["Order"])) {
         $orders = array("Order" => [$orders["Order"]]);
     }
     $notImportedOrders = [];
     /**
      * Then treat the orders
      */
     foreach ($orders["Order"] as $orderArray) {
         /**
          * I) create the addresses
          */
         /**
          * Get delivery address, and format empty fields
          */
         $deliveryAddressArray =& $orderArray["ShippingAddress"];
         $deliveryCountryId = CountryQuery::create()->findOneByIsoalpha2(strtolower($deliveryAddressArray["Country"]))->getId();
         foreach ($deliveryAddressArray as &$value) {
             if (is_array($value)) {
                 $value = "";
             }
         }
         /**
          * Same for invoice address
          */
         $invoiceAddressArray =& $orderArray["BillingAddress"];
         $invoiceCountry = CountryQuery::create()->findOneByIsoalpha2(strtolower($invoiceAddressArray["Country"]));
         $invoiceCountryId = $invoiceCountry->getId();
         foreach ($invoiceAddressArray as &$value) {
             if (is_array($value)) {
                 $value = "";
             }
         }
         $title = CustomerTitleQuery::create()->findOne()->getId();
         /**
          * Create the order addresses
          */
         $deliveryAddressEvent = new AddressCreateOrUpdateEvent("Delivery address", $title, $deliveryAddressArray["FirstName"], $deliveryAddressArray["LastName"], $deliveryAddressArray["Street"], $deliveryAddressArray["Street1"], $deliveryAddressArray["Street2"], $deliveryAddressArray["PostalCode"], $deliveryAddressArray["Town"], $deliveryCountryId, $deliveryAddressArray["PhoneMobile"], $deliveryAddressArray["Phone"], $deliveryAddressArray["Company"]);
         $deliveryAddressEvent->setCustomer($this->shoppingFluxCustomer);
         $dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $deliveryAddressEvent);
         $invoiceAddressEvent = new AddressCreateOrUpdateEvent("Invoice address", $title, $invoiceAddressArray["FirstName"], $invoiceAddressArray["LastName"], $invoiceAddressArray["Street"], $invoiceAddressArray["Street1"], $invoiceAddressArray["Street2"], $invoiceAddressArray["PostalCode"], $invoiceAddressArray["Town"], $deliveryCountryId, $invoiceAddressArray["PhoneMobile"], $invoiceAddressArray["Phone"], $invoiceAddressArray["Company"]);
         $invoiceAddressEvent->setCustomer($this->shoppingFluxCustomer);
         $dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $invoiceAddressEvent);
         /**
          * II) Add the products to a cart
          */
         /**
          * Format the products array
          */
         if ($orderArray["NumberOfProducts"] == "1") {
             $orderArray["Products"] = array("Product" => [$orderArray["Products"]["Product"]]);
         }
         $productsArray =& $orderArray["Products"]["Product"];
         /**
          * Create a fake cart
          */
         $cart = new Cart();
         /**
          * And fulfil it with the products
          */
         foreach ($productsArray as &$productArray) {
             $ids = explode("_", $productArray["SKU"]);
             $cartPse = ProductSaleElementsQuery::create()->findPk($ids[1]);
             $calculator->load($cartPse->getProduct(), $shopCountry);
             $price = $calculator->getUntaxedPrice((double) $productArray["Price"]);
             $cart->addCartItem((new CartItem())->setProductSaleElements($cartPse)->setProduct($cartPse->getProduct())->setQuantity($productArray["Quantity"])->setPrice($price)->setPromoPrice(0)->setPromo(0));
         }
         /**
          * III) Create/Save the order
          */
         /**
          * Construct order model
          */
         $lang = LangQuery::create()->findOneByLocale($invoiceCountry->getLocale());
         $currency = CurrencyQuery::create()->findOneByCode("EUR");
         $order = OrderQuery::create()->findOneByRef($orderArray["IdOrder"]);
         if ($order !== null) {
             $order->delete();
         }
         $order = new Order();
         $order->setPostage($orderArray["TotalShipping"])->setChoosenDeliveryAddress($deliveryAddressEvent->getAddress()->getId())->setChoosenInvoiceAddress($invoiceAddressEvent->getAddress()->getId())->setDeliveryModuleId(ShoppingFluxConfigQuery::getDeliveryModuleId())->setPaymentModuleId($this->shoppingFluxPaymentModuleId)->setTransactionRef($orderArray["Marketplace"]);
         /**
          * Construct event
          */
         $orderEvent = new OrderManualEvent($order, $currency, $lang, $cart, $this->shoppingFluxCustomer);
         $orderEvent->setDispatcher($dispatcher);
         $dispatcher->dispatch(TheliaEvents::ORDER_CREATE_MANUAL, $orderEvent);
         $placedOrder = $orderEvent->getPlacedOrder();
         $placedOrder->setRef($orderArray["IdOrder"])->setPaid();
         $validOrders[] = ["IdOrder" => $placedOrder->getRef(), "Marketplace" => $placedOrder->getTransactionRef()];
     }
     /**
      * IV) Valid the orders to Shopping Flux
      */
     $request = new Request("ValidOrders");
     foreach ($validOrders as $validOrder) {
         $request->addOrder($validOrder);
     }
     $validOrdersApi = new ValidOrders($response->getToken(), $response->getMode());
     $validOrdersApi->setRequest($request);
     $validOrdersResponse = $validOrdersApi->getResponse();
     if ($validOrdersResponse->isInError()) {
         $this->logger->error($response->getFormattedError());
     }
 }
    public function testGetOrder()
    {
        $rawData = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
            <Result>
                    <Request>
                    <Date>2011-08-09T18:38:16+02:00</Date>
                    <Call>GetOrders</Call>
                    <Token>abcdef0123456789abcdef123456789abcdef123</Token>
                    <Mode>Sandbox</Mode>
                </Request>
                <Response>
                    <Orders>
                        <Order>
                            <IdOrder>123456820006-123456127010</IdOrder>
                            <Marketplace>eBay</Marketplace>
                            <TotalAmount>10.99</TotalAmount>
                            <TotalProducts>7.99</TotalProducts>
                            <TotalShipping>3.0</TotalShipping>
                            <NumberOfProducts>1</NumberOfProducts>
                            <OrderDate>2011-07-08T15:32:53+02:00</OrderDate>
                            <BillingAddress>
                                <LastName>Nom</LastName>
                                <FirstName/>
                                <Phone>0123456789</Phone>
                                <Street>1 rue du paradis</Street>
                                <PostalCode>75000</PostalCode>
                                <Town>Paris</Town>
                                <Country>FR</Country>
                                <Email/>
                            </BillingAddress>
                            <ShippingAddress>
                                <LastName>Nom</LastName>
                                <FirstName/>
                                <Phone>0123456789</Phone>
                                <Street>1 rue du paradis</Street>
                                <PostalCode>75000</PostalCode>
                                <Town>Paris</Town>
                                <Country>FR</Country>
                                <Email/>
                            </ShippingAddress>
                            <Products>
                                <Product>
                                    <SKU>1234</SKU>
                                    <Quantity>1</Quantity>
                                    <Price>7.99</Price>
                                </Product>
                            </Products>
                        </Order>

                    </Orders>
                </Response>
            </Result>
XML;
        $response = new GetOrdersResponse($rawData);
        $api = new GetOrders("foo");
        $api->setResponse($response);
        $event = new ApiCallEvent($api);
        $event->setDispatcher($this->getMock("Symfony\\Component\\EventDispatcher\\EventDispatcher"));
        $this->listener->processGetOrders($event);
    }