diff --git a/app/Controller/Payment/AccountController.php b/app/Controller/Payment/AccountController.php new file mode 100644 index 0000000..955a0c9 --- /dev/null +++ b/app/Controller/Payment/AccountController.php @@ -0,0 +1,140 @@ +requestService = $requestService; + $this->userService = $userService; + $this->paymentService = $paymentService; + } + + private function createRequestLog($data) { + $params = [ + 'app_id' => '', + 'data' => json_encode($data), + ]; + return $this->requestService->createRequestLog('', $params); + } + + private function checkUser(RequestInterface $request) { + $token = $request->input('token'); + if (empty($token)) { + throw new BusinessException('未登录'); + } + $result = Redis::get(RedisKey::getUserTokenKey($token)); + if (empty($result)) { + throw new BusinessException('未登录'); + } + $userInfo = json_decode($result, true); + if (empty($userInfo)) { + throw new BusinessException('未登录'); + } + return $userInfo; + } + + public function register(RequestInterface $request) + { + $username = $request->input('username'); + $password = $request->input('password'); + if (strlen($username) < 6 || strlen($username) > 20) { + throw new BusinessException('账号必须为6~20位'); + } + if (strlen($password) < 6 || strlen($password) > 20) { + throw new BusinessException('密码必须为6~20位'); + } + $account = Account::where('username', $username)->first(); + if (!empty($account)) { + throw new BusinessException('账号已存在'); + } + $account = new Account(); + $account->salt = StringHelper::getRandomString(6); + $account->username = $username; + $account->password = md5($password . $account->salt); + $account->save(); + return $this->success(); + } + + public function login(RequestInterface $request) + { + $username = $request->input('username'); + $password = $request->input('password'); + $account = Account::where('username', $username)->first(); + if (empty($account)) { + throw new BusinessException('账号或密码错误'); + } + if (md5($password . $account->salt) != $account->password) { + throw new BusinessException('账号或密码错误'); + } + $userInfo = json_encode(['userId' => $account->account_id]); + $token = StringHelper::getRandomString(16); + Redis::set(RedisKey::getUserTokenKey($token), $userInfo); + Redis::expire(RedisKey::getUserTokenKey($token), 3600); + return $this->success(['token' => $token]); + } + + public function openAccount(RequestInterface $request) + { + $userInfo = $this->checkUser($request); + $requestLog = $this->createRequestLog(['userId' => 'ACT_' . $userInfo['userId']]); + $url = $this->userService->register($requestLog->getData(), $requestLog->app, $requestLog->request_token); + return $this->success(['url' => $url]); + } + + public function bindCard(RequestInterface $request) + { + $userInfo = $this->checkUser($request); + $requestLog = $this->createRequestLog(['userId' => 'ACT_' . $userInfo['userId']]); + $url = $this->userService->bindCard($requestLog->getData(), $requestLog->app, $requestLog->request_token); + return $this->success(['url' => $url]); + } + + public function transferPay(RequestInterface $request) + { + $userInfo = $this->checkUser($request); + $bankCard = BankCard::where('user_id', 'ACT_' . $userInfo['userId'])->first(); + $requestLog = $this->createRequestLog([ + 'userId' => 'ACT_' . $userInfo['userId'], + 'goodsName' => '充值', + 'agreementNo' => $bankCard->agreement_no, + 'notifyUrl' => 'http://www.baidu.com', + 'returnUrl' => 'http://www.baidu.com', + 'amount' => intval($request->input('amount') * 100), + 'outOrderNo' => time() . rand(1000, 9999), + 'validDate' => date('Y-m-d', time() + 12*3600), + 'marketInfo' => [ + 'amount' => 0, + 'remark' => 'test', + ], + 'splitInfoList' => [ + [ + 'splitUserId' => 'RLX1990', + 'sellerFlag' => 1, + 'splitAmount' => 100, + 'subOutOrderNo' => time() . rand(1000, 9999), + ] + ] + ]); + $acsNo = $this->paymentService->transferPay($requestLog->getData(), $requestLog->app, $requestLog->request_token); + return $this->success(['acsNo' => $acsNo]); + } +} diff --git a/app/Controller/Payment/NotifyController.php b/app/Controller/Payment/NotifyController.php index 283aea1..731b022 100644 --- a/app/Controller/Payment/NotifyController.php +++ b/app/Controller/Payment/NotifyController.php @@ -46,7 +46,11 @@ class NotifyController extends AbstractController $userId = $requestLog->getDataValue('userId'); $appId = $requestLog->app_id; - $this->userService->rsyncUser($params['loginNo'], $appId, $userId); + $source = 0; + if ($requestLog->request_uri == '/register-user') { + $source = 1; + } + $this->userService->rsyncUser($params['loginNo'], $appId, $userId, $source); $result = $this->notify( $requestLog->getDataValue('notifyUrl'), diff --git a/app/Controller/Payment/PaymentController.php b/app/Controller/Payment/PaymentController.php index e0eadc4..196d54c 100644 --- a/app/Controller/Payment/PaymentController.php +++ b/app/Controller/Payment/PaymentController.php @@ -57,8 +57,8 @@ class PaymentController extends AbstractController public function unbindCard(RequestInterface $request) { [$app, $data, $token] = $this->parseReqest($request, UnbindCardRequest::class); - $data = $this->userService->unbindCard($data, $app); - return $this->success($data); + $this->userService->unbindCard($data, $app); + return $this->success(); } public function pwdForget(RequestInterface $request) @@ -108,8 +108,8 @@ class PaymentController extends AbstractController public function refundApply(RequestInterface $request) { [$app, $data, $token] = $this->parseReqest($request, UnbindCardRequest::class); - $data = $this->paymentService->refundApply($data, $app); - return $this->success($data); + $this->paymentService->refundApply($data, $app); + return $this->success(); } public function refundConfirm(RequestInterface $request) @@ -122,8 +122,8 @@ class PaymentController extends AbstractController public function refundCancel(RequestInterface $request) { [$app, $data, $token] = $this->parseReqest($request, UnbindCardRequest::class); - $data = $this->paymentService->refundApply($data, $app); - return $this->success($data); + $this->paymentService->refundApply($data, $app); + return $this->success(); } public function queryBindCards(RequestInterface $request) diff --git a/app/Helper/RedisKey.php b/app/Helper/RedisKey.php index 2dd0b6d..926a1d5 100644 --- a/app/Helper/RedisKey.php +++ b/app/Helper/RedisKey.php @@ -29,4 +29,8 @@ class RedisKey public static function getRequestTokenKey($token) { return 'request_token:' . $token; } + + public static function getUserTokenKey($token) { + return 'user_token:' . $token; + } } \ No newline at end of file diff --git a/app/Model/Account.php b/app/Model/Account.php new file mode 100644 index 0000000..76e0a38 --- /dev/null +++ b/app/Model/Account.php @@ -0,0 +1,10 @@ +save(); $platformAccount = User::getPlatformAccount(); - $fee = $platformAccount ? floor($order->amount * 0.007) : 0; + $feeRate = 0.007; + if ($user->source == 1) { + $feeRate = 0.0023; + } + $fee = $platformAccount ? floor($order->amount * $feeRate) : 0; if ($fee <= 0) { $fee = 1; } diff --git a/app/Service/UserService.php b/app/Service/UserService.php index 913c0ac..a5bb041 100644 --- a/app/Service/UserService.php +++ b/app/Service/UserService.php @@ -41,13 +41,13 @@ class UserService extends AbstractService return $url; } - public function rsyncUser($memberId, $appId, $userId) { + public function rsyncUser($memberId, $appId, $userId, $source = 0) { $baofu = new Baofu(); $userInfo = $baofu->queryCustomerInfo($memberId); - return $this->saveUser($userInfo, $appId, $userId); + return $this->saveUser($userInfo, $appId, $userId, $source); } - public function saveUser($userInfo, $appId, $userId) { + public function saveUser($userInfo, $appId, $userId, $source = 0) { $memberId = $userInfo['loginNo'] ?? $userInfo['loginMobile']; $user = User::where('app_id', $appId)->where('member_id', $memberId)->first(); if ($user) { @@ -62,6 +62,7 @@ class UserService extends AbstractService $user->certificate_no = $userInfo['certificateNo'] ?? ''; $user->user_type = $userInfo['customerType']; $user->apply_no = $userInfo['applyNo'] ?? ''; + $user->source = $source; } else { $user = new User(); $user->user_id = $userId; @@ -78,6 +79,7 @@ class UserService extends AbstractService $user->certificate_no = $userInfo['certificateNo'] ?? ''; $user->user_type = $userInfo['customerType']; $user->apply_no = $userInfo['applyNo'] ?? ''; + $user->source = $source; } $user->save(); return $user; diff --git a/config/routes.php b/config/routes.php index dc71d41..ac97e3a 100644 --- a/config/routes.php +++ b/config/routes.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Controller\Payment\AccountController; use App\Controller\Payment\NotifyController; use App\Controller\Payment\PaymentController; use App\Controller\Payment\ReturnController; @@ -36,6 +37,14 @@ Router::addGroup('/payment',function () { Router::post('/query-sign-entrust', [PaymentController::class, 'querySignEntrust']); }, ['middleware' => [\App\Middleware\RequestLogMiddleware::class, \App\Middleware\AppAuthMiddleWare::class]]); +Router::addGroup('/account',function () { + Router::post('/login', [AccountController::class, 'login']); + Router::post('/register', [AccountController::class, 'register']); + Router::post('/open-account', [AccountController::class, 'openAccount']); + Router::post('/bind-card', [AccountController::class, 'bindCard']); + Router::post('/transfer-pay', [AccountController::class, 'transferPay']); +}, []); + Router::addGroup('/notify',function () { Router::addRoute(['GET', 'POST'], '/register/{token}', [NotifyController::class, 'register']); Router::addRoute(['GET', 'POST'], '/bind-card/{token}', [NotifyController::class, 'bindCard']); diff --git a/public/account.html b/public/account.html new file mode 100644 index 0000000..bedfa49 --- /dev/null +++ b/public/account.html @@ -0,0 +1,119 @@ + + +
+ + + +