php編寫刷網課自助下單系統(第三方支付實例)

此項目是因爲本人剛剛入門php且在校代刷網課而編寫的,因爲在上課時間不方便接單,故特地寫一個自助下單系統來實現客戶自助下單。
本項目主要實現如下功能:
1.用戶下單
2.用戶支付
3.用戶經過帳號查詢訂單
4.管理員登陸查看全部訂單

設計思路:項目比較簡單,基本就是與數據庫之間的通訊,進行數據的增刪改查。支付使用的是第三方支付接口。

如下是具體代碼:
用戶提交訂單頁面
index.phpjavascript

 1 <?php
 2  header('Content-type: text/html; charset=UTF8');
 3 ?>
 4 <html>
 5 <head>
 6 <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
 7 </head>
 8 <body>
 9 <div class="content" align="center">
10 <form action="add.php" method="post">
11 <span>帳號:</span>
12 <input type="text" name="id" placeholder="帳號" required><br><br>
13 <span>密碼:</span>
14 <input type="password" name="pwd" placeholder="密碼" required><br><br>
15 <span>學校全稱:</span>
16 <input type="text" name="school" placeholder="學校全稱" required><br><br>
17 <span>課程平臺:</span>
18 <input type="text" name="platform" placeholder="課程平臺" value="智慧樹" required><br><br>
19 <span>課程門數:</span>
20 <input type="text" name="count" placeholder="課程門數" id="num" required><br><br>
21 <span>課程名稱:</span><br>
22 <textarea  name="classname" placeholder="請輸入課程名稱,多門課程用逗號分開" required>
23 </textarea><br>
24 <div class="list">
25 <p class="p1">總價:</p>
26 <input id="price" name="sprice"class="p1" value="0"  readonly="readonly">
27 <p class="p1">元</p>
28 </div>
29 <br>
30  <input type="submit" value="提交" class="submit" id="button">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
31 
32     <input type = "reset" value = "重置" class="reset">
33 </form>
34 </div>
35 </body>
36 <style>
37 body{
38     background:url(image/bg2.jpg);
39 }
40 .list{
41     width:120px;
42     height:50px;
43     background:url(image/bg5.jpg);
44 }
45 .p1{
46     float:left;
47 }
48 #price{
49     margin-top:15px;
50     width:50px;
51     color:red;
52 }
53 textarea{
54     height:100px;
55     padding: 5px 0px 0px 5px;
56     width: 70%;
57 }
58 </style>
59 <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js">
60 </script>
61 <script>    
62         $(function(){        
63             $('#num').on('input  propertychange',function(){                
64                 var num = $('#num').val();
65                 var price = num*15;
66                  $("#price").val(price);
67             })
68         });            
69 </script>
70 </html>


