MovieRepository.php 41 KB

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