summaryrefslogtreecommitdiff
path: root/views/mods/mod_paypalapi/overview_button.phtml
blob: dfe54905d66a16f003592b37e52a329ad474225d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
	
	declare(strict_types=1);
	
	/**
	 * @author: daniel
	 * @date: 13.06.24
	 * @time: 14:48
	 */
	
	namespace wpsg;
	
?>

<?php GiroPayPalAPI::getInstance()->integrateSDK(); ?>


<div style="width:300px; float:right;">
	<div id="giropay-btn"></div>
</div>

<script>
	
	paypal.Buttons({
		fundingSource: paypal.FUNDING.GIROPAY,
		style: {
			label: "pay",
		},
		createOrder() {
			
			const form = document.getElementById('wpsg_form');
   
			if (form.checkValidity()) {
			
				return fetch("<?php echo $this->getUrl(\wpsg_ShopController::URL_BASKET, 'wpsg_mod_paypalapi', 'giroPayCreateOrderFromSession', [ ], true); ?>", {
					method: "POST",
					headers: {
						"Content-Type": "application/json",
					},
					body: { }
				})
				.then((response) => response.json())
				.then((order) => order.id);

			} else form.reportValidity();
			
		},
		onApprove(data) {
			
			return fetch("<?php echo $this->getUrl(\wpsg_ShopController::URL_BASKET, 'wpsg_mod_paypalapi', 'giroPayApproveOrder', [], true); ?>", {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
				},
				body: JSON.stringify({
					orderID: data.orderID
				})
			})
				.then((response) => response.json())
				.then((orderData) => {
				
					location.href = orderData.order_done_url;
					
					// Successful capture! For dev/demo purposes:
					// console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
					
					// const transaction = orderData.order_paypal.purchase_units[0].payments.captures[0];
					
					//console.log('Transaction Status:',	transaction.status);
					//console.log('Transaction ID:', transaction.id);
					
			});
			
		},
		onCancel(data, actions) {
 
			console.log(`Order Canceled - ID: ${data.orderID}`);
			
			location.href = '<?php echo $this->getUrl(\wpsg_ShopController::URL_BASKET, 'wpsg_mod_paypalapi', 'giroPayCancelOrder', [], true); ?>';
			
		},
		onError(err) {
			
			<?php if ($GLOBALS['wpsg_sc']->get_option('wpsg_debugModus') === "1") { ?>console.error(err);<?php } ?>
			
		}
	}).render("#giropay-btn");
	
</script>