-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_balance.php
41 lines (34 loc) · 1.28 KB
/
update_balance.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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'vendor/autoload.php';
require 'config.php';
use MongoDB\Client;
header('Content-Type: application/json');
function updateBalance($user_id, $balance) {
$uri = 'mongodb+srv://likhonsheikhbd:376lmB9Smh1RdMpD@havencoin.xio29um.mongodb.net/?retryWrites=true&w=majority&appName=Havencoin';
$client = new Client($uri);
try {
$collection = $client->havencoin->user_balances;
$result = $collection->updateOne(
['user_id' => (int)$user_id],
['$set' => ['balance' => (int)$balance]],
['upsert' => true]
);
if ($result->getModifiedCount() > 0 || $result->getUpsertedCount() > 0) {
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Failed to update balance']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
}
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['user_id']) && isset($data['balance'])) {
updateBalance($data['user_id'], $data['balance']);
} else {
echo json_encode(['success' => false, 'error' => 'Missing parameters']);
}
?>