Subversion Repositories wpShopGermany4

Rev

Blame | Last modification | View Log | RSS feed

<?php
        
        declare(strict_types=1);
        
        /**
         * @author: daniel
         * @date: 05.03.24
         * @time: 07:20
         */
        
        namespace wpsgKlarna;
 
        $view = $this->view['wpsg_mod_klarna'];
        
        /** @var \wpsg_order $oOrder */
        $oOrder = $view['oOrder'];
        
        $authorize_arg = [
                'billing_address' => [
                        'given_name' => $oOrder->getInvoiceFirstName(),
                        'family_name' => $oOrder->getInvoiceName(),
                        'email' => $oOrder->getCustomer()->getEMail(),
                        'street_address' => $oOrder->getInvoiceStreet(),
                        'postal_code' => $oOrder->getInvoiceZip(),
                        'city' => $oOrder->getInvoiceCity(),
                ]
        ];
        
?>

<script>
        
        window.klarnaAsyncCallback = () => {

                const el_pppl_loading_blocker = document.getElementById('pppl_loading_blocker');
                
                Klarna.Payments.init({
                        client_token: '<?php echo $view['client_token']; ?>'
                });
                
                Klarna.Payments.load({
                        container: '#wpsg_mod_klarna_widget'
                }, {
                
                }, function (res) {

                        console.log(res);
                        
                });
                
                document.getElementById('<?php echo $view['button_id']; ?>').addEventListener('click', (event) => {

                        event.preventDefault();
                        
                        if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'block';
                        
                        Klarna.Payments.authorize(
                                {}, {},
                                function(res) {
 
                                        console.log(res);
                                
                                        if (res.authorization_token) {

                                                try {
 
                                                        fetch('<?php echo $view['url_setAuthToken']; ?>', {
                                                                method: 'POST',
                                                                headers: {
                                                                        "Content-Type": "application/json"
                                                                },
                                                                body: JSON.stringify({
                                                                        'authorization_token': res.authorization_token
                                                                })
                                                        }).then(response => {

                                                                return response.json();

                                                        }).then(data => {

                                                                if (data.success) {

                                                                        location.href = data.href;

                                                                } else throw Error();

                                                        }).catch(e => {

                                                                if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'none';

                                                        });
                                                        
                                                } catch (error) {

                                                        if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'none';

                                                }
                                                
                                        } else {
                                                
                                                if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'none';
                                                
                                        }
                                        
                                }
                        );

                });

        };
        
</script>

<script src="https://x.klarnacdn.net/kp/lib/v1/api.js" async></script>