php编写刷网课自助下单系统(第三方支付实例)
更新一下,这个项目是19年的时候还在学校的时候写的,当时也是自己自学php写了练手的, 本来是不打算更新的了,但是看到还是有些同学可以借鉴到里面的一小部分东西,所以重新编排了一下格式,以方便大家参考。
代码地址:https://github.com/raingnight/ViewCourse-
使用到的第三方支付(小叮当支付):https://www.xddpay.com/
我的博客:https://fx7.top
此项目是由于本人刚刚入门php且在校代刷网课而编写的,由于在上课时间不方便接单,故特意写一个自助下单系统来实现客户自助下单。
本项目主要实现以下功能:
- 1.用户下单
- 2.用户支付
- 3.用户通过账号查询订单
- 4.管理员登录查看所有订单
设计思路:
项目比较简单,基本就是与数据库之间的通信,进行数据的增删改查。支付使用的是第三方支付接口。
以下是具体代码
用户提交订单页面:index.php
<?php
header('Content-type: text/html; charset=UTF8');
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
</head>
<body>
<div class="content" align="center">
<form action="add.php" method="post">
<span>账号:</span>
<input type="text" name="id" placeholder="账号" required><br><br>
<span>密码:</span>
<input type="password" name="pwd" placeholder="密码" required><br><br>
<span>学校全称:</span>
<input type="text" name="school" placeholder="学校全称" required><br><br>
<span>课程平台:</span>
<input type="text" name="platform" placeholder="课程平台" value="智慧树" required><br><br>
<span>课程门数:</span>
<input type="text" name="count" placeholder="课程门数" id="num" required><br><br>
<span>课程名称:</span><br>
<textarea name="classname" placeholder="请输入课程名称,多门课程用逗号分开" required>
</textarea><br>
<div class="list">
<p class="p1">总价:</p>
<input id="price" name="sprice"class="p1" value="0" readonly="readonly">
<p class="p1">元</p>
</div>
<br>
<input type="submit" value="提交" class="submit" id="button">
<input type = "reset" value = "重置" class="reset">
</form>
</div>
</body>
<style>
body{
background:url(image/bg2.jpg);
}
.list{
width:120px;
height:50px;
background:url(image/bg5.jpg);
}
.p1{
float:left;
}
#price{
margin-top:15px;
width:50px;
color:red;
}
textarea{
height:100px;
padding: 5px 0px 0px 5px;
width: 70%;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(function(){
$('#num').on('input propertychange',function(){
var num = $('#num').val();
var price = num*15;
$("#price").val(price);
})
});
</script>
</html>
处理订单并生成支付数据页面:add.php
<?php
header('Content-type: text/html; charset=UTF8');
?>
<?php
require_once "connect.php";
$id=$_POST["id"];
$pwd=$_POST["pwd"];
$school=$_POST["school"];
$platform=$_POST["platform"];
$count=$_POST["count"];
$classname=$_POST["classname"];
$price=$_POST["sprice"];
if($id==null){
echo "<script>alert('请输入账号!');location='index.php';</script>";
};
if($pwd==null){
echo "<script>alert('请输入密码!');location='index.php';</script>";
};
if($school==null){
echo "<script>alert('请输入学校全称!');location='index.php';</script>";
};
if($platform==null){
echo "<script>alert('请输入平台名称!');location='index.php';</script>";
};
if($count==null){
echo "<script>alert('请输入课程数量!');location='index.php';</script>";
};
$sql="insert into orderdetail (id,pwd,school,platform,count,classname,time)values('$id','$pwd','$school','$platform','$count','$classname',now())";
mysql_query("set names 'utf8'");
mysql_query($sql);
mysql_close;
//echo "<script>alert('信息提交成功,跳转付款页面!');location='chosetopay.html';</script>";
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>刷客在线支付</title>
</head>
<body>
<center>
<h1>请您确认订单信息:</h1>
<span>账号:</span><?php echo $id ?><br>
<span>密码:</span><?php echo $pwd ?><br>
<span>学校:</span><?php echo $school ?><br>
<span>平台:</span><?php echo $platform ?><br>
<span>课程名:</span><?php echo $classname ?>
<br><br><br>
</center>
<div align="center">
<form>
<p><input id="inputmoney" type="text" name="inputmoney" class="form-control" placeholder="请输入金额" required value="<?php echo $price.'.00'?>" readonly="readonly"></p>
<div class="radio">
<label>
<p><input type="radio" name="demo1" id="demo1-alipay" value="43" checked="">
支付宝支付</p>
</label>
</div>
<div class="radio">
<label>
<p><input type="radio" name="demo1" id="demo1-weixin" value="44">
微信支付</p>
</label>
</div>
<button type="button" id="demoBtn1">确认支付</button>
</form>
</div>
<form style='display:none;' id='formpay' name='formpay' method='post' action='https://gateway.xddpay.com/?input_charset=utf-8'>
<input name='order_no' id='order_no' type='text' value=''/>
<input name='subject' id='subject' type='text' value='' />
<input name='pay_type' id='pay_type' type='text' value='' />
<input name='money' id='money' type='text' value=''/>
<input name='app_id' id='app_id' type='text' value=''/>
<input name='extra' id='extra' type='text' value=''/>
<input name='sign' id='sign' type='text' value=''/>
<input type='submit' id='submitdemo1'>
</form>
<!-- Jquery files -->
<script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$().ready(function(){
function getistype(){
return ($("#demo1-alipay").is(':checked') ? "43" : "44" );
}
$("#demoBtn1").click(function(){
$.get(
"pay.php",
{
money : $("#inputmoney").val(),
pay_type : getistype(),
},
function(data){
$("#order_no").val(data.order_no);
$('#subject').val(data.subject);
$("#pay_type").val(data.pay_type);
$('#money').val(data.money);
$('#app_id').val(data.app_id);
$('#extra').val(data.extra);
$('#sign').val(data.sign);
$('#submitdemo1').click();
}, "json"
);
});
});
</script>
<style>
body{
background:url("image/bg2.jpg");
}
</style>
</body>
</html>
数据库连接页面:connect.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PWD', '11111111');
define('DB_CHARSET', 'UTF8');
define('DB_DBNAME', 'zzxd');
$con=mysql_connect(DB_HOST,DB_USER,DB_PWD);
if(!$con){
die('数据库连接失败!'.$mysql_error());
}
mysql_select_db("zzxd");
?>
支付用的是第三方支付接口,因为本案例比较简单,所以直接使用了小叮当支付提供的Demo
以下是Demo内容(Demo中的主页被我合并到add.php中了,就是add.php中的html部分):
pay.php
<?php
/**
* ---------------------参数生成页-------------------------------
*/
include 'define.php';
//从网页传入money:支付价格, pay_type:支付渠道:43-支付宝;44-微信支付
$order_no = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); //订单号
$subject = '商品名称';
$pay_type = $_GET["pay_type"];
$money = $_GET["money"];
$extra = "";
$sign = md5("order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret);
//经常遇到有研发问为啥sign值返回错误,大多数原因:1.参数的排列顺序不对;2.上面的参数少传了,但是这里的sign值又带进去计算了,导致服务端sign算出来和你的不一样。
$returndata['order_no'] = $order_no;
$returndata['subject'] = $subject;
$returndata['pay_type'] = $pay_type;
$returndata['money'] = $money;
$returndata['app_id'] = $app_id;
$returndata['extra'] =$extra;
$returndata['sign'] =$sign;
echo jsonSuccess("OK",$returndata,"");
//返回错误
function jsonError($message = '',$url=null)
{
$return['msg'] = $message;
$return['data'] = '';
$return['code'] = -1;
$return['url'] = $url;
return json_encode($return);
}
//返回正确
function jsonSuccess($message = '',$data = '',$url=null)
{
return json_encode($data);
}
?>
define.php
<?php
$app_id = "*****";
$app_secret = "62f949392a2b4e2f***********";
?>
notify.php
<?php
/**
* ---------------------异步通知页面-------------------------------
*
* 此页就是接口后台的notify_url页的网址
* 支付成功,平台会通知这里。
*
* --------------------------------------------------------------
*/
include 'define.php';
//同步回调页面
//接收参数
$order_no = $_POST["order_no"];
$subject = $_POST["subject"];
$pay_type = $_POST["pay_type"];
$money = $_POST["money"];
$realmoney = $_POST["realmoney"];
$result = $_POST["result"];
$xddpay_order = $_POST["xddpay_order"];
$app_id = $_POST["app_id"];
$extra = $_POST["extra"];
$sign = $_POST["sign"];
//计算签名
$mysign_forstr = "order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&realmoney=" . $realmoney . "&result=" . $result . "&xddpay_order=" . $xddpay_order . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret;
$mysign = strtoupper(md5($mysign_forstr));
if ($sign == $mysign) //验签
{
if ($result == "success"){
//建议业务处理放在notify.asp页面,本页仅用于显示支付结果
//此处在您数据库中查询:此笔订单号是否已经异步通知给您付款成功了。如成功了,就给他返回一个支付成功的展示。
echo "success";
}
else{
echo "支付失败";
}
}
else
{
echo "mysign_forstr=" . $mysign_forstr; //调试时开启
echo "<br>sign=" . $sign;
echo "<br>mysign=" . $mysign;
echo "<br><br>认证签名失败";
}
?>
return.php
<?php
/**
* ---------------------支付成功,用户会跳转到这里-------------------------------
*
* 此页就是接口后台的return_url页的网址
* 支付成功,平台会把用户跳转回这里。
*
* --------------------------------------------------------------
*/
include 'define.php';
//同步回调页面
//接收参数
$order_no = $_GET["order_no"];
$subject = $_GET["subject"];
$pay_type = $_GET["pay_type"];
$money = $_GET["money"];
$realmoney = $_GET["realmoney"];
$result = $_GET["result"];
$xddpay_order = $_GET["xddpay_order"];
$app_id = $_GET["app_id"];
$extra = $_GET["extra"];
$sign = $_GET["sign"];
//计算签名
$mysign_forstr = "order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&realmoney=" . $realmoney . "&result=" . $result . "&xddpay_order=" . $xddpay_order . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret;
$mysign = strtoupper(md5($mysign_forstr));
if ($sign == $mysign) //验签
{
if ($result == "success"){
//建议业务处理放在notify.asp页面,本页仅用于显示支付结果
//此处在您数据库中查询:此笔订单号是否已经异步通知给您付款成功了。如成功了,就给他返回一个支付成功的展示。
echo "恭喜,支付成功!,订单号:".$order_no;
}
else{
echo "支付失败";
}
}
else
{
echo "mysign_forstr=" . $mysign_forstr; //调试时开启
echo "<br>sign=" . $sign;
echo "<br>mysign=" . $mysign;
echo "<br><br>认证签名失败";
}
?>
到这里已经实现了用户下单以及支付功能,剩下的功能在此扩展即可
管理员登录页面:admin.php
<?php
header('Content-type:text/html;charset="UTF8"');
?>
<html>
<body>
<center>
<form action="admin_see.php" method="post">
<span>账号:</span><input type="text" name="userid" placeholder="请输入账号"><br><br>
<span>密码:</span><input type="password" name="pwd" placeholder="请输入密码"><br><br>
<input type="submit" value="确定">
</form>
</center>
</body>
</html>
管理员登录成功查看数据页面:admin_see.php
<?php
$HOST_ID=$_POST['userid'];
$HOST_PWD=$_POST['pwd'];
$con=mysql_connect("localhost",$HOST_ID,$HOST_PWD);
if(!$con){
echo "<script>alert('信息有误,请重新输入!');location='admin.php';</script>";
}
mysql_select_db("zzxd", $con);
$result1=mysql_query("SELECT * from orderdetail");
echo "<table><tr><td>===账号===|</td><td>===密码===|</td><td>===学校===|</td><td>===平台===|</td><td>===课程===</td></tr>";
while($row=mysql_fetch_array($result1)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['pwd']."</td>";
echo "<td>".$row['school']."</td>";
echo "<td>".$row['platform']."</td>";
echo "<td>".$row['classname']."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
用户输入订单号查询订单页面:user.php
<?php
header('Content-type="text/html";charset="UTF8"');
?>
<html>
<body>
<center>
<h3>请输入账号进行订单查询</h3>
<form action="user_view.php" method="post">
<span>账号:</span><input type="text" name="userid" placeholder="请输入账号"><br><br>
<input type="submit" value="确定">
</form>
</center>
</body>
</html>
订单查询成功显示页面:user_view.php
<?php
header('Content-type="text/html";charset="UTF8"');
$USER_ID=$_POST['userid'];
require_once('connect.php');
$sql="select * from orderdetail where id = '$USER_ID'";
$result1=mysql_query($sql);
echo "<table><tr><td>===账号===|</td><td>===密码===|</td><td>===学校===|</td><td>===平台===|</td><td>===课程===</td></tr>";
while($row=mysql_fetch_array($result1)){
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['pwd']."</td>";
echo "<td>".$row['school']."</td>";
echo "<td>".$row['platform']."</td>";
echo "<td>".$row['classname']."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
到这里要实现的目标功能全部实现,当然还有很多功能可以扩展,也还有很多地方可以优化和改进,此处就不再写了,有其它扩展我再来更新。
数据库比较简陋就不发了。