

(function () {
  var MinMaxAllowed,
      InCompatibleShippingMethods,
      buttonContainer = 'div#paypal-button-container';

      try {
        MinMaxAllowed = JSON.parse('{"EUR":{"MaxPriceValue":null,"MinPriceValue":null}}');
      } catch (ex) {
      }
      try {
        InCompatibleShippingMethods = JSON.parse('{}');
      } catch (ex) {
      }

  var initPayPal = function() {
    var isOk = 1;

    if (window.paypal) {
      try {
        var FUNDING_SOURCES = [
          
        ];

        FUNDING_SOURCES.forEach(function(fundingSource) {
            var button = paypal.Buttons({
              style: {
                layout: 'vertical',
                color: fundingSource === paypal.FUNDING.PAYPAL || fundingSource === paypal.FUNDING.PAYLATER ? 'gold' : 'white',
                shape: 'rect',
                label: 'paypal',
                height: 55,
              },
              fundingSource: fundingSource,
              createOrder: function (data, actions) {
                return xhrRequest();
              },
              onApprove: function (data, actions) {
                xhrRequestApprove(fundingSource);
              },
              onCancel: function (data, actions) {
                console.log("Customer cancelled the PayPal Checkout Flow");
              },
              onError: function () {
                console.log("An Error occurred as part of the PayPal JS SDK");
              }
            });

            if ( button.isEligible() ) {
                button.render(buttonContainer);
            }
        });

        /*  */
        

      } catch (e) {
        isOk = 0;
        console.log(e);
      }
    } else {
      isOk = 0;
    }

    if (!isOk) {
      
    }
  },

  xhrRequest = function() {
    return new Promise(function(resolve, reject) {
      if (XMLHttpRequest && cart) {
        var xhr = new XMLHttpRequest(),
          targetUrl = 'https://www.omnibus-modell-shop.de/epages/es10631306.sf?ViewAction=JSONViewResponse&ChangeAction=JSONCreatePayPalPPCPOrder',
          data = {
            'BasketGUID': cart.cartId,
          };

        xhr.open('POST', targetUrl, true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.addEventListener('load', function () {
          if (xhr.status < 200 || xhr.status >= 300) {
            reject(new Error(xhr.responseText + xhr.statusText));
          } else {
            try {
              var response = JSON.parse(xhr.responseText);

              if (response.success) {
                resolve(response.orderID);
              } else {
                reject(new Error(xhr.responseText + xhr.statusText));
              }
            } catch (e) {
              reject(e);
            }
          }
        });

        xhr.send(JSON.stringify(data));
      }
    })
  },

  xhrRequestApprove = function(fundingSource) {
    if (XMLHttpRequest && cart) {
      var xhr = new XMLHttpRequest(),
        targetUrl = 'https://www.omnibus-modell-shop.de/epages/es10631306.sf?ViewAction=JSONViewResponse&ChangeAction=JSONApprovePayPalPPCPOrder',
        data = {
          'PickupToken': 'MWUwOTI2ZDlmODM3YzNiMDg5MDkwMGEyYWYxYmJmYWUwNjYwM2ZiZjc4OWFjMzRjYmM1MDg3NTY1OTcwN2VhNF8xNjgxNjIzODkw',
          'BasketGUID': cart.cartId,
          'LocaleID': shopLocale,
          'FundingSource': fundingSource,
        };

      xhr.open('POST', targetUrl, true);
      xhr.setRequestHeader('Content-Type', 'application/json');

      xhr.addEventListener('load', function () {
        if (xhr.status < 200 || xhr.status >= 300) {
          console.warn(new Error(xhr.responseText + xhr.statusText));
        } else {
          try {
            var response = JSON.parse(xhr.responseText);

            if (response.success) {
              window.location = response.redirect;
            } else {
              console.warn(new Error(xhr.responseText + xhr.statusText));
            }
          } catch (e) {
            console.log(e);
          }
        }
      });

      xhr.send(JSON.stringify(data));
    }
  };

  if (window.eComEventTarget) {
    var cart,
        script,
        shopLocale,
        ppcpUpdateCart = function (e) {
          var container = document.querySelector(buttonContainer),
              timeout = 500,
              CompatibleWithCurrentShipping = 1;

          if ( container ) {
            timeout = 0;
          }

          /*  */
          setTimeout(function(){

            if ( ! container ) {
                container = document.querySelector(buttonContainer);
            }
            var cartHasItems = typeof cart.productLineItems === 'object' && cart.productLineItems.length > 0 ? true : false;

            if ( container && cartHasItems ) {
              /*  */
              var cartCurrencyId = cart.grandTotal.currency;

              container.setAttribute('data-pp-amount', cart.grandTotal.amount);

              /*  */
              if ( cart.shippingLineItem.shippingMethod.name ) {
                  try {
                    var shippingNameEncoded = btoa(cart.shippingLineItem.shippingMethod.name);
                    if ( InCompatibleShippingMethods[shippingNameEncoded] ) {
                      CompatibleWithCurrentShipping = 0;
                    }
                  } catch (ex) {
                  }
              }

              if ( ! CompatibleWithCurrentShipping ) {
                container.style.display = 'none';
              } else if ( typeof MinMaxAllowed === 'object' && MinMaxAllowed[cartCurrencyId]['MinPriceValue'] && cart.grandTotal.amount < MinMaxAllowed[cartCurrencyId]['MinPriceValue'] ) {
                container.style.display = 'none';
              } else if ( typeof MinMaxAllowed === 'object' && MinMaxAllowed[cartCurrencyId]['MaxPriceValue'] && cart.grandTotal.amount > MinMaxAllowed[cartCurrencyId]['MaxPriceValue'] ) {
                container.style.display = 'none';
              } else {
                container.style.display = '';
              }

              if (window.paypal && e.type === 'cart:view') {
                initPayPal();
              }
            }

          }, timeout);
        };

    /*  */
    

    window.eComEventTarget.addEventListener('cart:view', function(event) {
      cart = event.detail.cart;
      shopLocale = event.detail.shopLocale;

      ppcpUpdateCart(event);
    });

    window.eComEventTarget.addEventListener('cart:update', function(event) {
      cart = event.detail.cart;

      ppcpUpdateCart(event);
    });

    script = document.createElement('script');
    script.src = 'https://www.paypal.com/sdk/js?integration-date=2022-03-24&client-id=AZBJxTEq4o-XZdu0dMj5Z6j6s5XMS-9XmgWDEMTYr_G1ZeOmvS8NdGY_1qZ5tdttYE5mqnm0Z_oNiaZ1&currency=EUR&intent=capture&components=buttons,funding-eligibility';
    script.setAttribute("defer", true);
    script.setAttribute('data-partner-attribution-id', 'ePages_GoDaddy_PPCP');
    document.body.appendChild(script);

    /*  */
    document.head.insertAdjacentHTML('beforeend', '<style>div#paypal-button-container span[id*="zoid-paypal-message"]{display: block;}</style>');
  }
})();

 
