@php
$store = request()->store;
$provider_slug = 'paypal_'.strtolower($store['ref_prefix']);
$provider["provider"] = $provider_slug;
$PaymentDetails = (new \twa\ecomcart\controllers\EcomPurchasesController)->getGatewayCurrency($total,$provider);

$currency = $PaymentDetails['currency'];
$total = $PaymentDetails['value'];

@endphp

<div id="paypal-payment-btn"></div>

@php
$config = config('omnipay.'.$provider_slug);
if($config && array_key_exists('data',$config) && array_key_exists('client_id',$config['data']) ){
$client_id = $config['data']['client_id'];
}

@endphp



<script src="https://www.paypal.com/sdk/js?client-id={{$client_id}}&currency={{$currency}}&intent=authorize">
</script>
<script>
    paypal.Buttons({
        style : { color : 'white', height : 55 },
        createOrder: function(data, actions) {
            if($('#mainform')[0].checkValidity()) {
                return actions.order.create({
                    intent : "AUTHORIZE",
                    purchase_units: [
                        {
                            amount: {
                                    value: {{ round($total,2) }}
                            },
                        }
                    ],
                    application_context: {
                        shipping_preference : "NO_SHIPPING"
                    }
                });
            }else{
                alert('billing address is required');
                return false;
            }
        },
        
        onApprove: function(data, actions) {
            try { ShowLoader(); } catch (error) {
            $('body').append('<div class="dimmer" style="display:flex; align-items: center ; justify-content: center; font-size: 16px; color: #000"> Please be patient! We are validating your transaction! </div>');
            }
            actions.order.authorize().then(function(authorization) {
            var authorizationID = authorization.purchase_units[0].payments.authorizations[0].id
              $('<input>').attr({ type: 'hidden', name: 'gateway_init' , value : authorizationID }).appendTo('#mainform');
              $('#payment-submit').show();
              $('#paypal-payment-btn').hide();
              $('#payment-submit').click();
            });
        }
    }).render( '#paypal-payment-btn');

</script>