ตัดบัตรทรู

ขออนุญาตคุณ xindy ยืมหน้าเว็บนะครับ
อันนี้เป็นระบบให้ยูเซอร์เติมเงิน สำหรับ mysql เผื่อใครเขียนไม่ค่อยเก่ง ผมก็ยังเขียนไม่เก่ง เอามาแบ่งๆกัน
แก้จากของคุณ xindy นิดหน่อย
แก้ไข config ที่ class.truewallet.php 

index.php
โค๊ด:
<?php@session_start();
if(!isset(
$_SESSION['uid'])) $_SESSION['uid'] = '';?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Test API TrueWallet</title>

    <!-- Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kanit" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
<script type="text/javascript">
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
}
</script>
  </head>
  <style>
html,body {
  padding: 0px;
  margin-top:25px;
  width: 100%;
  height: 100%;
}
body {
  font-family: 'Kanit', sans-serif;
  font-size: 14px;
  background: #1c1e21;
  color:  #878383 ;
  position: relative;
}
.form-signin {
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
  margin-bottom: 10px;
}
.form-signin .checkbox {
  font-weight: normal;
}
.form-signin .form-control {
  position: relative;
  height: auto;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
  padding: 10px;
  font-size: 16px;
}
.form-signin .form-control:focus {
  z-index: 2;
}
.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}
.error_code {
    color: #F00;
    font-size: 15px;
    font-width: bold;
    margin-left: 15px;
    line-height: 16px;
    margin-bottom: 4px;
    height: 16px;
    text-align: center;
}
  </style>
  <body style="background: url(bg.png);">
<div class="container free_tck">
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">TrueWallet ( Test API )</div>
<div class="panel-body">
<?php
if($_SESSION['uid'] == '') { ?>
    <form role="form" method="post" class="col-md-12">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">User</div>
<input type="text" id="user" name="user" class="form-control" placeholder="UserName" required autocomplete="off">
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Pass</div>
<input type="password" id="pass" name="pass" class="form-control" placeholder="Password" required autocomplete="off">
</div>
</div>
<button class="btn btn-lg btn-primary btn-block free_btn" name="login" type="submit">เข้าสู่ระบบ</button>
</form>
<?php
if(!isset($_POST['login'])) $_POST['login'] = ''; else { include("class.truewallet.php"); $wallet = new TrueWallet(); $wallet->Connect(); if(!$wallet->Login($_POST['user'],$_POST['pass'],false)) echo "cant login"; else { $_SESSION['uid'] = $_POST['user']; header("Location: ./"); } } } else if($_SESSION['uid'] <> '') { ?>
<form role="form" method="post" class="col-md-12">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">True Money</div>
<input type="text" id="number" name="number" class="form-control" placeholder="รหัสบัตรทรูมันนี่" onkeypress="return isNumberKey(event)" maxlength=14 required autocomplete="off">
</div>
</div>
<button class="btn btn-lg btn-primary btn-block free_btn" name="topup" type="submit">เติมเงิน</button>
</form>

<form role="form" method="post" class="col-md-12">
<p></p>
<button class="btn btn-lg btn-primary btn-block free_btn" name="logout" type="submit">ออกจากระบบ</button>
</form>
<?php
if(!isset($_POST['topup'])) $_POST['topup'] = ''; else { include("class.truewallet.php"); $wallet = new TrueWallet(); $wallet->Connect(); $a $wallet->Topup($_SESSION['uid'],$_POST['number']); print_r($a); } if(!isset($_POST['logout'])) $_POST['logout'] = ''; else { @session_destroy(); header("Location: ./"); } } ?>
</div>
</div>
</div>
</div>
    </div> <!-- /container -->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>

