diff --git a/app/code/Magento/Checkout/Controller/Index/Index.php b/app/code/Magento/Checkout/Controller/Index/Index.php index 6d8c3951b8cb7..f15f4259923f8 100644 --- a/app/code/Magento/Checkout/Controller/Index/Index.php +++ b/app/code/Magento/Checkout/Controller/Index/Index.php @@ -34,8 +34,6 @@ public function execute() $this->_customerSession->regenerateId(); $this->_objectManager->get('Magento\Checkout\Model\Session')->setCartWasUpdated(false); - $currentUrl = $this->_url->getUrl('*/*/*', ['_secure' => true]); - $this->_objectManager->get('Magento\Customer\Model\Session')->setBeforeAuthUrl($currentUrl); $this->getOnepage()->initCheckout(); $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->set(__('Checkout')); diff --git a/app/code/Magento/Checkout/view/frontend/web/js/model/checkout-data-resolver.js b/app/code/Magento/Checkout/view/frontend/web/js/model/checkout-data-resolver.js index d90ed59afd197..888c6b5d61c2c 100644 --- a/app/code/Magento/Checkout/view/frontend/web/js/model/checkout-data-resolver.js +++ b/app/code/Magento/Checkout/view/frontend/web/js/model/checkout-data-resolver.js @@ -128,7 +128,7 @@ define([ } if (!availableRate && window.checkoutConfig.selectedShippingMethod) { - availableRate = true; + availableRate = window.checkoutConfig.selectedShippingMethod; selectShippingMethodAction(window.checkoutConfig.selectedShippingMethod); } diff --git a/app/code/Magento/Directory/Model/Country/Postcode/Config/Reader.php b/app/code/Magento/Directory/Model/Country/Postcode/Config/Reader.php index e3fa38d728c86..6e6f5ef6b15e0 100644 --- a/app/code/Magento/Directory/Model/Country/Postcode/Config/Reader.php +++ b/app/code/Magento/Directory/Model/Country/Postcode/Config/Reader.php @@ -7,6 +7,13 @@ class Reader extends \Magento\Framework\Config\Reader\Filesystem { + /** + * List of id attributes for merge + * + * @var array + */ + protected $_idAttributes = ['/config/zip' => 'countryCode']; + /** * Construct the FileSystem Reader Class * diff --git a/app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php b/app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php index 6f16a816f21df..677f32a47baef 100644 --- a/app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php +++ b/app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php @@ -47,19 +47,14 @@ public function __construct( * Get gift message * * @param \Magento\Sales\Api\OrderRepositoryInterface $subject - * @param callable $proceed - * @param int $orderId + * @param \Magento\Sales\Api\Data\OrderInterface $resultOrder * @return \Magento\Sales\Api\Data\OrderInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function aroundGet( + public function afterGet( \Magento\Sales\Api\OrderRepositoryInterface $subject, - \Closure $proceed, - $orderId + \Magento\Sales\Api\Data\OrderInterface $resultOrder ) { - /** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */ - $resultOrder = $proceed($orderId); - $resultOrder = $this->getOrderGiftMessage($resultOrder); $resultOrder = $this->getOrderItemGiftMessage($resultOrder); @@ -74,7 +69,8 @@ public function aroundGet( */ protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order) { - if ($order->getExtensionAttributes() && $order->getExtensionAttributes()->getGiftMessage()) { + $extensionAttributes = $order->getExtensionAttributes(); + if ($extensionAttributes && $extensionAttributes->getGiftMessage()) { return $order; } @@ -86,7 +82,7 @@ protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $o } /** @var \Magento\Sales\Api\Data\OrderExtension $orderExtension */ - $orderExtension = $this->orderExtensionFactory->create(); + $orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create(); $orderExtension->setGiftMessage($giftMessage); $order->setExtensionAttributes($orderExtension); @@ -101,10 +97,12 @@ protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $o */ protected function getOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order) { - if (null !== $order->getItems()) { + $orderItems = $order->getItems(); + if (null !== $orderItems) { /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */ - foreach ($order->getItems() as $orderItem) { - if ($orderItem->getExtensionAttributes() && $orderItem->getExtensionAttributes()->getGiftMessage()) { + foreach ($orderItems as $orderItem) { + $extensionAttributes = $orderItem->getExtensionAttributes(); + if ($extensionAttributes && $extensionAttributes->getGiftMessage()) { continue; } @@ -119,7 +117,9 @@ protected function getOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterfac } /** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */ - $orderItemExtension = $this->orderItemExtensionFactory->create(); + $orderItemExtension = $extensionAttributes + ? $extensionAttributes + : $this->orderItemExtensionFactory->create(); $orderItemExtension->setGiftMessage($giftMessage); $orderItem->setExtensionAttributes($orderItemExtension); } diff --git a/app/code/Magento/GiftMessage/Model/Plugin/OrderSave.php b/app/code/Magento/GiftMessage/Model/Plugin/OrderSave.php index cbea067d99805..f9e1a3fa4eaa7 100644 --- a/app/code/Magento/GiftMessage/Model/Plugin/OrderSave.php +++ b/app/code/Magento/GiftMessage/Model/Plugin/OrderSave.php @@ -35,18 +35,16 @@ public function __construct( * Save gift message * * @param \Magento\Sales\Api\OrderRepositoryInterface $subject - * @param callable $proceed - * @param \Magento\Sales\Api\Data\OrderInterface $order + * @param \Magento\Sales\Api\Data\OrderInterface $resultOrder * @return \Magento\Sales\Api\Data\OrderInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @throws CouldNotSaveException */ - public function aroundSave( + public function afterSave( \Magento\Sales\Api\OrderRepositoryInterface $subject, - \Closure $proceed, - \Magento\Sales\Api\Data\OrderInterface $order + \Magento\Sales\Api\Data\OrderInterface $resultOrder ) { /** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */ - $resultOrder = $proceed($order); $resultOrder = $this->saveOrderGiftMessage($resultOrder); $resultOrder = $this->saveOrderItemGiftMessage($resultOrder); @@ -58,24 +56,24 @@ public function aroundSave( * * @param \Magento\Sales\Api\Data\OrderInterface $order * @return \Magento\Sales\Api\Data\OrderInterface + * @throws CouldNotSaveException */ protected function saveOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order) { + $extensionAttributes = $order->getExtensionAttributes(); if ( - null !== $order->getExtensionAttributes() && - null !== $order->getExtensionAttributes()->getGiftMessage() + null !== $extensionAttributes && + null !== $extensionAttributes->getGiftMessage() ) { /* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */ - $giftMessage = $order->getExtensionAttributes()->getGiftMessage(); - if (null !== $giftMessage) { - try { - $this->giftMessageOrderRepository->save($order->getEntityId(), $giftMessage); - } catch (\Exception $e) { - throw new CouldNotSaveException( - __('Could not add gift message to order: "%1"', $e->getMessage()), - $e - ); - } + $giftMessage = $extensionAttributes->getGiftMessage(); + try { + $this->giftMessageOrderRepository->save($order->getEntityId(), $giftMessage); + } catch (\Exception $e) { + throw new CouldNotSaveException( + __('Could not add gift message to order: "%1"', $e->getMessage()), + $e + ); } } return $order; @@ -86,18 +84,21 @@ protected function saveOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $ * * @param \Magento\Sales\Api\Data\OrderInterface $order * @return \Magento\Sales\Api\Data\OrderInterface + * @throws CouldNotSaveException */ protected function saveOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order) { - if (null !== $order->getItems()) { + $items = $order->getItems(); + if (null !== $items) { /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */ - foreach ($order->getItems() as $orderItem) { + foreach ($items as $orderItem) { + $extensionAttribute = $orderItem->getExtensionAttributes(); if ( - null !== $orderItem->getExtensionAttributes() && - null !== $orderItem->getExtensionAttributes()->getGiftMessage() + null !== $extensionAttribute && + null !== $extensionAttribute->getGiftMessage() ) { /* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */ - $giftMessage = $orderItem->getExtensionAttributes()->getGiftMessage(); + $giftMessage = $extensionAttribute->getGiftMessage(); try { $this->giftMessageOrderItemRepository->save( $order->getEntityId(), diff --git a/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderGetTest.php b/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderGetTest.php new file mode 100644 index 0000000000000..d78092b3137ea --- /dev/null +++ b/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderGetTest.php @@ -0,0 +1,242 @@ +giftMessageOrderRepositoryMock = $this->getMock( + \Magento\GiftMessage\Api\OrderRepositoryInterface::class + ); + $this->giftMessageOrderItemRepositoryMock = $this->getMock( + \Magento\GiftMessage\Api\OrderItemRepositoryInterface::class + ); + $this->orderExtensionFactoryMock = $this->getMock( + \Magento\Sales\Api\Data\OrderExtensionFactory::class, + ['create'], + [], + '', + false + ); + $this->orderItemExtensionFactoryMock = $this->getMock( + \Magento\Sales\Api\Data\OrderItemExtensionFactory::class, + ['create'], + [], + '', + false + ); + $this->orderMock = $this->getMock( + \Magento\Sales\Api\Data\OrderInterface::class + ); + $this->orderExtensionMock = $this->getMock( + \Magento\Sales\Api\Data\OrderExtension::class, + ['getGiftMessage', 'setGiftMessage'], + [], + '', + false + ); + $this->giftMessageMock = $this->getMock( + \Magento\GiftMessage\Api\Data\MessageInterface::class + ); + $this->orderItemMock = $this->getMock( + \Magento\Sales\Api\Data\OrderItemInterface::class + ); + $this->orderItemExtensionMock = $this->getMock( + \Magento\Sales\Api\Data\OrderItemExtension::class, + ['setGiftMessage', 'getGiftMessage'], + [], + '', + false + ); + $this->orderRepositoryMock = $this->getMock( + \Magento\Sales\Api\OrderRepositoryInterface::class + ); + + $this->collectionMock = $this->getMock( + \Magento\Sales\Model\ResourceModel\Order\Collection::class, + [], + [], + '', + false + ); + + $this->plugin = new \Magento\GiftMessage\Model\Plugin\OrderGet( + $this->giftMessageOrderRepositoryMock, + $this->giftMessageOrderItemRepositoryMock, + $this->orderExtensionFactoryMock, + $this->orderItemExtensionFactoryMock + ); + } + + public function testAfterGetGiftMessageOnOrderLevel() + { + //set Gift Message for Order + $orderId = 1; + $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId); + $this->orderMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderExtensionMock); + $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]); + $this->giftMessageOrderRepositoryMock + ->expects($this->once()) + ->method('get') + ->with($orderId) + ->willReturn($this->giftMessageMock); + $this->orderExtensionMock + ->expects($this->once()) + ->method('setGiftMessage') + ->with($this->giftMessageMock) + ->willReturnSelf(); + $this->orderMock + ->expects($this->once()) + ->method('setExtensionAttributes') + ->with($this->orderExtensionMock) + ->willReturnSelf(); + + // set Gift Message on Item Level + $this->orderMock->expects($this->once())->method('getItems')->willReturn([]); + $this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock); + } + + public function testAfterGetGiftMessageOnItemLevel() + { + //set Gift Message for Order + $orderId = 1; + $orderItemId = 2; + $this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId); + $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId); + $this->orderMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderExtensionMock); + $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn($this->giftMessageMock); + + // set Gift Message on Item Level + $this->orderMock->expects($this->once())->method('getItems')->willReturn([$this->orderItemMock]); + $this->orderItemMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderItemExtensionMock); + $this->orderItemExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]); + $this->giftMessageOrderItemRepositoryMock + ->expects($this->once()) + ->method('get') + ->with($orderId, $orderItemId) + ->willReturn($this->giftMessageMock); + $this->orderItemExtensionMock + ->expects($this->once()) + ->method('setGiftMessage') + ->with($this->giftMessageMock) + ->willReturnSelf(); + $this->orderItemMock + ->expects($this->once()) + ->method('setExtensionAttributes') + ->with($this->orderItemExtensionMock) + ->willReturnSelf(); + $this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock); + } + + public function testGetAfterWhenMessagesAreNotSet() + { + $orderId = 1; + $orderItemId = 2; + //set Gift Message for Order + $this->orderMock->expects($this->exactly(2))->method('getEntityId')->willReturn($orderId); + $this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId); + $this->orderMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderExtensionMock); + $this->orderExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]); + $this->giftMessageOrderRepositoryMock + ->expects($this->once()) + ->method('get') + ->with($orderId) + ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException()); + $this->orderExtensionMock + ->expects($this->never()) + ->method('setGiftMessage'); + + // set Gift Message on Item Level + $this->orderMock->expects($this->once())->method('getItems')->willReturn([$this->orderItemMock]); + $this->orderItemMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderItemExtensionMock); + $this->orderItemExtensionMock->expects($this->once())->method('getGiftMessage')->willReturn([]); + $this->giftMessageOrderItemRepositoryMock + ->expects($this->once()) + ->method('get') + ->with($orderId, $orderItemId) + ->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException()); + $this->orderItemExtensionMock + ->expects($this->never()) + ->method('setGiftMessage'); + + $this->plugin->afterGet($this->orderRepositoryMock, $this->orderMock); + } +} diff --git a/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderSaveTest.php b/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderSaveTest.php new file mode 100644 index 0000000000000..d4ae167341ef2 --- /dev/null +++ b/app/code/Magento/GiftMessage/Test/Unit/Model/Plugin/OrderSaveTest.php @@ -0,0 +1,197 @@ +giftMessageOrderRepositoryMock = $this->getMock( + \Magento\GiftMessage\Api\OrderRepositoryInterface::class + ); + $this->giftMessageOrderItemRepositoryMock = $this->getMock( + \Magento\GiftMessage\Api\OrderItemRepositoryInterface::class + ); + $this->orderMock = $this->getMock( + \Magento\Sales\Api\Data\OrderInterface::class + ); + $this->orderExtensionMock = $this->getMock( + \Magento\Sales\Api\Data\OrderExtension::class, + ['getGiftMessage', 'setGiftMessage'], + [], + '', + false + ); + $this->giftMessageMock = $this->getMock( + \Magento\GiftMessage\Api\Data\MessageInterface::class + ); + $this->orderItemMock = $this->getMock( + \Magento\Sales\Api\Data\OrderItemInterface::class + ); + $this->orderItemExtensionMock = $this->getMock( + \Magento\Sales\Api\Data\OrderItemExtension::class, + ['setGiftMessage', 'getGiftMessage'], + [], + '', + false + ); + $this->orderRepositoryMock = $this->getMock( + \Magento\Sales\Api\OrderRepositoryInterface::class + ); + + $this->plugin = new OrderSave( + $this->giftMessageOrderRepositoryMock, + $this->giftMessageOrderItemRepositoryMock + ); + } + + public function testAfterSaveGiftMessages() + { + // save Gift Message on order level + $orderId = 1; + $orderItemId = 2; + $this->orderMock->expects($this->exactly(2))->method('getEntityId')->willReturn($orderId); + $this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId); + $this->orderMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderExtensionMock); + $this->orderExtensionMock + ->expects($this->exactly(2)) + ->method('getGiftMessage') + ->willReturn($this->giftMessageMock); + $this->giftMessageOrderRepositoryMock + ->expects($this->once()) + ->method('save') + ->with($orderId, $this->giftMessageMock); + + // save Gift Messages on item level + $this->orderMock->expects($this->once())->method('getItems')->willReturn([$this->orderItemMock]); + $this->orderItemMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderItemExtensionMock); + $this->orderItemExtensionMock + ->expects($this->exactly(2)) + ->method('getGiftMessage') + ->willReturn($this->giftMessageMock); + $this->giftMessageOrderItemRepositoryMock + ->expects($this->once())->method('save') + ->with($orderId, $orderItemId, $this->giftMessageMock); + $this->plugin->afterSave($this->orderRepositoryMock, $this->orderMock); + } + + /** + * @expectedException \Magento\Framework\Exception\CouldNotSaveException + * @expectedMessage Could not add gift message to order:Test message + */ + public function testAfterSaveIfGiftMessagesNotExist() + { + // save Gift Message on order level + $orderId = 1; + $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId); + $this->orderMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderExtensionMock); + $this->orderExtensionMock + ->expects($this->exactly(2)) + ->method('getGiftMessage') + ->willReturn($this->giftMessageMock); + $this->giftMessageOrderRepositoryMock + ->expects($this->once()) + ->method('save') + ->willThrowException(new \Exception('TestMessage')); + + // save Gift Messages on item level + $this->orderMock->expects($this->never())->method('getItems'); + $this->plugin->afterSave($this->orderRepositoryMock, $this->orderMock); + } + + /** + * @expectedException \Magento\Framework\Exception\CouldNotSaveException + * @expectedMessage Could not add gift message to order:Test message + */ + public function testAfterSaveIfItemGiftMessagesNotExist() + { + // save Gift Message on order level + $orderId = 1; + $orderItemId = 2; + $this->orderMock->expects($this->once())->method('getEntityId')->willReturn($orderId); + $this->orderItemMock->expects($this->once())->method('getItemId')->willReturn($orderItemId); + $this->orderMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn(null); + $this->orderExtensionMock + ->expects($this->never()) + ->method('getGiftMessage'); + + // save Gift Messages on item level + $this->orderMock->expects($this->once())->method('getItems')->willReturn([$this->orderItemMock]); + $this->orderItemMock + ->expects($this->once()) + ->method('getExtensionAttributes') + ->willReturn($this->orderItemExtensionMock); + $this->orderItemExtensionMock + ->expects($this->exactly(2)) + ->method('getGiftMessage') + ->willReturn($this->giftMessageMock); + $this->giftMessageOrderItemRepositoryMock + ->expects($this->once())->method('save') + ->with($orderId, $orderItemId, $this->giftMessageMock) + ->willThrowException(new \Exception('TestMessage')); + $this->plugin->afterSave($this->orderRepositoryMock, $this->orderMock); + } +} diff --git a/app/code/Magento/GiftMessage/view/frontend/web/js/action/gift-options.js b/app/code/Magento/GiftMessage/view/frontend/web/js/action/gift-options.js index e4cb14f8f0fb1..0b3e967a0f9e6 100644 --- a/app/code/Magento/GiftMessage/view/frontend/web/js/action/gift-options.js +++ b/app/code/Magento/GiftMessage/view/frontend/web/js/action/gift-options.js @@ -5,29 +5,40 @@ /*global define*/ define( [ - '../model/url-builder', + 'Magento_GiftMessage/js/model/url-builder', 'mage/storage', 'Magento_Ui/js/model/messageList', 'Magento_Checkout/js/model/error-processor', - 'mage/url' + 'mage/url', + 'Magento_Checkout/js/model/quote' ], - function(urlBuilder, storage, messageList, errorProcessor, url) { - "use strict"; - return function(giftMessage, remove) { - url.setBaseUrl(giftMessage.getConfigValue('baseUrl')); - var quoteId = giftMessage.getConfigValue('quoteId'); + function (urlBuilder, storage, messageList, errorProcessor, url, quote) { + 'use strict'; + + return function (giftMessage, remove) { var serviceUrl; + + url.setBaseUrl(giftMessage.getConfigValue('baseUrl')); + if (giftMessage.getConfigValue('isCustomerLoggedIn')) { serviceUrl = urlBuilder.createUrl('/carts/mine/gift-message', {}); + if (giftMessage.itemId != 'orderLevel') { - serviceUrl = urlBuilder.createUrl('/carts/mine/gift-message/:itemId', {itemId: giftMessage.itemId}); + serviceUrl = urlBuilder.createUrl('/carts/mine/gift-message/:itemId', { + itemId: giftMessage.itemId + }); } } else { - serviceUrl = urlBuilder.createUrl('/guest-carts/:cartId/gift-message', {cartId: quoteId}); + serviceUrl = urlBuilder.createUrl('/guest-carts/:cartId/gift-message', { + cartId: quote.getQuoteId() + }); + if (giftMessage.itemId != 'orderLevel') { serviceUrl = urlBuilder.createUrl( '/guest-carts/:cartId/gift-message/:itemId', - {cartId: quoteId, itemId: giftMessage.itemId} + { + cartId: quote.getQuoteId(), itemId: giftMessage.itemId + } ); } } @@ -39,16 +50,16 @@ define( gift_message: giftMessage.getSubmitParams(remove) }) ).done( - function(result) { + function (response) { giftMessage.reset(); - _.each(giftMessage.getAfterSubmitCallbacks(), function(callback) { + _.each(giftMessage.getAfterSubmitCallbacks(), function (callback) { if (_.isFunction(callback)) { callback(); } }); } ).fail( - function(response) { + function (response) { errorProcessor.process(response); } ); diff --git a/app/code/Magento/GiftMessage/view/frontend/web/js/model/url-builder.js b/app/code/Magento/GiftMessage/view/frontend/web/js/model/url-builder.js index b126dd7155d82..1520de20d575c 100644 --- a/app/code/Magento/GiftMessage/view/frontend/web/js/model/url-builder.js +++ b/app/code/Magento/GiftMessage/view/frontend/web/js/model/url-builder.js @@ -3,36 +3,19 @@ * See COPYING.txt for license details. */ /*jshint browser:true jquery:true*/ -/*global alert*/ define( - ['jquery'], - function($) { - return { - method: "rest", - storeCode: window.giftOptionsConfig.storeCode, - version: 'V1', - serviceUrl: ':method/:storeCode/:version', + [ + 'jquery', + 'Magento_Checkout/js/model/url-builder' + ], + function ($, urlBuilder) { + 'use strict'; - createUrl: function(url, params) { - var completeUrl = this.serviceUrl + url; - return this.bindParams(completeUrl, params); - }, - bindParams: function(url, params) { - params.method = this.method; - params.storeCode = this.storeCode; - params.version = this.version; - - var urlParts = url.split("/"); - urlParts = urlParts.filter(Boolean); - - $.each(urlParts, function(key, part) { - part = part.replace(':', ''); - if (params[part] != undefined) { - urlParts[key] = params[part]; - } - }); - return urlParts.join('/'); + return $.extend( + urlBuilder, + { + storeCode: window.giftOptionsConfig.storeCode } - }; + ); } );