-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransact_verify.php
78 lines (68 loc) · 2.37 KB
/
transact_verify.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
<?php
require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
include 'session.php';
// Get reference value from query string and check if it's empty
$reference = $_GET['reference'];
if (empty($reference)) {
header('Location: donate');
exit;
}
// Send cURL request to Paystack API to verify payment
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . rawurlencode($reference),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . $_ENV['PAYSTACK_SECRET_KEY'],
"Cache-Control: no-cache",
],
]);
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error = curl_error($curl);
echo "cURL Error: $error";
exit;
}
curl_close($curl);
// Decode JSON response from Paystack API
$result = json_decode($response, true);
// If payment was successful, retrieve information from response and insert into database
if ($result['data']['status'] == 'success') {
$status = $result['data']['status'];
$reference = $result['data']['reference'];
$amount = $result['data']['amount'] / 100;
$lname = $result['data']['customer']['last_name'];
$fname = $result['data']['customer']['first_name'];
$fullname = $fname . ' ' . $lname;
$Cus_email = $result['data']['customer']['email'];
$Date_time = date('Y-m-d');
$qty = 0;
$set = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = substr(str_shuffle($set), 0, 7);
$stmt = $conn->prepare("INSERT INTO payments (customerid, status, amount, reference, fullname, date, email, book, quantity, code) VALUES(:customerid, :status, :amount, :reference, :fullname, :date, :email, :book, :quantity, :code)");
$stmt->execute([
':customerid' => 0,
':status' => $status,
':amount' => $amount,
':reference' => $reference,
':fullname' => $fullname,
':date' => $Date_time,
':email' => $Cus_email,
':book' => 0,
':quantity' => $qty,
':code' => $code
]);
$_SESSION['success'] = "Payment verification Successful";
header('Location: donate');
exit();
} else {
$_SESSION['error'] = "Payment verification failed: " . $result['data']['gateway_response'];
header('Location: donate');
}