PaymentBusiness.php 12 KB

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