Thinkphp5.0整合支付宝在线下单【原创

来源:https://www.sucaihuo.com/php/3218.html 2017-11-17 16:02浏览(4247) 收藏

thinkphp5.0支付宝在线支付下单整个流程,包括创建订单、支付成功回调更新订单状态、最终跳转到商户订单详情页
Thinkphp5.0整合支付宝在线下单
分类:PHP > Thinkphp 难易:中级
查看演示 下载资源 下载积分: 998 积分

支付宝在线支付控制器代码

public function alipay() {//发起支付宝支付
         $order_no = date("YmdHis") . rand(100000, 999999);
        if (request()->isPost()) { //支付表单提交,并唤起支付宝在线支付
            //调用 application\index\model\Pay.php
            $Pay = new Pay;
            $result = $Pay->alipay([
                'notify_url' => request()->domain() . url('index/index/alipay_notify'),
                'return_url' => request()->domain() . url('index/index/alipay_return')."?order_no=".$order_no."&",
                'out_trade_no' => input('post.orderid/s', '', 'trim,strip_tags'),
                'subject' => input('post.subject/s', '', 'trim,strip_tags'),
                'total_fee' => input('post.total_fee/f'), //订单金额,单位为元
                'body' => input('post.body/s', '', 'trim,strip_tags'),
            ]);
            if (!$result['code']) {
                return $this->error($result['msg']);
            }
            return $result['msg'];
        }
        //创建订单
        db('order_sucaihuo')->insert(array(
            'order_no' => $order_no,
            'order_money' => 0.1, //订单金额
            'state' => 0, //支付状态 0 未支付, 1已支付
            'uid' => 1, //用户uid
            'addtime' => time(), //下单时间
            'update_time' => 0 //支付时间
        ));
        $this->view->orderid = $order_no;
        return $this->fetch();
    }
    //支付宝客户端会每隔一段时间请求一次
    public function alipay_notify() {//异步订单通知
        $Pay = new Pay;
        $result = $Pay->notify_alipay();
        if ($result == 'success') {

            $pay_info = $_REQUEST;
            $order_no = $pay_info['out_trade_no'];
            $order_info = db('order_sucaihuo')->where('order_no', $order_no)->find();
            //若是未付款则更新
            if ($order_info['state'] == 0) {
                $data['trade_no'] = $pay_info['trade_no'];
                $data['state'] = 1;
                $data['update_time'] = time();
                db('order_sucaihuo')->where("order_no", $order_no)->update($data);
            }
        }
        //测试支付回调,linux记得开启777写入权限
        file_put_contents("notify.txt", $result);
        file_put_contents("request.txt", json_encode($_REQUEST));
   }

压缩包有订单表

CREATE TABLE IF NOT EXISTS `order_sucaihuo` (
  `id` int(11) unsigned NOT NULL,
  `uid` int(11) NOT NULL,
  `order_no` varchar(30) NOT NULL,
  `trade_no` varchar(150) DEFAULT NULL COMMENT '交易号',
  `order_money` decimal(10,2) DEFAULT '0.00',
  `state` int(2) NOT NULL DEFAULT '0',
  `addtime` int(10) NOT NULL,
  `update_time` int(10) DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=6718 DEFAULT CHARSET=utf8;
标签: 支付支付宝
声明:本文为原创文章,如需转载,请注明来源sucaihuo.com并保留原文链接:https://www.sucaihuo.com/php/3218.html
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2