PaymentBusiness.php 12 KB

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