class.truewallet.php
โค๊ด:
<?phpclass TrueWallet{ //mysql private $DbHost  ''; //ไอพี sql private $DbUser  ''; //ไอดี sql private $DbPass  ''; //รหัส sql private $DbName  ''; //ชื่อฐานข้อมูล private $DbPort  ''; //พอร์ต sql private $tbName  ''; //ชื่อตารางที่เก็บข้อมูลยูเซอร์ private $fdID  ''; //ฟิลด์ไอดี private $fdPS  ''; //ฟิลด์พาสเวิร์ด private $fdCS ''; //ฟิลด์พ้อย private $DbQuery; //True Money private $trueID  ''; //อีเมลสำหรับล็อคอินทรูวอเล็ต private $truePS ''; //พาสเวิร์ดสำหรับล็อคอินทรูวอเล็ต private $passhash; //Config TrueWallet ห้ามแก้ไขหากไม่รู้ค่าที่แท้จริง private $login_type "email" private $api_signin "https://api-ewm.truemoney.com/api/v1/signin?&" private $api_profile "https://api-ewm.truemoney.com/api/v1/profile/" private $api_topup "https://api-ewm.truemoney.com/api/api/v1/topup/mobile/" private $device_os "android" private $device_id "d520d0d12d0d48cb89394905168c6ed5" private $device_type "CPH1611" private $device_version "6.0.1" private $app_name "wallet" private $app_version "2.9.14" private $deviceToken "fUUbZJ9nwBk:APA91bHHgBBHhP9rqBEon_BtUNz3rLHQ-sYXnezA10PRSWQTwFpMvC9QiFzh-CqPsbWEd6x409ATC5RVsHAfk_-14cSqVdGzhn8iX2K_DiNHvpYfMMIzvFx_YWpYj5OaEzMyIPh3mgtx" private $mobileTracking "dJyFzn/GIq7lrjv2RCsZbphpp0L/W2 PsOTtOpg352mgWrt4XAEAAA==" private $walletToken ''; //End Config //พ้อยที่จะได้รับ ตามราคาบัตร private $point1  50; private $point2  90; private $point3  150; private $point4  300; private $point5  500; private $point6  1000; // public function __construct() { $this->passhash sha1($this->trueID.$this->truePS); } private function GetToken(){ $url $this->api_signin.'device_os='.$this->device_os.'&device_id='.$this->device_id.'&device_type='.$this->device_type.'&device_version='.$this->device_version.'&app_name='.$this->app_name.'&app_version='.$this->app_version; $header = array( "Host: api-ewm.truemoney.com", "Content-Type: application/json" ); $postfield = array( "username"=>$this->trueID, "password"=>$this->passhash, "type"=>$this->login_type, "deviceToken"=>$this->deviceToken, "mobileTracking"=>$this->mobileTracking, ); return $this->wallet_curl($url,json_encode($postfield),$header); }
public function Profile(){ $url $this->api_profile.$this->walletToken.'?&device_os=android&device_id='.$this->device_id.'&device_type='.$this->device_type.'&device_version='.$this->device_version.'&app_name='.$this->app_name.'&app_version='.$this->app_version; $header = array("Host: api-ewm.truemoney.com"); return $this->wallet_curl($url,false,$header); } /*
*/
private function wallet_curl($url,$data,$header){ $ch curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_HTTPHEADER,$header); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);   if($data){ curl_setopt($ch,CURLOPT_CUSTOMREQUEST"POST");                                                                      curl_setopt($ch,CURLOPT_POSTFIELDS$data);          }                                   curl_setopt($ch,CURLOPT_RETURNTRANSFERtrue);    curl_setopt($ch,CURLOPT_USERAGENT,''); $result curl_exec($ch); return json_decode($result,true); } /*
@param1 $user = ไอดีที่จะเติม
@param2 $card = เลขบัตรทรูมันนี่

return code -9999 = ยูเซอร์ที่จะเติมว่างเปล่า หรือ เลขบัตรว่างเปล่า หรือ เลขบัตรไม่ใช่ตัวเลข หรือ เลขบัตรน้อยกว่า 14 หลัก
return code -1001 = บัตรใช้ไปแล้ว หรือ มีในฐานข้อมูลแล้ว
return code -1002 = เติมผิด หรือ บัตรมั่ว
return code 20000 = เติมสำเร็จ
*/
public function Topup($user,$card){ //เช็คไอดีและบัตร if($user == '' || $card == '' || !is_numeric($card) || strlen($card) < 14) { return -9999; } //เช็คเลขบัตรซ้ำ if(!$this->CheckCard($card)) { return -1001; } // $token $this->GetToken(); $url $this->api_topup.time()."/".$token['data']['accessToken']."/cashcard/".$card; $header = array("Host: api-ewm.truemoney.com"); $topup $this->wallet_curl($url,true,$header);
if($topup['code'] < 0){ //-1001 = บัตรใช้ไปแล้ว  // -1002 = บัตรผิด หรือ บัตรมั่ว $amount 0; $status $topup['code']; }else if($topup['transactionId'] <> ''){ //เติมเงินสำเร็จ $amount $topup['amount']; $status 20000; } if(!$this->InsertCard($user$card$amount$status)){ //หาก insert ไม่ได้ให้สร้าง error log $update "INSERT INTO truemoney (card, uid, uip, amount, status, time) VALUES ('$card', '$user', '".$this->GetUserIP()."', $amount$status, '".$this->GetTime()."')"; $this->LogError("Error : " .$update); $this->LogError("Card : " $card); $this->LogError("User : " $user); $this->LogError("IP : " $this->GetUserIP()); $this->LogError("Amount : " $amount); $this->LogError("Status : " $status); $this->LogError("Time : " $this->GetTime()); $this->LogError("============================================================="); } if($amount && !$this->Update($this->tbName$this->fdCS$this->GetPoint($amount), $user)){ //หาก update ไม่ได้ให้สร้าง error log $update "UPDATE $this->tbName SET $this->fdCS = $this->fdCS   $this->GetPoint($amount) WHERE $this->fdID = '$user'"; $this->LogError("Error : " $update); $this->LogError("User : " $user); $this->LogError("Amount : " $this->GetPoint($amount)); $this->LogError("Time : " $this->GetTime()); $this->LogError("============================================================="); } return $status; } /**/ public function Connect(){ $this->DbQuery = new mysqli($this->DbHost$this->DbUser$this->DbPass$this->DbName$this->DbPort); if ($this->DbQuery->connect_errno { printf("Connect failed: %s
"
$this->DbQuery->connect_error); exit(); } $this->DbQuery->query("SET NAMES UTF8"); } /**/ public function Close(){ $this->DbQuery->close(); } /*
@param1 $user = ไอดี
@param2 $card = หมายเลขบัตรทรูมันนี่
@param3 $amount = ราคาบัตร
@param4 $status = สถานะ
*/
private function InsertCard($user$card$amount$status){ $query "INSERT INTO truemoney (card, uid, uip, amount, status, time) VALUES ('$card', '$user', '".$this->GetUserIP()."', $amount$status, '".$this->GetTime()."')"; if(!mysqli_query($this->DbQuery$query)) { return false; } return true; } /*
@param1 $table = ชื่อตารางของฐานข้อมูล
@param2 $column = ชื่อคอลั่มน์ของฐานข้อมูล
@param3 $value = ค่าที่จะอัพเดท ตัวอย่าง หากต้องการลบให้ใส่ -5 / หากต้องการบวกให้ใส่ 5
@param4 $where = จะให้อัพเดทที่ไอดีไหน/ตรงไหน
*/
private function Update($table$field$value$where){ $query = ("UPDATE $table SET $field = $field   $value WHERE $this->fdID = '$where'"); if(!mysqli_query($this->DbQuery$query)) { return false; } return true; } /*
@param1 $user = ไอดี
@param2 $pass = พาส
@param3 $md5  = 0 ไม่ใช้ md5 / 1 ใช้ md5
ตัวอย่าง ไม่ใช้ MD5 = LogIn('testID', 'testPS', 0);
ตัวอย่าง ใช้ MD5 = LogIn('testID', 'testPS', 1);
*/
public function Login($user$pass$md5){ $pass $md5 md5($pass) : $pass; $query "SELECT * FROM $this->tbName where $this->fdID = '$user' and $this->fdPS = '$pass'"; if(!mysqli_query($this->DbQuery$query)) return false; $result $this->DbQuery->query($query); if ($result->num_rows <= 0) return false;
return true; } /*
@param1 $card = เลขบัตรทรูมันนี่
*/
private function CheckCard($card){ $result $this->DbQuery->query("SELECT card FROM truemoney WHERE card = '$card'"); if ($result->num_rows 0) return false; return true; } private function GetPoint($amount){ if($amount == 50) return $this->point1; else if($amount == 90) return $this->point2; else if($amount == 150) return $this->point3; else if($amount == 300) return $this->point4; else if($amount == 500) return $this->point5; else if($amount == 1000) return $this->point6; return 0; }
private function GetUserIP() { $client  = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote  = @$_SERVER['REMOTE_ADDR']; if(filter_var($clientFILTER_VALIDATE_IP)) $ip $client; elseif(filter_var($forwardFILTER_VALIDATE_IP)) $ip $forward; else $ip $remote; return $ip; } private function GetTime() { return date("Y-m-d H:i:s"); } /*
@param1 $str = log text
*/
private function LogError($str){ if (!file_exists('log')) { @mkdir('log'0777true); @file_put_contents('log/index.html'''); } $now1 date("Y-m-d"); $now2 date("Y-m-d H:i:s"); $file 'log/log_'.$now1.'.txt'; $current = @file_get_contents($file); $current .= '['.$now2.'] '.$str."
"
; @file_put_contents($file$current); }
}
?>

truemoney.sql
โค๊ด:
DROP TABLE IF EXISTS `truemoney`;
CREATE TABLE `truemoney` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `card` varchar(14) COLLATE utf8_unicode_ci NOT NULL,
  `uid` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `uip` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  `amount` int(4) NOT NULL DEFAULT '0',
  `status` int(11) NOT NULL,
  `time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ค้นหาอะไรก็เจอ