version = $version; $this->accountId = \Yii::$app->params['ctrip_config']['accountId']; $this->signkey = \Yii::$app->params['ctrip_config']['signkey']; $this->time = date('Y-m-d H:i:s'); } /** * Des:通用方法 只是用一些通用方法 * Name: util * @return CtripUtil * @author 倪宗锋 */ public static function util() { if (static::$util == null) { static::$util = new self(); } return static::$util; } /*****==========发送报文===========***************/ /** * Des:获取初始化函数 * Name: init * @param string $version * @return CtripUtil * @author 倪宗锋 */ public static function init($version = '2.0') { return new self($version); } /** * Des:返回值 * Name: exec * @param $serviceName * @param $data * @return array * @author 倪宗锋 */ public function exec($serviceName, $data) { $this->serverName = $serviceName; $this->data = $data; $this->setBody(); $this->setHeader(); $this->request = [ 'request' => array_merge( $this->header, $this->body ) ]; $curl = new CurlInterface($this->request, 2); $this->response = $curl->execute(\Yii::$app->params['ctrip_config']['api'], 'POST'); if (isset($this->response['header']['resultCode']) && $this->response['header']['resultCode'] === '0000') { return Util::returnArrSu('', $this->response); } $msg = empty($this->response['header']['resultMessage']) ? '系统繁忙,请稍后再试!' : $this->response['header']['resultMessage']; $code = empty($this->response['header']['resultCode']) ? '0005' : $this->response['header']['resultCode']; return Util::returnArrEr($msg, [], $code); } /** * Des:设置头部 * Name: setHeader * @author 倪宗锋 */ private function setHeader() { $this->header = [ 'header' => [ 'accountId' => $this->accountId, 'serviceName' => $this->serverName, 'requestTime' => $this->time, 'version' => $this->version, ] ]; $this->header['header']['sign'] = $this->setSign(); } /** * Des:设置body * Name: setBody * @author 倪宗锋 */ private function setBody() { $this->body = [ 'body' => $this->data ]; $this->requestBodyXml = Util::arrayToXml($this->body); } /** * Des:设置加密函数 * Name: setSign * @return string * @author 倪宗锋 */ private function setSign() { $signString = $this->accountId; $signString .= $this->header['header']['serviceName']; $signString .= $this->header['header']['requestTime']; $signString .= base64_encode($this->requestBodyXml); $signString .= $this->version; $signString .= $this->signkey; return md5($signString); } /******************==============接收数据处理===============***********************/ /** * Des:获取返回值 * Name: getRequest * @return array header 头部 body 报文体 * @author 倪宗锋 */ public function getRequest() { $data = file_get_contents("php://input"); try { preg_match_all("/body>(.*)?\/body>/", $data, $bodyXml); if (empty($bodyXml[0][0]) == false) { $this->requestBodyXml = '<' . $bodyXml[0][0]; } $this->request = Util::xmlToArray($data); $this->header = empty($this->request['header']) ? ['header' => []] : ['header' => $this->request['header']]; $this->body = empty($this->request['body']) ? ['body' => []] : ['body' => $this->request['body']]; if ($this->accountId != $this->header['header']['accountId']) { return Util::returnArrEr('账户不存在', '', '0004'); } $sign = $this->setSign(); if (!isset($this->header['header']['sign']) || $sign != $this->header['header']['sign']) { return Util::returnArrEr('签名校验失败', ['sign' => $sign], '0003'); } return Util::returnArrSu('', $this->request); } catch (\Exception $e) { return Util::returnArrEr('xml解析失败', '', '0002'); } } /** * Des:获取body参数 * Name: getBody * @return array * @author 倪宗锋 */ public function getBody() { return $this->body['body']; } /** * Des:重置路由访问地址 * Name: setAction * @param $action * @param $actionName * @author 倪宗锋 */ public function setAction($action, $actionName) { $action->controller->action->id = strtolower($actionName); $action->controller->action->actionMethod = 'action' . ucfirst($actionName); $action->id = strtolower($actionName); $action->actionMethod = 'action' . ucfirst($actionName); } /******************==============设置返回值===============***********************/ /** * Des:返回错误信息 * Name: returnErr * @param $msg * @param $code * @param $body * @return string * @author 倪宗锋 */ public function returnErr($msg, $code, $body = []) { $return = [ 'response' => [ 'header' => [ 'resultCode' => $code, 'resultMessage' => $msg ] ] ]; if (empty($body) == false) { $return['response']['body'] = $body; } $return = '' . Util::arrayToXml($return); \Yii::$app->response->format = 'xml'; \Yii::$app->response->content = $return; return; } /** * Des:返回成功信息 * Name: returnErr * @param $msg * @param $body * @return string * @author 倪宗锋 */ public function returnSu($msg = 'success', $body = []) { $return = [ 'response' => [ 'header' => [ 'resultCode' => '0000', 'resultMessage' => $msg ] ] ]; if (empty($body) == false) { $return['response']['body'] = $body; } $return = '' . Util::arrayToXml($return); \Yii::$app->response->format = 'xml'; \Yii::$app->response->content = $return; return; } /** * Des:取消审核操作 * Name: NoticeOrderCancel * @param $orderId * @param $result_type int 审核结果 2审核成功 3审核失败 4 超时 * @param $remark string 审核备注 * @return array * @author 倪宗锋 */ public static function NoticeOrderCancel($orderId, $result_type, $remark = '') { $cookies = $_COOKIE; $user_id = empty($cookies['user_id']) ? 0 : $cookies['user_id']; $orderTitle = new OrderTitle(); $orderInfo = $orderTitle->getTitleInfo($orderId); if (empty($orderInfo['order_title_id'])) { return Util::returnArrEr('订单不存在'); } if ($orderInfo['is_connected_order'] != 1) {//校验订单是否直连订单 return Util::returnArrEr("非携程直连订单"); } if (empty($orderInfo['sequence_id'])) { return Util::returnArrEr("没有申请退款流程"); } $transaction = \Yii::$app->db->beginTransaction(); try { # 1 设置取消通知操作记录 $orderTitleCancelRequest = new OrderTitleCancelRequest(); $up = [ 'update_time' => date('Y-m-d H:i:s'), 'request_status' => $result_type, 'operate_user_id' => $user_id, ]; $flag = $orderTitleCancelRequest->updateStatusById($up, $orderInfo['id']); if ($flag == false) { return Util::returnArrEr("操作失败![sql]"); } # 2 设置订单取消 if ($result_type == 2) {//审核通过才会发起取消通知 $data = [ 'sign' => $orderId, 'user_id' => $user_id ]; $curl = new CurlInterface($data, 4); $result = $curl->execute('http://' . CS1_DOMAIN . '/api/submit-order/cancel-tourist', 'POST'); if (!isset($result['code']) || $result['code'] != '0') { $transaction->rollBack(); $msg = empty($result['info']) ? '取消订单失败!' : $result['info']; return Util::returnArrEr($msg, '', $result['code']); } } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); return Util::returnArrEr('操作失败![sys]'); } try { # 3 发送通知 $data = [ 'otaOrderId' => $orderInfo['outside_order_no'], 'vendorOrderId' => $orderInfo['order_title_id'], 'sequenceId' => $orderInfo['sequence_id'], 'cancelCount' => 0, 'charge' => 0, 'orderStatus' => $result_type == 2 ? 3 : 4, 'remark' => empty($remark) ? $orderInfo['remark'] : $remark ]; if ($result_type == 2) {//订单取消申请审核成功 $data['cancelCount'] = $orderInfo['cus_num'];//取消数等于总数 } self::init()->exec('NoticeOrderCancel', $data); } catch (Exception $e) { } return Util::returnArrSu(); } /** * Des:发送出游提醒 * Name: NoticeOrderConsumed * @param $otaOrderId * @param $vendorOrderId * @param $prodCnt * @return array * @author 倪宗锋 */ public static function NoticeOrderConsumed($otaOrderId, $vendorOrderId, $prodCnt) { $data = [ 'otaOrderId' => $otaOrderId, 'vendorOrderId' => $vendorOrderId, 'useDate' => date('Y-m-d'), 'amount' => $prodCnt, 'count' => $prodCnt, 'useCount' => $prodCnt, 'cancelCount' => 0, 'orderStatus' => 5 ]; $ctripUtil = CtripUtil::init()->exec('NoticeOrderConsumed', $data); return $ctripUtil; } }