OrderController.php 27 KB

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