OrderController.php 32 KB

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