notify_log_start($paytype); $pay = Service::checkNotify($paytype); if (!$pay) { echo '签名错误'; exit; } //验证,拿订单号等信息 $data = $pay->verify(); $out_trade_no = $data['out_trade_no']; //订单查询 $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find(); if(empty($info)){ return $pay->success()->send(); exit; } if($info['order_status'] != 0) { return $pay->success()->send(); exit; } //你可以在此编写订单逻辑 $rs = $this->order_notify_do($out_trade_no); if($rs === false){ //不论结果都应返回success return $pay->success()->send(); exit; }else{ //不论结果都应返回success return $pay->success()->send(); exit; } //默认 return $pay->success()->send(); exit; } //异步逻辑 private function order_notify_do($out_trade_no){ Db::startTrans(); $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find(); if (empty($orderInfo)) { Db::rollback(); return false; } if($orderInfo['order_status'] != 0){ Db::rollback(); return false; } //逻辑开始 $update = [ 'status'=>1, 'paytime'=>time(), 'pay_out_trade_no'=>$out_trade_no, ]; $rs_order = Db::name('order')->where('id',$orderInfo['table_id'])->update($update); if($rs_order === false){ Db::rollback(); return false; } //逻辑结束 //状态 $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]); if($ros === false) { Db::rollback(); return false; } //产品销量增加 $product_id = Db::name('order')->where('id',$orderInfo['table_id'])->value('product_id'); $ticket_number = Db::name('order_ticket')->where('order_id',$orderInfo['table_id'])->sum('ticket_number'); Db::name('product')->where('id',$product_id)->setInc('sell_number',$ticket_number); //默认提交 Db::commit(); return true; } //异步日志 private function notify_log_start($paytype = 'wechat'){ //记录支付回调数据 ignore_user_abort(); // run script in background set_time_limit(30); // 日志文件 start $log_base_dir = '../paylog/'.$paytype.'/'; if (!is_dir($log_base_dir)) { mkdir($log_base_dir, 0770, true); @chmod($log_base_dir, 0770); } $notify_file = $log_base_dir.'notify.txt'; if(!file_exists($notify_file)) { @touch($notify_file); @chmod($notify_file, 0770); } if(filesize($notify_file)>5242880)//大于5M自动切换 { rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt'); } if(!file_exists($notify_file)) { @touch($notify_file); @chmod($notify_file, 0770); } // 日志文件 end //开始写入 $_REQUEST = isset($_REQUEST) ? $_REQUEST : array(); if($_REQUEST && $paytype == 'alipay') { file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND); } else { $xml = file_get_contents("php://input"); file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND); $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND); } ini_set('display_errors','On'); return $notify_file; } }