OrderController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-30
  6. * Time: 下午10:58
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\DocterServiceTime;
  10. use App\Models\DocterSetting;
  11. use App\Models\Nurse;
  12. use App\Models\Order;
  13. use App\Models\OrderNurse;
  14. use App\Models\OrderPack;
  15. use App\Models\OrderPatient;
  16. use App\Models\OrderVaccine;
  17. use App\Models\Patient;
  18. use App\Models\Payment;
  19. use App\Models\SchedulePeriod;
  20. use App\Models\ServicePack;
  21. use App\Models\Team;
  22. use App\Models\TeamDocter;
  23. use App\Models\TimePeriod;
  24. use App\Models\User;
  25. use App\Models\UserCoupon;
  26. use App\Models\Vaccine;
  27. use EasyWeChat\Factory;
  28. use DB;
  29. use Exception;
  30. class OrderController extends AuthController
  31. {
  32. public function consultPlaceOrder()
  33. {
  34. $req = request()->post();
  35. $this->validate(request(), [
  36. 'payment_type' => 'required|in:1,2,3',
  37. 'product_type' => 'required|in:1,2',
  38. 'docter_id' => 'required|integer',
  39. 'patient_id' => 'required|integer',
  40. 'total_amount' => 'required|integer',
  41. 'user_coupon_id' => 'integer',
  42. 'phone' => 'required_if:product_type,1',
  43. 'phone_minutes' => 'required_if:product_type,1|integer',
  44. 'symptoms' => 'required_if:product_type,2|max:2000',
  45. 'medical_imgs' => 'required_if:product_type,2|json|max:3000',
  46. 'order_pack_id' => 'required_if:payment_type,3|integer',
  47. ]);
  48. $user = $this->user;
  49. //判断是否在服务时间内
  50. $now_line = (int)date('Hi');
  51. if (!DocterServiceTime::where('docter_id', $req['docter_id'])->where('type', $req['product_type'])->where('start_time_line', '<=', $now_line)->where('end_time_line', '>', $now_line)->exists()) {
  52. return out(null, 10011, '当前不在医生服务时间内,不能下单');
  53. }
  54. $discount_amount = 0;
  55. if (!empty($req['user_coupon_id'])) {
  56. //计算优惠金额
  57. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
  58. }
  59. $payment_amount = $req['total_amount'] - $discount_amount;
  60. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  61. //图文咨询订单未结束时不能针对同一医生再次下图文订单
  62. if ($req['product_type'] == 2 && Order::where('docter_id', $req['docter_id'])->where('product_type', 2)->where('user_id', $user['id'])->whereIn('order_status', [2,3])->exists()) {
  63. return out(null, 10012, '您已经下过该医生的图文订单了,并且订单还未完成');
  64. }
  65. $order_status = $payment_status = 1;
  66. $payment_time = 0;
  67. DB::beginTransaction();
  68. try {
  69. //保存订单数据
  70. $create = [
  71. 'user_id' => $user['id'],
  72. 'payment_type' => $req['payment_type'],
  73. 'product_type' => $req['product_type'],
  74. 'docter_id' => $req['docter_id'],
  75. 'total_amount' => $req['total_amount'],
  76. 'payment_amount' => $payment_amount,
  77. 'discount_amount' => $discount_amount,
  78. 'patient_id' => $req['patient_id'],
  79. 'order_status' => $order_status,
  80. 'payment_status' => $payment_status,
  81. 'payment_time' => $payment_time,
  82. ];
  83. if ($req['payment_type'] == 3) {
  84. $create['pay_order_pack_id'] = $req['order_pack_id'];
  85. }
  86. $order = Order::create($create);
  87. $order_sn = build_sn($order['id']);
  88. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  89. //保存订单患者信息
  90. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  91. $addPatient['order_id'] = $order['id'];
  92. $addPatient['patient_id'] = $req['patient_id'];
  93. if ($req['product_type'] == 1) {
  94. $addPatient['phone'] = $req['phone'];
  95. $addPatient['phone_minutes'] = $req['phone_minutes'];
  96. }
  97. elseif ($req['product_type'] == 2) {
  98. $addPatient['symptoms'] = $req['symptoms'];
  99. $addPatient['medical_imgs'] = $req['medical_imgs'];
  100. }
  101. OrderPatient::create($addPatient);
  102. //如果有优惠券就标记优惠券为已使用
  103. if (!empty($req['user_coupon_id'])) {
  104. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  105. }
  106. DB::commit();
  107. } catch (Exception $e) {
  108. DB::rollBack();
  109. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  110. }
  111. return out(['order_id' => $order['id']]);
  112. }
  113. public function appointPlaceOrder()
  114. {
  115. $req = request()->post();
  116. $this->validate(request(), [
  117. 'payment_type' => 'required|in:1,2,3',
  118. 'product_type' => 'required|in:3,4,5',
  119. 'patient_id' => 'required|integer',
  120. 'organization_id' => 'required|integer',
  121. 'schedule_date' => 'required|date',
  122. 'time_period_id' => 'required|integer',
  123. 'total_amount' => 'required|integer',
  124. 'user_coupon_id' => 'integer',
  125. 'docter_id' => 'required_if:product_type,3|integer',
  126. 'vaccine_id' => 'required_if:product_type,4|integer',
  127. 'nurse_ids' => 'required_if:product_type,5|json',
  128. 'order_pack_id' => 'required_if:payment_type,3|integer',
  129. ]);
  130. $user = $this->user;
  131. $product_type = $req['product_type'];
  132. $discount_amount = 0;
  133. if (!empty($req['user_coupon_id'])) {
  134. //计算优惠金额
  135. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $product_type);
  136. }
  137. $payment_amount = $req['total_amount'] - $discount_amount;
  138. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  139. //疫苗和儿保订单未结束时不能再次下单
  140. if (in_array($req['product_type'], [4,5]) && Order::whereIn('product_type', [4,5])->where('user_id', $user['id'])->whereIn('order_status', [2,3])->exists()) {
  141. return out(null, 10012, '您已经下过'.config('config.product_type_map')[$req['product_type']].'订单,并且订单还未完成');
  142. }
  143. //检查号源
  144. if ($product_type == 3) {
  145. $schedulePeriod = SchedulePeriod::where('docter_id', $req['docter_id'])->where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->where('schedule_type', 1)->first();
  146. if (empty($schedulePeriod)) {
  147. return out(null, 10012, '医生无该时间段的排班');
  148. }
  149. $docterSettings = DocterSetting::select(['service_num'])->where('docter_id', $req['docter_id'])->where('type', 1)->first();
  150. if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
  151. return out(null, 10014, '医生该时间段已经预约满了');
  152. }
  153. }
  154. elseif (in_array($product_type, [4,5])) {
  155. $schedule_type_map = [4 => 2, 5 => 3];
  156. $schedulePeriod = SchedulePeriod::where('time_period_id', $req['time_period_id'])->where('schedule_date', $req['schedule_date'])->where('organization_id', $req['organization_id'])->where('schedule_type', $schedule_type_map[$product_type])->first();
  157. if (empty($schedulePeriod)) {
  158. return out(null, 10013, '机构无该时间段的排班');
  159. }
  160. $docterSettings = DocterSetting::select(['service_num'])->where('org_id', $req['organization_id'])->where('type', $schedule_type_map[$product_type])->first();
  161. if ($docterSettings['service_num'] <= $schedulePeriod['order_sn']) {
  162. return out(null, 10015, '机构该时间段已经预约满了');
  163. }
  164. }
  165. //疫苗预约检查库存是否足够
  166. if ($req['product_type'] == 4) {
  167. $stock = Vaccine::where('id', $req['vaccine_id'])->value('stock');
  168. if ($stock <= 0) {
  169. return out(null, 10009, '该疫苗库存不足');
  170. }
  171. }
  172. $order_status = $payment_status = 1;
  173. $payment_time = 0;
  174. DB::beginTransaction();
  175. try {
  176. //保存订单数据
  177. $create = [
  178. 'user_id' => $user['id'],
  179. 'docter_id' => $req['docter_id'] ?? 0,
  180. 'patient_id' => $req['patient_id'],
  181. 'organization_id' => $req['organization_id'],
  182. 'payment_type' => $req['payment_type'],
  183. 'product_type' => $product_type,
  184. 'order_status' => $order_status,
  185. 'payment_status' => $payment_status,
  186. 'total_amount' => $req['total_amount'],
  187. 'payment_amount' => $payment_amount,
  188. 'discount_amount' => $discount_amount,
  189. 'payment_time' => $payment_time,
  190. ];
  191. if ($req['payment_type'] == 3) {
  192. $create['pay_order_pack_id'] = $req['order_pack_id'];
  193. }
  194. $order = Order::create($create);
  195. $order_sn = build_sn($order['id']);
  196. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  197. //保存订单患者信息
  198. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  199. $addPatient['order_id'] = $order['id'];
  200. $addPatient['patient_id'] = $req['patient_id'];
  201. $addPatient['organization_id'] = $req['organization_id'];
  202. $addPatient['time_period_id'] = $req['time_period_id'];
  203. $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
  204. $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
  205. $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
  206. $orderPatient = OrderPatient::create($addPatient);
  207. //保存订单疫苗信息
  208. if ($req['product_type'] == 4) {
  209. $addVaccine = Vaccine::select(['id as vaccine_id', 'type as vaccine_type', 'price as vaccine_price', 'name as vaccine_name', 'remark as vaccine_remark', 'supplier as vaccine_supplier'])->where('id', $req['vaccine_id'])->where('org_id', $req['organization_id'])->first()->getOriginal();
  210. $addVaccine['order_id'] = $order['id'];
  211. $addVaccine['order_patient_id'] = $orderPatient['id'];
  212. $addVaccine = array_filter($addVaccine);
  213. OrderVaccine::create($addVaccine);
  214. }
  215. //保存儿保订单信息
  216. if ($req['product_type'] == 5) {
  217. $nurse_ids = json_decode($req['nurse_ids'], true);
  218. foreach ($nurse_ids as $k => $v) {
  219. $addNurse = Nurse::select(['id as nurse_id', 'price as nurse_price', 'name as nurse_name', 'remark as nurse_remark'])->where('id', $v)->first()->getOriginal();
  220. $addNurse['order_id'] = $order['id'];
  221. $addNurse['order_patient_id'] = $orderPatient['id'];
  222. OrderNurse::create($addNurse);
  223. }
  224. }
  225. //如果有优惠券就标记优惠券为已使用
  226. if (!empty($req['user_coupon_id'])) {
  227. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  228. }
  229. //疫苗预约 订单直接完成
  230. Order::payCompletedHandle($order['id']);
  231. DB::commit();
  232. } catch (Exception $e) {
  233. DB::rollBack();
  234. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  235. }
  236. return out(['order_id' => $order['id']]);
  237. }
  238. public function packPlaceOrder()
  239. {
  240. $req = request()->post();
  241. $this->validate(request(), [
  242. 'payment_type' => 'required|in:1,2',
  243. 'patient_id' => 'required|integer',
  244. 'total_amount' => 'required|integer',
  245. 'user_coupon_id' => 'integer',
  246. 'service_pack_id' => 'required|integer',
  247. 'is_security' => 'required|in:0,1',
  248. 'guardian_name' => 'required|max:50',
  249. 'relationship_type' => 'required|integer',
  250. 'is_need_insurance' => 'in:0,1',
  251. ]);
  252. $user = $this->user;
  253. $discount_amount = 0;
  254. if (!empty($req['user_coupon_id'])) {
  255. //计算优惠金额
  256. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
  257. }
  258. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  259. $payment_amount = $req['total_amount'] - $discount_amount;
  260. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  261. $order_status = $payment_status = 1;
  262. $payment_time = 0;
  263. DB::beginTransaction();
  264. try {
  265. //保存订单数据
  266. $order = Order::create([
  267. 'user_id' => $user['id'],
  268. 'patient_id' => $req['patient_id'],
  269. 'payment_type' => $req['payment_type'],
  270. 'product_type' => 6,
  271. 'total_amount' => $req['total_amount'],
  272. 'payment_amount' => $payment_amount,
  273. 'discount_amount' => $discount_amount,
  274. 'order_status' => $order_status,
  275. 'payment_status' => $payment_status,
  276. 'payment_time' => $payment_time,
  277. ]);
  278. $order_sn = build_sn($order['id']);
  279. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  280. //保存订单患者信息
  281. $addPatient['order_id'] = $order['id'];
  282. $addPatient['patient_id'] = $req['patient_id'];
  283. OrderPatient::create($addPatient);
  284. //保存订单服务包表
  285. $addPack = ServicePack::select(['id as service_pack_id', 'name as pack_name', 'intro as pack_intro', 'price as pack_price', 'insurance_policy', 'insurance_img_url', 'team_id', 'phone_minutes', 'chat_num', 'appoint_num', 'vaccine_limit_amount', 'nurses_limit_amount', 'effective_days', 'label'])->where('id', $req['service_pack_id'])->first()->getOriginal();
  286. $addPack['user_id'] = $user['id'];
  287. $addPack['order_id'] = $order['id'];
  288. $addPack['is_security'] = $req['is_security'];
  289. $addPack['guardian_name'] = $req['guardian_name'];
  290. $addPack['relationship_type'] = $req['relationship_type'];
  291. $addPack['start_time'] = time();
  292. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  293. $addPack['total_phone_minutes'] = $addPack['phone_minutes'];
  294. $addPack['total_chat_num'] = $addPack['chat_num'];
  295. $addPack['total_appoint_num'] = $addPack['appoint_num'];
  296. $addPack['total_vaccine_limit_amount'] = $addPack['vaccine_limit_amount'];
  297. $addPack['total_nurses_limit_amount'] = $addPack['nurses_limit_amount'];
  298. $addPack['label'] = json_decode($addPack['label'], true);
  299. $addPack['is_need_insurance'] = !empty($req['is_need_insurance']) ? $req['is_need_insurance'] : 0;
  300. $addPack['team_id'] = json_decode($addPack['team_id'], true);
  301. OrderPack::create($addPack);
  302. //如果有优惠券就标记优惠券为已使用
  303. if (!empty($req['user_coupon_id'])) {
  304. UserCoupon::where('id', $req['user_coupon_id'])->update(['order_id' => $order['id'], 'status' => 2, 'use_time' => time()]);
  305. }
  306. DB::commit();
  307. } catch (Exception $e) {
  308. DB::rollBack();
  309. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  310. }
  311. return out(['order_id' => $order['id']]);
  312. }
  313. public function orderList()
  314. {
  315. $req = request()->post();
  316. $this->validate(request(), [
  317. 'list_type' => 'required|in:0,1,2',
  318. 'product_type' => 'integer',
  319. 'order_status' => 'integer',
  320. 'time_sort' => 'in:0,1',
  321. 'is_pack_expire' => 'in:0,1,2',
  322. ]);
  323. $user = $this->user;
  324. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('user_id', $user['id']);
  325. if (!empty($req['product_type'])) {
  326. $builder->where('product_type', $req['product_type']);
  327. }
  328. else {
  329. if (!empty($req['list_type'])) {
  330. if ($req['list_type'] == 1) {
  331. $builder->whereIn('product_type', [1,2]);
  332. }
  333. elseif ($req['list_type'] == 2) {
  334. $builder->whereIn('product_type', [3,4,5]);
  335. }
  336. }
  337. else {
  338. $builder->where('product_type', '<', 7);
  339. }
  340. }
  341. if (!empty($req['order_status'])) {
  342. $builder->where('order_status', $req['order_status']);
  343. }
  344. if (!empty($req['is_pack_expire'])) {
  345. $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
  346. if ($req['is_pack_expire'] == 1) {
  347. $tmpBuilder->where('order_packs.end_time', '<', time());
  348. }
  349. else {
  350. $tmpBuilder->where('order_packs.end_time', '>=', time());
  351. }
  352. $order_ids = $tmpBuilder->pluck('o.id')->toArray();
  353. $builder->whereIn('id', $order_ids);
  354. }
  355. if (!empty($req['time_sort'])) {
  356. $builder->orderBy('id', 'desc');
  357. }
  358. else {
  359. $builder->orderBy('id', 'asc');
  360. }
  361. $data = $builder->paginate();
  362. return out($data);
  363. }
  364. public function orderDetail()
  365. {
  366. $req = request()->post();
  367. $this->validate(request(), [
  368. 'order_id' => 'required|integer'
  369. ]);
  370. $user = $this->user;
  371. $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first()->toArray();
  372. if (!empty($data['order_pack'])) {
  373. $data['order_pack']['team'] = [];
  374. if (!empty($data['order_pack']['team_id'])) {
  375. $data['order_pack']['team'] = Team::with(['docter.office', 'docter.qualification'])->whereIn('id', $data['order_pack']['team_id'])->get()->toArray();
  376. }
  377. }
  378. return out($data);
  379. }
  380. public function topup()
  381. {
  382. $req = request()->post();
  383. $this->validate(request(), [
  384. 'amount' => 'required|integer',
  385. ]);
  386. $user = $this->user;
  387. DB::beginTransaction();
  388. try {
  389. //保存订单数据
  390. $order = Order::create([
  391. 'user_id' => $user['id'],
  392. 'product_type' => 7,
  393. 'total_amount' => $req['amount'],
  394. 'payment_amount' => $req['amount'],
  395. ]);
  396. $order_sn = build_sn($order['id']);
  397. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  398. //生成支付交易单
  399. $trade_sn = build_sn($order['id'], 3, 'T');
  400. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  401. Payment::create([
  402. 'user_id' => $user['id'],
  403. 'order_id' => $order['id'],
  404. 'trade_sn' => $trade_sn,
  405. 'amount' => $req['amount'],
  406. 'remark' => $payBody,
  407. ]);
  408. //请求支付
  409. $payment = Factory::payment(config('config.wechat_pay'));
  410. $result = $payment->order->unify([
  411. 'body' => $payBody,
  412. 'out_trade_no' => $trade_sn,
  413. 'total_fee' => $req['amount'],
  414. 'trade_type' => 'JSAPI',
  415. 'openid' => $user['openid'],
  416. ]);
  417. if (empty($result['prepay_id'])) {
  418. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  419. return out(null, 702, $errorMsg);
  420. }
  421. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  422. DB::commit();
  423. } catch (Exception $e) {
  424. DB::rollBack();
  425. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  426. }
  427. return out($config);
  428. }
  429. public function orderCancel()
  430. {
  431. $req = request()->post();
  432. $this->validate(request(), [
  433. 'order_id' => 'required|integer'
  434. ]);
  435. $user = $this->user;
  436. $order = Order::with(['orderPatient'])->where('id', $req['order_id'])->first()->toArray();
  437. if ($order['order_status'] == 4) {
  438. return out(null, 10001, '订单已完成,不能取消了');
  439. }
  440. if ($order['order_status'] == 5 || $order['order_status'] == 6) {
  441. return out(null, 10002, '订单已取消了,请勿重复操作');
  442. }
  443. if (in_array($order['product_type'], [1, 2])) {
  444. if ($order['order_status'] == 3) {
  445. return out(null, 10004, '医生已接单,不能再取消订单了');
  446. }
  447. }
  448. elseif (in_array($order['product_type'], [4])) {
  449. if ($order['order_patient']['appoint_start_time'] - 3600 < time()) {
  450. return out(null, 10003, '预约时间临近,不能取消订单了');
  451. }
  452. }
  453. DB::beginTransaction();
  454. try {
  455. Order::orderCancel($req['order_id'], '用户取消订单');
  456. //发送取消订单消息
  457. $order = Order::with(['docter', 'orderPatient', 'organization'])->where('id', $req['order_id'])->first()->toArray();
  458. $keyword2 = config('config.product_type_map')[$order['product_type']];
  459. if (in_array($order['product_type'], [1,2,3])) {
  460. $server_name = $order['docter']['name'].'医生';
  461. }
  462. else {
  463. $server_name = $order['organization']['name'];
  464. }
  465. $payment_status_text = '';
  466. if ($order['product_type'] != 4) {
  467. $payment_status_text = '支付状态:'.config('config.payment_status_map')[$order['payment_status']].' ';
  468. }
  469. $official_arr = [$user['openid'], $order['order_sn'], $keyword2, round($order['total_amount']/100, 2), date('Y-m-d H:i:s', $order['payment_time']), $order['order_patient']['name'], $server_name, $order['order_notes'], $payment_status_text];
  470. $subscribe_arr = [$user['openid'], $order['order_sn'], $keyword2, $order['order_notes']];
  471. send_wechat_message(7, $official_arr, $subscribe_arr);
  472. DB::commit();
  473. } catch (Exception $e) {
  474. DB::rollBack();
  475. return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
  476. }
  477. return out();
  478. }
  479. public function orderPackPayList()
  480. {
  481. $req = request()->post();
  482. $this->validate(request(), [
  483. 'docter_id' => 'required|integer',
  484. ]);
  485. $user = $this->user;
  486. $orderPacks = OrderPack::where('user_id', $user['id'])->where('end_time', '>', time())->get()->toArray();
  487. if (!empty($orderPacks)) {
  488. foreach ($orderPacks as $k => $v) {
  489. if ($v['usable_status'] == 0) {
  490. unset($orderPacks[$k]);
  491. }
  492. if (!empty($v['team_id'])) {
  493. if (!TeamDocter::whereIn('team_id', $v['team_id'])->where('docter_id', $req['docter_id'])->exists()) {
  494. unset($orderPacks[$k]);
  495. }
  496. }
  497. }
  498. $orderPacks = array_values($orderPacks);
  499. }
  500. return out($orderPacks);
  501. }
  502. public function orderPay()
  503. {
  504. $req = request()->post();
  505. $this->validate(request(), [
  506. 'order_id' => 'required|integer',
  507. 'pay_password' => 'max:20',
  508. ]);
  509. $user = $this->user;
  510. $order = Order::where('id', $req['order_id'])->first()->toArray();
  511. if ($order['order_status'] != 1) {
  512. return out(null, 10001, '该订单不能支付了');
  513. }
  514. if (in_array($order['payment_type'], [2,3]) && $order['payment_amount'] > 0 && empty($req['pay_password'])) {
  515. return out(null, 10011, '请输入支付密码');
  516. }
  517. if (!empty($req['pay_password'])) {
  518. if (empty($user['pay_password'])) {
  519. return out(null, 60010, '未设置支付密码');
  520. }
  521. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  522. return out(null, 10001, '密码错误');
  523. }
  524. }
  525. if ($order['payment_type'] == 2) {
  526. if ($user['balance'] < $order['payment_amount']) {
  527. return out(null, 601, '余额不足');
  528. }
  529. }
  530. Order::checkOrder($req['order_id']);
  531. $config = null;
  532. DB::beginTransaction();
  533. try {
  534. //判断是微信支付
  535. if ($order['payment_type'] == 1) {
  536. //生成支付交易单
  537. if ($order['payment_amount'] > 0) {
  538. $trade_sn = build_sn($order['id'], 3, 'T');
  539. $payBody = '超级宝妈-'.config('config.product_type_map')[$order['product_type']];
  540. Payment::create([
  541. 'user_id' => $user['id'],
  542. 'order_id' => $order['id'],
  543. 'trade_sn' => $trade_sn,
  544. 'amount' => $order['payment_amount'],
  545. 'remark' => $payBody,
  546. ]);
  547. //请求支付
  548. $payment = Factory::payment(config('config.wechat_pay'));
  549. $result = $payment->order->unify([
  550. 'body' => $payBody,
  551. 'out_trade_no' => $trade_sn,
  552. 'total_fee' => $order['payment_amount'],
  553. 'trade_type' => 'JSAPI',
  554. 'openid' => $user['openid'],
  555. ]);
  556. if (empty($result['prepay_id'])) {
  557. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  558. return out(null, 702, $errorMsg);
  559. }
  560. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  561. }
  562. }
  563. //判断是余额支付
  564. elseif ($order['payment_type'] == 2) {
  565. if ($order['payment_amount'] > 0) {
  566. //改变用户余额
  567. $change_amount = 0 - $order['payment_amount'];
  568. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
  569. }
  570. }
  571. if ($order['payment_amount'] == 0 || in_array($order['payment_type'], [2, 3])) {
  572. Order::payCompletedHandle($order['id']);
  573. }
  574. DB::commit();
  575. } catch (Exception $e) {
  576. DB::rollBack();
  577. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  578. }
  579. return out($config);
  580. }
  581. }