From 5e1d57324dd0db8d7674c30281b1e45588ce3619 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Mon, 23 Jun 2025 13:52:59 +0800 Subject: [PATCH] update --- .../Controllers/Admin/CourseController.php | 1 + .../Controllers/Mobile/CourseController.php | 87 ++++++++++++++++++- config/app.php | 7 +- routes/api.php | 5 ++ 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Admin/CourseController.php b/app/Http/Controllers/Admin/CourseController.php index 599d77c..6b044ba 100755 --- a/app/Http/Controllers/Admin/CourseController.php +++ b/app/Http/Controllers/Admin/CourseController.php @@ -189,6 +189,7 @@ class CourseController extends BaseController * @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), description="课程内容"), * @OA\Parameter(name="total", in="query", @OA\Schema(type="integer"), description="开课人数"), * @OA\Parameter(name="class", in="query", @OA\Schema(type="string"), description="所在班级"), + * @OA\Parameter(name="price", in="query", @OA\Schema(type="string"), description="价格"), * @OA\Parameter(name="is_arrange", in="query", @OA\Schema(type="integer"), description="是否排课-0否1是"), * @OA\Parameter(name="is_fee", in="query", @OA\Schema(type="integer"), description="是否缴费-0否1是"), * @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), description="课程状态:0待发布, 1已发布"), diff --git a/app/Http/Controllers/Mobile/CourseController.php b/app/Http/Controllers/Mobile/CourseController.php index 8fa64b5..1bc832f 100755 --- a/app/Http/Controllers/Mobile/CourseController.php +++ b/app/Http/Controllers/Mobile/CourseController.php @@ -6,6 +6,7 @@ namespace App\Http\Controllers\Mobile; use App\Helpers\ResponseCode; +use App\Models\AccompanyOrder; use App\Models\AppointmentTotalLog; use App\Models\Config; use App\Models\Course; @@ -13,7 +14,10 @@ use App\Models\CourseAppointmentTotal; use App\Models\CourseContent; use App\Models\CourseSign; use App\Models\Notice; +use App\Models\Order; use App\Models\User; +use EasyWeChat\Factory; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; class CourseController extends CommonController @@ -185,7 +189,10 @@ class CourseController extends CommonController 'course_id' => $all['course_id'], 'user_id' => $this->getUserId(), 'data' => $all['data'] ?? [], - 'change_data' => $all['change_data'] ?? [] + 'change_data' => $all['change_data'] ?? [], + 'no' => getBatchNo(), + 'money' => $course->price, + 'title' => $course->name ]); // 发短信 $smsSign = Config::getValueByKey('sms_sign'); @@ -511,4 +518,82 @@ class CourseController extends CommonController return $this->success($detail); } + /** + * @OA\Get( + * path="/api/mobile/course/pay", + * tags={"小程序-课程"}, + * summary="获取支付参数", + * @OA\Parameter(name="no", in="query", @OA\Schema(type="string"), required=false, description="no"), + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * @OA\Response( + * response="200", + * description="暂无" + * ) + * ) + */ + public function pay() + { + $all = \request()->all(); + $messages = [ + 'no.required' => 'no必填', + ]; + $validator = Validator::make($all, [ + 'no' => 'required', + ], $messages); + if ($validator->fails()) { + return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); + } + $courseSign = CourseSign::where('no', $all['no'])->first(); + if (!$courseSign) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '订单不存在']); + } + $course = Course::find($courseSign->course_id); + if ($course->fee_type != 2) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '支付类型错误']); + } + // 下单 + $config = [ + 'app_id' => config('app.applet_appid'), + 'mch_id' => config('app.applet_mch_id'), + 'key' => config('app.applet_key') + ]; + $app = Factory::payment($config); + $result = $app->order->unify([ + 'trade_type' => 'NATIVE', + 'product_id' => $courseSign->no, + 'body' => $courseSign->title, + 'out_trade_no' => $courseSign->no, + 'total_fee' => $courseSign->money * 100, + 'notify_url' => getDomain() . '/api/mobile/course/pay_callback', + ]); + return $this->success(compact('result')); + } + + public function payCallback() + { + $config = [ + 'app_id' => config('app.applet_appid'), + 'mch_id' => config('app.applet_mch_id'), + 'key' => config('app.applet_key') + ]; + // 获取支付参数 + $app = Factory::payment($config); + $response = $app->handlePaidNotify(function ($message, $fail) { + if ($message['return_code'] === 'SUCCESS') { + if ($message['result_code'] === 'SUCCESS') { + $order = CourseSign::where('no', $message['out_trade_no'])->first(); + if ($order) { + $order->fee_status = 1; + $order->save(); + } + return true; + } + } else { + return $fail('通信失败,请稍后再通知我'); + } + return true; // 返回处理完成 + }); + $response->send(); + } + } diff --git a/config/app.php b/config/app.php index b464aa5..e9b2738 100755 --- a/config/app.php +++ b/config/app.php @@ -4,8 +4,11 @@ use Illuminate\Support\Facades\Facade; return [ - 'applet_appid' => env('APPLET_APPID', 'wx55e93f19273ad0a0'), - 'applet_secret' => env('APPLET_SECRET', '0510e8239277b49881818b77de18a9d2'), + 'applet_appid' => env('APPLET_APPID', ''), + 'applet_secret' => env('APPLET_SECRET', ''), + + 'applet_mch_id' => env('APPLET_MCH_ID', ''), + 'applet_key' => env('APPLET_KEY', ''), 'sms_appid' => env('SMS_APPID', 'EUCP-EMY-SMS1-4UWZZ'), 'sms_secretKey' => env('SMS_SECRETKEY', '431BD4F22D176233'), diff --git a/routes/api.php b/routes/api.php index 719fcc8..2b27de9 100755 --- a/routes/api.php +++ b/routes/api.php @@ -205,6 +205,8 @@ Route::group(["namespace" => "Mobile", "prefix" => "mobile"], function () { Route::get('course/news', [\App\Http\Controllers\Mobile\CourseController::class, "news"]); // 新闻详情 Route::get('course/news-detail', [\App\Http\Controllers\Mobile\CourseController::class, "newsDetail"]); + // 支付回调 + Route::any('course/pay_callback', [\App\Http\Controllers\Mobile\CourseController::class, "payCallback"]); Route::group(['middleware' => ['sanctum.jwt:mobile']], function () { // 其他 Route::post('upload-file', [\App\Http\Controllers\Mobile\UploadController::class, "uploadFile"]); @@ -225,6 +227,9 @@ Route::group(["namespace" => "Mobile", "prefix" => "mobile"], function () { Route::get('course/my-course-content', [\App\Http\Controllers\Mobile\CourseController::class, "myCourseContent"]); Route::get('course/get-sign', [\App\Http\Controllers\Mobile\CourseController::class, "getSign"]); Route::post('course/update-sign', [\App\Http\Controllers\Mobile\CourseController::class, "updateSign"]); + // 获取缴费二维码 + Route::get('course/pay', [\App\Http\Controllers\Mobile\CourseController::class, "pay"]); + Route::get('course/contents', [\App\Http\Controllers\Mobile\CourseController::class, "contents"]); Route::get('course/user-list', [\App\Http\Controllers\Mobile\CourseController::class, "userList"]);