GM_Alex / Glittersport

// ==UserScript==
// @name         Glittersport
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Unicorns!!!
// @author       Benjamin Gutmann & Alexander Schneider
// @match        https://shop.ritter-sport.de/b2c/checkout/onepage/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var actions = [
        {
            url: 'https://shop.ritter-sport.de/b2c/checkout/onepage/saveShipping/',
            data: jQuery('#co-billing-form').serialize(),
            postCall: function(data) {
                alert('Bestellung saveShipping erfolgreich gesetzt');
                checkout.loadWaiting = false;
            }
        },
        {
            url: 'https://shop.ritter-sport.de/b2c/checkout/onepage/saveShippingMethod/',
            data: {
                shipping_method: 'tablerate_with_tax_bestway'
            },
            postCall: function(data) {
                alert('Bestellung saveShippingMethod erfolgreich gesetzt');
                checkout.loadWaiting = false;
            }
        },
        {
            url: 'https://shop.ritter-sport.de/b2c/checkout/onepage/savePayment/',
            data: {
                'payment[method]': 'bankpayment'
            },
            postCall: function(data) {
                alert('Bestellung savePayment erfolgreich gesetzt');
                jQuery("body").append(data.html);

                triggerActions([
                    {
                        url: review.saveUrl,
                        data: {
                            'agreement[1]': 1
                        },
                        postCall: function(data) {
                            alert('Bestellung erfolgreich');
                        }
                    }
                ]);
            }
        }
    ];

    var triggerActions = function(actions) {
        var action = actions[0];

        jQuery.ajax({
            type: 'POST',
            url: action.url,
            data: action.data,
            success: function(data) {
                action.postCall(data);
                actions.shift();

                if (actions.length > 0) {
                    triggerActions(actions);
                }
            },
            error: function() {
                triggerActions(actions);
            }
        });
    };


    var checkout = new Checkout(accordion, {
        progress: 'https://shop.ritter-sport.de/b2c/checkout/onepage/progress/',
        review: 'https://shop.ritter-sport.de/b2c/checkout/onepage/review/',
        saveMethod: 'https://shop.ritter-sport.de/b2c/checkout/onepage/saveMethod/',
        failure: '#'
    });
    checkout.loadWaiting = false;

    var $button = jQuery('<div>').css({
            width: '40px',
            height: '40px',
            position: 'fixed',
            bottom: 0,
            left: 0,
            cursor: 'pointer',
            'background-color': 'red',
            'z-index':9999
        })
        .addClass('ctaButton')
        .click(function() {
            jQuery(this).css('background-color', 'green');
            triggerActions(actions);
        });

    jQuery('body').append($button);
})();