處理訂單並生成支付數據頁面
add.phpphp

  1 <?php
  2  header('Content-type: text/html; charset=UTF8');
  3 ?>
  4 <?php    
  5     require_once "connect.php";
  6     $id=$_POST["id"];
  7     $pwd=$_POST["pwd"];
  8     $school=$_POST["school"];
  9     $platform=$_POST["platform"];
 10     $count=$_POST["count"];
 11     $classname=$_POST["classname"];
 12     $price=$_POST["sprice"];
 13     if($id==null){
 14             echo "<script>alert('請輸入帳號!');location='index.php';</script>";
 15         };
 16     if($pwd==null){
 17             echo "<script>alert('請輸入密碼!');location='index.php';</script>";
 18         };
 19     if($school==null){
 20             echo "<script>alert('請輸入學校全稱!');location='index.php';</script>";
 21         };
 22     if($platform==null){
 23             echo "<script>alert('請輸入平臺名稱!');location='index.php';</script>";
 24         };
 25     if($count==null){
 26             echo "<script>alert('請輸入課程數量!');location='index.php';</script>";
 27         };
 28     $sql="insert into orderdetail (id,pwd,school,platform,count,classname,time)values('$id','$pwd','$school','$platform','$count','$classname',now())";
 29     mysql_query("set names 'utf8'");
 30     mysql_query($sql);
 31     mysql_close;
 32     //echo "<script>alert('信息提交成功,跳轉付款頁面!');location='chosetopay.html';</script>";
 33 ?>
 34 
 35 <html>
 36 <head>
 37 <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
 38     <title>刷客在線支付</title>
 39 </head>
 40 <body>
 41 <center>
 42 <h1>請您確認訂單信息:</h1>
 43 <span>帳號:</span><?php echo $id ?><br>
 44 <span>密碼:</span><?php echo $pwd ?><br>
 45 <span>學校:</span><?php echo $school ?><br>
 46 <span>平臺:</span><?php echo $platform ?><br>
 47 <span>課程名:</span><?php echo $classname ?>
 48 <br><br><br>
 49 </center>
 50 <div align="center">
 51     <form>
 52         <p><input id="inputmoney" type="text" name="inputmoney" class="form-control" placeholder="請輸入金額" required value="<?php echo $price.'.00'?>" readonly="readonly"></p>
 53         <div class="radio">
 54             <label>
 55                 <p><input type="radio" name="demo1" id="demo1-alipay" value="43" checked="">
 56                     支付寶支付</p>
 57             </label>
 58         </div>
 59         <div class="radio">
 60             <label>
 61                 <p><input type="radio" name="demo1" id="demo1-weixin" value="44">
 62                 微信支付</p>
 63             </label>
 64         </div>
 65         <button type="button" id="demoBtn1">確認支付</button>
 66     </form>
 67 </div>
 68     <form style='display:none;' id='formpay' name='formpay' method='post' action='https://api.6688pay.com:8080/?input_charset=utf-8'>
 69         <input name='order_no' id='order_no' type='text' value=''/>
 70         <input name='subject' id='subject' type='text' value='' />
 71         <input name='pay_type' id='pay_type' type='text' value='' />
 72         <input name='money' id='money' type='text' value=''/>
 73         <input name='app_id' id='app_id' type='text' value=''/>        
 74         <input name='extra' id='extra' type='text' value=''/>
 75         <input name='sign' id='sign' type='text' value=''/>
 76         <input type='submit' id='submitdemo1'>
 77     </form>
 78 
 79 <!-- Jquery files -->
 80 <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.11.1/jquery.min.js"></script>
 81 <script type="text/javascript">
 82 $().ready(function(){
 83     function getistype(){
 84         return ($("#demo1-alipay").is(':checked') ? "43" : "44" );
 85     }
 86 
 87     $("#demoBtn1").click(function(){
 88         $.get(
 89             "pay.php",
 90             {
 91                 money : $("#inputmoney").val(),
 92                 pay_type : getistype(),
 93             },
 94             function(data){
 95                 $("#order_no").val(data.order_no);
 96                 $('#subject').val(data.subject);
 97                 $("#pay_type").val(data.pay_type);                
 98                 $('#money').val(data.money);
 99                 $('#app_id').val(data.app_id);
100                 $('#extra').val(data.extra);
101                 $('#sign').val(data.sign);
102                 $('#submitdemo1').click();
103             }, "json"
104         );
105     });
106 });
107 </script>
108 <style>
109 body{
110     background:url("image/bg2.jpg");
111 }
112 </style>
113 </body>
114 </html> 


數據庫鏈接頁面
connect.phphtml

 1 <?php
 2     define('DB_HOST', 'localhost');  
 3     define('DB_USER', 'root');  
 4     define('DB_PWD', '11111111');
 5     define('DB_CHARSET', 'UTF8');  
 6     define('DB_DBNAME', 'zzxd');
 7     
 8     $con=mysql_connect(DB_HOST,DB_USER,DB_PWD);
 9     if(!$con){
10         die('數據庫鏈接失敗!'.$mysql_error());
11     }
12     mysql_select_db("zzxd");
13 ?>


支付用的是第三方支付接口,由於本案例比較簡單(主要是本人暫時用不來api)因此直接使用了它提供的Demojava

(此處使用的是小叮噹支付)mysql


如下是Demo內容(Demo中的主頁被我合併到add.php中了,就是add.php中的html部分):

pay.php

jquery

 1 <?php
 2 /**
 3  * ---------------------參數生成頁-------------------------------
 4  */
 5     include 'define.php';
 6     //從網頁傳入money:支付價格, pay_type:支付渠道:43-支付寶;44-微信支付
 7     $order_no = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);    //訂單號
 8     $subject = '商品名稱';
 9     $pay_type = $_GET["pay_type"];
