-
Notifications
You must be signed in to change notification settings - Fork 3
/
pay.php
149 lines (122 loc) · 3.8 KB
/
pay.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
require_once 'loadCredentials.php';
$Payone = new \Payone\Payone([
'mid' => $_ENV['PAYONE_MERCHANT_ID'],
'portalid' => $_ENV['PAYONE_PORTAL_ID'],
'aid' => $_ENV['PAYONE_SUBACCOUNT_ID'],
'key' => $_ENV['PAYONE_KEY'],
'mode' => $_ENV['PAYONE_MODE'],
'integrator_name' => $_ENV['PAYONE_INTEGRATOR_NAME']
]);
$postBody = json_decode(file_get_contents('php://input'), true);
$paymentMethod = $postBody['paymentMethod'];
$surname = $postBody['surname'];
$country = $postBody['country'];
$frontendCartItems = $postBody['items'];
$cartItems = [];
foreach ($frontendCartItems as $frontendCartItem) {
$cartItem = getItemById($frontendCartItem['id']);
$cartItem['quantity'] = $frontendCartItem['quantity'];
$cartItems[] = $cartItem;
}
$backToAppUrl = "";
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { // if it's a Gitpod instance, then use Gitpod's workspace URL; else - ours from, most probably, localhost
$backToAppUrl = "https://{$_SERVER['HTTP_X_FORWARDED_HOST']}";
} else {
$backToAppUrl = "http://{$_SERVER['HTTP_HOST']}";
}
$payoneResponse = null;
if ($paymentMethod === 'card') {
$pseudoCardPAN = $postBody['pseudoCardPan'];
$payoneResponse = $Payone->sendRequest([
// Payment data
'request' => 'preauthorization',
'clearingtype' => 'cc',
'reference' => uniqid(),
'amount' => getTotalPriceForItems42And43(),
'currency' => 'EUR',
// Customer data
'lastname' => $surname,
'country' => $country,
// Cart items
'items' => $cartItems,
// Redirect URLs
'successurl' => "$backToAppUrl?paymentStatus=success",
'errorurl' => "$backToAppUrl?paymentStatus=error",
'backurl' => "$backToAppUrl?paymentStatus=back",
// Payment data specific to 'clearingtype'
'pseudocardpan' => $pseudoCardPAN
]);
} else if ($paymentMethod === 'paypal') {
$payoneResponse = $Payone->sendRequest([
// Payment data
'request' => 'preauthorization',
'clearingtype' => 'wlt',
'reference' => uniqid(),
'amount' => getTotalPriceForItems42And43(),
'currency' => 'EUR',
// Customer data
'lastname' => $surname,
'country' => $country,
// Cart items
'items' => $cartItems,
// Redirect URLs
'successurl' => "$backToAppUrl?paymentStatus=success",
'errorurl' => "$backToAppUrl?paymentStatus=error",
'backurl' => "$backToAppUrl?paymentStatus=back",
// Payment data specific to 'clearingtype'
'wallettype' => 'PPE'
]);
} else if ($paymentMethod === 'paydirekt') {
$firstName = $postBody['firstName'];
$zip = $postBody['zip'];
$city = $postBody['city'];
$payoneResponse = $Payone->sendRequest([
// Payment data
'request' => 'preauthorization',
'clearingtype' => 'wlt',
'reference' => uniqid(),
'amount' => getTotalPriceForItems42And43(),
'currency' => 'EUR',
// Customer data
'lastname' => $surname,
'country' => $country,
// Cart items
'items' => $cartItems,
// Redirect URLs
'successurl' => "$backToAppUrl?paymentStatus=success",
'errorurl' => "$backToAppUrl?paymentStatus=error",
'backurl' => "$backToAppUrl?paymentStatus=back",
// Payment data specific to 'clearingtype'
'wallettype' => 'PDT',
'shipping_firstname' => $firstName,
'shipping_lastname' => $surname,
'shipping_zip' => $zip,
'shipping_city' => $city,
'shipping_country' => $country,
]);
}
echo json_encode($payoneResponse);
function getItemById($id) {
if ($id === 42) {
return [
'id' => 42,
'type' => 'goods',
'SKU' => 'BR-SKA-551-12',
'price' => 1500,
'description' => 'Cool item #1',
];
}
if ($id === 43) {
return [
'id' => 43,
'type' => 'goods',
'SKU' => 'QL-NBB-477-48',
'price' => 840,
'description' => 'Cool item #1',
];
}
}
function getTotalPriceForItems42And43() {
return 3840;
}