Source code mẫu đổi thẻ cào tích hợp API cho website Phương thức Get
Gửi thẻ
Phương thức: GET
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Nạp thẻ</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row" style="margin-top: 50px;">
<div class="col-md-8" style="float:none;margin:0 auto;">
<div class="form-group">
<label>Loại thẻ:</label>
<select class="form-control" id="card_type">
<option value="">Chọn loại thẻ</option>
<option value="VIETTEL">Viettel</option>
<option value="MOBIFONE">Mobifone</option>
<option value="VINAPHONE">Vinaphone</option>
<option value="VIETNAMOBILE">Vietnamobile</option>
<option value="ZING">Zing</option>
</select>
</div>
<div class="form-group">
<label>Mệnh giá:</label>
<select class="form-control" id="card_amount">
<option value="">Chọn mệnh giá</option>
<option value="20000">20.000</option>
<option value="50000">50.000</option>
<option value="100000">100.000</option>
<option value="200000">200.000</option>
<option value="500000">500.000</option>
<option value="1000000">1.000.000</option>
</select>
</div>
<div class="form-group">
<label>Số seri:</label>
<input type="text" class="form-control" id="serial" placeholder="Nhập số seri trên thẻ" />
</div>
<div class="form-group">
<label>Mã thẻ:</label>
<input type="text" class="form-control" id="pin" placeholder="Nhập Mã Thẻ In Sau Lớp Bạc Mỏng " />
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-block" onclick="napthe(this)">Gửi Thẻ</button>
</div>
</form>
</br>
<script>
function napthe() {
var card_type = $("#card_type").val();
var card_amount = $("#card_amount").val();
var serial = $("#serial").val();
var pin = $("#pin").val();
if (!card_type || !card_amount || !serial || !pin) {
swal("lỗi", "Thiếu Thông Tin", "error");
} else {
$.ajax({
type: "POST",
url: "napthe.php",
data: {
card_type,
card_amount,
serial,
pin,
},
dataType: "json",
success: function (res) {
if (res.success) {
swal("Thành công", res.success, "success");
} else {
swal("Lỗi", res.error, "error");
}
},
error: function (err) {
console.log(err);
},
});
}
}
</script>
</button>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</span>
</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/js/bootstrap.js"></script>
</script>
</body>
</html>
Xử Lý:
<?php
//UPDATE 04/05/2023
//TRONG QUÁ TRÌNH KẾT NỐI, NẾU CẦN HỖ TRỢ VUI LÒNG BÁO ADMIN Doithecao360.com
// Doithecao360.com CHẤP NHẬN CẢ GET VÀ POST KHI GỬI THẺ
$partnet_key = "";// key tại Doithecao360.com
$partner_id = ""; //Partner ID tại Doithecao360.com
$type = strtoupper($_POST['card_type']); // loại thẻ
$pin = $_POST['pin']; // mã thẻ
$serial = $_POST['serial'];// seri
$amount = $_POST['card_amount']; // Mệnh giá
$request_id = rand(100009, 999999);// mã đơn hàng (Khi callback sẽ trả kèm request_id này)
$url = 'https://Doithecao360.com/chargingws/v2?sign='.md5($partnet_key.$pin.$serial).'&telco='.$type.'&code='.$pin.'&serial='.$serial.'&amount='.$amount.'&request_id='.$request_id.'&partner_id='.$partner_id.'&command=charging'; //URL GET
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data, true);
if ($result['status'] == 100) {
$res['error'] = $result['message'];// lỗi kèm thông báo về
die(json_encode($res));
}
if ($result['status'] == 1) {
$res['success'] = "Nạp thẻ thành công";// thẻ đúng
die(json_encode($res));
}
if ($result['status'] == 2) {
$res['error'] = "Sai mệnh giá thẻ";// sai mệnh giá
die(json_encode($res));
}
if ($result['status'] == 3) {
$res['error'] = "Vui lòng kiểm tra lại thẻ";// thẻ lỗi
die(json_encode($res));
}
if ($result['status'] == 4) {
$res['error'] = "bảo trì"; // bảo trì
die(json_encode($res));
}
if ($result['status'] == 99) {
$res['success'] = "Gửi thẻ thành công";// đang duyệt
die(json_encode($res));
}
else {
$res['error'] = $result['message'];// lỗi không xác định , kèm thông báo
die(json_encode($res));
}
Nhận kết quả trả thẻ:
<?php
if (isset($_GET['request_id']) && isset($_GET['callback_sign']))
{
//GHI LOG
$file = @fopen('log.txt', 'a');
if ($file)
{
$data = "[" .date('Y/m/d H:i:s', time()) ."]" .json_decode($_GET).PHP_EOL;
fwrite($file, $data);
}
/// status = 1 ==> thẻ đúng
/// status = 2 ==> thẻ sai mệnh giá
/// status = 3 ==> thẻ lỗi
/// status = 99 ==> thẻ vẫn chờ xử lý
//// Kết quả trả về sẽ có các trường như sau:
$partner_key = 'b51c4deb8d15d84767ef417632b89999';// key của quý khách tại Doithecao360.com
//ĐỐI CHỮ KÝ, CŨNG CÓ THỂ BỎ QUA ĐỐI CHIẾU NẾU CẢM THẤY KHÔNG CẦN THIẾT
$callback_sign = md5($partner_key . $_GET['code'] . $_GET['serial']);
if ($_GET['callback_sign'] == $callback_sign) {
$getdata['status'] = $_GET['status']; // Trạng thái thẻ
$getdata['message'] = $_GET['message']; // thông báo kèm theo thẻ
$getdata['request_id'] = $_GET['request_id']; /// Mã giao dịch bạn gửi lên
$getdata['trans_id'] = $_GET['trans_id']; /// Mã giao dịch của Doithecao360.com
$getdata['declared_value'] = $_GET['declared_value']; /// Mệnh giá mà bạn khai báo
$getdata['value'] = $_GET['value']; /// Mệnh giá thực tế của thẻ
$getdata['amount'] = $_GET['amount']; /// Số tiền bạn nhận về (VND)
$getdata['code'] = $_GET['code']; /// Mã nạp
$getdata['serial'] = $_GET['serial']; /// Serial thẻ
$getdata['telco'] = $_GET['telco']; /// Nhà mạng
print_r($getdata);
}
if ($_GET['status'] == 1 )
{
//XỬ LÝ THẺ ĐÚNG
}
if ($_GET['status'] == 2 )
{
//XỬ LÝ THẺ SAI MỆNH GIÁ
}
if ($_GET['status'] == 3 )
{
//XỬ LÝ THẺ LỖI
}
else
{
// NGOẠI LỆ
}
}
else
{
echo ('Doithecao360.com xin chào !') ;
}
?>