|
@@ -0,0 +1,116 @@
|
|
|
|
+<?php
|
|
|
|
+/**
|
|
|
|
+ * Created by PhpStorm.
|
|
|
|
+ * User: zilongs
|
|
|
|
+ * Date: 20-9-30
|
|
|
|
+ * Time: 下午10:58
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+namespace App\Http\Controllers\Api\v1;
|
|
|
|
+
|
|
|
|
+use App\Models\Order;
|
|
|
|
+use App\Models\OrderPatient;
|
|
|
|
+use App\Models\Patient;
|
|
|
|
+use App\Models\Payment;
|
|
|
|
+use App\Models\TimePeriod;
|
|
|
|
+use App\Models\UserCoupon;
|
|
|
|
+use DB;
|
|
|
|
+use EasyWeChat\Factory;
|
|
|
|
+
|
|
|
|
+class OrderController extends AuthController
|
|
|
|
+{
|
|
|
|
+ public function placeOrder()
|
|
|
|
+ {
|
|
|
|
+ $req = request()->post();
|
|
|
|
+ $this->validate(request(), [
|
|
|
|
+ 'product_type' => 'required|in:1,2,3',
|
|
|
|
+ 'docter_id' => 'required|integer',
|
|
|
|
+ 'patient_id' => 'required|integer',
|
|
|
|
+ 'total_amount' => 'required|integer',
|
|
|
|
+ 'user_coupon_id' => 'integer',
|
|
|
|
+ 'phone' => 'required_if:product_type,1',
|
|
|
|
+ 'phone_minutes' => 'required_if:product_type,1|integer',
|
|
|
|
+ 'symptoms' => 'required_if:product_type,2|max:2000',
|
|
|
|
+ 'medical_imgs' => 'required_if:product_type,2|json|max:3000',
|
|
|
|
+ 'schedule_date' => 'required_if:product_type,3|date',
|
|
|
|
+ 'time_period_id' => 'required_if:product_type,3|integer',
|
|
|
|
+ 'organization_id' => 'required_if:product_type,3|integer'
|
|
|
|
+ ]);
|
|
|
|
+ $user = $this->user;
|
|
|
|
+
|
|
|
|
+ $discount_amount = 0;
|
|
|
|
+ if (!empty($req['user_coupon_id'])) {
|
|
|
|
+ //计算优惠金额
|
|
|
|
+ $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
|
|
|
|
+ }
|
|
|
|
+ $payment_amount = $req['total_amount'] - $discount_amount;
|
|
|
|
+
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
+ try {
|
|
|
|
+ //保存订单数据
|
|
|
|
+ $order = Order::create([
|
|
|
|
+ 'user_id' => $user['id'],
|
|
|
|
+ 'product_type' => $req['product_type'],
|
|
|
|
+ 'docter_id' => $req['docter_id'],
|
|
|
|
+ 'total_amount' => $req['total_amount'],
|
|
|
|
+ 'payment_amount' => $payment_amount,
|
|
|
|
+ 'discount_amount' => $discount_amount,
|
|
|
|
+ ]);
|
|
|
|
+ $order_sn = build_sn($order['id']);
|
|
|
|
+ Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
|
|
|
|
+ //保存订单患者信息
|
|
|
|
+ $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship', 'info', 'card_type', 'card_number'])->where('id', $req['patient_id'])->first()->toArray();
|
|
|
|
+ $addPatient['order_id'] = $order['id'];
|
|
|
|
+ $addPatient['patient_id'] = $req['patient_id'];
|
|
|
|
+ if ($req['product_type'] == 1) {
|
|
|
|
+ $addPatient['phone'] = $req['phone'];
|
|
|
|
+ $addPatient['phone_minutes'] = $req['phone_minutes'];
|
|
|
|
+ }
|
|
|
|
+ elseif ($req['product_type'] == 2) {
|
|
|
|
+ $addPatient['symptoms'] = $req['symptoms'];
|
|
|
|
+ $addPatient['medical_imgs'] = $req['medical_imgs'];
|
|
|
|
+ }
|
|
|
|
+ elseif ($req['product_type'] == 3) {
|
|
|
|
+ $addPatient['organization_id'] = $req['organization_id'];
|
|
|
|
+ $addPatient['time_period_id'] = $req['time_period_id'];
|
|
|
|
+ $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
|
|
|
|
+ $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
|
|
|
|
+ $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
|
|
|
|
+ }
|
|
|
|
+ OrderPatient::create($addPatient);
|
|
|
|
+ //生成支付交易单
|
|
|
|
+ $trade_sn = build_sn($order['id'], 3, 'T');
|
|
|
|
+ $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
|
|
|
|
+ Payment::create([
|
|
|
|
+ 'user_id' => $user['id'],
|
|
|
|
+ 'order_id' => $order['id'],
|
|
|
|
+ 'trade_sn' => $trade_sn,
|
|
|
|
+ 'amount' => $payment_amount,
|
|
|
|
+ 'remark' => $payBody,
|
|
|
|
+ ]);
|
|
|
|
+ //请求支付
|
|
|
|
+ $payment = Factory::payment(config('config.wechat_pay'));
|
|
|
|
+ $result = $payment->order->unify([
|
|
|
|
+ 'body' => $payBody,
|
|
|
|
+ 'out_trade_no' => $trade_sn,
|
|
|
|
+ 'total_fee' => $payment_amount,
|
|
|
|
+ 'trade_type' => 'JSAPI',
|
|
|
|
+ 'openid' => $user['openid'],
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ if (empty($result['prepay_id'])) {
|
|
|
|
+ $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
|
|
|
|
+ return out(null, 702, $errorMsg);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
|
|
|
|
+
|
|
|
|
+ DB::commit();
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ DB::rollBack();
|
|
|
|
+ return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return out($config);
|
|
|
|
+ }
|
|
|
|
+}
|