10     $money = $_GET["money"];
11     $extra = "";
12     
13     $sign = md5("order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret);
14     //常常遇到有研發問爲啥sign值返回錯誤,大多數緣由:1.參數的排列順序不對;2.上面的參數少傳了,可是這裏的sign值又帶進去計算了,致使服務端sign算出來和你的不同。
15 
16     $returndata['order_no'] = $order_no;
17     $returndata['subject'] = $subject;
18     $returndata['pay_type'] = $pay_type;
19     $returndata['money'] = $money;
20     $returndata['app_id'] = $app_id;
21     $returndata['extra'] =$extra;
22     $returndata['sign'] =$sign;
23     echo jsonSuccess("OK",$returndata,"");
24 
25 
26     //返回錯誤
27     function jsonError($message = '',$url=null)
28     {
29         $return['msg'] = $message;
30         $return['data'] = '';
31         $return['code'] = -1;
32         $return['url'] = $url;
33         return json_encode($return);
34     }
35 
36     //返回正確
37     function jsonSuccess($message = '',$data = '',$url=null)
38     {
39         return json_encode($data);
40     }
41 
42 ?>


define.php

sql

 1 <?php    
 2     $app_id = "*****";
 3     $app_secret = "62f949392a2b4e2f***********";
 4 ?>
 5 ```
 6 notify.php
 7 
 8 ```
 9 <?php
10 /**
11  * ---------------------異步通知頁面-------------------------------
12  *
13  * 此頁就是接口後臺的notify_url頁的網址
14  * 支付成功,平臺會通知這裏。
15  *
16  * --------------------------------------------------------------
17  */
18     include 'define.php';
19 
20     //同步回調頁面
21     //接收參數
22     $order_no = $_POST["order_no"];
23     $subject = $_POST["subject"];
24     $pay_type = $_POST["pay_type"];
25     $money = $_POST["money"];
26     $realmoney = $_POST["realmoney"];
27     $result = $_POST["result"];
28     $xddpay_order = $_POST["xddpay_order"];
29     $app_id = $_POST["app_id"];
30     $extra = $_POST["extra"];
31     $sign = $_POST["sign"];
32     
33     //計算簽名
34     $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;
35     $mysign = strtoupper(md5($mysign_forstr));
36     
37     if ($sign == $mysign) //驗籤
38     {
39         if ($result == "success"){
40             //建議業務處理放在notify.asp頁面,本頁僅用於顯示支付結果
41             //此處在您數據庫中查詢:此筆訂單號是否已經異步通知給您付款成功了。如成功了,就給他返回一個支付成功的展現。
42             echo "success";
43         }
44         else{
45             echo "支付失敗";
46         }
47     }
48     else
49     {
50         echo "mysign_forstr=" . $mysign_forstr;    //調試時開啓
51         echo "<br>sign=" . $sign;
52         echo "<br>mysign=" . $mysign;
53         echo "<br><br>認證簽名失敗";
54     }
55 
56 ?>

 

return.php數據庫

 1 <?php
 2 /**
 3  * ---------------------支付成功,用戶會跳轉到這裏-------------------------------
 4  *
 5  * 此頁就是接口後臺的return_url頁的網址
 6  * 支付成功,平臺會把用戶跳轉回這裏。
 7  *
 8  * --------------------------------------------------------------
 9  */
10     include 'define.php';
11 
12     //同步回調頁面
13     //接收參數
14     $order_no = $_GET["order_no"];
15     $subject = $_GET["subject"];
16     $pay_type = $_GET["pay_type"];
17     $money = $_GET["money"];
18     $realmoney = $_GET["realmoney"];
19     $result = $_GET["result"];
20     $xddpay_order = $_GET["xddpay_order"];
21     $app_id = $_GET["app_id"];
22     $extra = $_GET["extra"];
23     $sign = $_GET["sign"];
24     
25     //計算簽名
26     $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;
27     $mysign = strtoupper(md5($mysign_forstr));
28     
29     if ($sign == $mysign) //驗籤
30     {
31         if ($result == "success"){
32             //建議業務處理放在notify.asp頁面,本頁僅用於顯示支付結果
33             //此處在您數據庫中查詢:此筆訂單號是否已經異步通知給您付款成功了。如成功了,就給他返回一個支付成功的展現。
34             echo "恭喜,支付成功!,訂單號:".$order_no;
35         }
36         else{
37             echo "支付失敗";
38         }
39     }
40     else
41     {
42         echo "mysign_forstr=" . $mysign_forstr;    //調試時開啓
43         echo "<br>sign=" . $sign;
44         echo "<br>mysign=" . $mysign;
45         echo "<br><br>認證簽名失敗";
46     }
47 
48 ?>

 


到這裏已經實現了用戶下單以及支付功能,剩下的功能在此擴展便可
管理員登陸頁面:
admin.php

json

 1 <?php
 2 header('Content-type:text/html;charset="UTF8"');
 3 ?>
 4 <html>
 5 <body>
 6 <center>
 7 <form action="admin_see.php" method="post">
 8     <span>帳號:</span><input type="text" name="userid" placeholder="請輸入帳號"><br><br>
 9     <span>密碼:</span><input type="password" name="pwd" placeholder="請輸入密碼"><br><br>
10     <input type="submit" value="肯定">
11 </form>
12 </center>
13 </body>
14 </html>

 


管理員登陸成功查看數據頁面:
admin_see.php

api

 1 <?php
 2 $HOST_ID=$_POST['userid'];
 3 $HOST_PWD=$_POST['pwd'];
 4 $con=mysql_connect("localhost",$HOST_ID,$HOST_PWD);
 5 if(!$con){
 6     echo "<script>alert('信息有誤,請從新輸入!');location='admin.php';</script>";
 7 }
 8 mysql_select_db("zzxd", $con);
 9 $result1=mysql_query("SELECT * from orderdetail");
10 echo "<table><tr><td>===帳號===|</td><td>===密碼===|</td><td>===學校===|</td><td>===平臺===|</td><td>===課程===</td></tr>";
11 while($row=mysql_fetch_array($result1)){
12     echo "<tr>";
13     echo "<td>".$row['id']."</td>";
14     echo "<td>".$row['pwd']."</td>";
15     echo "<td>".$row['school']."</td>";
16     echo "<td>".$row['platform']."</td>";
17    echo "<td>".$row['classname']."</td>";
18     echo "</tr>";
19 }
20 echo "</table>";
21 
22 mysql_close($con);
23 
24 ?>

 


用戶輸入訂單號查詢訂單頁面
user.php

 1 <?php
 2 header('Content-type="text/html";charset="UTF8"');
 3 ?>
 4 <html>
 5 <body>
 6 <center>
 7 <h3>請輸入帳號進行訂單查詢</h3>
 8 <form action="user_view.php" method="post">
 9     <span>帳號:</span><input type="text" name="userid" placeholder="請輸入帳號"><br><br>
10     <input type="submit" value="肯定">
11 </form>
12 </center>
13 </body>
14 </html>

 


訂單查詢成功顯示頁面:
user_view.php

 1 <?php
 2 header('Content-type="text/html";charset="UTF8"');
 3 $USER_ID=$_POST['userid'];
 4 require_once('connect.php');
 5 $sql="select * from orderdetail where id = '$USER_ID'";
 6 $result1=mysql_query($sql);
 7 echo "<table><tr><td>===帳號===|</td><td>===密碼===|</td><td>===學校===|</td><td>===平臺===|</td><td>===課程===</td></tr>";
 8 while($row=mysql_fetch_array($result1)){
 9     echo "<tr>";
10     echo "<td>".$row['id']."</td>";
11     echo "<td>".$row['pwd']."</td>";
12     echo "<td>".$row['school']."</td>";
13     echo "<td>".$row['platform']."</td>";
14    echo "<td>".$row['classname']."</td>";
15     echo "</tr>";
16 }
17 echo "</table>";
18 
19 mysql_close($con);
20 ?>

 

到這裏要實現的目標功能所有實現,固然還有不少功能能夠擴展,也還有不少地方能夠優化和改進,此處就再也不寫了,有其它擴展我再來更新。數據庫比較簡陋就不發了。

相關文章
相關標籤/搜索