public function notify(){ $time = time(); if($_POST['trade_status'] == 'TRADE_SUCCESS') { //商户订单号 $out_trade_no = $_POST['out_trade_no']; //支付宝交易号 $trade_no = $_POST['trade_no']; //交易状态 $trade_status = $_POST['trade_status']; //买家支付宝账号 $buyer_email = $_POST['buyer_email']; $model = M('Order'); $data['order_sn'] = $out_trade_no; $vo = $model->field('id,type,order_sn,bond,member_id,member_name,realname,seller_id,title,actual_paid,pay_status,status')->where($data)->find(); //echo $model->getlastsql(); //判断购买订单是否存在 if(!$vo){ $this->update_remark('订单不存在'); //echo "success";exit; }else{ //支付判断 if($vo['pay_status']>0){ $this->update_remark('订单已支付'); } //价格判断 if($vo['actual_paid']!=$_POST['total_fee']){ $this->update_remark('支付价格错误'); } $model->startTrans();//启用事务 //传参数存在 if($_POST['extra_common_param']){ $paramstr = $_POST['extra_common_param']; if($paramstr){ $arr = explode(',',$paramstr); foreach($arr as $val){ $ar = explode('|',$val); $key = $ar[0]; $v = $ar[1]; $$key = $v; } } } //支付渠道 if($payment_channel){ $this->payment_channel = $payment_channel; } //订单状态修改 $sdata['pay_status'] = 1; $sdata['pay_order_id'] = $trade_no; $sdata['payment_mode'] = 1; $sdata['payment_company'] = $this->payment_company; $sdata['payment_channel'] = $this->payment_channel; $sdata['pay_time'] = $time; $result = $model->where($data)->save($sdata); //修改详细订单状态 (废弃,制至支付完成后处理) /* if($result){ $odmodel = M('Order_detail'); $od_data['oid'] = $vo['id']; $od_sdata['status'] = 1; $od_sdata['pay_time'] = $time; $result = $odmodel->where($od_data)->save($od_sdata); unset($od_data); }else{ $this->update_remark('订单状态修改失败'); } */ } dump($vo);exit; //买家账户记录 $wallet_model = M('Member_wallet'); $rdata['member_id'] = $vo['member_id']; $wallet = $wallet_model->where($rdata)->find(); $rmodel = D('Record'); $rdata['pay_type'] = $pay_type ? $pay_type : 2; $rdata['member_name'] = $vo['member_name']; //$rdata['realname'] = $vo['realname']; $rdata['payment_mode'] = 1; $rdata['payment_company'] = $payment_company ? $payment_company : 'alipay'; $rdata['order_id'] = $vo['order_id']; $rdata['pay_order_id'] = $trade_no; $rdata['buyer'] = $buyer_email; $rdata['balance'] = $wallet['balance'] ? $wallet['balance'] : 0; $rdata['content'] = $_POST['body'] ? $_POST['body'] : ''; $rdata['pay'] = $_POST['total_fee']; $rdata['create_time'] = $time; $rdata['status'] = 1; $rdata['pay_time'] = $_POST['gmt_payment'] ? strtotime($_POST['gmt_payment']) : $time; $rid = $rmodel->add($rdata); //拍卖,扣除买家保证金 if($vo['bond']){ $wallet_data2['member_id'] = $vo['member_id']; if($result)$result=$wallet_model->where($wallet_data2)->setDec('frozen',$vo['bond']); //记录变化 $wlmodel = M('member_wallet_log'); $wl_data['title'] = '扣除保证金并完成支付,订单号:'.$vo['order_id']; $wl_data['member_id'] = $vo['member_id']; $wl_data['oid'] = $vo['id']; $wl_data['pay_type'] = 3; $wl_data['pay'] = $vo['bond']; $wl_data['content'] = '扣除保证金并完成支付,订单号:<a href="'.__APP__.'/Orders/index/order_sn/'.$vo['order_sn'].'">'.$vo['order_sn'].'</a>'; $wl_data['create_time'] = time(); if($result)$result=$wlmodel->add($wl_data); } //卖家资金记录 if($vo['seller_id']){ } if($result){ //支付完成后 $result = after_pay($vo); } if($result && $rid){ $model->commit(); $rstatus = 1; }else{ $model->rollback(); //记录淘宝传值信息 $this->update_remark('支付失败'); } $log['order_sn'] = $vo['order_sn']; $log['utype'] = 1; $log['user_id'] = $vo['member_id']; $log['user_name'] = $vo['member_name']; $log['msg'] = '支付宝支付'; $log['action'] = '完成支付'; $log['create_time'] = $time; $log['ip'] = _get_ip(); order_log($log); //记录淘宝传值信息 $this->update_remark('支付完成'); echo "success"; }else{ echo "fail"; } }
/** * 余额支付 */ public function balance_pay(){ if(!$_POST['mo_sn'] && !$_POST['order_sn']){ ajaxErrReturn('订单不存在'); } if($this->user['password']!=md5($_POST['password'].$this->user['salt'].$this->user['salt'][1])){ ajaxErrReturn('密码错误'); } $model = M('Order'); $od_model = M('OrderDetail'); $wallet_model = M('MemberWallet'); $time = time(); //$trade_no = build_order_no($this->user['id']); $model->startTrans();//启用事务 if($_POST['mo_sn']){ $data['mo_sn'] = $_POST['mo_sn']; }else{ $data['order_sn'] = $_POST['order_sn']; } $data['member_id'] = $this->user['id']; //$data['pay_status'] = 0; $list = $model->field('id,seller_id,bond,type,actual_paid,order_sn,order_time,member_id,member_name,pay_status,status')->where($data)->select(); if(!$list){ ajaxErrReturn('订单不存在'); } foreach($list as $vo){ $total_fee += $vo['actual_paid']; } if($this->user['balance']<$total_fee){ ajaxErrReturn('可用余额不足'); } $totalprice = 0 ; foreach($list as $vo){ $totalprice += $vo['actual_paid']; if(!$vo){ ajaxErrReturn($vo['order_sn'].'订单不存在'); } if($vo['order_time'] && time()>$vo['order_time']){ ajaxErrReturn($vo['order_sn'].'订单已过期'); } if($vo['type']>2){ ajaxErrReturn($vo['order_sn'].'订单有误'); } if($vo['pay_status']>0){ ajaxErrReturn($vo['order_sn'].'订单已支付'); } //$time = time(); $trade_no = build_order_no($this->user['id']); $subject = '购买商品:'; $body = '购买商品'; //订单状态修改 $wdata['id'] = $vo['id']; $sdata['payment_company'] = '网站余额'; $sdata['payment_channel'] = 'wxwap'; $sdata['pay_status'] = 1; $sdata['pay_order_id'] = $trade_no; $sdata['pay_time'] = $time; $result = $model->where($wdata)->save($sdata); if(!$result){ $model->rollback(); ajaxErrReturn('支付失败'); } //修改余额 $wallet_data['member_id'] = $vo['member_id']; $result = $wallet_model->where($wallet_data)->setDec('balance',$vo['actual_paid']); if(!$result){ $model->rollback(); ajaxErrReturn('支付失败'); } $wl_data['title'] = '完成支付,订单号:'.$vo['order_sn']; $content = '共支付'.$vo['actual_paid']; //记录买家财务账单 unset($rdata); $rmodel = M('Record'); $rdata['member_id'] = $vo['member_id']; $wallet = $wallet_model->where($wallet_data)->find(); $rdata['member_name'] = $vo['member_name']; $rdata['realname'] = $vo['realname']; $rdata['order_sn'] = $vo['order_sn']; $rdata['pay_type'] = 2; $rdata['payment_mode'] = 1; $rdata['payment_company'] = $this->configs['company_name']; $rdata['payment_channel'] = '网站余额'; $rdata['pay_order_sn'] = $trade_no; $rdata['buyer'] = $vo['member_name']; $rdata['content'] = $content; $rdata['balance'] = $wallet['balance'] ? $wallet['balance'] : 0; $rdata['amount'] = $vo['actual_paid']; $rdata['create_time'] = $time; $rdata['status'] = 1; $rdata['pay_time'] = time(); if($result)$result = $rmodel->add($rdata); //支付完成后处理 $result = after_pay($vo); if(!$result){ $model->rollback(); ajaxErrReturn('支付失败'); } $log['order_sn'] = $vo['order_sn']; $log['utype'] = 1; $log['user_id'] = $this->user['id']; $log['user_name'] = $this->user['username']; $log['msg'] = '账户余额支付'; $log['action'] = '完成支付'; $log['create_time'] = time(); $log['ip'] = _get_ip(); order_log($log); } if($result){ $model->commit(); $msg['notice'] = '支付成功'; if($_REQUEST['order_id']){ $gourl = U('Order/paycomplet',array('order_id'=>$_REQUEST['order_id'])); }else{ $gourl = U('Order/paycomplet',array('mo_sn'=>$_REQUEST['mo_sn'])); } $msg['gourl'] = $gourl; ajaxSucReturn($msg); }else{ $model->rollback(); ajaxErrReturn('支付失败'); } }