OrderController.php 26 KB

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