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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
<?php
/**
*
* Class ki skrbi za placila s paypalom
*
*/
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
class UserNarocilaPaypal{
private $narocilo;
private $paypal_client;
public function __construct($narocilo_id){
global $mysql_database_name;
if($narocilo_id > 0){
// Dobimo podatke narocila
$sqlNarocilo = sisplet_query("SELECT un.*, u.name, u.surname, u.email, up.name AS package_name, up.description AS package_description, up.price AS package_price
FROM user_access_narocilo un, users u, user_access_paket up
WHERE un.id='".$narocilo_id."' AND un.usr_id=u.id AND un.package_id=up.id");
if(mysqli_num_rows($sqlNarocilo) > 0){
$this->narocilo = mysqli_fetch_array($sqlNarocilo);
}
else{
die("Napaka pri komunikaciji s paypal! Narocilo ne obstaja.");
}
// Ustvarimo okolje za paypal
if($mysql_database_name == 'real1kasi')
$environment = new ProductionEnvironment(AppSettings::getInstance()->getSetting('paypal-client_id'), AppSettings::getInstance()->getSetting('paypal-secret'));
else
$environment = new SandboxEnvironment(AppSettings::getInstance()->getSetting('paypal-client_id'), AppSettings::getInstance()->getSetting('paypal-secret'));
$this->paypal_client = new PayPalHttpClient($environment);
}
else {
die("Napaka pri komunikaciji s paypal! Manjka ID naročila.");
}
}
// Placamo narocilo s paypal
public function paypalCreatePayment(){
global $site_url;
$response = array();
$UA = new UserNarocila();
$cena = $UA->getPrice($this->narocilo['package_name'], $this->narocilo['trajanje'], $this->narocilo['discount'], $this->narocilo['time']);
if($this->narocilo['trajanje'] == 1)
$months_string = 'mesec';
elseif($this->narocilo['trajanje'] == 2)
$months_string = 'meseca';
elseif($this->narocilo['trajanje'] == 3 || $this->narocilo['trajanje'] == 4)
$months_string = 'mesece';
else
$months_string = 'mesecev';
// Zavezanec iz tujine ima racun/predracun brez ddv
if($UA->isWithoutDDV($this->narocilo['id'])){
$ddv = 0;
$cena_za_placilo = $cena['final_without_tax'];
}
else{
$ddv = 1;
$cena_za_placilo = $cena['final'];
}
// Podatki narocila
$orderDetails = array(
'ime' => '1KA naročnina (paket '.strtoupper($this->narocilo['package_name']). ' - '.$this->narocilo['trajanje'].' '.$months_string.')',
'narocilo_id' => $this->narocilo['id'],
'cena' => $cena_za_placilo,
);
// Ustvarimo order na paypal, da se lahko potem user prijavi in ga placa
$paypal_response = $this->paypalCreateOrder($orderDetails);
if(!isset($paypal_response['success']) || $paypal_response['success'] == false){
return $paypal_response;
}
// Vstavimo plačilo v bazo
$sqlNarocilo = sisplet_query("INSERT INTO user_access_paypal_transaction
(transaction_id, narocilo_id, price, currency_type, time, status)
VALUES
('".$paypal_response['transaction_id']."', '".$this->narocilo['id']."', '".$cena_za_placilo."', 'EUR', NOW(), 'CREATED')
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
$response['paypal_link'] = $paypal_response['paypal_link'];
$response['success'] = true;
return $response;
}
// Posljemo podatke za placilo paypalu
private function paypalCreateOrder($orderDetails){
global $site_url;
global $lang;
$response = array();
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
//$request->headers["prefer"] = "return=representation";
if($lang['id'] == '2'){
$drupal_url_confirm = $site_url.'/d/en/purchase/paypal?narocilo_id='.$orderDetails['narocilo_id'];
$drupal_url_cancel = $site_url.'/d/en/purchase/paypal-cancel?narocilo_id='.$orderDetails['narocilo_id'];
}
else{
$drupal_url_confirm = $site_url.'/d/sl/narocilo/paypal?narocilo_id='.$orderDetails['narocilo_id'];
$drupal_url_cancel = $site_url.'/d/sl/narocilo/paypal-cancel?narocilo_id='.$orderDetails['narocilo_id'];
}
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => [[
"reference_id" => $orderDetails['narocilo_id'],
'description' => $orderDetails['ime'],
"amount" => [
"value" => $orderDetails['cena'],
"currency_code" => "EUR"
]
]],
"application_context" => [
"cancel_url" => $drupal_url_cancel,
"return_url" => $drupal_url_confirm,
'brand_name' => '1KA'
]
];
try {
// Poklicemo paypal api za ustvarjanje narocila
$paypal_response = $this->paypal_client->execute($request);
if($paypal_response->result->status != 'CREATED'){
$response['error'] = 'ERROR! Order was not created.';
$response['success'] = false;
return $response;
}
// Dobimo id paypal narocila
$response['transaction_id'] = $paypal_response->result->id;
// Dobimo link za preusmeritev stranke, da potrdi narocilo in potem lahko izvedemo "capture"
foreach($paypal_response->result->links as $link){
if($link->rel == 'approve')
$response['paypal_link'] = $link->href;
}
}
catch (HttpException $e) {
$response['error'] = $e->getMessage();
$response['success'] = false;
return $response;
}
$response['success'] = true;
return $response;
}
// Zakljucimo placilo, ce je bilo placilo ok odobreno preko paypala s strani stranke
public function paypalCaptureOrder(){
$response = array();
// Preverimo plačilo v bazo
$sqlNarociloPaypal = sisplet_query("SELECT transaction_id
FROM user_access_paypal_transaction
WHERE narocilo_id='".$this->narocilo['id']."'
");
if (!$sqlNarociloPaypal){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
// Narocilo ne obstaja (ni v bazi paypal narocil)
if (mysqli_num_rows($sqlNarociloPaypal) == 0){
$response['error'] = 'ERROR! Paypal order does not exist.';
$response['success'] = false;
return $response;
}
$rowNarociloPaypal = mysqli_fetch_array($sqlNarociloPaypal);
// Preverimo, ce je bilo vse ok placano - POST request to /v2/checkout/orders
$request = new OrdersCaptureRequest($rowNarociloPaypal['transaction_id']);
//$request->prefer('return=representation');
try {
// Poklicemo paypal api kjer preverimo placilo narocila
$paypal_response = $this->paypal_client->execute($request);
}
catch (HttpException $e) {
$response['error'] = $e->getMessage();
$response['success'] = false;
return $response;
}
// Posodobimo status narocila
$sqlNarocilo = sisplet_query("UPDATE user_access_paypal_transaction
SET status='".$paypal_response->result->status."'
WHERE transaction_id='".$paypal_response->result->id."'
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
// Nastavimo narocilo na placano, aktiviramo paket in vrnemo racun
$narocilo = new UserNarocila();
$payment_response = $narocilo->payNarocilo($this->narocilo['id']);
if($payment_response['success'] == true){
$response['racun'] = $payment_response['racun'];
$response['success'] = true;
}
else{
$response['error'] = $payment_response['error'];
$response['success'] = false;
}
$response['narocilo_id'] = $this->narocilo['id'];
$response['success'] = true;
return $response;
}
// Preklicemo placilo, ce je bilo placilo preklicano preko paypala s strani stranke
public function paypalCancelOrder(){
$response = array();
// Posodobimo status narocila
$sqlNarocilo = sisplet_query("UPDATE user_access_paypal_transaction
SET status='CANCELLED'
WHERE narocilo_id='".$this->narocilo['id']."'
");
if (!$sqlNarocilo){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
// Nastavimo status narocila na storniran
$sqlNarociloStatus = sisplet_query("UPDATE user_access_narocilo SET status='2' WHERE id='".$this->narocilo['id']."'");
if (!$sqlNarociloStatus){
$response['error'] = 'ERROR! '.mysqli_error($GLOBALS['connect_db']);
$response['success'] = false;
return $response;
}
$response['success'] = true;
return $response;
}
}
|