作者 郭鑫

金额记录表及手机支付

<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/7/21
* Time: 10:45
*/
namespace api\portal\controller;
use api\portal\controller\NotifyCommonController;
use think\Db;
use traits\controller\Jump;
use api\portal\model\OrderModel;
class AlipaywapController extends NotifyCommonController
{
public function _initialize()
{
require_once VENDOR_PATH.'alipay_wap/config.php';
require_once VENDOR_PATH.'alipay_wap/wappay/service/AlipayTradeService.php';
require_once VENDOR_PATH.'alipay_wap/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
$this->config = $config;
}
public function alipay() {
$data = $this->request->param();
$config = $this->config;
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = trim($data['order_sn']);
//订单名称,必填
$subject = trim($data['name']);
//付款金额,必填
// $total_amount = trim($data['total_amount']);
$total_amount = 0.01;
//商品描述,可空
$body = '';
//超时时间
$timeout_express="1m";
$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$payResponse = new \AlipayTradeService($config);
$result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);
return $result;
}
// 回调
public function notify_url() {
$config = $this->config;
$arr=$_POST;
$alipaySevice = new \AlipayTradeService($config);
$alipaySevice->writeLog(var_export($_POST,true));
$result = $alipaySevice->check($arr);
if($result) {
//商户订单号
$out_trade_no = $_POST['out_trade_no'];
//支付宝交易号
$trade_no = $_POST['trade_no'];
//交易状态
$trade_status = $_POST['trade_status'];
if($_POST['trade_status'] == 'TRADE_FINISHED') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
$order_model = new OrderModel();
$where['order_sn'] = $out_trade_no;
// $where['money'] = $data['total_amount'];
$where['money'] = 1;
$orderInfo = $order_model->where($where)->find();
if(empty($orderInfo) || $orderInfo['status'] == 9) {
echo 'fail';exit;
}
if($orderInfo['status'] == 1) {
$info['pay_type'] = 4;
$info['pay_time'] = time();
$info['status'] = 2;
$info['transaction_id'] = $trade_no;
$order_model->startTrans();
$results = $order_model->where($where)->update($info);
if(!$results) {
$order_model->rollback();
echo 'fail';exit;
} else {
$order_model->commit();
}
}
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
}
else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
$order_model = new OrderModel();
$where['order_sn'] = $out_trade_no;
// $where['money'] = $data['total_amount'];
$where['money'] = 1;
$orderInfo = $order_model->where($where)->find();
if(empty($orderInfo) || $orderInfo['status'] == 9) {
echo 'fail';exit;
}
if($orderInfo['status'] == 1) {
$info['pay_type'] = 4;
$info['pay_time'] = time();
$info['status'] = 2;
$info['transaction_id'] = $trade_no;
$order_model->startTrans();
$results = $order_model->where($where)->update($info);
if(!$results) {
$order_model->rollback();
echo 'fail';exit;
} else {
$order_model->commit();
}
}
//注意:
//付款完成后,支付宝系统发送该交易状态通知
}
echo "success"; //请不要修改或删除
}else {
//验证失败
echo "fail"; //请不要修改或删除
}
}
//
public function return_url() {
$config = $this->config;
$arr = $_GET;
$alipaySevice = new \AlipayTradeService($config);
$result = $alipaySevice->check($arr);
// var_dump($_GET);
// echo '<br/>';
// var_dump($result);exit;
if ($result) {
//商户订单号
$out_trade_no = htmlspecialchars($_GET['out_trade_no']);
//支付宝交易号
$trade_no = htmlspecialchars($_GET['trade_no']);
$data = $_GET;
$where['order_sn'] = $data['out_trade_no'];
// $where['money'] = $data['total_amount'];
$where['money'] = 1;
$order_model = new OrderModel();
$orderInfo = $order_model->where($where)->find();
if($orderInfo['status'] == 1) {
$info['pay_type'] = 4;
$info['pay_time'] = time();
$info['status'] = 2;
$info['transaction_id'] = $data['trade_no'];
$order_model->startTrans();
$results = $order_model->where($where)->update($info);
if(!$results) {
$order_model->rollback();
} else {
$order_model->commit();
if($orderInfo['type'] == 1) {
$this->redirect(url('portal/Index/callback'));
}
if($orderInfo['type'] == 2) {
$this->redirect(url('portal/Index/callback'));
}
if($orderInfo['type'] == 3) {
$this->redirect(url('portal/Index/callback'));
}
}
}
}
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: 29925
* Date: 2018/7/23
* Time: 14:56
*/
namespace api\portal\model;
use api\common\model\CommonModel;
class MoneyDetailModel extends CommonModel
{
}
\ No newline at end of file
... ...