MovieRepository.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/8
  6. */
  7. namespace app\common\repositories\movie;
  8. use app\common\dao\movie\order\MovieOrderDao;
  9. use app\common\model\movie\order\MovieOrder;
  10. use app\common\repositories\BaseRepository;
  11. use app\common\repositories\merchant\order\OrderMerchantRepository;
  12. use app\common\repositories\store\order\StoreOrderRepository;
  13. use app\traits\HuiPay;
  14. use crmeb\services\ApiResponseService;
  15. use think\exception\ValidateException;
  16. use think\facade\Db;
  17. /**
  18. * Class MovieRepository
  19. * @package app\common\repositories\movie
  20. * @author xaboy
  21. * @mixin MovieOrderDao
  22. */
  23. class MovieRepository extends BaseRepository
  24. {
  25. use HuiPay;
  26. protected $dao;
  27. const premium_ratio = 0.2;//溢价比例
  28. const yanglaojin_ratio = 0.3;
  29. const pv_ratio = 0.7;
  30. const tenMinute = 600;
  31. /**
  32. * ProductRepository constructor.
  33. * @param MovieOrderDao $dao
  34. */
  35. public function __construct(MovieOrderDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * @param $page
  41. * @param $limit
  42. * @param $initial
  43. * @param $city_name
  44. * @return false|int|mixed
  45. */
  46. public function getMovieCity($page = 1, $limit = 10, $initial = '', $city_name = '')
  47. {
  48. return DouHuoMallFilmApiRequest::getMovieCity($page, $limit, $initial = '', $city_name);
  49. }
  50. /**
  51. * @param $city_sign
  52. * @return false|int|mixed
  53. */
  54. public function getMovieArea($city_sign = '')
  55. {
  56. return DouHuoMallFilmApiRequest::getMovieArea($city_sign);
  57. }
  58. /**
  59. * @param $page
  60. * @param $limit
  61. * @param $city_sign
  62. * @param $area_sign
  63. * @return false|int|mixed
  64. */
  65. public function getCinema($page = 1, $limit = 10, $city_sign = '', $area_sign = '')
  66. {
  67. return DouHuoMallFilmApiRequest::getCinema($page, $limit, $city_sign, $area_sign);
  68. }
  69. /**
  70. * @param $page
  71. * @param $limit
  72. * @param $cinema_sign
  73. * @param $film_sign
  74. * @return false|int|mixed
  75. */
  76. public function getFilm($page = 1, $limit = 10, $cinema_sign = '', $film_sign = '')
  77. {
  78. return DouHuoMallFilmApiRequest::getFilm($page, $limit, $cinema_sign, $film_sign);
  79. }
  80. /**
  81. * @param $film_sign
  82. * @param $cinema_sign
  83. * @return mixed|string
  84. */
  85. public function getCinemaSched($film_sign = '', $cinema_sign = '')
  86. {
  87. $result = DouHuoMallFilmApiRequest::getCinemaSched($film_sign, $cinema_sign);
  88. $cinemaSchedList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  89. ? $result['data']['list'] : [];
  90. foreach ($cinemaSchedList as $dateDay => &$fileSchedValue) {
  91. foreach ($fileSchedValue as $key => $value) {
  92. $pcYanglaojinGongxian = static::getPvYanglaojinScore($value['cost_price']);
  93. $fileSchedValue[$key]['price'] = $pcYanglaojinGongxian['cost_price']; // 销售价
  94. $fileSchedValue[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  95. $fileSchedValue[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  96. $fileSchedValue[$key]['score'] = $pcYanglaojinGongxian['score'];
  97. $fileSchedValue[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  98. // unset($fileSchedValue[$key]['channel_price']);
  99. }
  100. }
  101. return $cinemaSchedList;
  102. }
  103. /**
  104. * @param $page
  105. * @param $limit
  106. * @param $film_sign
  107. * @param $longitude
  108. * @param $latitude
  109. * @param $city_sign
  110. * @return mixed|string
  111. */
  112. public function getFilmSched($page = 1, $limit = 10, $film_sign = '', $longitude = '', $latitude = '', $city_sign = '')
  113. {
  114. return DouHuoMallFilmApiRequest::getFilmSched($page, $limit, $film_sign, $longitude, $latitude, $city_sign);
  115. }
  116. /**
  117. * @param $relation_id
  118. * @param $sched_sign
  119. * @return mixed|string
  120. */
  121. public function getSchedSeat($relation_id = '', $sched_sign = '')
  122. {
  123. $result = DouHuoMallFilmApiRequest::getSchedSeat($relation_id, $sched_sign);
  124. $sched_seatList = [];
  125. // 根据场次id、场次编号获取锁座订单
  126. $seatOrder = $this->dao->search(['relation_id' => $relation_id, 'sched_sign' => $sched_sign, 'status' => [0, 1]])->field('relation_id,sched_sign,sched_seat')->select()->toArray();
  127. if (!empty($seatOrder)) {
  128. $tmpsched_seat = array_column($seatOrder, 'sched_seat');
  129. foreach ($tmpsched_seat as $ssKey => $ssValue) {
  130. $ssValue = json_decode($ssValue, true);
  131. foreach ($ssValue as $k => $v) {
  132. $sched_seatList[$v['seat_sign']] = $v['seat_sign'];
  133. }
  134. }
  135. }
  136. $seat = [];
  137. $schedSeatList = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) ? $result['data'] : [];
  138. foreach ($schedSeatList as $key => $value) {
  139. $tmpprice = [];
  140. foreach ($value['price'] as $pKey => $pValue) {
  141. $tmpprice[$pKey] = ['channel_type' => $pKey, 'price' => $pValue];
  142. }
  143. $channel_price = [];
  144. if (isset($tmpprice['P2'])) {
  145. $channel_price = $tmpprice['P2'];
  146. } elseif (isset($tmpprice['P1'])) {
  147. $channel_price = $tmpprice['P1'];
  148. } elseif (isset($tmpprice['P0'])) {
  149. $channel_price = $tmpprice['P0'];
  150. }
  151. unset($schedSeatList[$key]['price']);
  152. $pcYanglaojinGongxian = self::getPvYanglaojinScore($channel_price['price']);
  153. $schedSeatList[$key]['channel_sn'] = base64_encode(json_encode($channel_price, JSON_FORCE_OBJECT));
  154. $schedSeatList[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  155. $schedSeatList[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  156. $schedSeatList[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  157. $schedSeatList[$key]['score'] = $pcYanglaojinGongxian['score'];
  158. $schedSeatList[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  159. $schedSeatList[$key]['relation_id'] = $relation_id; // 场次ID
  160. $schedSeatList[$key]['sched_sign'] = $sched_sign; // 场次编码
  161. if ($value['status'] == 1) {
  162. $schedSeatList[$key]['status'] = isset($sched_seatList[$value['seatSign']]) ? 0 : 1;
  163. }
  164. $seat[$value['rowNo']][] = $schedSeatList[$key];
  165. }
  166. if ($seat) ksort($seat);
  167. return array_values($seat);
  168. }
  169. /**
  170. * 锁座订单
  171. *
  172. * @param $userinfo
  173. * @param $params
  174. * @return mixed
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\DbException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. */
  179. public function submitOrder($userinfo, $params)
  180. {
  181. /** @var StoreOrderRepository $storeOrder */
  182. $storeOrder = app()->make(StoreOrderRepository::class);
  183. $order_sn = substr($storeOrder->getNewOrderId(), 0, 20);
  184. $channel_sn = json_decode(base64_decode($params['channel_sn']), true);
  185. if (empty($channel_sn) || !isset($channel_sn['channel_type']) || empty($channel_sn['channel_type']) || !isset($channel_sn['price'])
  186. || empty($channel_sn['price']) || json_last_error()) {
  187. throw new ValidateException('请选择观影场次');
  188. }
  189. $sched_seat = json_decode($params['sched_seat'], true);
  190. if (json_last_error()) throw new ValidateException('锁座下单失败');
  191. $order_price = bcmul((string)$channel_sn['price'], (string)count($sched_seat), 2);
  192. Db::name('log')->insert(['data' => '---电影票支付订单数据---' . json_encode([
  193. $params['phone'] ?? $userinfo->phone, $channel_sn['channel_type'], $params['sched_sign'], array_column($sched_seat, 'seat_sign'), $order_sn, $order_price
  194. ])]);
  195. $submitOrder = DouHuoMallFilmApiRequest::submitOrder($params['phone'] ?? $userinfo->phone, $channel_sn['channel_type'], $params['sched_sign'], array_column($sched_seat, 'seat_sign'), $order_sn, $order_price);
  196. if (!empty($submitOrder) && isset($submitOrder['code']) && ($submitOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE) && isset($submitOrder['data']['plat_order'])) {
  197. /** @var CinemaRepository $cinemaRepository */
  198. $cinemaRepository = app()->make(CinemaRepository::class);
  199. $cinema = $cinemaRepository->getCinemaInfo($params['cinema_sign']);
  200. /** @var FilmRepository $filmRepository */
  201. $filmRepository = app()->make(FilmRepository::class);
  202. $filminfo = $filmRepository->getFilmInfo($params['film_sign']);
  203. $_order = [
  204. 'uid' => $userinfo->uid,
  205. 'mobile' => $params['phone'] ?? $userinfo->phone,
  206. 'phone' => $params['phone'] ?? $userinfo->phone,
  207. 'order_sn' => $order_sn,
  208. 'plat_order' => $submitOrder['data']['plat_order'],
  209. 'price' => $order_price,
  210. 'number' => count($sched_seat),
  211. 'order_price' => bcadd((string)$order_price, bcmul((string)$order_price, (string)self::premium_ratio, 2), 2),
  212. 'channel_type' => $channel_sn['channel_type'],
  213. 'city_sign' => $cinema['city_sign'],
  214. 'city_name' => $cinema['city_name'],
  215. 'area_code' => $cinema['area_sign'],
  216. 'area_name' => $cinema['area_name'],
  217. 'cinema_sign' => $cinema['cinema_sign'],
  218. 'cinema_name' => $cinema['cinema_name'],
  219. 'address' => $cinema['address'],
  220. 'longitude' => $cinema['longitude'],
  221. 'latitude' => $cinema['latitude'],
  222. 'hall_sign' => $params['hall_sign'],
  223. 'hall_name' => $params['hall_name'],
  224. 'relation_id' => $params['relation_id'],
  225. 'sched_sign' => $params['sched_sign'],
  226. 'film_sign' => $filminfo['film_sign'],
  227. 'film_name' => $filminfo['film_name'],
  228. 'show_date' => $params['show_date'],
  229. 'show_time' => $params['show_time'],
  230. 'sched_seat' => $params['sched_seat'], // 场次座位
  231. 'create_time' => time()
  232. ];
  233. $order_id = Db::transaction(function () use ($userinfo, $_order) {
  234. $orderModel = $this->dao->create($_order);
  235. if ($orderModel) return $orderModel['order_id'];
  236. throw new ValidateException('锁座下单失败');
  237. });
  238. return compact('order_id', 'order_sn');
  239. } else {
  240. throw new ValidateException($submitOrder['msg']);
  241. }
  242. }
  243. /**
  244. * @param $userinfo
  245. * @param $order_sn
  246. * @param $pay_type
  247. * @return mixed
  248. * @throws \think\db\exception\DataNotFoundException
  249. * @throws \think\db\exception\DbException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. */
  252. public function confirmOrder($userinfo, $order_id, $pay_type, $code)
  253. {
  254. $orderinfo = $this->dao->search(['order_id' => $order_id])->find();
  255. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  256. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 2) throw new ValidateException('订单已超时');
  257. if ($orderinfo['paid'] == 0 && $orderinfo['status'] == 3) throw new ValidateException('订单已取消');
  258. if ($orderinfo['paid'] == 1) throw new ValidateException('订单已支付');
  259. $res = $this->movie_pay(strtolower($pay_type), $code, '', $orderinfo, $orderinfo['film_name'], env('APP_URL') . "/api/hf_notify/movie", 'delay');
  260. Db::name('log')->insert(['data' => '汇付电影票订单支付--- 返回参数' . json_encode($res)]);
  261. if (isset($res['id'])) {
  262. $type = 0;
  263. if ($pay_type == 'wx') $type = 2;
  264. if (strtolower($pay_type) == 'ali') $type = 1;
  265. $orderinfo->pay_type = $type;
  266. $orderinfo->update_time = time();
  267. $orderinfo->save();
  268. return $res;
  269. }
  270. throw new ValidateException('支付失败');
  271. }
  272. /**
  273. * 支付
  274. *
  275. * @param $payType
  276. * @param $openid
  277. * @param $_order
  278. * @param $title
  279. * @param $notify_url
  280. * @param $pay_mode
  281. * @param $appid
  282. * @return array|mixed
  283. */
  284. private function pay($payType, $openid, $_order, $title, $notify_url, $pay_mode = null, $appid = '')
  285. {
  286. switch ($payType) {
  287. case 'wx':
  288. $pay_wx = systemConfig('pay_wx');
  289. if ($pay_wx == 2) {
  290. $appid = env('WECHAT_MP.APPID');
  291. $new_secret = env('WECHAT_MP.SECRET');
  292. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  293. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  294. } elseif ($pay_wx == 3) {
  295. $appid = env('WECHAT_TDYL.APPID');
  296. $new_secret = env('WECHAT_TDYL.SECRET');
  297. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  298. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  299. }
  300. $type = '2';
  301. $params = [
  302. 'order_no' => $_order['order_sn'],
  303. 'app_id' => $app_id,
  304. 'pay_channel' => 'wx_lite',
  305. 'pay_amt' => (string)$_order['order_price'],
  306. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  307. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  308. 'device_info' => [
  309. 'device_ip' => app('request')->ip()
  310. ],
  311. 'expend' => json_encode(['open_id' => $openid, 'wx_app_id' => $appid]),
  312. // 'fee_mode' => 'I',
  313. 'notify_url' => $notify_url,
  314. 'pay_mode' => $pay_mode
  315. ];
  316. break;
  317. case 'ALI':
  318. case 'ali':
  319. $type = '1';
  320. // 天地博爱科技通道
  321. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  322. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  323. $params = [
  324. 'order_no' => $_order['order_sn'],
  325. 'app_id' => $app_id,
  326. 'pay_channel' => 'alipay_qr',
  327. 'pay_amt' => (string)$_order['order_price'],
  328. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  329. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  330. 'device_info' => [
  331. 'device_ip' => app('request')->ip()
  332. ],
  333. // 'fee_mode' => 'I',
  334. 'notify_url' => $notify_url
  335. ];
  336. break;
  337. default:
  338. throw new ValidateException('请选择支付方式');
  339. break;
  340. }
  341. return $this->Payment_create_traits_a($params, $type, $key);
  342. }
  343. /**
  344. * 电影票支付
  345. *
  346. * @param $payType
  347. * @param $code
  348. * @param $wechat_user_id
  349. * @param $orderinfo
  350. * @param $title
  351. * @param $notify_url
  352. * @param $pay_mode
  353. * @param $appid
  354. * @return array|mixed|void
  355. */
  356. private function movie_pay($payType, $code, $wechat_user_id, $orderinfo, $title, $notify_url, $pay_mode = null, $appid = '')
  357. {
  358. if ($payType == 'wx') {
  359. if (!$code || $code == '' || $code == 'undefined') {
  360. throw new ValidateException('授权失败,code为空');
  361. }
  362. if ($appid == '') {
  363. $pay_wx = systemConfig('pay_wx');
  364. if ($pay_wx == 2) {
  365. $appid = env('WECHAT_MP.APPID');
  366. $new_secret = env('WECHAT_MP.SECRET');
  367. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  368. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  369. } elseif ($pay_wx == 3) {
  370. $appid = env('WECHAT_TDYL.APPID');
  371. $new_secret = env('WECHAT_TDYL.SECRET');
  372. Db::name('log')->insert(['data' => 'appid---' . $appid]);
  373. Db::name('log')->insert(['data' => 'new_secret---' . $new_secret]);
  374. $app_id = 'app_383a75dd-c03d-44ab-8f58-92c60fe9b9d0';
  375. $key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
  376. }
  377. } else {
  378. $app_id = 'app_0827b960-b932-45ec-b800-4e850091b419';
  379. $key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  380. $new_secret = "c8cbe24fa3b83f77a88b79554b3aeab4";
  381. }
  382. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $new_secret . "&js_code=" . $code . "&grant_type=authorization_code";
  383. $get = curlGet($url);
  384. Db::name('log')->insert(['data' => 'open_id---' . json_encode($get)]);
  385. if (!isset($get['openid']) || empty($get['openid'])) {
  386. throw new ValidateException('授权失败');
  387. }
  388. $member_id = 0;
  389. $type = '2';
  390. $pay_channel = 'wx_lite';
  391. $expend = ['open_id' => $get['openid'], 'wx_app_id' => $appid];
  392. if (false) {
  393. // if ($orderinfo['order_type'] = "movie") {
  394. $cfb = sprintf("%01.2f", round(bcmul($orderinfo['order_price'], '0.094', 5), 2));//9.4%的分账
  395. $cfb_member = "jsdk_member_1680162243019";
  396. // 平台
  397. $cxb = sprintf("%01.2f", round(bcsub($orderinfo['order_price'], $cfb, 5), 2));//剩下的归平台
  398. $div_members = [
  399. // [
  400. // 'member_id' => $member_id,
  401. // 'amount' => $cxb,
  402. // 'fee_flag' => 'Y'
  403. // ],
  404. // [
  405. // 'member_id' => $cfb_member,
  406. // 'amount' => $cfb,
  407. // 'fee_flag' => 'N'
  408. // ]
  409. ];
  410. } else {
  411. $div_members = [
  412. [
  413. 'member_id' => $member_id,
  414. 'amount' => $orderinfo['order_price'],
  415. 'fee_flag' => 'Y'
  416. ]
  417. ];
  418. }
  419. $params = [
  420. 'order_no' => $orderinfo['order_sn'],
  421. 'app_id' => $app_id,
  422. 'pay_channel' => $pay_channel,
  423. 'pay_amt' => (string)$orderinfo['order_price'],
  424. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  425. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  426. 'device_info' => [
  427. 'device_ip' => app('request')->ip()
  428. ],
  429. 'expend' => json_encode($expend),
  430. 'div_members' => json_encode($div_members),
  431. 'fee_mode' => 'I',
  432. 'notify_url' => $notify_url
  433. ];
  434. if ($pay_mode == 'delay') {
  435. $params['pay_mode'] = 'delay';
  436. unset($params['div_members']);
  437. unset($params['fee_mode']);
  438. }
  439. return $this->Payment_create_traits_b($params, $type, $key);
  440. } elseif ($payType == 'ali') {
  441. $member_id = 0;
  442. $type = '1';
  443. $pay_channel = 'alipay_qr';
  444. // 天地博爱科技通道
  445. $app_id = 'app_44108546-d27a-48ee-88c1-84b45b75114f';
  446. $key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
  447. // 天地博爱通道
  448. // $app_id='app_0827b960-b932-45ec-b800-4e850091b419';
  449. // $key='api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
  450. $div_members = [
  451. [
  452. 'member_id' => $member_id,
  453. 'amount' => $orderinfo['order_price'],
  454. 'fee_flag' => 'Y'
  455. ]
  456. ];
  457. $params = [
  458. 'order_no' => $orderinfo['order_sn'],
  459. 'app_id' => $app_id,
  460. 'pay_channel' => $pay_channel,
  461. 'pay_amt' => (string)$orderinfo['order_price'],
  462. 'goods_title' => strlen($title) > 64 ? mb_substr($title, 0, 64, 'utf-8') : $title,// 解决因为太长不能支付问题
  463. 'goods_desc' => strlen($title) > 42 ? mb_substr($title, 0, 127, 'utf-8') : $title,// 解决因为太长不能支付问题
  464. 'device_info' => [
  465. 'device_ip' => app('request')->ip()
  466. ],
  467. 'div_members' => json_encode($div_members),
  468. 'fee_mode' => 'I',
  469. 'notify_url' => $notify_url
  470. ];
  471. if ($pay_mode == 'delay') {
  472. $params['pay_mode'] = 'delay';
  473. unset($params['div_members']);
  474. unset($params['fee_mode']);
  475. }
  476. return $this->Payment_create_traits_a($params, $type, $key);
  477. }
  478. }
  479. /**
  480. * @param $userinfo
  481. * @param $order_sn
  482. * @return mixed|string
  483. */
  484. public function getOrderInfo($userinfo, $order_sn)
  485. {
  486. $orderinfo = $this->dao->search(['order_sn' => $order_sn, 'uid' => $userinfo['uid']])->find();
  487. if (empty($orderinfo)) throw new ValidateException('订单不存在');
  488. $time = time();
  489. $result = DouHuoMallFilmApiRequest::getOrderInfo($order_sn);
  490. if (!empty($result) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  491. $plat_orderinfo = $result['data'];
  492. $plat_orderinfo['oid'] = $orderinfo['order_id'];
  493. $plat_orderinfo['type'] = ($orderinfo['pay_type'] == 1) ? 'wx' : ($orderinfo['pay_type'] == 2 ? 'ali' : 'wx');
  494. $plat_orderinfo['order_price'] = $orderinfo['order_price'];
  495. $plat_orderinfo['order_sn'] = $order_sn;
  496. $plat_orderinfo['create_time'] = strtotime($orderinfo['create_time']);// 下单时间戳
  497. $plat_orderinfo['phone'] = $orderinfo['mobile'];
  498. $plat_orderinfo['sched_seat'] = json_decode($orderinfo['sched_seat'], true);
  499. $plat_orderinfo['cinema'] = [
  500. 'cinema_sign' => $orderinfo['cinema_sign'],
  501. 'cinema_name' => $orderinfo['cinema_name'],
  502. 'address' => $orderinfo['address'],
  503. 'longitude' => $orderinfo['longitude'],
  504. 'latitude' => $orderinfo['latitude'],
  505. 'hall_sign' => $orderinfo['hall_sign'],
  506. 'hall_name' => $orderinfo['hall_name'],
  507. 'tel' => Db::name('movie_cinema')->where('cinema_sign', $orderinfo['cinema_sign'])->value('tel')
  508. ];
  509. $film = Db::name('movie_film')->where('film_sign', $orderinfo['film_sign'])->find();
  510. $plat_orderinfo['film'] = [
  511. 'film_sign' => $orderinfo['film_sign'],
  512. 'film_name' => $orderinfo['film_name'],
  513. 'language' => $film['language'],
  514. 'cover_img' => $film['cover_img'],
  515. 'version' => $film['version'],
  516. ];
  517. $plat_orderinfo['sched_time'] = date('Y-m-d H:i', strtotime($result['data']['sched_time']));
  518. $plat_orderinfo['schedend_time'] = date('m-d H:i', strtotime($result['data']['sched_time']) + $film['duration'] * 60);
  519. // 订单状态 0:待支付;1:已出票;3:已取消
  520. $statuspayinfo = $this->getOrderStatus($plat_orderinfo['create_time'], $plat_orderinfo['status']);
  521. $plat_orderinfo['status'] = $statuspayinfo['status'];
  522. $plat_orderinfo['state'] = $statuspayinfo['state'];
  523. $plat_orderinfo['time_remaining'] = $statuspayinfo['time_remaining'];// 支付倒计时
  524. unset($plat_orderinfo['plat_order']);
  525. unset($plat_orderinfo['third_order']);
  526. return $plat_orderinfo;
  527. }
  528. throw new ValidateException('订单不存在');
  529. }
  530. /**
  531. * 获取订单状态
  532. *
  533. * @param $create_time
  534. * @param $plat_status
  535. * @return array
  536. */
  537. private function getOrderStatus($create_time, $plat_status = 0)
  538. {
  539. $time_remaining = 0;
  540. $time = time();
  541. // 订单状态 0:待支付;1:已出票;3:已取消
  542. $create_time = $create_time + self::tenMinute;
  543. if (($plat_status == 0) || ($plat_status == 1)) {
  544. if ($create_time < $time) {
  545. $status = 3;
  546. $state = '支付超时,已取消';
  547. } else {
  548. $status = 0;
  549. $state = '待付款';
  550. $time_remaining = $time - $create_time;
  551. }
  552. } elseif ($plat_status == 5) {
  553. $status = 3;
  554. $state = '支付超时,已取消';
  555. } elseif ($plat_status == 6) {
  556. $status = 1;
  557. $state = '已出票';
  558. } else {
  559. $status = 3;
  560. $state = '支付超时,已取消';
  561. }
  562. return compact('status', 'state', 'time_remaining');
  563. }
  564. /**
  565. * @return array
  566. * @throws \think\db\exception\DataNotFoundException
  567. * @throws \think\db\exception\DbException
  568. * @throws \think\db\exception\ModelNotFoundException
  569. */
  570. public function getLocalMovieCity()
  571. {
  572. $city_name = [];
  573. $city = Db::name('movie_city')->field('city_sign,city_name,initial')->where('is_show', 1)->order('id asc')->select();
  574. foreach ($city as $key => $value) {
  575. $city_name[$value['initial']][] = ['city_sign' => $value['city_sign'], 'city_name' => $value['city_name']];
  576. }
  577. $initial = !empty($city) ? array_values(array_column($city->toArray(), 'initial', 'initial')) : [];
  578. return compact('city_name', 'initial');
  579. }
  580. /**
  581. * 获取pv、养老金、售价、积分
  582. *
  583. * @param $cost_price
  584. * @return array
  585. */
  586. public static function getPvYanglaojinScore($cost_price)
  587. {
  588. $premium = round($cost_price * self::premium_ratio, 2);
  589. $cost_price = bcadd((string)$cost_price, (string)$premium, 2);
  590. $pv = $gongxian = $score = bcmul((string)$premium, self::pv_ratio, 2);
  591. $yanglaojin = bcmul((string)$premium, self::yanglaojin_ratio, 2);
  592. return compact('premium', 'cost_price', 'pv', 'yanglaojin', 'gongxian', 'score');
  593. }
  594. /**
  595. * 计算佣金分佣奖励
  596. *
  597. * @param $pay_type
  598. * @param $order_sn
  599. * @param $post_data
  600. * @return void
  601. * @throws \think\db\exception\DataNotFoundException
  602. * @throws \think\db\exception\DbException
  603. * @throws \think\db\exception\ModelNotFoundException
  604. */
  605. public function updateMovieOrderAndcalculationCommissionDividendAward($pay_type, $order_sn, $post_data)
  606. {
  607. $orderinfo = $this->dao->search(['order_sn' => $order_sn])->find();
  608. if ($orderinfo) throw new ValidateException('订单不存在');
  609. Db::startTrans();
  610. $orderinfo->paid = 1;
  611. $orderinfo->pay_time = time();
  612. $orderinfo->pay_type = $pay_type;
  613. $orderinfo->status = 1;
  614. $orderinfo->channel_trade_no = $post_data['id'];
  615. $orderinfo->pay_open_id = $post_data['expend']['open_id'];
  616. $orderinfo->update_time = time();
  617. // 支付方式 1支付宝 2微信
  618. if ($pay_type == 1) {
  619. $orderinfo->alipay_no = $post_data['out_trans_id'];
  620. }
  621. if ($pay_type == 2) {
  622. $orderinfo->weixin_no = $post_data['out_trans_id'];
  623. }
  624. $res = $orderinfo->save();
  625. if ($res) {
  626. $confirmOrder = DouHuoMallFilmApiRequest::confirmOrder($orderinfo['order_sn'], $orderinfo['price']);
  627. if (!empty($confirmOrder) && isset($confirmOrder['code']) && ($confirmOrder['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE)) {
  628. // 计算佣金、分红奖励
  629. $this->calculationCommissionDividendAward($orderinfo);
  630. // 提交事务
  631. Db::commit();
  632. } else {
  633. Db::name('log')->insert(['data' => '电影票订单支付回调向【微唯宝】--- 告知电影订单已支付失败,订单编号:' . $order_sn]);
  634. // 回滚事务
  635. Db::rollback();
  636. throw new ValidateException('向微维宝确认订单失败');
  637. }
  638. } else {
  639. Db::name('log')->insert(['data' => '汇付电影票订单支付回调--- 更新电影订单表失败,订单编号:' . $order_sn]);
  640. // 回滚事务
  641. Db::rollback();
  642. throw new ValidateException('更新订单失败');
  643. }
  644. }
  645. /**
  646. * 计算佣金、分红奖励
  647. * @param $orderinfo
  648. * @return void
  649. * @throws \think\db\exception\DataNotFoundException
  650. * @throws \think\db\exception\DbException
  651. * @throws \think\db\exception\ModelNotFoundException
  652. */
  653. private function calculationCommissionDividendAward($orderinfo)
  654. {
  655. /** @var StoreOrderRepository $storeOrderRepository */
  656. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  657. // 电影分佣比例配置
  658. $movie_commission_rate_config = Db::name('system_config_value')->where('config_key', 'movie_commission_rate_config')->find();
  659. // 让利金额
  660. $concession_pri = bcmul((string)$orderinfo['price'], $movie_commission_rate_config['premium_rate'], 2);
  661. // 养老金
  662. $ylj_pri = bcmul($concession_pri, $movie_commission_rate_config['consumer_yanglaojin_rate'], 2);
  663. // 业绩PV
  664. $pv = bcmul($concession_pri, $movie_commission_rate_config['pv_rate'], 2);
  665. //-------------------消费者收益---------------------
  666. // PV值
  667. $storeOrderRepository->con_log_pv($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送PV值");
  668. // 养老金
  669. $this->distributionAnnuityEarnings($orderinfo['uid'], $ylj_pri, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '购买电影票获得养老金' . $ylj_pri, 0, 0);
  670. // 消费者 - 红包 - 老用户发红包奖励
  671. $user = Db::name("user")->where('uid', $orderinfo["uid"])->find();
  672. if ($user['is_old'] == 1) {
  673. $storeOrderRepository->giveHongBao($orderinfo["uid"], $pv, $orderinfo['order_id'], '购买电影票赠送红包');
  674. }
  675. // 积分
  676. $storeOrderRepository->score_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送积分");
  677. // 贡献值
  678. $storeOrderRepository->con_log($orderinfo['uid'], $pv, $orderinfo['order_id'], 13, "购买电影票赠送贡献值");
  679. //-------------------消费者收益---------------------
  680. // 推广者
  681. if ($user['spread_uid'] != 0) {
  682. //查询{下单用户}的上级
  683. $spread_data = Db::name('user')->where('uid', $user['spread_uid'])->find();
  684. $rate = $storeOrderRepository->getUserGroupRate($spread_data['user_group']);
  685. //养老金
  686. $spread_yanglaojin = bcmul($pv, $movie_commission_rate_config['spread_yanglaojin_rate'], 2);
  687. $this->distributionAnnuityEarnings(
  688. $spread_data['uid'], $spread_yanglaojin, $orderinfo['order_id'], 5, $orderinfo['order_sn'], '电影票-推广者获得养老金' . $spread_yanglaojin, 0, 0
  689. );
  690. // 直推佣金
  691. $yj_amount_90 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['commission_directthrust_rate'], 2);
  692. $this->distributionAnnuityEarnings(
  693. $spread_data['uid'], $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-推广者获得直推佣金' . $yj_amount_90,
  694. 0, 0
  695. );
  696. // 复购金-推广者的佣金抽10%到个人复购金
  697. $fgj_amount_10 = round(bcmul($pv, $rate, 2) * $movie_commission_rate_config['repurchase_directthrust_rate'], 2);
  698. $storeOrderRepository->fugou_user_log($spread_data['uid'], $fgj_amount_10, $orderinfo['order_id'], 1);
  699. // 积分
  700. $score = bcmul($pv, $rate, 2);
  701. $storeOrderRepository->score_log($spread_data['uid'], $score, $orderinfo['order_id'], 13, "电影票-推广者获得赠送积分");
  702. // 业务员及以上身份
  703. if ($spread_data['user_group'] > 1) {
  704. // 贡献值
  705. $storeOrderRepository->con_log($spread_data['uid'], $pv, $orderinfo['order_id'], 13, "电影票-推广者获得赠送贡献值");
  706. }
  707. // 普通用户推广人
  708. if ($spread_data['user_group'] == 1) {
  709. // 贡献值
  710. $gongxianValue = bcmul($pv, $movie_commission_rate_config['contributionvalue_spread_user_rate'], 2);
  711. $storeOrderRepository->con_log($spread_data['uid'], $gongxianValue, $orderinfo['order_id'], 13, "电影票-推广者用户身份获得赠送贡献值");
  712. }
  713. }
  714. // 锁客收益
  715. if ($user['mer_id'] != 0) {
  716. $merchant_uid = Db::name('merchant')->where('mer_id', $user['mer_id'])->value('uid');
  717. $merchant_user = Db::name('user')->where('uid', $merchant_uid)->find();
  718. if (!empty($merchant_user)) {
  719. // 锁客佣金
  720. $yj_amount_90 = round(bcmul($pv, $movie_commission_rate_config['commission_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['commission_lockpassenger_rate'], 2);
  721. $this->distributionAnnuityEarnings(
  722. $merchant_uid, $yj_amount_90, $orderinfo['order_id'], 3, $orderinfo['order_sn'], '电影票-商户锁定用户获得佣金' . $yj_amount_90,
  723. $user['mer_id'], 0
  724. );
  725. // 锁客复购金
  726. $fgj_amount_10 = round(bcmul($pv, $movie_commission_rate_config['repurchase_lockpassenger_merid_rate'], 2) * $movie_commission_rate_config['repurchase_lockpassenger_rate'], 2);
  727. $storeOrderRepository->fugou_user_log($merchant_uid, $fgj_amount_10, $orderinfo['order_id'], 1);
  728. }
  729. }
  730. }
  731. /**
  732. * 直推奖、养老金
  733. *
  734. * @param $uid
  735. * @param $num
  736. * @param $order_id
  737. * @param $comm_order_type
  738. * @param $order_sn
  739. * @param $mark
  740. * @param $merid
  741. * @param $ds
  742. * @return void
  743. */
  744. private function distributionAnnuityEarnings($uid, $num, $order_id, $comm_order_type, $order_sn, $mark, $merid, $ds = 0)
  745. {
  746. $name = '';
  747. if ($comm_order_type == 3) {
  748. $name = '直推奖';
  749. } else if ($comm_order_type == 5) {
  750. $name = '养老金';
  751. }
  752. $bill = [
  753. 'uid' => $uid,
  754. 'link_id' => $order_id,//关联订单id
  755. 'pm' => 1,//0:支出,1:获得
  756. 'title' => $name,//账单标题
  757. 'category' => 'now_money',//明细种类
  758. 'type' => 'commission',//明细类型
  759. 'number' => $num,//明细数字
  760. 'balance' => 0,//剩余
  761. 'mark' => $mark,//备注
  762. 'create_time' => date('Y-m-d H:i:s'),//添加时间
  763. 'status' => $ds,//0待确定,1有效,-1无效
  764. 'commission_type' => $comm_order_type,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费
  765. //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金
  766. //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’
  767. 'order_sn' => $order_sn,//订单号
  768. 'tripartite' => 0,//
  769. 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上
  770. 'mer_id' => $merid,//商户id
  771. 'source' => 0,//1线下 0线上
  772. 'order_type' => 1,//1自营 2 第三方 订单类型
  773. 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金
  774. 'gzc' => 0,//养老金是否已进入公证处log表0:未进入。1:已进入
  775. 'take_time' => '',//结算时间
  776. 'district_id' => '',//
  777. 'street_id' => '',//
  778. 'month' => '',//月份
  779. ];
  780. Db::name('user_bill')->insert($bill);
  781. }
  782. /**
  783. * 订单列表
  784. *
  785. * @param $userinfo
  786. * @param $page
  787. * @param $limit
  788. * @param $type
  789. * @return array
  790. * @throws \think\db\exception\DataNotFoundException
  791. * @throws \think\db\exception\DbException
  792. * @throws \think\db\exception\ModelNotFoundException
  793. */
  794. public function getOrderList($userinfo, $page, $limit, $type = -1)
  795. {
  796. $where['uid'] = $userinfo['uid'];
  797. switch ($type) {
  798. case 0:
  799. $where['status'] = [0];
  800. break;
  801. case 1:
  802. case 4:
  803. $where['status'] = [1, 4];
  804. break;
  805. case 2:
  806. case 3:
  807. $where['status'] = [2, 3];
  808. break;
  809. default:
  810. break;
  811. }
  812. $film = [];
  813. $field = 'order_sn,order_price,city_name,area_name,cinema_name,address,hall_name,film_sign,film_name,show_date,show_time,sched_seat,number,status,create_time';
  814. $movie_order = $this->dao->search($where)
  815. ->when($page && $limit, function ($query) use ($page, $limit) {
  816. $query->page($page, $limit);
  817. })->order('order_id desc')->field([$field])->select()->toArray();
  818. foreach ($movie_order as $key => $value) {
  819. if (!isset($film[$value['film_sign']])) $film[$value['film_sign']] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  820. $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  821. $movie_order[$key]['cover_img'] = $film[$value['film_sign']];
  822. $movie_order[$key]['time_remaining'] = 0;
  823. // 订单状态 0:待支付;1:已出票;3:已取消
  824. $statuspayinfo = $this->getOrderStatus(strtotime($value['create_time']), $value['status']);
  825. $movie_order[$key]['status'] = $statuspayinfo['status'];
  826. $movie_order[$key]['state'] = $statuspayinfo['state'];
  827. $movie_order[$key]['time_remaining'] = $statuspayinfo['time_remaining'];// 支付倒计时
  828. }
  829. $count = $this->dao->search($where)->count();
  830. return compact('movie_order', 'count');
  831. }
  832. /**
  833. * 所有未支付订单列表
  834. *
  835. * @param $status
  836. * @param $paid
  837. * @return array
  838. * @throws \think\db\exception\DataNotFoundException
  839. * @throws \think\db\exception\DbException
  840. * @throws \think\db\exception\ModelNotFoundException
  841. */
  842. public function getOrderListAll($status = [], $paid = 0)
  843. {
  844. $where = ['status' => $status, 'paid' => $paid];
  845. $field = 'order_id,order_price,city_name,area_name,cinema_name,address,hall_name,film_sign,film_name,show_date,show_time,sched_seat,number,status,create_time';
  846. // foreach ($movie_order as $key => $value) {
  847. // $movie_order[$key]['sched_seat'] = json_decode($value['sched_seat'], true);
  848. // $movie_order[$key]['cover_img'] = Db::name('movie_film')->where('film_sign', $value['film_sign'])->value('cover_img');
  849. // }
  850. return $this->dao->search($where)->order('create_time asc')->field([$field])->select()->toArray();
  851. }
  852. }