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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
<?php /******************************************** * ReviewOrder.php * * This file is called after the user clicks on a button during * the checkout process to use PayPal's Express Checkout. The * user logs in to their PayPal account. * * This file is called twice. * * On the first pass, the code executes the if statement: * * if (! isset ($token)) * * The code collects transaction parameters from the form * displayed by SetExpressCheckout.html then constructs and * sends a SetExpressCheckout request string to the PayPal * server. The paymentType variable becomes the PAYMENTACTION * parameter of the request string. The RETURNURL parameter * is set to this file; this is how ReviewOrder.php is called * twice. * * On the second pass, the code executes the else statement. * * On the first pass, the buyer completed the authorization in * their PayPal account; now the code gets the payer details * by sending a GetExpressCheckoutDetails request to the PayPal * server. Then the code calls GetExpressCheckoutDetails.php. * * Note: Be sure to check the value of PAYPAL_URL. The buyer is * sent to this URL to authorize payment with their PayPal * account. For testing purposes, this should be set to the * PayPal sandbox. * * Called by SetExpressCheckout.html. * * Calls GetExpressCheckoutDetails.php, CallerService.php, * and APIError.php. ********************************************/
require_once 'CallerService.php'; require_once '../webadmin/basic_info.php';
$session_id = session_id();
$session_data = $_SESSION; if (!empty($session_data["nvpReqArray"]["PWD"])) { $session_data["nvpReqArray"]["PWD"] = ""; } if (!empty($session_data["nvpReqArray"]["USER"])) { $session_data["nvpReqArray"]["USER"] = ""; } if (!empty($session_data["nvpReqArray"]["SIGNATURE"])) { $session_data["nvpReqArray"]["SIGNATURE"] = ""; }
if(isset($_SESSION["langcode"])){ $langcode = $_SESSION["langcode"]; }else{ if(isset($_GET["langcode"])){ $langcode = $_GET["langcode"]; $_SESSION["langcode"] = $langcode; }else{ $langcode = "en"; $_SESSION["langcode"] = $langcode; } }
if(!empty($_SESSION["warehousing_web"])){ $warehousing_date = $_SESSION["warehousing_web"]["warehousing_date"]; $date = new DateTime($warehousing_date); $date->modify('+1 month'); $recurring_payment_start_date = $date->format("Y-m-d");
$rent_month = $_SESSION["warehousing_web"]["rent_month"]; }else{ $recurring_payment_start_date = date("Y-m-d"); $rent_month = 0; }
if ($langcode == "tc") { $LOCALECODE = "zh_HK"; $total_months_fee_text = "所有月份租金"; $first_month_fee_text = "首月租金"; $deposit_text = "按金"; $new_customer_text = "新客戶"; $discount_text = "首月半價"; $recurring_payment_text = "租金定期自動付款 - 開始日期: ".$recurring_payment_start_date.",每月自動付款 $".$_SESSION["warehousing_web"]["discounted_price"].",共 ".($rent_month-1)." 個月"; $total_amount_zero_text = "此帳單已支付。"; $hold_room_error = "你未能在指定限時內完成網上租倉,預留給你的倉庫單位已被釋出,請重新進行租倉流程。"; } else { $LOCALECODE = "US"; $total_months_fee_text = "Total Months Fee"; $first_month_fee_text = "First Month Fee"; $deposit_text = "Deposit"; $new_customer_text = "New Customer"; $discount_text = "First Month Fee With 50% Discount"; $recurring_payment_text = "Monthly Rent Fees Recurring Payment - Start From: ".$recurring_payment_start_date." Auto Paid $".$_SESSION["warehousing_web"]["discounted_price"]." Monthly, Charge ".($rent_month-1)." Months."; $total_amount_zero_text = "You have paid this bill."; $hold_room_error = "You do not complete the online storage renting in a limit time, the room which reserves for you is released. Please redo the online storage renting again."; }
$subtotal = 0; $v_total = 0; $nvpstr = "";
$i = 0;
if (isset($_GET["id"]) && (int)$_GET["id"] > 0 && !empty($_GET["type"])) { //user click email paypal link
$email_location = "/../".$langcode."/rent_email.php?type=INVOICE&id=" . (int)$_GET["id"]; $cancelurl = "/../".$langcode."/index.php"; $errorurl = "/../".$langcode."/index.php?error=1";
$id = (int)$_GET["id"];
$balance = 0; $code = ""; if ($_GET["type"] == "INVOICE") { $invoice_info = get_invoice($id); $balance = $invoice_info["invoice_balance"]; $code = $invoice_info["invoice_code"]; }
if ($_GET["type"] == "DEPOSIT") { $deposit_info = get_deposit($id); $balance = $deposit_info["deposit_balance"]; $code = $deposit_info["invoice_code"]; }
$type_info = get_master_type_code("PAYMENT_TYPE", $_GET["type"]);
$subtotal = $balance; $v_total = $balance;
$nvpstr .= "&L_NAME$i=" . urlencode($type_info["name_" . $langcode]); $nvpstr .= "&L_AMT$i=" . urlencode($balance); //HKD $nvpstr .= "&L_QTY$i=" . urlencode(1); $nvpstr .= "&L_NUMBER$i=" . urlencode($code); $i++; } else { //customer online rent room and do payment
$email_location = "../" . $langcode . "/rent_email.php"; $cancelurl = "/../" . $langcode . "/confirm_rent.php"; $errorurl = "/../" . $langcode . "/confirm_rent.php?error=1";
if (!empty($_SESSION["warehousing_web"])) {
//final check the room status $sql = "select * from room where status = ? and online_hold_time < ? and session_id = ? and id = ?"; $parameters = array("OPEN_FOR_INTERNAL", date("Y-m-d H:i:s"), session_id(), $_SESSION["warehousing_web"]["room_id"]); $check_room = bind_pdo($sql, $parameters, "selectone");
if(!empty($check_room)){ echo "<script>alert('".$hold_room_error."'); window.location.href='../".$langcode."/rent.php?id=".$_SESSION["warehousing_web"]["master_room_id"]."';</script>"; exit; }
if ($_SESSION["warehousing_web"]["prepaid"] == 1) { $nvpstr .= "&L_NAME$i=" . urlencode($total_months_fee_text); $nvpstr .= "&L_AMT$i=" . urlencode($_SESSION["warehousing_web"]["discounted_price"]); //HKD $nvpstr .= "&L_QTY$i=" . urlencode($_SESSION["warehousing_web"]["rent_month"]);
$subtotal += $_SESSION["warehousing_web"]["discounted_price"] * $_SESSION["warehousing_web"]["rent_month"]; $i++; } else { //only first month $nvpstr .= "&L_NAME$i=" . urlencode($first_month_fee_text); $nvpstr .= "&L_AMT$i=" . urlencode($_SESSION["warehousing_web"]["discounted_price"]); //HKD $nvpstr .= "&L_QTY$i=" . urlencode(1);
$subtotal += $_SESSION["warehousing_web"]["discounted_price"]; $i++; }
$nvpstr .= "&L_NAME$i=" . urlencode($deposit_text); $nvpstr .= "&L_AMT$i=" . urlencode($_SESSION["warehousing_web"]["deposit"]); //HKD $nvpstr .= "&L_QTY$i=" . urlencode(1);
$subtotal += $_SESSION["warehousing_web"]["deposit"]; $i++;
if (!empty($_SESSION["warehousing_web"]["order_product"])) { foreach ($_SESSION["warehousing_web"]["order_product"] as $product) { $product_info = get_product($product["product_id"]);
$nvpstr .= "&L_NAME$i=" . urlencode($product_info["name_" . $langcode]); $nvpstr .= "&L_AMT$i=" . urlencode($product["price"]); //HKD $nvpstr .= "&L_QTY$i=" . urlencode($product["qty"]); $nvpstr .= "&L_NUMBER$i=" . urlencode($product_info["code"]);
$subtotal += $product["price"] * $product["qty"];
$i++; } }
if ($_SESSION["warehousing_web"]["first_month_half_price"] > 0) { $nvpstr .= "&L_NAME$i=" . urlencode($discount_text); $nvpstr .= "&L_AMT$i=" . urlencode(-$_SESSION["warehousing_web"]["first_month_half_price"]); //HKD $nvpstr .= "&L_QTY$i=" . urlencode(1); $nvpstr .= "&L_DESC$i=" . urlencode($new_customer_text);
$subtotal -= $_SESSION["warehousing_web"]["first_month_half_price"]; $i++; }
$v_total = $subtotal;
$i++; } }
if (!$_SESSION["warehousing_web"]["auto_paid"]) { //normal express checkout
//TODO: process order with zero amount if ($v_total < 0.1) { echo "<script>alert('".$total_amount_zero_text."'); window.location.href='../".$langcode."/index.php';</script>"; exit; }
$token = $_REQUEST['token'];
if (!isset($token)) {
/* The servername and serverport tells PayPal where the buyer should be directed back to after authorizing payment. In this case, its the local webserver that is running this script Using the servername and serverport, the return URL is the first portion of the URL that buyers will return to after authorizing payment */ $serverName = $_SERVER['SERVER_NAME']; $serverPort = $_SERVER['SERVER_PORT']; $url = dirname('http://' . $serverName . ':' . $serverPort . $_SERVER['REQUEST_URI']);
$nvpstr .= "&LOCALECODE=" . urlencode($LOCALECODE); $currency = "HKD"; $nvpstr .= "&CURRENCYCODE=" . urlencode($currency); $nvpstr .= "&ITEMAMT=" . urlencode($subtotal); $nvpstr .= "&NOSHIPPING=1"; $nvpstr .= "&AMT=" . urlencode($v_total); $nvpstr .= "&ReturnUrl=" . urlencode($url . "/ReviewOrder.php"); $nvpstr .= "&cancelURL=" . urlencode($url . $cancelurl); $nvpstr .= "&PAYMENTACTION=Sale";
/* Make the call to PayPal to set the Express Checkout token If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment. If an error occured, show the resulting errors */ $resArray = hash_call("SetExpressCheckout", $nvpstr); $_SESSION['reshash'] = $resArray;
$ack = strtoupper($resArray["ACK"]);
if ($ack == "SUCCESS") {
$sql = "insert into paypal_record (session_id, order_id, nvpstr, createdate) values (?, ?, ?, ?)"; $parameters = array($session_id, $order_info{"order_id"}, $nvpstr, $nowdate); if (!($sth = $dbh->prepare($sql))) { throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo())); }
if (!$sth->execute($parameters)) { throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo())); }
$_SESSION['paypalid'] = $dbh->lastInsertId();
if (isset($_SESSION['paypalid']) && !empty($_SESSION['paypalid'])) { // Redirect to paypal.com here $token = urldecode($resArray["TOKEN"]); $payPalURL = PAYPAL_URL . $token; header("Location: " . $payPalURL); } else { $_SESSION['my_error_no'] = 'Unexpected SQL error in paypal record insert'; header("Location: APIError.php"); exit; } } else { $_SESSION['my_error_no'] = 'Unexpected ACK in SetExpressCheckout'; header("Location: APIError.php"); exit;
}
} else { /* At this point, the buyer has completed in authorizing payment at PayPal. The script will now call PayPal with the details of the authorization, incuding any shipping information of the buyer. Remember, the authorization is not a completed transaction at this state - the buyer still needs an additional step to finalize the transaction */
$token = urlencode($_REQUEST['token']);
/* Build a second API request to PayPal, using the token as the ID to get the details on the payment authorization */ $nvpstr = "&TOKEN=" . $token;
//$nvpstr = $nvpHeader.$nvpstr; /* Make the API call and store the results in an array. If the call was a success, show the authorization details, and provide an action to complete the payment. If failed, show the error */ $resArray = hash_call("GetExpressCheckoutDetails", $nvpstr); $_SESSION['reshash'] = $resArray; $ack = strtoupper($resArray["ACK"]);
if ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') { $_SESSION['token'] = $_REQUEST['token']; $_SESSION['payer_id'] = $_REQUEST['PayerID'];
ini_set('session.bug_compat_42', 0); ini_set('session.bug_compat_warn', 0);
$serverName = $_SERVER['SERVER_NAME']; $resArray = $_SESSION['reshash'];
$nvpstr .= "&ITEMAMT=" . urlencode($resArray['ITEMAMT']); $nvpstr .= "&NOSHIPPING=" . urlencode($resArray['NOSHIPPING']); $nvpstr .= "&AMT=" . urlencode($resArray['AMT']); $nvpstr .= "&TOKEN=" . urlencode($resArray['TOKEN']); $nvpstr .= "&PAYERID=" . urlencode($resArray['PAYERID']); $nvpstr .= "&PAYMENTACTION=" . urlencode("Sale"); $nvpstr .= "&CURRENCYCODE=" . urlencode($resArray['CURRENCYCODE']); $nvpstr .= "&IPADDRESS=" . urlencode($serverName);
//TODO: process order with zero amount if ($resArray['AMT'] < 0.1 && $v_total < 0.1) { header("Location: " . $errorurl); exit; }
$resArray_str = var_export($resArray, true); $resArray = hash_call("DoExpressCheckoutPayment", $nvpstr);
$ack = strtoupper($resArray["ACK"]);
if ($ack != 'SUCCESS' && $ack != 'SUCCESSWITHWARNING') { $resArray["PassVar"] = $nvpstr; $_SESSION['reshash'] = $resArray; $_SESSION['my_error_no'] = 'Unexpected ACK in DoExpressCheckoutPayment'; header("Location: APIError.php"); }
//if (!empty($resArray['PAYMENTINFO_0_TRANSACTIONID']) && $resArray['PAYMENTINFO_0_PAYMENTSTATUS'] == 'Completed') { //paypal successful // Update DB record $sql = "update paypal_record set paystatus = ?, nvpstr2 = ? where paypal_id=?"; $parameters = array("1", $resArray_str, $_SESSION['paypalid']); if (!($sth = $dbh->prepare($sql))) { throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo())); }
if (!$sth->execute($parameters)) { throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo())); } else { $_SESSION["paystatus"] = 1; header("Location: " . $email_location); exit; } //}else{ //$_SESSION['my_error_no'] = 'Unexpected Payment Info in DoExpressCheckoutPayment'; //header("Location: APIError.php"); //}
} else { //Redirecting to APIError.php to display errors. $_SESSION['my_error_no'] = 'Unexpected ACK in GetExpressCheckoutDetails'; header("Location: APIError.php");
}
} } else { //recurring payment with express checkout if (!isset($_REQUEST['token'])) {
//total amount checking if ($v_total < 0.1 || $_SESSION["warehousing_web"]["discounted_price"] < 0.1) { echo "<script>alert('".$total_amount_zero_text."'); window.location.href='../".$langcode."/index.php';</script>"; exit; }
$serverName = $_SERVER['SERVER_NAME']; $serverPort = $_SERVER['SERVER_PORT']; $url = dirname('http://' . $serverName . ':' . $serverPort . $_SERVER['REQUEST_URI']);
$nvpstr .= "&L_BILLINGTYPE0=" . urlencode("RecurringPayments"); $nvpstr .= "&L_BILLINGAGREEMENTDESCRIPTION0=" . urlencode($recurring_payment_text); $nvpstr .= "&NOSHIPPING=1"; $nvpstr .= "&LOCALECODE=" . urlencode($LOCALECODE); $currency = "HKD"; $nvpstr .= "&CURRENCYCODE=" . urlencode($currency); $nvpstr .= "&ITEMAMT=" . urlencode($subtotal); $nvpstr .= "&AMT=" . urlencode($v_total); $nvpstr .= "&PAYMENTACTION=Sale"; $nvpstr .= "&ReturnUrl=" . urlencode($url . "/ReviewOrder.php"); $nvpstr .= "&cancelURL=" . urlencode($url . $cancelurl);
$resArray = hash_call("SetExpressCheckout", $nvpstr);
if (!empty($resArray["TOKEN"])) { $token = urldecode($resArray["TOKEN"]); $payPalURL = PAYPAL_URL . $token; header("Location: " . $payPalURL); }
} else { /* At this point, the buyer has completed in authorizing payment at PayPal. The script will now call PayPal with the details of the authorization, incuding any shipping information of the buyer. Remember, the authorization is not a completed transaction at this state - the buyer still needs an additional step to finalize the transaction */
$token = urlencode($_REQUEST['token']);
/* Build a second API request to PayPal, using the token as the ID to get the details on the payment authorization */ $nvpstr = "&TOKEN=" . $token;
/* Make the API call and store the results in an array. If the call was a success, show the authorization details, and provide an action to complete the payment. If failed, show the error */ $resArray = hash_call("GetExpressCheckoutDetails", $nvpstr);
$ack = strtoupper($resArray["ACK"]);
if ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
ini_set('session.bug_compat_42', 0); ini_set('session.bug_compat_warn', 0);
$serverName = $_SERVER['SERVER_NAME'];
$nvpstr .= ""; $serverName = $_SERVER['SERVER_NAME']; $serverPort = $_SERVER['SERVER_PORT']; $url = dirname('http://' . $serverName . ':' . $serverPort . $_SERVER['REQUEST_URI']);
$nvpstr .= "&TOKEN=" . urlencode($_REQUEST['token']); $nvpstr .= "&INITAMT=" . urlencode($resArray["AMT"]); $nvpstr .= "&PROFILESTARTDATE=" . urlencode($recurring_payment_start_date."T00:00:00Z"); $nvpstr .= "&DESC=" . urlencode($recurring_payment_text); $nvpstr .= "&BILLINGPERIOD=" . urlencode("Month"); $nvpstr .= "&BILLINGFREQUENCY=" . urlencode("1"); $nvpstr .= "&TOTALBILLINGCYCLES=" . urlencode(($_SESSION["warehousing_web"]["rent_month"]-1)); $nvpstr .= "&AMT=" . urlencode($_SESSION["warehousing_web"]["discounted_price"]); $nvpstr .= "&CURRENCYCODE=" . urlencode("HKD");
$nvpstr .= "&ReturnUrl=" . urlencode($url . "/ReviewOrder.php"); $nvpstr .= "&cancelURL=" . urlencode($url . $cancelurl);
$resArray = hash_call("CreateRecurringPaymentsProfile", $nvpstr);
$ack = strtoupper($resArray["ACK"]); if ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
ini_set('session.bug_compat_42', 0); ini_set('session.bug_compat_warn', 0);
$data = array( "order_id" => 1, "customer_id" => 1, "profileid" => $resArray["PROFILEID"], "profilestatus" => $resArray["PROFILESTATUS"], "timestamp" => $resArray["TIMESTAMP"], "correlationid" => $resArray["CORRELATIONID"], "ack" => $resArray["ACK"], "session_id" => $session_id, "_result" => json_encode($resArray), "_nvp" => $nvpstr, "_session" => json_encode($session_data), "_request" => json_encode($_REQUEST), "_server" => json_encode($_SERVER), "createdate" => $nowdate, );
$result = insert_record("paypal_recurring_payment", $data);
$_SESSION["paystatus"] = 1; //create recurring payment success header("Location: ../".$langcode."/rent_email.php"); exit;
} else { $data = array( "order_id" => 1, "customer_id" => 1, "profileid" => "", "profilestatus" => "", "timestamp" => "", "correlationid" => "", "ack" => $resArray["ACK"], "session_id" => $session_id, "_result" => json_encode($resArray), "_nvp" => $nvpstr, "_session" => json_encode($session_data), "_request" => json_encode($_REQUEST), "_server" => json_encode($_SERVER), "createdate" => $nowdate, );
$result = insert_record("paypal_recurring_payment", $data);
//Redirecting to APIError.php to display errors. $_SESSION['my_error_no'] = 'Unexpected ACK in GetExpressCheckoutDetails'; header("Location: APIError.php");
}
} else {
$data = array( "order_id" => 1, "customer_id" => 1, "profileid" => "", "profilestatus" => "", "timestamp" => "", "correlationid" => "", "ack" => $resArray["ACK"], "session_id" => $session_id, "_result" => json_encode($resArray), "_nvp" => $nvpstr, "_session" => json_encode($session_data), "_request" => json_encode($_REQUEST), "_server" => json_encode($_SERVER), "createdate" => $nowdate, );
$result = insert_record("paypal_recurring_payment", $data);
//Redirecting to APIError.php to display errors. $_SESSION['my_error_no'] = 'Unexpected ACK in GetExpressCheckoutDetails'; header("Location: APIError.php");
} } }
|