-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerchantHome.php
176 lines (163 loc) · 6.23 KB
/
merchantHome.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
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
<?php
error_reporting(E_ALL);
ob_start();
session_start();
include_once 'doeconnect.php';
require __DIR__ . '/vendor/autoload.php';
use ZuluCrypto\StellarSdk\Keypair;
use ZuluCrypto\StellarSdk\Server;
use ZuluCrypto\StellarSdk\XdrModel\Asset;
use ZuluCrypto\StellarSdk\XdrModel\AccountId;
use ZuluCrypto\StellarSdk\XdrModel\Operation\SetOptionsOp;
if(isset($_POST['btn-balance']))
{
$username = $_SESSION['username'];
$query = "SELECT Publickey,Seed FROM Users WHERE Username = '$username'";
$result = mysqli_query($conn,$query);
if(!$result)
{
print 'Error : Query failed to execute';
}
$row = mysqli_fetch_array($result);
$publickey = $row['Publickey'];
$seed = $row['Seed'];
$server = Server::testNet();
$account = $server->getAccount($publickey);
foreach ($account->getBalances() as $balance)
{
if($balance->getAssetCode() == "DOECredits")
{
$doebalance = $balance->getBalance();
}
}
}
if(isset($_POST['btn-transaction']))
{
//retrieve source,destination,amount
$sourcename = $_SESSION['username'];
$destname = $_POST['destination'];
$amount = $_POST['amount'];
//interaction with db
$query1 = "SELECT Publickey,Seed FROM Users where Username like '$sourcename'";
$query2 = "SELECT Publickey,Seed FROM Users where Username like '$destname'";
$result1 = mysqli_query($conn,$query1);
$result2 = mysqli_query($conn,$query2);
$row1 = mysqli_fetch_array($result1);
$row2 = mysqli_fetch_array($result2);
$sourcePublickey = $row1['Publickey'];
$sourceSeed = $row1['Seed'];
$destPublickey = $row2['Publickey'];
$destSeed = $row2['Seed'];
//speak to stellar and get keypair
$sourceKeypair = Keypair::newFromSeed($sourceSeed);
$receivingKeypair = Keypair::newFromSeed($destSeed);
//Asset initialisation
$asset = new Asset(Asset::TYPE_ALPHANUM_12);
$asset->setAssetCode("DOECredits");
$issuer = new AccountId('GBLJGGNZB7GTTC2D2AZ3HPAAIO4CQMES442C4OEI6PDZFRG4MBQL6ZOS');
$asset->setIssuer($issuer);
//server start
$server = Server::testNet();
//build transaction
$txEnvelope = $server->buildTransaction($sourceKeypair)
->addCustomAssetPaymentOp($asset, $amount, $receivingKeypair->getPublicKey())
->getTransactionEnvelope();
//source signs transaction
$txEnvelope->sign($sourceKeypair);
//encode transaction
$b64Tx = base64_encode($txEnvelope->toXdr());
//submit transaction
$transaction = $server->submitB64Transaction($b64Tx);
$transactionID = $transaction->getTransactionHash();
$query3 = "Select ID from Users where Username like '$sourcename'";
$query4 = "Select ID from Users where Username like '$destname'";
$result3 = mysqli_query($conn,$query3);
$result4 = mysqli_query($conn,$query4);
$row3 = mysqli_fetch_array($result3);
$row4 = mysqli_fetch_array($result4);
$sourceID = $row3['ID'];
$destID = $row4['ID'];
$query5 = "INSERT INTO Transactions(TransactionID,SourceID,DestinationID,Amount)
VALUES('$transactionID','$sourceID','$destID','$amount')";
$result5 = mysqli_query($conn,$query5);
if(!result5)
{
echo "Error : Failed to execute query";
}
}
if(isset($_POST['btn-recharge']))
{
header("Location: recharge.php");
}
if(isset($_POST['btn-paybill']))
{
header("Location: payBill.php");
}
if (isset($_POST['btn-logout']))
{
unset($_SESSION['username']);
session_unset();
session_destroy();
header("Location: index.php");
exit;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>HOME</title>
<link rel="stylesheet" href="CSS/Form.css" type="text/css"/>
</head>
<body background="CSS/thumb-1920-840086.jpg">
<div class="login-page">
<div class="form">
<form method="post" class="login-form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
<a class="message">TRANSACTION</a>
<br>
<br>
<input type="text" name="amount" class="form-control" placeholder="Enter Amount">
<br>
<input type="text" name="destination" class="form-control" placeholder="Enter destination username">
<br><br>
<button type="submit" class="btn btn-block btn-primary" name="btn-transaction">Submit transaction</button>
</div>
</div>
<div class="login-form">
<div class="form">
<button type="submit" class="btn btn-block btn-primary" name="btn-recharge">Recharge</button>
<br><br>
<button type="submit" class="btn btn-block btn-primary" name="btn-paybill">Pay Bill</button>
<br><br>
<button type="submit" class="btn btn-block btn-primary" name="btn-balance">View Balance</button>
<h2><?php
if(isset($doebalance))
{
echo 'Balance : '.$doebalance;
}
?> </h2>
<button type="submit" class="btn btn-block btn-primary" name="btn-merchants">View Merchants</button>
<h2><?php
if(isset($_POST['btn-merchants']))
{
$query = "SELECT Username FROM Users WHERE AccountType = 'Merchant'";
$result = mysqli_query($conn,$query);
echo "Available Merchants :";
echo "<br>";
while($row = mysqli_fetch_assoc($result))
{
foreach($row as $cname => $cvalue)
{
print "$cvalue\t";
echo "<br>";
}
print "\r\n";
}
}
?> </h2>
<button type="submit" class="btn btn-block btn-primary" name="btn-logout">Log Out</button>
</form>
</div>
</div>
</body>
</html>
<?php ob_end_flush(); ?>