PaymentBusiness.php 12 KB

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