*/ private function initWechat() { $this->app = Factory::officialAccount(Config::get('wechat')); } /** * Notes : 获取jssdk配置 * * @Date : 2022/9/20 13:32 * @Author : */ public function jsSdk(): Json { $this->initWechat(); $APIs = [ 'updateAppMessageShareData', 'updateTimelineShareData', 'hideMenuItems', ]; $url = $GLOBALS['data']['data']['url']; $this->app->jssdk->setUrl($url); $config = $this->app->jssdk->buildConfig($APIs, false, false,false); return show(SUCCESS_MESSAGE, SUCCESS_CODE, $config); } /** * Notes : 获取微信授权的地址并跳转 * * @Date : 2022/9/9 11:29 * @Author : */ public function url(): Json { $url = $GLOBALS['data']['data']['url']; /// 来源用户, $fromUid = $GLOBALS['data']['data']['from_uid'] ?? 0; if ($fromUid) { AppUser::where('id', $fromUid)->inc('share_times')->update(); } $redirect = Route::buildUrl('wechat/code') ->vars(['callback' => $url, 'parent_id' => $fromUid]) ->suffix(false) ->domain(true); $this->initWechat(); return show(SUCCESS_MESSAGE, SUCCESS_CODE, $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect)); } /** * Notes : 微信授权回调地址,获取到用户信息后,自行保存 * * @Date : 2022/9/9 11:29 * @Author : */ public function code() { $this->initWechat(); $code = Request::get('code'); $parent_id = Request::get('parent_id'); $wechatUser = $this->app->oauth->userFromCode($code); $callback = Request::get('callback'); $user = AppUser::where('openid', $wechatUser->getId())->find(); if (! $user) { $user = AppUser::create([ 'nickname' => $wechatUser->getNickname(), 'avatar' => $wechatUser->getAvatar(), 'identity' => 0, 'openid' => $wechatUser->getId(), ]); Db::name("app_users")->where("id", $parent_id)->inc("share_count", 1)->update(); AppUserRelation::create([ 'parent_id' => $parent_id, 'user_id' => $user->id, ]); } $tokenData = ['userid' => $user->id, 'loginTime' => time(), 'rankStr' => strRand(5)]; $token = authcode(json_encode($tokenData), 'ENCODE'); $sep = str_contains($callback, '?') ? '&' : '?'; return redirect($callback.$sep.'token='.$token.'&user_id='.$user->id); } /** * Notes : 显示支付的页面,支付逻辑 * * @Date : 2022/9/9 11:32 * @Author : */ public function payment() { $clientToken = Request::get('token'); $orderId = Request::get('order_id'); $callback = Request::get('callback'); $sep = str_contains($callback, '?') ? '&' : '?'; $callback = $callback.$sep.'refresh=true'; if ($clientToken) { $tk = json_decode(authcode($clientToken), true); $userId = $tk['userid']; } $user = AppUser::findOrEmpty($userId); if (! $user) { exit('NO USER'); } $order = \app\model\Order::find($orderId); if (empty($order)) { exit('订单信息不存在'); } if (\app\model\Order::where([ "user_id" => $order['user_id'], "student_id" => $order['student_id'], "status" => 1, ])->find()) { exit('已经为该孩子助力过了'); } $notifyUrl = Route::buildUrl('wechat/paid') ->suffix(false) ->domain(true) ->build(); $outTradeNo = time().rand(100000, 999999); Payment::create([ 'trade_no' => $outTradeNo, 'order_id' => $order->id, ]); $config = Config::get('wechat.payment'); $payment = Factory::payment($config); $unify = $payment->order->unify([ 'body' => '商品订单', 'out_trade_no' => $outTradeNo, 'total_fee' => $order->amount * 100, 'notify_url' => $notifyUrl, 'trade_type' => 'JSAPI', 'openid' => $user->openid, ]); if ($unify['result_code'] == 'FAIL') { exit($unify['err_code_des']); } $prepayId = $unify['prepay_id']; $jssdk = $payment->jssdk->bridgeConfig($prepayId, false); return View::fetch('', ['jssdk' => $jssdk, 'callback' => $callback]); } /** * Notes : 支付完成回调 * * @Date : 2022/9/9 11:33 * @Author : */ public function paid() { $config = Config::get('wechat.payment'); $payment = Factory::payment($config); return $payment->handlePaidNotify(function ($message, $fail) { $order = Payment::where('trade_no', $message['out_trade_no'])->find(); if (! $order || $order->paid_at) { return true; } if (key_exists('result_code', $message) && $message['result_code'] === 'SUCCESS') { $order->paid(); //订单支付成功。查询用户信息 $order_detail = \app\model\Order::where("id", $order->order_id)->find(); $order_detail->paid(); //为用户增加助力值 $zhuLi = env("ZHULI_VALUE") ?? 150; Db::name("student")->inc("hot", $zhuLi)->where("id", $order_detail->student_id)->update(); Db::name("student")->inc("hot_count", 1)->where("id", $order_detail->student_id)->update(); } return $fail('Order not exists.'); }); } }