-
Notifications
You must be signed in to change notification settings - Fork 0
/
stk.php
122 lines (93 loc) · 3.11 KB
/
stk.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
<?php
include './credentials.php';
include_once('./vendor/autoload.php');
use IntaSend\IntaSendPHP\Collection;
function initiateStkPush($amount, $phone_number)
{
global $credentials;
$collection = new Collection();
$collection->init($credentials);
//initiating the stk push
$response = $collection->mpesa_stk_push($amount, $phone_number);
//Get Invoive ID
$invoice_id = $response->invoice->invoice_id;
//initialize status variable
$status = "PROCESSING";
//check the status
while ($status == "PROCESSING") {
sleep(1);
//check
$statusResponse = $collection->status($invoice_id);
//get update on status
$status = $statusResponse->invoice->state;
}
return $status;
}
if (isset($_POST['deposit'])) {
$phone_number = $_POST['phonenumber'];
$amount = $_POST['amount'];
$status = initiateStkPush($amount, $phone_number);
header("Location:stk.php?status=$status");
exit();
}
?>
<!DOCTYPE html>
<html data-theme="forest">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>STK Push TUT</title>
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.10.2/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="hero min-h-screen bg-base-200">
<div class="hero-content flex-col lg:flex-row-reverse">
<span>
<?php
if (isset($_GET['status'])) {
$status = $_GET['status'];
if ($status === 'COMPLETE') {
$class = 'alert-success';
$message = 'Payment Successful';
} elseif ($status === 'FAILED') {
$class = 'alert-error';
$message = 'Payment Cancelled';
} else {
$class = 'alert-warning';
$message = 'Payment Pending';
}
?>
<div class="alert <?php echo $class; ?> shadow-lg max-w-sm" id="statusAlert" style="width:400px!important;">
<div>
<span><?php echo $message; ?></span>
</div>
</div>
<?php } ?>
</span>
<div class="text-center lg:text-left">
<img src="./pay.svg" alt="illustration" width="400">
</div>
<div class="card shrink-0 w-full max-w-sm shadow-2xl bg-base-100">
<form class="card-body" method="POST" action="stk.php">
<div class="form-control">
<label class="label">
<span class="label-text">Phone Number:</span>
</label>
<input type="text" placeholder="254..." class="input input-bordered" name="phonenumber" required />
</div>
<div class="form-control">
<label class="label">
<span class="label-text">Amount:</span>
</label>
<input type="text" placeholder="Enter amount" class="input input-bordered" name="amount" required />
</div>
<div class="form-control mt-6">
<button class="btn btn-primary" type="submit" name="deposit">Make Payment</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>