怎样将订单存入数据库(需要在服务器上测试)
我把我的代码git到远程服务器上后(当然此时config.php里面的配置参数也要相应的进行修改),中间的马赛克位置填你服务器的域名或者公网IP地址:
notify_url.php代码
<?php
require_once 'config.php';
require_once 'pagepay/service/AlipayTradeService.php';
require_once("../include/memcache.php");
$arr=$_POST;
$alipaySevice = new AlipayTradeService($config);
$alipaySevice->writeLog(var_export($_POST,true));
$result = $alipaySevice->check($arr);
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
4、验证app_id是否为该商户本身。
*/
if($result) {//验证成功
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表
//商户订单号
$out_trade_no = $_POST['out_trade_no'];
//支付宝交易号
$trade_no = $_POST['trade_no'];
//交易状态
$trade_status = $_POST['trade_status'];
//订单标题
$subject = $_POST['subject'];
//订单金额
$total_amount = $_POST['total_amount'];
//实收金额
$receipt_amount = $_POST['receipt_amount'];
//交易支付时间
$gmt_payment = $_POST['gmt_payment'];
//买家支付宝账号
$buyer_logon_id = $_POST['buyer_logon_id'];//例如: 159****5620
//买家在支付宝的用户id
$buyer_user_id = $_POST['buyer_user_id'];//例如:2088101117955611
//订单描述
$body = $_POST['body'];
$string_arr = explode("&", $body );
$name = $string_arr[0];
$word = $string_arr[1];
$email = $string_arr[2];
$tel = $string_arr[3];
if($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
}
else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//付款完成后,支付宝系统发送该交易状态通知
}
// pdo_query("update donate set word = 'success' where donate_id=1;");
$sql = "insert into trade(out_trade_no, trade_no, trade_status,
subject, total_amount, receipt_amount,
gmt_payment, buyer_logon_id, buyer_user_id,
body, name, word, email, tel, trade_time)
values (?,?,?,?,?,?,?,?,?,?,
?,?,?,?,NOW());";
pdo_query($sql,$out_trade_no, $trade_no, $trade_status, $subject,
$total_amount,$receipt_amount,
$gmt_payment, $buyer_logon_id, $buyer_user_id,
$body, $name, $word, $email, $tel);
if ($subject == '捐赠'){
$sql = "insert into donate(
donate_money, receipt_money, name, word,
email, tel, out_trade_no,trade_no, subject, time)
VALUES (?,?,?,?,?,?,?,?,?, NOW())";
pdo_query($sql, $total_amount, $receipt_amount, $name, $word,
$email, $tel, $out_trade_no,$trade_no, $subject);
}
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
echo "success"; //请不要修改或删除
}else {
// pdo_query("update donate set word = 'fail' where donate_id=1;");
//验证失败
echo "fail";
}
?>
return_url.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
/* *
* 功能:支付宝页面跳转同步通知页面
* 版本:2.0
* 修改日期:2017-05-01
* 说明:
* 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
*************************页面功能说明*************************
* 该页面可在本机电脑测试
* 可放入HTML等美化页面的代码、商户业务逻辑程序代码
*/
require_once("config.php");
require_once 'pagepay/service/AlipayTradeService.php';
require_once("../include/memcache.php");
$arr=$_GET;
$alipaySevice = new AlipayTradeService($config);
$result = $alipaySevice->check($arr);
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
4、验证app_id是否为该商户本身。
*/
if($result) {//验证成功
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//请在这里加上商户的业务逻辑程序代码
//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表
//商户订单号
$out_trade_no = htmlspecialchars($_GET['out_trade_no']);
//支付宝交易号
$trade_no = htmlspecialchars($_GET['trade_no']);
$sql = "select subject from trade where trade_no=?";
$result = pdo_query($sql, $trade_no);
//订单标题
$subject = trim($result[0][0]);
if($subject == '捐赠'){
echo "<script>alert('感谢您的热心捐赠!');</script>";
header("location:../donate.php");
}
else{
echo "<script>alert('这是其他类付款');</script>";
header("location:../index.php");
}
echo "验证成功<br />支付宝交易号:".$trade_no."<br>订单标题:".$subject;
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else {
//验证失败
echo "验证失败";
echo "<script>alert('支付失败!如对订单有疑惑,请咨询管理员');</script>";
header("location:../index.php");
}
?>
<title>支付结果</title>
</head>
<body>
</body>
</html>