Subversion Repositories wpShopGermany4

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8426 daniel 1
<?php
2
 
3
	declare(strict_types=1);
4
 
5
	/**
6
	 * @author: daniel
7
	 * @date: 05.03.24
8
	 * @time: 07:20
9
	 */
10
 
11
	namespace wpsgKlarna;
12
 
13
	$view = $this->view['wpsg_mod_klarna'];
14
 
15
	/** @var \wpsg_order $oOrder */
16
	$oOrder = $view['oOrder'];
17
 
18
	$authorize_arg = [
19
		'billing_address' => [
20
			'given_name' => $oOrder->getInvoiceFirstName(),
21
			'family_name' => $oOrder->getInvoiceName(),
22
			'email' => $oOrder->getCustomer()->getEMail(),
23
			'street_address' => $oOrder->getInvoiceStreet(),
24
			'postal_code' => $oOrder->getInvoiceZip(),
25
			'city' => $oOrder->getInvoiceCity(),
26
		]
27
	];
28
 
29
?>
30
 
31
<script>
32
 
33
	window.klarnaAsyncCallback = () => {
34
 
35
		const el_pppl_loading_blocker = document.getElementById('pppl_loading_blocker');
36
 
37
		Klarna.Payments.init({
38
			client_token: '<?php echo $view['client_token']; ?>'
39
		});
40
 
41
		Klarna.Payments.load({
42
			container: '#wpsg_mod_klarna_widget'
43
		}, {
44
 
45
		}, function (res) {
46
 
47
			console.log(res);
48
 
49
		});
50
 
51
		document.getElementById('<?php echo $view['button_id']; ?>').addEventListener('click', (event) => {
52
 
53
			event.preventDefault();
54
 
55
			if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'block';
56
 
57
			Klarna.Payments.authorize(
58
				{}, {},
59
				function(res) {
60
 
61
					console.log(res);
62
 
63
					if (res.authorization_token) {
64
 
65
						try {
66
 
67
							fetch('<?php echo $view['url_setAuthToken']; ?>', {
68
								method: 'POST',
69
								headers: {
70
									"Content-Type": "application/json"
71
								},
72
								body: JSON.stringify({
73
									'authorization_token': res.authorization_token
74
								})
75
							}).then(response => {
76
 
77
								return response.json();
78
 
79
							}).then(data => {
80
 
81
								if (data.success) {
82
 
83
									location.href = data.href;
84
 
85
								} else throw Error();
86
 
87
							}).catch(e => {
88
 
89
								if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'none';
90
 
91
							});
92
 
93
						} catch (error) {
94
 
95
							if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'none';
96
 
97
						}
98
 
99
					} else {
100
 
101
						if (el_pppl_loading_blocker) el_pppl_loading_blocker.style.display = 'none';
102
 
103
					}
104
 
105
				}
106
			);
107
 
108
		});
109
 
110
	};
111
 
112
</script>
113
 
114
<script src="https://x.klarnacdn.net/kp/lib/v1/api.js" async></script>