Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

365 строки
12 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm CtripUtil.php
  13. * Create By 2018/2/2 9:52 $
  14. */
  15. namespace common\util;
  16. use backend\modules\api\models\OrderTitle;
  17. use backend\modules\api\util\Util;
  18. use backend\modules\zzcs\models\OrderTitleCancelRequest;
  19. use yii\base\Exception;
  20. class CtripUtil
  21. {
  22. private $version = '';//接口版本
  23. private $accountId = '';//供应商系统分配给OTA的账户
  24. private $signkey = '';//秘钥
  25. private $time = '';//时间
  26. private $header = [];//头部
  27. private $body = [];//报文体
  28. private $serverName = '';//接口名称
  29. private $request = '';//请求报文
  30. private $response = '';//响应报文
  31. private $data = [];//传递的参数
  32. private static $util = null;
  33. private $requestBodyXml = '';
  34. private function __construct($version = '2.0')
  35. {
  36. $this->version = $version;
  37. $this->accountId = \Yii::$app->params['ctrip_config']['accountId'];
  38. $this->signkey = \Yii::$app->params['ctrip_config']['signkey'];
  39. $this->time = date('Y-m-d H:i:s');
  40. }
  41. /**
  42. * Des:通用方法 只是用一些通用方法
  43. * Name: util
  44. * @return CtripUtil
  45. * @author 倪宗锋
  46. */
  47. public static function util()
  48. {
  49. if (static::$util == null) {
  50. static::$util = new self();
  51. }
  52. return static::$util;
  53. }
  54. /*****==========发送报文===========***************/
  55. /**
  56. * Des:获取初始化函数
  57. * Name: init
  58. * @param string $version
  59. * @return CtripUtil
  60. * @author 倪宗锋
  61. */
  62. public static function init($version = '2.0')
  63. {
  64. return new self($version);
  65. }
  66. /**
  67. * Des:返回值
  68. * Name: exec
  69. * @param $serviceName
  70. * @param $data
  71. * @return array
  72. * @author 倪宗锋
  73. */
  74. public function exec($serviceName, $data)
  75. {
  76. $this->serverName = $serviceName;
  77. $this->data = $data;
  78. $this->setBody();
  79. $this->setHeader();
  80. $this->request = [
  81. 'request' => array_merge(
  82. $this->header,
  83. $this->body
  84. )
  85. ];
  86. $curl = new CurlInterface($this->request, 2);
  87. $this->response = $curl->execute(\Yii::$app->params['ctrip_config']['api'], 'POST');
  88. if (isset($this->response['header']['resultCode']) && $this->response['header']['resultCode'] === '0000') {
  89. return Util::returnArrSu('', $this->response);
  90. }
  91. $msg = empty($this->response['header']['resultMessage']) ? '系统繁忙,请稍后再试!' : $this->response['header']['resultMessage'];
  92. $code = empty($this->response['header']['resultCode']) ? '0005' : $this->response['header']['resultCode'];
  93. return Util::returnArrEr($msg, [], $code);
  94. }
  95. /**
  96. * Des:设置头部
  97. * Name: setHeader
  98. * @author 倪宗锋
  99. */
  100. private function setHeader()
  101. {
  102. $this->header = [
  103. 'header' => [
  104. 'accountId' => $this->accountId,
  105. 'serviceName' => $this->serverName,
  106. 'requestTime' => $this->time,
  107. 'version' => $this->version,
  108. ]
  109. ];
  110. $this->header['header']['sign'] = $this->setSign();
  111. }
  112. /**
  113. * Des:设置body
  114. * Name: setBody
  115. * @author 倪宗锋
  116. */
  117. private function setBody()
  118. {
  119. $this->body = [
  120. 'body' => $this->data
  121. ];
  122. $this->requestBodyXml = Util::arrayToXml($this->body);
  123. }
  124. /**
  125. * Des:设置加密函数
  126. * Name: setSign
  127. * @return string
  128. * @author 倪宗锋
  129. */
  130. private function setSign()
  131. {
  132. $signString = $this->accountId;
  133. $signString .= $this->header['header']['serviceName'];
  134. $signString .= $this->header['header']['requestTime'];
  135. $signString .= base64_encode($this->requestBodyXml);
  136. $signString .= $this->version;
  137. $signString .= $this->signkey;
  138. return md5($signString);
  139. }
  140. /******************==============接收数据处理===============***********************/
  141. /**
  142. * Des:获取返回值
  143. * Name: getRequest
  144. * @return array header 头部 body 报文体
  145. * @author 倪宗锋
  146. */
  147. public function getRequest()
  148. {
  149. $data = file_get_contents("php://input");
  150. try {
  151. preg_match_all("/body>(.*)?\/body>/", $data, $bodyXml);
  152. if (empty($bodyXml[0][0]) == false) {
  153. $this->requestBodyXml = '<' . $bodyXml[0][0];
  154. }
  155. $this->request = Util::xmlToArray($data);
  156. $this->header = empty($this->request['header']) ? ['header' => []] : ['header' => $this->request['header']];
  157. $this->body = empty($this->request['body']) ? ['body' => []] : ['body' => $this->request['body']];
  158. if ($this->accountId != $this->header['header']['accountId']) {
  159. return Util::returnArrEr('账户不存在', '', '0004');
  160. }
  161. $sign = $this->setSign();
  162. if (!isset($this->header['header']['sign']) || $sign != $this->header['header']['sign']) {
  163. return Util::returnArrEr('签名校验失败', ['sign' => $sign], '0003');
  164. }
  165. return Util::returnArrSu('', $this->request);
  166. } catch (\Exception $e) {
  167. return Util::returnArrEr('xml解析失败', '', '0002');
  168. }
  169. }
  170. /**
  171. * Des:获取body参数
  172. * Name: getBody
  173. * @return array
  174. * @author 倪宗锋
  175. */
  176. public function getBody()
  177. {
  178. return $this->body['body'];
  179. }
  180. /**
  181. * Des:重置路由访问地址
  182. * Name: setAction
  183. * @param $action
  184. * @param $actionName
  185. * @author 倪宗锋
  186. */
  187. public function setAction($action, $actionName)
  188. {
  189. $action->controller->action->id = strtolower($actionName);
  190. $action->controller->action->actionMethod = 'action' . ucfirst($actionName);
  191. $action->id = strtolower($actionName);
  192. $action->actionMethod = 'action' . ucfirst($actionName);
  193. }
  194. /******************==============设置返回值===============***********************/
  195. /**
  196. * Des:返回错误信息
  197. * Name: returnErr
  198. * @param $msg
  199. * @param $code
  200. * @param $body
  201. * @return string
  202. * @author 倪宗锋
  203. */
  204. public function returnErr($msg, $code, $body = [])
  205. {
  206. $return = [
  207. 'response' => [
  208. 'header' => [
  209. 'resultCode' => $code,
  210. 'resultMessage' => $msg
  211. ]
  212. ]
  213. ];
  214. if (empty($body) == false) {
  215. $return['response']['body'] = $body;
  216. }
  217. $return = '<?xml version="1.0" encoding="UTF-8"?>' . Util::arrayToXml($return);
  218. \Yii::$app->response->format = 'xml';
  219. \Yii::$app->response->content = $return;
  220. return;
  221. }
  222. /**
  223. * Des:返回成功信息
  224. * Name: returnErr
  225. * @param $msg
  226. * @param $body
  227. * @return string
  228. * @author 倪宗锋
  229. */
  230. public function returnSu($msg = 'success', $body = [])
  231. {
  232. $return = [
  233. 'response' => [
  234. 'header' => [
  235. 'resultCode' => '0000',
  236. 'resultMessage' => $msg
  237. ]
  238. ]
  239. ];
  240. if (empty($body) == false) {
  241. $return['response']['body'] = $body;
  242. }
  243. $return = '<?xml version="1.0" encoding="UTF-8"?>' . Util::arrayToXml($return);
  244. \Yii::$app->response->format = 'xml';
  245. \Yii::$app->response->content = $return;
  246. return;
  247. }
  248. /**
  249. * Des:取消审核操作
  250. * Name: NoticeOrderCancel
  251. * @param $orderId
  252. * @param $result_type int 审核结果 2审核成功 3审核失败 4 超时
  253. * @param $remark string 审核备注
  254. * @return array
  255. * @author 倪宗锋
  256. */
  257. public static function NoticeOrderCancel($orderId, $result_type, $remark = '')
  258. {
  259. $cookies = $_COOKIE;
  260. $user_id = empty($cookies['user_id']) ? 0 : $cookies['user_id'];
  261. $orderTitle = new OrderTitle();
  262. $orderInfo = $orderTitle->getTitleInfo($orderId);
  263. if (empty($orderInfo['order_title_id'])) {
  264. return Util::returnArrEr('订单不存在');
  265. }
  266. if ($orderInfo['is_connected_order'] != 1) {//校验订单是否直连订单
  267. return Util::returnArrEr("非携程直连订单");
  268. }
  269. if (empty($orderInfo['sequence_id'])) {
  270. return Util::returnArrEr("没有申请退款流程");
  271. }
  272. $transaction = \Yii::$app->db->beginTransaction();
  273. try {
  274. # 1 设置取消通知操作记录
  275. $orderTitleCancelRequest = new OrderTitleCancelRequest();
  276. $up = [
  277. 'update_time' => date('Y-m-d H:i:s'),
  278. 'request_status' => $result_type,
  279. 'operate_user_id' => $user_id,
  280. ];
  281. $flag = $orderTitleCancelRequest->updateStatusById($up, $orderInfo['id']);
  282. if ($flag == false) {
  283. return Util::returnArrEr("操作失败![sql]");
  284. }
  285. # 2 设置订单取消
  286. if ($result_type == 2) {//审核通过才会发起取消通知
  287. $data = [
  288. 'sign' => $orderId,
  289. 'user_id' => $user_id
  290. ];
  291. $curl = new CurlInterface($data, 4);
  292. $result = $curl->execute('http://' . CS1_DOMAIN . '/api/submit-order/cancel-tourist', 'POST');
  293. if (!isset($result['code']) || $result['code'] != '0') {
  294. $transaction->rollBack();
  295. $msg = empty($result['info']) ? '取消订单失败!' : $result['info'];
  296. return Util::returnArrEr($msg, '', $result['code']);
  297. }
  298. }
  299. $transaction->commit();
  300. } catch (Exception $e) {
  301. $transaction->rollBack();
  302. return Util::returnArrEr('操作失败![sys]');
  303. }
  304. try {
  305. # 3 发送通知
  306. $data = [
  307. 'otaOrderId' => $orderInfo['outside_order_no'],
  308. 'vendorOrderId' => $orderInfo['order_title_id'],
  309. 'sequenceId' => $orderInfo['sequence_id'],
  310. 'cancelCount' => 0,
  311. 'charge' => 0,
  312. 'orderStatus' => $result_type == 2 ? 3 : 4,
  313. 'remark' => empty($remark) ? $orderInfo['remark'] : $remark
  314. ];
  315. if ($result_type == 2) {//订单取消申请审核成功
  316. $data['cancelCount'] = $orderInfo['cus_num'];//取消数等于总数
  317. }
  318. self::init()->exec('NoticeOrderCancel', $data);
  319. } catch (Exception $e) {
  320. }
  321. return Util::returnArrSu();
  322. }
  323. /**
  324. * Des:发送出游提醒
  325. * Name: NoticeOrderConsumed
  326. * @param $otaOrderId
  327. * @param $vendorOrderId
  328. * @param $prodCnt
  329. * @return array
  330. * @author 倪宗锋
  331. */
  332. public static function NoticeOrderConsumed($otaOrderId, $vendorOrderId, $prodCnt)
  333. {
  334. $data = [
  335. 'otaOrderId' => $otaOrderId,
  336. 'vendorOrderId' => $vendorOrderId,
  337. 'useDate' => date('Y-m-d'),
  338. 'amount' => $prodCnt,
  339. 'count' => $prodCnt,
  340. 'useCount' => $prodCnt,
  341. 'cancelCount' => 0,
  342. 'orderStatus' => 5
  343. ];
  344. $ctripUtil = CtripUtil::init()->exec('NoticeOrderConsumed', $data);
  345. return $ctripUtil;
  346. }
  347. }