头像

Tp5.0整合支付宝在线下单

2019-03-28 10:17:22 收藏    来源:PHP代码   浏览()   评论 ( 0 )   

Tp5.0整合支付宝在线下单
Thinkphp5.0支付宝在线支付下单整个流程,包括创建订单、支付成功回调更新订单状态、最终跳转到商户订单详情页
下载资源:0

手机扫码访问:

下载资源 下载积分:800

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

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('sql')->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('sql')->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('sql')->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 `sql` ( 
  `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;


标签: 支付
声明:转载请注明来源(PHP代码)并保留原文链接:http://www.phpdaima.com/pay-1028.html
广告不存在
评论0

后面还有条评论,点击查看>>

温馨提示:为规范评论内容,垃圾评论一律封号...

Tp5.0整合支付宝在线下单
我的积分余额: 0.0 已下载次数: 0
所需积分:800800 开始下载
×