|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm CtripUtil.php
- * Create By 2018/2/2 9:52 $
- */
-
- namespace common\util;
-
-
- use backend\modules\api\models\OrderTitle;
- use backend\modules\api\util\Util;
- use backend\modules\zzcs\models\OrderTitleCancelRequest;
- use yii\base\Exception;
-
- class CtripUtil
- {
- private $version = '';//接口版本
- private $accountId = '';//供应商系统分配给OTA的账户
- private $signkey = '';//秘钥
- private $time = '';//时间
- private $header = [];//头部
- private $body = [];//报文体
- private $serverName = '';//接口名称
- private $request = '';//请求报文
- private $response = '';//响应报文
- private $data = [];//传递的参数
- private static $util = null;
- private $requestBodyXml = '';
-
- private function __construct($version = '2.0')
- {
- $this->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 = '<?xml version="1.0" encoding="UTF-8"?>' . 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 = '<?xml version="1.0" encoding="UTF-8"?>' . 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;
- }
- }
|