{if !$ppcp_script_tag_rendered}

    <script type="application/json" fncls="fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99">
        {
            "f":"{$fraudnet_session_identifier}",
            "s":"{$source_website_identifier}",
            "sandbox":{$sandbox}
        }
    </script>
    <noscript>
        <img src="https://c.paypal.com/v1/r/d/b/ns?f={$fraudnet_session_identifier}&s={$source_website_identifier}&js=0&r=1" />
    </noscript>

    <script>

        console.log('paypal-checkout-script-tag setting ppcp constants');

        window.paypal_checkout_constant =
            {
                BUTTON_SIZE: {$button_size|intval},
                BUTTON_COLOR: "{$button_color}",
                BUTTON_SHAPE: "{$button_shape}",
                BUTTON_TYPE_AP: "{$button_type_ap}",
                version: "{$plugin_version}",
                language: "{$lng}",
                language_short: "{$lng_short}",
                currency: "{$currency}",
                TEXT_ERROR_CONDITIONS_ACCEPTED: "{txt key=ERROR_CONDITIONS_ACCEPTED}",
                TEXT_SHIPPING_COSTS: "{txt key=TEXT_SHIPPING_COSTS}",
                WARNING_NO_SHIPPING_FOR_ZONE: "{txt key=WARNING_NO_SHIPPING_FOR_ZONE}",
                PPCP_SESSION_ID: "{$ppcp_session_id}",
                googlePaymentsEnvironment: "{$googlePaymentsEnvironment}"
            }

        let billingContact_ap = {$billingContact_ap};
        let shippingContact_ap = {$shippingContact_ap};
        let currentCountryCode_ap = "{$country}";
        let currentTotal_ap = 0;
        let totalLabel_ap = "{$totalLabel}";
        let subTotalLabel_ap = "{$subTotalLabel}";

        const ppcpSetTimeout = 50;
        const ppcpMaxTimeout = 10000;
        let ppcpWaited = 0;
        async function checkPaypalScriptLoaded()
        {
            ppcpWaited += ppcpSetTimeout;
            if(ppcpWaited >= ppcpMaxTimeout)
            {
                console.warn("waited for paypal sdk " + ppcpWaited + " ms so far. ABORTING");
                return;
            }
            if (typeof window.paypal === "object")
            {
                console.info("paypal sdk loaded after " + ppcpWaited + " ms. emitting event PayPalSdkLoaded");
                const event = new Event("PayPalSdkLoaded");
                document.dispatchEvent(event);
            }
            else {
                console.debug("waiting for paypal sdk " + ppcpWaited + " ms so far");
                setTimeout(checkPaypalScriptLoaded, ppcpSetTimeout);
            }
        }

        console.log("script tag. adding DOMContentLoaded listener");
        document.addEventListener('DOMContentLoaded', function ()
        {
            console.log('paypal-checkout-script-tag  DOMContentLoaded')
            try {
                const terms_cb = document.querySelector('input[type=checkbox][name=conditions_accepted]');
                if(terms_cb)
                {
                    let div = document.createElement('div');
                    div.innerText = window.paypal_checkout_constant.TEXT_ERROR_CONDITIONS_ACCEPTED;
                    div.id = 'TEXT_ERROR_CONDITIONS_ACCEPTED';
                    div.classList.add('alert', 'alert-danger');
                    div.style.display = 'none';
                    terms_cb.closest("div").prepend(div);

                    terms_cb.addEventListener('change', (e) => {
                        if (e.currentTarget.checked) {
                            document.getElementById('TEXT_ERROR_CONDITIONS_ACCEPTED').style.display = 'none';
                        } else {
                            document.getElementById('TEXT_ERROR_CONDITIONS_ACCEPTED').style.display = 'block';
                        }
                    })
                }
                console.log("ppcp display.php DOMContentLoaded. injecting ppcp sdk script");

                const paypal_script = "https://www.paypal.com/sdk/js?{$query_params}";
                let script = document.createElement("script");
                script.setAttribute("src", paypal_script);
                script.setAttribute("data-partner-attribution-id", "{$partner_attribution_id}");
                script.setAttribute("data-client-token", "{$client_token}");
                script.setAttribute("data-user-id-token","{$user_token}");
                script.setAttribute("onload", "console.log('PayPalSdkLoaded'); let ppcplevent = new Event('PayPalSdkLoaded'); document.dispatchEvent(ppcplevent);");
                document.head.appendChild(script);

                // apple pay laden
                const apple_script = "https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js";
                script = document.createElement("script");
                script.setAttribute("src", apple_script);
                document.head.appendChild(script);

                // fraudnet nachladen
                const fraudnet_script = "https://c.paypal.com/da/r/fb.js";
                script = document.createElement("script");
                script.setAttribute("src", fraudnet_script);
                document.head.appendChild(script);

                //checkPaypalScriptLoaded();
            }
            catch(e)
            {
                console.log(e);
            }
        });

        document.addEventListener('PayPalSdkLoaded', function ()
        {
            console.log("PayPalSdkLoaded. injecting googlepay sdk script");
            // google pay laden
            const google_script = "https://pay.google.com/gp/p/js/pay.js";
            script = document.createElement("script");
            script.setAttribute("src", google_script);
            script.setAttribute("async", "");
            script.setAttribute("onload", "console.log('GooglePaySdkLoaded'); let gpslevent = new Event('GooglePaySdkLoaded'); document.dispatchEvent(gpslevent);");
            document.head.appendChild(script);
        });

    </script>

    {assign var=ppcp_script_tag_rendered value=1 scope="global"}
{/if}
