<?php
$url = salesloo_url_payment(salesloo_vars('__invoice'));
?>
<div id="paypal-button" class="flex w-full mt-5">

</div>

<script src="https://www.paypal.com/sdk/js?client-id=<?php echo salesloo_get_option('paypal_client_id'); ?>"></script>
<script>
    paypal.Buttons({
        createOrder: function(data, actions) {

            return fetch('<?php echo $url; ?>?pay', {
                method: 'POST',
                headers: {
                    'content-type': 'application/json'
                },
                body: JSON.stringify({
                    __nonce: '<?php echo wp_create_nonce('paypal-create-order'); ?>'
                })
            }).then(function(res) {
                return res.json();
            }).then(function(data) {
                return data.result.id
            });
        },
        onApprove: function(data, actions) {
            location.replace('<?php echo $url; ?>?token=' + data.orderID + '&PayerID=' + data.payerID);
        }
    }).render('#paypal-button');
</script>