payrecordModel = new \app\common\model\base\pay\Payrecord;//支付记录模型 $this->orderModel = new \app\common\model\ticket\Order;//购票订单模型 $this->routeShift = new \app\common\model\service\RouteShift; $this->StationServiceIface = new \app\common\iface\Stationservice; $this->TransportIface = new \app\common\iface\Transport; $this->wxpay = new \weixin\Wxpay; $this->passengerModel = new \app\common\model\ticket\Passenger; } /** * 支付成功后的业务处理 */ protected function ConfirmPay($poid,$tran_id='',$trade_type='') { try{ $where = []; $where[] = ['poid','=',$poid]; $where[] = ['status','=',0]; $payInfo = $this->payrecordModel->where($where)->find(); if(empty($payInfo)){ return false; } $uid = $payInfo->uid; //更新支付订单状态 $payInfo->paytime = time(); $payInfo->tran_id = $tran_id; $payInfo->status = 1; $payInfo->save(); $this->payrecordModel::startTrans(); switch($payInfo->ptype){ case 1://站务系统订单支付 //修改订单状态 $orderInfo = $this->orderModel->where("orderNo",$payInfo->oid)->find(); $orderInfo->status = 2; $orderInfo->pay_at = date('Y-m-d H:i:s'); $orderInfo->save(); //修改车票状态 $this->passengerModel->where('orderNo',$payInfo->oid)->update(['status'=>2]); //向站务系统提交支付成功回调 $noticeRes = $this->StationServiceIface->orderNotice($orderInfo->origin,$orderInfo->contactId,$payInfo->oid); $this->payrecordModel::commit(); return true; break; case 2://定制客运订单支付 $orderInfo = $this->orderModel->where("orderNo",$payInfo->oid)->find(); $oldOrderNo = cache('confirmpay_orderNo')??''; //向定制客运接口提交订单 if($oldOrderNo!=$payInfo->oid){//避免重复向定制客运提交订单 $res = $this->CommitTransportOrder($orderInfo); if('OK'==$res['status']){ //修改订单状态 $orderInfo->status = 2; $orderInfo->pay_at = date('Y-m-d H:i:s'); $orderInfo->save(); //修改车票状态 $this->passengerModel->where('orderNo',$payInfo->oid)->update(['status'=>2]); $this->payrecordModel::commit(); return true; }else{ //接口下单失败则立即退款 $res = $this->wxpay->Refund($tran_id,$payInfo->amount,$payInfo->amount,$res['msg']); WLog('ConfirmPay',"定制客运下单失败,进行退款".json_encode($res,JSON_UNESCAPED_UNICODE)); $this->payrecordModel::rollback(); return false; } cache('confirmpay_orderNo',$payInfo->oid,3600); } return true; break; default: $this->payrecordModel::rollback(); return false; break; } }catch(\Exception $e){ WLog('ConfirmPay',$e->getMessage()); $this->payrecordModel::rollback(); return false; } } /** * 提交定制客运接口订单 */ private function CommitTransportOrder($orderInfo) { $param = [ 'mainlineid' => $orderInfo->routeId, 'DailyVehicleID' => $orderInfo->shiftId, 'ticketcount' => $orderInfo->count, 'getonstationID' => $orderInfo->startStationId, 'getoffstationID' => $orderInfo->endStationId, 'EstimateDepartTime'=> $orderInfo->beginDateTime, 'contactphone' => $orderInfo->buyer->mobile, 'contacts' => $this->getContacts($orderInfo->passenger), 'payNo' => $orderInfo->orderNo, 'amount' => $orderInfo->amount, 'actmoney' => $orderInfo->actmoney, ]; $res = $this->TransportIface->commitOrder($param); WLog('ConfirmPay','TransportIface:'.json_encode($res,JSON_UNESCAPED_UNICODE)); return $res; } /**获取乘车人信息 */ private function getContacts($passenger) { $contactAry = []; foreach($passenger as $contact){ $contactAry[] = implode(',',[$contact->username,$contact->mobile,'1023001',$contact->cardNo]); } return implode('#',$contactAry); } }