Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright © Magento, Inc. All rights reserved.
  3.  * See COPYING.txt for license details.
  4.  */
  5.  
  6. define([
  7.     'jquery',
  8.     'Magento_Checkout/js/model/quote',
  9.     'Magento_Checkout/js/model/url-builder',
  10.     'mage/storage',
  11.     '../model/payment/gift-card-messages',
  12.     'Magento_Checkout/js/model/error-processor',
  13.     'Magento_Customer/js/model/customer',
  14.     'Magento_Checkout/js/model/full-screen-loader',
  15.     'Magento_Checkout/js/action/get-payment-information',
  16.     'Magento_Checkout/js/model/totals'
  17. ], function (
  18.     $,
  19.     quote,
  20.     urlBuilder,
  21.     storage,
  22.     messageList,
  23.     errorProcessor,
  24.     customer,
  25.     fullScreenLoader,
  26.     getPaymentInformationAction,
  27.     totals
  28. ) {
  29.     'use strict';
  30.  
  31.     return function (giftCardCode) {
  32.         var serviceUrl,
  33.             payload,
  34.             message = 'Gift Card ' + giftCardCode + ' was added.';
  35.  
  36.         /**
  37.          * Checkout for guest and registered customer.
  38.          */
  39.         if (!customer.isLoggedIn()) {
  40.             serviceUrl = urlBuilder.createUrl('/carts/guest-carts/:cartId/giftCards', {
  41.                 cartId: quote.getQuoteId()
  42.             });
  43.             payload = {
  44.                 cartId: quote.getQuoteId(),
  45.                 giftCardAccountData: {
  46.                     'gift_cards': giftCardCode
  47.                 }
  48.             };
  49.         } else {
  50.             serviceUrl = urlBuilder.createUrl('/carts/mine/giftCards', {});
  51.             payload = {
  52.                 cartId: quote.getQuoteId(),
  53.                 giftCardAccountData: {
  54.                     'gift_cards': giftCardCode
  55.                 }
  56.             };
  57.         }
  58.         messageList.clear();
  59.         fullScreenLoader.startLoader();
  60.         storage.post(
  61.             serviceUrl, JSON.stringify(payload)
  62.         ).done(function (response) {
  63.             var deferred = $.Deferred();
  64.  
  65.             if (response) {
  66.                 totals.isLoading(true);
  67.                 getPaymentInformationAction(deferred);
  68.                 $.when(deferred).done(function () {
  69.                     totals.isLoading(false);
  70.                 });
  71.                 messageList.addSuccessMessage({
  72.                     'message': message
  73.                 });
  74.             }
  75.         }).fail(function (response) {
  76.             totals.isLoading(false);
  77.             errorProcessor.process(response, messageList);
  78.         }).always(function () {
  79.             fullScreenLoader.stopLoader();
  80.         });
  81.     };
  82. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement