OrderController.php 24 KB

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