最近我正在使用PHP和PHPMyAdmin开发充值系统。
注意-在充值系统中,所有充值并非始终都成功。因此,我添加了代码以检查充值API提供程序的充值状态。 (按照充值API规则,在充值任何数字后,它会显示待处理的1-2分钟,然后变为成功或失败)。如果充值失败,则应使用带有此代码的cron job将金额退还给用户。在这里,我们有3种类型的用户-管理员,分销商,代理商(所有用户均可通过充值获得佣金)
状态cron工作代码-
<?php
require_once 'config.php';
//echo $trialtoken."<br>";
$info="(select * from postpaid WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY) order by id DESC limit 300) order by id";
$query=mysqli_query($link,$info);
$rows=mysqli_num_rows($query);
if($rows>0)
{
while($result=mysqli_fetch_assoc($query))
{
$id = $result["id"];
$statusee = $result["status"];
$orderid = $result["orderId"];
$amount1 = $result["amount"];
$charge = $result["charge"];
$dis_comm = $result["dist_comm"];
$operatorName = $result["operatorName"];
$username = $result["username"];
$mobile = $result["mobile"];
$mode = $result["mode"];
$txid1=$result["opTxid"];
$date=$result["date"];
echo $id."<br>";
/*----------------------------------------------------------------------------------------------------------------*/
$find = "select * from users where mobile= '$username'";
$query1=mysqli_query($link,$find);
$mmm = mysqli_num_rows($query1);
$result = mysqli_fetch_array($query1);
$balance=$result['balance'];
$comm =$result['set_comm'];
$malik=$result['under'];
$package=$result['package'];
//echo $balance."<br>";
/*$t="trial";
$p="pro";
$b="basic";
/*-----------------------------------------------------------------------------*/
$abc = "select * from dis_users where dis_mobile = '$malik'";
$xyz=mysqli_query($link,$abc);
$row = mysqli_num_rows($xyz);
$sss = mysqli_fetch_array($xyz);
$man=$sss['balance'];
/*--------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
if($statusee=="REFUND")
{
//echo "already refund<br>";
}
elseif($statusee=="SUCCESS")
{
//echo "already success recharge<br>";
}
elseif($statusee=="Disputed")
{
//echo "already success recharge<br>";
}
elseif($statusee=="FAILED" || $statusee=="PENDING")
{
/*-----------------------------------------------------------------------------------------------*/
// THE RECHARGE STATUS CHECK API LINK
$url="http://api.rechapi.com/api_status.php?format=xml&token=$rechtoken&orderId=$orderid";
$request_timeout = 60; // 60 seconds timeout
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$curl_error = curl_errno($ch);
$getserver= curl_getinfo($ch);
curl_close($ch);
if($getserver["http_code"]==200 || $curl_error=="28")
{
$xml = simplexml_load_string($output);
foreach($xml as $nxml){
//$orderid=$nxml[id];
$status=$nxml->status;
$mobile=$nxml->mobile;
$txid=$nxml->transactionId;
$amount=$nxml->amount;
$error_code=$nxml->error_code;
$bal=$nxml->bal;
$resText=$nxml->resText;
$nomore = 0;
/*-----------------------------------------------------------------------------------*/
//echo "<br> STATUS - $status, Mobile - $mobile , Amount - $amount , TRANSACTION ID - $txid, API BALANCE - $bal , REASON - $resText <hr />";
if($status=="PENDING")
{
if(mysqli_query($link,"update postpaid set status='$status', opTxid='$txid', errorCode='$error_code' , resText='$resText' where id='$id'"))
{
//echo "Recharge accepted and its status is PENDING.";
}
}
elseif($status=="SUCCESS")
{
if(mysqli_query($link,"update postpaid set status='$status' , opTxid='$txid', errorCode='$error_code' , resText='$resText' where id='$id'" ))
{
//echo "Recharge success and its status is success , Transaction id is - $txid .";
}
}
elseif($status=="Disputed"){
if(mysqli_query($link,"update postpaid set status='$status' , opTxid='$txid', errorCode='$error_code' , resText='$resText' where id='$id'" ))
{
echo "Recharge Disputed and its status is disputed , Transaction id is - $txid .";
}
}
elseif($status=="FAILED" || $status=="$nomore" )
{
if(mysqli_query($link,"update postpaid set status='$status' , opTxid='$txid', errorCode='$error_code' , resText='$resText' where id='$id'" ))
{
//echo "Recharge failed and its status is failed , Transaction id is - $txid .";
$b = $balance+$charge;
$f_com = $man-$dis_comm;
$sqli="update users set balance='$b' where mobile='$username'";
if($qu=mysqli_query($link,$sqli))
{
if(mysqli_query($link,"update dis_users set balance='$f_com' where dis_mobile='$malik'"))
{
$statuse="REFUND";
mysqli_query($link,"update postpaid set status='$statuse' where id='$id'");
}
}
else
{
mysqli_error($link);
}
}
}
else{}
}
}
}
else{}
}
}
else
{
// echo "data not found";
}
?>
我遇到的最大问题是同时进行多个用户余额数据库更新。例如,用户为手机号码充值,与此同时,用户收到因cron作业检查码充值失败而产生的退款。在这种情况下,我的代码无法正常工作。因为两次更新之间(充值金额扣除和增加退款金额)之间,数据库中未计算每个更新查询中的一个。
示例-用户余额100美元
充电-10 $
并退款-20 $
因此,最终金额应为110 $,但未计算充值金额。因此,余额显示为120 $,有时不计算收到的退款金额。如果两个事务都在一个非常独特的情况下同时完成,但是经过长时间的调试和错误检查之后,我发现问题出在我的代码上。因此,请帮助我修复它。我想一个一个地更新数量数据库。
充电动作代码-
<?php
require_once './config/config.php';
session_start();
if(!isset($_SESSION['mobile']) || empty($_SESSION['mobile'])){
header("location: index.php");
}else{
$username=$_SESSION['mobile'];
$find = "select * from users where mobile = '$username'";
if($query=mysqli_query($link,$find))
{
$rows = mysqli_num_rows($query);
if($rows > 0)
{
while($result = mysqli_fetch_array($query))
{
$balance=$result['balance'];
$malik=$result['under'];
$comm =$result['set_comm'];
$package = $result['package'];
}
}
}
$t="trial";
$p="pro";
$b="basic";
if ($p == $package) {
$rechtoken = $token;
} elseif ($b == $package) {
$rechtoken = $token;
} else {
$rechtoken = $trialtoken;
}
function opName($operatorId){
switch ($operatorId) {
case 28:
$operatorName= "VIDEOCON DTH";
break;
case 26:
$operatorName= "SUN DTH";
break;
case 24:
$operatorName= "BIG TV DTH";
break;
case 27:
$operatorName= "TATA SKY DTH";
break;
case 25:
$operatorName= "DISH DTH";
break;
case 23:
$operatorName= "AIRTEL DTH";
break;
default:
$operatorName= "";
}
return $operatorName;
}
$te=3;
$amount=$_POST['amount'];
if($balance < $amount || $balance < $te )
{
echo "<p id='p'>your account balance is low. You have to recharge your wallet <br/> <a href='index.php'>Back to Home</a></p>";
header("location:low.php");
}
else
{
if(isset($_POST['submit'])){
$mobile=$_POST['number'];
$amount=$_POST['amount'];
$operatorId=$_POST['operator'];
$operatorName=opName($operatorId);
date_default_timezone_set("Asia/Calcutta");
$date = date("Y-m-d h:i:s");
/*-----------------------------------------------------------------------------------*/
$dis_com = "select * from package where operator = '$operatorName'";
$gffy=mysqli_query($link,$dis_com);
$rows = mysqli_num_rows($gffy);
if($rows > 0)
{
while($top = mysqli_fetch_array($gffy))
{
$p1= $top['trial'];
$p2= $top['basic'];
$p3= $top['pro'];
}
}
/*-----------------------------------------------------------------------------------*/
$commission = "select * from set_comm where operator = '$operatorName'";
$query=mysqli_query($link,$commission);
$rows = mysqli_num_rows($query);
if($rows > 0)
{
while($res = mysqli_fetch_array($query))
{
$a=$res['operator'];
$e=$res['0'];
$f=$res['10%'];
$g=$res['20%'];
$h=$res['30%'];
$i=$res['40%'];
$j=$res['50%'];
$k=$res['60%'];
$l=$res['70%'];
$m=$res['80%'];
$n=$res['90%'];
$o=$res['100%'];
}
}
if($comm == 10)
{
$bft= $f;
}
elseif($comm == 20)
{
$bft= $g;
}
elseif($comm == 30)
{
$bft= $h;
}
elseif($comm == 40)
{
$bft= $i;
}
elseif($comm == 50)
{
$bft= $j;
}
elseif($comm == 60)
{
$bft= $k;
}
elseif($comm == 70)
{
$bft= $l;
}
elseif($comm == 80)
{
$bft= $m;
}
elseif($comm == 90)
{
$bft= $n;
}
elseif($comm == 100)
{
$bft= $o;
}
elseif($comm == 0)
{
$bft= $e;
}
else
{
}
/*-----------------------------------------------------------------------------------*/
if($package == 'trial')
{
$zt=$p1;
}
elseif($package == 'basic')
{
$zt=$p2;
}
elseif($package == 'pro')
{
$zt=$p3;
}
//echo "PACKAGE of Agent: $zt<br>";
/*-----------------------------------------------------------------------------------*/
$abc = "select * from dis_users where dis_mobile = '$malik'";
$xyz=mysqli_query($link,$abc);
$rows = mysqli_num_rows($xyz);
if($rows > 0)
{
while($sss = mysqli_fetch_array($xyz))
{
$a=$sss['package'];
$man=$sss['balance'];
$cc=$sss['set_comm'];
}
}
if($a=="trial")
{
$ram=$p1;
}
elseif($a=="basic")
{
$ram=$p2;
}
elseif($a=="pro")
{
$ram=$p3;
}
else
{
//echo "package has to be fixed<br/>";
}
//echo "DIS_PACKAGE: $ram<br>";
/*--------------------------------------------------------------------------------*/
if($cc == 10)
{
$ddd= $f;
}
elseif($cc == 20)
{
$ddd= $g;
}
elseif($cc == 30)
{
$ddd= $h;
}
elseif($cc == 40)
{
$ddd= $i;
}
elseif($cc == 50)
{
$ddd= $j;
}
elseif($cc == 60)
{
$ddd= $k;
}
elseif($cc == 70)
{
$ddd= $l;
}
elseif($cc == 80)
{
$ddd= $m;
}
elseif($cc == 90)
{
$ddd= $n;
}
elseif($cc == 100)
{
$ddd= $o;
}
elseif($cc == 0)
{
$ddd= $e;
}
else
{
}
echo "SET COMM: $bft<br>";
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
//$zz=$zt/100;/*important*/
//echo $zz."<br>";
/*--------------------------------------------------------------------------------*/
if($malik == !null)
{
$ram1=($ram*$ddd)/100;
$tam=($ram1*$bft)/100;/*important*/
$ag=($amount*$tam)/100;
//echo "Final commission of Agent: $tam <br>";
$per=$ram1-$tam;/*important*/
$com=$amount*($per/100);/*important*/
//echo "Final commission of Distributor: $per <br>";
$b=($balance-$amount)+$ag;/*important*/
//echo "Total Amount of Agent: $b<br>";
$f_com = $man+$com;/*important*/
//echo "Distributor Amount: $f_com<br>";
$sql="update users set balance='$b' where mobile='$username'";
if($query=mysqli_query($link,$sql))
{
mysqli_query($link,"update dis_users set balance='$f_com' where dis_mobile='$malik'");
echo '<script>window.alert("click here to recharge")
</script>';
}
else
{
mysqli_error($link);
}
}
else{
$tn=$zt*($bft/100);/* neew line for agent set com */
$ag=$amount*($tn/100);
$b=($balance-$amount)+$ag;
//echo "Agent commission not under distributor: .$z.<br>";
$sql="update users set balance='$b' where mobile='$username'";
if($query=mysqli_query($link,$sql))
{
}
else
{
mysqli_error($link);
}
}
/*-----------------------------------------------------------------------------------*/
$charge = $amount-$ag;
$status="PENDING";
$insert="insert into dth (mobile,username,amount,charge,old_balance,current_balance,dist_comm,agent_comm,operatorName,status,mode,date)
values('$mobile','$username','$amount','$charge','$balance','$b','$com','$ag','$operatorName','$status','$package','$date')";
if($query= mysqli_query($link,$insert))
{
$id = mysqli_insert_id($link);
echo "<br> URID - $id, Mobile - $mobile , Amount - $amount , OperatorId- $operatorId , Operator Name - $operatorName , Status - $status <hr />";
if($balance>$b)
{
$url="http://api.rechapi.com/recharge.php?format=xml&token=$rechtoken&mobile=$mobile&amount=$amount&opid=$operatorId&urid=$id";
$request_timeout = 60; // 60 seconds timeout
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$curl_error = curl_errno($ch);
$getserver= curl_getinfo($ch);
curl_close($ch);
if($getserver["http_code"]==200 || $curl_error=="28" )
{
if($curl_error == 28)
{
$status="PENDING";
}
else{
$xml = simplexml_load_string($output);
$orderId=$xml->orderId;
$status=$xml->status;
$mobile=$xml->mobile;
$amount=$xml->amount;
$operatorId=$xml->operatorId;
$error_code=$xml->error_code;
$service=$xml->service;
$resText=$xml->resText;
}
if($status=="PENDING")
{
if(mysqli_query($link,"update dth set status='$status' , orderId='$orderId' , errorCode='$error_code' , resText='$resText' where id='$id'"))
{
echo 'Recharge accepted and its status is PENDING.';
}
}
elseif($status=="SUCCESS")
{
if(mysqli_query($link,"update dth set status='$status' , orderId='$orderId' , opTxid='$operatorId', errorCode='$error_code' , resText='$resText' where id='$id'" ))
{
echo "Recharge success and its status is success.";
}
}
else {
if(mysqli_query($link,"update dth set status='$status' , orderId='$orderId' , errorCode='$error_code' , resText='$resText' where id='$id'"))
{
echo "Recharge failed and its status is failed Contact to your Distrubutor <a href='index.php'>Retry</a> ";
}
}
}
}
//echo "<br>".$output;
$sqlnw="update users set balance='$b' where mobile='$username'";
if($querynw=mysqli_query($link,$sqlnw))
{
mysqli_query($link,"update dis_users set balance='$f_com' where dis_mobile='$malik'");
//echo '<script>window.alert("click here to recharge")</script>';
}
else
{
mysqli_error($link);
}
$message = urlencode("Congratulation. Your recharge has been accepted of Rs.'$amount'");
$response_type = 'json';
$route = "4";
$postData = array(
'authkey' => $authkey,
'mobiles' => $mobile,
'message' => $message,
'sender' => $sender,
'route' => $route,
'response' => $response_type
);
$url = "https://control.msg91.com/api/sendhttp.php?";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
if (curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
curl_close($ch);
}
else
{
echo mysqli_error($link);
}
}
$_SESSION['lol'] = $_POST['amount'];
$_POST['idp'] =$id;
$_SESSION['ids'] = $_POST['idp'];
echo $_SESSION['ids'];
header("location: payment.php?status=$status&amount=$amount2&orderId=$id&reason=$resText&mobile=$mobile");
}
}
?>