PaymentBusiness.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace app\common\business;
  3. use app\common\model\BillModel;
  4. use app\common\model\HotelCanteenOrderModel;
  5. use app\common\model\HotelOrderModel;
  6. use app\common\model\OfflineShopOrderModel;
  7. use app\common\model\TravelOrderModel;
  8. use app\common\model\UniversityEventApplyModel;
  9. use app\utils\Common;
  10. use think\Db;
  11. class PaymentBusiness extends BusinessResult
  12. {
  13. /**
  14. * 注释
  15. */
  16. public const ORDER_TYPE = [
  17. 'hotel_order' => '酒店订单',
  18. 'hotel_canteen_order' => '餐厅订单',
  19. 'university_event_apply' => '活动订单',
  20. 'offline_shop_order' => '线下订单',
  21. 'travel_order' => '旅游订单',
  22. ];
  23. /**
  24. * 主表订单对应各自店铺表
  25. */
  26. public const ORDER_SHOP = [
  27. 'hotel_order' => 'hotel',
  28. 'hotel_canteen_order' => 'hotel_canteen',
  29. 'university_event_apply' => 'university_event',
  30. 'offline_shop_order' => 'offline_shop',
  31. 'travel_order' => 'travel'
  32. ];
  33. protected int $userId = 0;
  34. protected string $orderNo = '';
  35. protected string $tableName = '';
  36. public function __construct() {}
  37. /**
  38. * 获取门店信息(根绝tableName)
  39. * @param string $tableName
  40. * @param int $shopId
  41. * @return array|bool|\PDOStatement|string|\think\Model|null
  42. */
  43. public function getShopInfo(string $tableName,int $shopId)
  44. {
  45. $tableName = self::ORDER_SHOP[$tableName];
  46. return Db::name($tableName)->where(['id' => $shopId])->find();
  47. }
  48. /**
  49. * 订单成交
  50. * @param int $bill_id
  51. * @param string $third_no
  52. * @return bool
  53. * @throws \think\Exception
  54. * @throws \think\exception\PDOException
  55. */
  56. public function deal(string $bill_order_no, string $third_no = '')
  57. {
  58. $model = new BillModel();
  59. $bill = $model->getDetail(['order_no' => $bill_order_no]);
  60. if (!$bill || $bill['status'] !== 0) {
  61. return $this->error('订单已支付');
  62. }
  63. $update = [
  64. 'status' => 1,
  65. 'third_no' => $third_no,
  66. 'pay_time' => time(),
  67. ];
  68. switch ($bill['table_name']) {
  69. case 'hotel_order':
  70. case 'hotel_canteen_order':
  71. case 'university_event_apply':
  72. case 'travel_order':
  73. break;
  74. case 'offline_shop_order':
  75. // 线下无需核销,直接待执行
  76. $update['back_status'] = 1;// 返佣执行状态:0=无需执行,1=待执行,2=已推送,3=已执行
  77. // 发放商家收益
  78. $money = bcsub($bill['total_amount'],$bill['back_amount'],2);// 除去让利后的金额
  79. $wallet = new ShopWalletBusiness('offline_shop_order');
  80. if (!$wallet->change($bill['shop_id'],$money,'balance',ShopWalletBusiness::log_type[70],'线下消费','bill',$bill['id'])){
  81. Db::rollback();
  82. return $this->error($wallet->getMessage(),$wallet->getData());
  83. }
  84. break;
  85. }
  86. if (!$model->where('order_no', $bill_order_no)->update($update)){
  87. return $this->error('订单支付失败');
  88. }
  89. if (!Db::name($bill['table_name'])->where('id', $bill['table_id'])->update(['is_pay'=>1,'pay_time'=>time()])){
  90. return $this->error('订单支付失败');
  91. }
  92. return $this->success('支付成功');
  93. }
  94. /**
  95. * 创建订单
  96. * @param int $userId
  97. * @param string $orderNo
  98. * @param string $tableName
  99. * @return bool
  100. */
  101. public function createOrder(int $userId, string $orderNo, string $tableName)
  102. {
  103. $this->userId = $userId;
  104. $this->orderNo = $orderNo;
  105. $this->tableName = $tableName;
  106. if (!$this->$tableName()) {
  107. return $this->error($this->getMessage(), $this->getData());
  108. }
  109. return $this->success($this->getMessage(), $this->getData());
  110. }
  111. // 酒店订单
  112. private function hotel_order()
  113. {
  114. // 主表校验
  115. $model = new HotelOrderModel();
  116. $order = $model->getDetail(
  117. params: ['order_no', $this->orderNo],
  118. with : [
  119. 'hotel', 'room'
  120. ]
  121. );
  122. if (!$order) {
  123. return $this->error('订单不存在或已取消');
  124. }
  125. if ($order['is_pay'] == 1) {
  126. return $this->error('订单已支付');
  127. }
  128. $billModel = new BillModel();
  129. $bill = $billModel->getDetail(
  130. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  131. );
  132. if (!empty($bill) && $bill['status'] !== 0) {
  133. return $this->error('订单已支付');
  134. }
  135. // 如果没有订单 则需要新创建支付订单
  136. if ($bill) {
  137. $billId = $bill['id'];
  138. } else {
  139. $bill = [
  140. 'user_id' => $this->userId,
  141. 'order_no' => Common::createOrderNo('B'),
  142. 'total_amount' => $order['pay_amount'],
  143. 'pay_amount' => $order['pay_amount'],
  144. 'createtime' => time(),
  145. 'num' => $order['num'],
  146. 'table_name' => $this->tableName,
  147. 'table_id' => $order['id'],
  148. 'shop_id' => $order['hotel']['id'],
  149. 'shop_name' => $order['hotel']['name'],
  150. 'shop_logo' => $order['hotel']['image'],
  151. 'back_rate' => $order['hotel']['back_rate'],
  152. 'args' => json_encode([
  153. [
  154. 'image' => $order['room']['image'],// 图片
  155. 'name' => $order['room']['name'],// 规格名
  156. 'num' => $order['num'],// 购买数量
  157. // 酒店独有
  158. 'days' => $order['days'],
  159. 'start_date' => $order['start_date'],
  160. 'end_date' => $order['end_date'],
  161. ]
  162. ], JSON_UNESCAPED_UNICODE),
  163. ];
  164. if (!$billId = $billModel->insertGetId($bill)) {
  165. return $this->error('支付订单创建失败');
  166. }
  167. }
  168. return $this->success('支付订单创建成功', [
  169. 'bill_id' => $billId,
  170. ]);
  171. }
  172. // 餐厅订单
  173. private function hotel_canteen_order()
  174. {
  175. // 主表校验
  176. $model = new HotelCanteenOrderModel();
  177. $order = $model->getDetail(
  178. params: ['order_no', $this->orderNo],
  179. with : [
  180. 'canteen', 'room'
  181. ]
  182. );
  183. if (!$order) {
  184. return $this->error('订单不存在或已取消');
  185. }
  186. if ($order['is_pay'] == 1) {
  187. return $this->error('订单已支付');
  188. }
  189. $billModel = new BillModel();
  190. $bill = $billModel->getDetail(
  191. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  192. );
  193. if (!empty($bill) && $bill['status'] !== 0) {
  194. return $this->error('订单已支付');
  195. }
  196. // 如果没有订单 则需要新创建支付订单
  197. if ($bill) {
  198. $billId = $bill['id'];
  199. } else {
  200. $bill = [
  201. 'user_id' => $this->userId,
  202. 'order_no' => Common::createOrderNo('B'),
  203. 'total_amount' => $order['pay_amount'],
  204. 'pay_amount' => $order['pay_amount'],
  205. 'createtime' => time(),
  206. 'num' => 1,
  207. 'table_name' => $this->tableName,
  208. 'table_id' => $order['id'],
  209. 'shop_id' => $order['canteen']['id'],
  210. 'shop_name' => $order['canteen']['name'],
  211. 'shop_logo' => $order['canteen']['image'],
  212. 'back_rate' => $order['canteen']['back_rate'],
  213. 'args' => json_encode([
  214. [
  215. 'image' => $order['room']['image'],// 图片
  216. 'name' => $order['room']['name'],// 规格名
  217. 'num' => 1,// 购买数量
  218. // 餐厅独有
  219. 'get_to_time' => $order['get_to_time'],
  220. ]
  221. ], JSON_UNESCAPED_UNICODE),
  222. ];
  223. if (!$billId = $billModel->insertGetId($bill)) {
  224. return $this->error('支付订单创建失败');
  225. }
  226. }
  227. return $this->success('支付订单创建成功', [
  228. 'bill_id' => $billId,
  229. ]);
  230. }
  231. // 活动订单
  232. private function university_event_apply()
  233. {
  234. // 主表校验
  235. $model = new UniversityEventApplyModel();
  236. $order = $model->getDetail(
  237. params: ['order_no', $this->orderNo],
  238. with : [
  239. 'events'
  240. ]
  241. );
  242. if (!$order) {
  243. return $this->error('订单不存在或已取消');
  244. }
  245. if ($order['is_pay'] == 1) {
  246. return $this->error('订单已支付');
  247. }
  248. $billModel = new BillModel();
  249. $bill = $billModel->getDetail(
  250. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  251. );
  252. if (!empty($bill) && $bill['status'] !== 0) {
  253. return $this->error('订单已支付');
  254. }
  255. // 如果没有订单 则需要新创建支付订单
  256. if ($bill) {
  257. $billId = $bill['id'];
  258. } else {
  259. $bill = [
  260. 'user_id' => $this->userId,
  261. 'order_no' => Common::createOrderNo('B'),
  262. 'total_amount' => $order['pay_amount'],
  263. 'pay_amount' => $order['pay_amount'],
  264. 'createtime' => time(),
  265. 'num' => $order['num'],
  266. 'table_name' => $this->tableName,
  267. 'table_id' => $order['id'],
  268. 'shop_id' => $order['events']['id'],
  269. 'shop_name' => $order['events']['name'],
  270. 'shop_logo' => $order['events']['image'],
  271. 'back_rate' => $order['events']['back_rate'],
  272. 'args' => json_encode([
  273. [
  274. 'image' => $order['events']['image'],// 图片
  275. 'name' => $order['events']['name'],// 规格名
  276. 'num' => $order['num'],// 购买数量
  277. // 餐厅独有
  278. 'start_time' => $order['events']['start_time'],
  279. ]
  280. ], JSON_UNESCAPED_UNICODE),
  281. ];
  282. if (!$billId = $billModel->insertGetId($bill)) {
  283. return $this->error('支付订单创建失败');
  284. }
  285. }
  286. return $this->success('支付订单创建成功', [
  287. 'bill_id' => $billId,
  288. ]);
  289. }
  290. // 线下订单
  291. private function offline_shop_order()
  292. {
  293. // 主表校验
  294. $model = new OfflineShopOrderModel();
  295. $order = $model->getDetail(
  296. params: ['order_no', $this->orderNo],
  297. with : [
  298. 'shop'
  299. ]
  300. );
  301. if (!$order) {
  302. return $this->error('订单不存在或已取消');
  303. }
  304. if ($order['is_pay'] == 1) {
  305. return $this->error('订单已支付');
  306. }
  307. $billModel = new BillModel();
  308. $bill = $billModel->getDetail(
  309. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  310. );
  311. if (!empty($bill) && $bill['status'] !== 0) {
  312. return $this->error('订单已支付');
  313. }
  314. // 如果没有订单 则需要新创建支付订单
  315. if ($bill) {
  316. $billId = $bill['id'];
  317. } else {
  318. $bill = [
  319. 'user_id' => $this->userId,
  320. 'order_no' => Common::createOrderNo('B'),
  321. 'total_amount' => $order['pay_amount'],
  322. 'pay_amount' => $order['pay_amount'],
  323. 'createtime' => time(),
  324. 'num' => 1,
  325. 'table_name' => $this->tableName,
  326. 'table_id' => $order['id'],
  327. 'shop_id' => $order['shop']['id'],
  328. 'shop_name' => $order['shop']['name'],
  329. 'shop_logo' => $order['shop']['image'],
  330. 'back_rate' => $order['shop']['back_rate'],
  331. 'args' => json_encode([
  332. [
  333. 'image' => $order['shop']['image'],// 图片
  334. 'name' => $order['shop']['name'],// 规格名
  335. 'num' => 1,// 购买数量
  336. ]
  337. ], JSON_UNESCAPED_UNICODE),
  338. ];
  339. if (!$billId = $billModel->insertGetId($bill)) {
  340. return $this->error('支付订单创建失败');
  341. }
  342. }
  343. return $this->success('支付订单创建成功', [
  344. 'bill_id' => $billId,
  345. ]);
  346. }
  347. // 旅游订单
  348. private function travel_order()
  349. {
  350. // 主表校验
  351. $model = new TravelOrderModel();
  352. $order = $model->getDetail(
  353. params: ['order_no', $this->orderNo],
  354. with : [
  355. 'travel'
  356. ]
  357. );
  358. if (!$order) {
  359. return $this->error('订单不存在或已取消');
  360. }
  361. if ($order['is_pay'] == 1) {
  362. return $this->error('订单已支付');
  363. }
  364. $billModel = new BillModel();
  365. $bill = $billModel->getDetail(
  366. params: ['table_id' => $order['id'], 'table_name' => $this->tableName],
  367. );
  368. if (!empty($bill) && $bill['status'] !== 0) {
  369. return $this->error('订单已支付');
  370. }
  371. // 如果没有订单 则需要新创建支付订单
  372. if ($bill) {
  373. $billId = $bill['id'];
  374. } else {
  375. $bill = [
  376. 'user_id' => $this->userId,
  377. 'order_no' => Common::createOrderNo('B'),
  378. 'total_amount' => $order['pay_amount'],
  379. 'pay_amount' => $order['pay_amount'],
  380. 'createtime' => time(),
  381. 'num' => $order['num'],
  382. 'table_name' => $this->tableName,
  383. 'table_id' => $order['id'],
  384. 'shop_id' => $order['travel']['id'],
  385. 'shop_name' => $order['travel']['name'],
  386. 'shop_logo' => $order['travel']['image'],
  387. 'back_rate' => $order['travel']['back_rate'],
  388. 'args' => json_encode([
  389. [
  390. 'image' => $order['travel']['image'],// 图片
  391. 'name' => $order['travel']['name'],// 规格名
  392. 'num' => $order['num'],// 购买数量
  393. ]
  394. ], JSON_UNESCAPED_UNICODE),
  395. ];
  396. if (!$billId = $billModel->insertGetId($bill)) {
  397. return $this->error('支付订单创建失败');
  398. }
  399. }
  400. return $this->success('支付订单创建成功', [
  401. 'bill_id' => $billId,
  402. ]);
  403. }
  404. }