PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/magento2-base/dev/tests/functional/tests/app/Magento/Sales/Test/Handler/OrderInjectable/Webapi.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 336 lines | 216 code | 26 blank | 94 comment | 14 complexity | aeb4961ff654104002da09e765a6a715 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Handler\OrderInjectable;
  7. use Magento\Bundle\Test\Fixture\BundleProduct;
  8. use Magento\ConfigurableProduct\Test\Fixture\ConfigurableProduct;
  9. use Magento\Downloadable\Test\Fixture\DownloadableProduct;
  10. use Magento\Sales\Test\Fixture\OrderInjectable;
  11. use Magento\Mtf\Fixture\FixtureInterface;
  12. use Magento\Mtf\Util\Protocol\CurlTransport;
  13. use Magento\Mtf\Handler\Webapi as AbstractWebapi;
  14. use Magento\Mtf\Util\Protocol\CurlTransport\WebapiDecorator;
  15. /**
  16. * Create new order via web API.
  17. */
  18. class Webapi extends AbstractWebapi implements OrderInjectableInterface
  19. {
  20. /**
  21. * Mapping values for data.
  22. *
  23. * @var array
  24. */
  25. protected $mappingData = [
  26. 'region_id' => [
  27. 'California' => '12',
  28. ],
  29. 'country_id' => [
  30. 'United States' => 'US',
  31. 'United Kingdom' => 'GB',
  32. ],
  33. ];
  34. /**
  35. * Order quote value.
  36. *
  37. * @var string
  38. */
  39. protected $quote;
  40. /**
  41. * First part of Web API url for creating order.
  42. *
  43. * @var string
  44. */
  45. protected $url;
  46. /**
  47. * Creating order using quote via web API.
  48. *
  49. * @param FixtureInterface|null $fixture [optional]
  50. * @return array
  51. */
  52. public function persist(FixtureInterface $fixture = null)
  53. {
  54. /** @var OrderInjectable $fixture */
  55. $this->createQuote($fixture);
  56. $this->url = $_ENV['app_frontend_url'] . 'rest/V1/carts/' . (int)$this->quote;
  57. $this->setProducts($fixture);
  58. $this->setCoupon($fixture);
  59. $this->setBillingAddress($fixture);
  60. $this->setShippingInformation($fixture);
  61. $this->setPaymentMethod($fixture);
  62. $orderId = $this->placeOrder();
  63. return ['id' => sprintf("%09d", $orderId)];
  64. }
  65. /**
  66. * Create checkout quote.
  67. *
  68. * @param OrderInjectable $order
  69. * @return void
  70. * @throws \Exception
  71. */
  72. protected function createQuote(OrderInjectable $order)
  73. {
  74. $url = $_ENV['app_frontend_url'] . 'rest/V1/customers/' . $order->getCustomerId()->getId() . '/carts';
  75. $data = '{"customerId": "' . $order->getCustomerId()->getId() . '"}';
  76. $this->webapiTransport->write($url, $data);
  77. $response = json_decode($this->webapiTransport->read(), true);
  78. $this->webapiTransport->close();
  79. if (!is_numeric($response)) {
  80. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  81. throw new \Exception('Could not create checkout quote viw web API!');
  82. }
  83. $this->quote = $response;
  84. }
  85. /**
  86. * Add products to quote.
  87. *
  88. * @param OrderInjectable $order
  89. * @return void
  90. * @throws \Exception
  91. */
  92. protected function setProducts(OrderInjectable $order)
  93. {
  94. $url = $_ENV['app_frontend_url'] . 'rest/V1/carts/' . $this->quote . '/items';
  95. $products = $order->getEntityId()['products'];
  96. foreach ($products as $product) {
  97. $data = [
  98. 'cartItem' => [
  99. 'sku' => $product->getSku(),
  100. 'qty' => isset($product->getCheckoutData()['qty']) ? $product->getCheckoutData()['qty'] : 1,
  101. 'quote_id' => $this->quote
  102. ]
  103. ];
  104. $methodName = 'prepare' . ucfirst($product->getDataConfig()['type_id']) . 'Options';
  105. if (method_exists($this, $methodName)) {
  106. $data['cartItem']['product_option'] = $this->$methodName($product);
  107. }
  108. $this->webapiTransport->write($url, $data);
  109. $response = (array)json_decode($this->webapiTransport->read(), true);
  110. $this->webapiTransport->close();
  111. if (isset($response['message'])) {
  112. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  113. throw new \Exception('Could not add product item to quote!');
  114. }
  115. }
  116. }
  117. /**
  118. * Set coupon to quote.
  119. *
  120. * @param OrderInjectable $order
  121. * @return void
  122. * @throws \Exception
  123. */
  124. protected function setCoupon(OrderInjectable $order)
  125. {
  126. if (!$order->hasData('coupon_code')) {
  127. return;
  128. }
  129. $url = $this->url . '/coupons/' . $order->getCouponCode()->getCouponCode();
  130. $data = [
  131. 'cartId' => $this->quote,
  132. 'couponCode' => $order->getCouponCode()->getCouponCode()
  133. ];
  134. $this->webapiTransport->write($url, $data, WebapiDecorator::PUT);
  135. $response = json_decode($this->webapiTransport->read(), true);
  136. $this->webapiTransport->close();
  137. if ($response !== true) {
  138. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  139. throw new \Exception('Could not apply coupon code!');
  140. }
  141. }
  142. /**
  143. * Set billing address to quote.
  144. *
  145. * @param OrderInjectable $order
  146. * @return void
  147. * @throws \Exception
  148. */
  149. protected function setBillingAddress(OrderInjectable $order)
  150. {
  151. $url = $this->url . "/billing-address";
  152. $address = $order->getBillingAddressId();
  153. unset($address['default_billing']);
  154. unset($address['default_shipping']);
  155. foreach (array_keys($this->mappingData) as $key) {
  156. if (isset($address[$key])) {
  157. $address[$key] = $this->mappingData[$key][$address[$key]];
  158. }
  159. }
  160. $data = ["address" => $address];
  161. $this->webapiTransport->write($url, $data);
  162. $response = json_decode($this->webapiTransport->read(), true);
  163. $this->webapiTransport->close();
  164. if (!is_numeric($response)) {
  165. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  166. throw new \Exception("Could not set billing addresss to quote!");
  167. }
  168. }
  169. /**
  170. * Set shipping information to quote
  171. *
  172. * @param OrderInjectable $order
  173. * @throws \Exception
  174. */
  175. protected function setShippingInformation(OrderInjectable $order)
  176. {
  177. if (!$order->hasData('shipping_method')) {
  178. return;
  179. }
  180. $url = $this->url . '/shipping-information';
  181. list($carrier, $method) = explode('_', $order->getShippingMethod());
  182. $address = $order->hasData('shipping_address_id')
  183. ? $order->getShippingAddressId()
  184. : $order->getBillingAddressId();
  185. unset($address['default_billing']);
  186. unset($address['default_shipping']);
  187. foreach (array_keys($this->mappingData) as $key) {
  188. if (isset($address[$key])) {
  189. $address[$key] = $this->mappingData[$key][$address[$key]];
  190. }
  191. }
  192. $data = [
  193. 'addressInformation' => [
  194. 'shippingAddress' => $address,
  195. 'shippingMethodCode' => $method,
  196. 'shippingCarrierCode' => $carrier,
  197. ]
  198. ];
  199. $this->webapiTransport->write($url, $data, WebapiDecorator::POST);
  200. $response = json_decode($this->webapiTransport->read(), true);
  201. $this->webapiTransport->close();
  202. if (!isset($response['payment_methods'], $response['totals'])) {
  203. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  204. throw new \Exception('Could not set shipping method to quote!');
  205. }
  206. }
  207. /**
  208. * Set payment method to quote.
  209. *
  210. * @param OrderInjectable $order
  211. * @return void
  212. * @throws \Exception
  213. */
  214. protected function setPaymentMethod(OrderInjectable $order)
  215. {
  216. $url = $this->url . '/selected-payment-method';
  217. $data = [
  218. "cartId" => $this->quote,
  219. "method" => $order->getPaymentAuthExpiration()
  220. ];
  221. $this->webapiTransport->write($url, $data, WebapiDecorator::PUT);
  222. $response = json_decode($this->webapiTransport->read(), true);
  223. $this->webapiTransport->close();
  224. if (!is_numeric($response)) {
  225. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  226. throw new \Exception('Could not set payment method to quote!');
  227. }
  228. }
  229. /**
  230. * Place order.
  231. *
  232. * @return array
  233. * @throws \Exception
  234. */
  235. protected function placeOrder()
  236. {
  237. $url = $this->url . '/order';
  238. $data = ["cartId" => $this->quote];
  239. $this->webapiTransport->write($url, $data, WebapiDecorator::PUT);
  240. $response = json_decode($this->webapiTransport->read(), true);
  241. $this->webapiTransport->close();
  242. if (!is_numeric($response)) {
  243. $this->eventManager->dispatchEvent(['webapi_failed'], [$response]);
  244. throw new \Exception('Could not place order via web API!');
  245. }
  246. return $response;
  247. }
  248. /**
  249. * Prepare configurable product options.
  250. *
  251. * @param ConfigurableProduct $product
  252. * @return array
  253. */
  254. protected function prepareConfigurableOptions(ConfigurableProduct $product)
  255. {
  256. $options = [];
  257. $attributesData = $product->getDataFieldConfig('configurable_attributes_data')['source']->getAttributesData();
  258. foreach ($product->getCheckoutData()['options']['configurable_options'] as $checkoutOption) {
  259. $options[] = [
  260. 'option_id' => $attributesData[$checkoutOption['title']]['attribute_id'],
  261. 'option_value' => $attributesData[$checkoutOption['title']]['options'][$checkoutOption['value']]['id'],
  262. ];
  263. }
  264. return ['extension_attributes' => ['configurable_item_options' => $options]];
  265. }
  266. /**
  267. * Prepare bundle product options.
  268. *
  269. * @param BundleProduct $product
  270. * @return array
  271. */
  272. protected function prepareBundleOptions(BundleProduct $product)
  273. {
  274. $options = [];
  275. foreach ($product->getCheckoutData()['options']['bundle_options'] as $checkoutOption) {
  276. foreach ($product->getBundleSelections()['bundle_options'] as $productOption) {
  277. if (strpos($productOption['title'], $checkoutOption['title']) !== false) {
  278. $option = [];
  279. foreach ($productOption['assigned_products'] as $productData) {
  280. if (strpos($productData['search_data']['name'], $checkoutOption['value']['name']) !== false) {
  281. $qty = isset($checkoutOption['qty'])
  282. ? $checkoutOption['qty']
  283. : $productData['data']['selection_qty'];
  284. $option['option_id'] = $productData['option_id'];
  285. $option['option_selections'][] = $productData['selection_id'];
  286. $option['option_qty'] = $qty;
  287. }
  288. }
  289. $options[] = $option;
  290. }
  291. }
  292. }
  293. return ['extension_attributes' => ['bundle_options' => $options]];
  294. }
  295. /**
  296. * Prepare downloadable product options.
  297. *
  298. * @param DownloadableProduct $product
  299. * @return array
  300. */
  301. protected function prepareDownloadableOptions(DownloadableProduct $product)
  302. {
  303. $checkoutData = $product->getCheckoutData();
  304. $links = [];
  305. foreach ($checkoutData['options']['links'] as $link) {
  306. $links[] = $link['id'];
  307. }
  308. return ['extension_attributes' => ['downloadable_option' => ['downloadable_links' => $links]]];
  309. }
  310. }