diff --git a/app/Controller/Recharge/RechargeController.php b/app/Controller/Recharge/RechargeController.php index ca5e130..c19c0b6 100644 --- a/app/Controller/Recharge/RechargeController.php +++ b/app/Controller/Recharge/RechargeController.php @@ -122,12 +122,44 @@ class RechargeController extends AbstractController ]); } + public function confirmBindCardAndPay(RequestInterface $request) { + [$app, $prePayLog] = $this->checkToken($request->input('stoken', '')); + + $amount = $request->input('amount'); + if ($prePayLog->amount == 0) { + $amount = $request->input('amount'); + if (empty($amount)) { + throw new BusinessException('请输入金额'); + } + if (!is_numeric($amount)) { + throw new BusinessException('请输入金额'); + } + if ($amount <= 0) { + throw new BusinessException('金额需大于0'); + } + } + + $outMemberId = $prePayLog->out_member_id; + $outOrderNo = $prePayLog->out_order_no; + $notifyUrl = $prePayLog->notify_url; + $amount = $prePayLog->amount > 0 ? $prePayLog->amount : intval($amount * 100); + + $smsNo = $request->input('smsNo'); + $smsCode = $request->input('smsCode'); + + $this->paymentService->protocolPayPreRequest($this->buildConfirmBindAndPay($outMemberId, $outOrderNo, $smsNo, $smsCode, $amount, $notifyUrl, $app)); + return $this->success([ + 'outMemberId' => $prePayLog->out_member_id, + 'bizData' => ['outOrderNo' => $prePayLog->out_order_no] + ]); + } + public function confirmPay(RequestInterface $request) { [$app, $prePayLog] = $this->checkToken($request->input('stoken', '')); $token = $request->input('token'); $protocol = $request->input('protocol'); $smsCode = $request->input('smsCode'); - $bizData = $this->paymentService->protocolPayConfirm($this->buildConfirmPayParams($token, $protocol, $smsCode, $app)); + $this->paymentService->protocolPayConfirm($this->buildConfirmPayParams($token, $protocol, $smsCode, $app)); return $this->success([ 'outMemberId' => $prePayLog->out_member_id, 'bizData' => ['outOrderNo' => $prePayLog->out_order_no] @@ -174,6 +206,26 @@ class RechargeController extends AbstractController return $params; } + private function buildConfirmBindAndPay($outMemberId, $outOrderNo, $smsNo, $smsCode, $amount, $notifyUrl, $app) + { + $params = [ + 'app_id' => $app->app_id, + 'timestamp' => time(), + 'nonce_str' => StringHelper::getRandomString(32), + 'data' => json_encode([ + 'outMemberId' => $outMemberId, + 'outOrderNo' => $outOrderNo, + 'payAmount' => $amount, + 'notifyUrl' => $notifyUrl, + 'smsNo' => $smsNo, + 'smsCode' => $smsCode, + ]), + ]; + $sign = \App\Helper\Platform\Signer::sign($params, $app->app_key); + $params['sign'] = $sign; + return $params; + } + private function buildConfirmPayParams($token, $protocol, $smsCode, $app) { $params = [ diff --git a/app/Service/PaymentService.php b/app/Service/PaymentService.php index fd0d929..e53e34d 100644 --- a/app/Service/PaymentService.php +++ b/app/Service/PaymentService.php @@ -110,10 +110,28 @@ class PaymentService extends AbstractService $order->save(); throw new BusinessException($result->getMessage()); } - $order->token = $result->get('token', ''); - $order->protocol = $result->get('protocol', ''); - $order->status = Order::STATUS_WAIT_PAY; - $order->save(); + if (is_null($result->get('payResult'))) { + $order->token = $result->get('token', ''); + $order->protocol = $result->get('protocol', ''); + $order->status = Order::STATUS_WAIT_PAY; + $order->save(); + } else { + $bankCard = BankCard::where('app_id', $app->app_id) + ->where('out_member_id', $data['outMemberId']) + ->where('sms_no', $data['smsNo']) + ->where('status', BankCard::STATUS_WAIT_CONFIRM) + ->first(); + + if ($bankCard) { + $bankCard->status = BankCard::STATUS_ACTIVE; + $bankCard->protocol = $result->get('protocol', ''); + $bankCard->save(); + } + + $order->protocol = $result->get('protocol', ''); + $order->status = Order::STATUS_WAIT_PAY; + $order->save(); + } return $result->getData(); } diff --git a/config/routes.php b/config/routes.php index 4c88573..5613b05 100644 --- a/config/routes.php +++ b/config/routes.php @@ -28,6 +28,7 @@ Router::addGroup('/payment',function () { Router::addGroup('/recharge',function () { Router::post('/recharge', [RechargeController::class, 'recharge']); Router::post('/confirm-bind-card', [RechargeController::class, 'confirmBindCard']); + Router::post('/confirm-bind-card-and-pay', [RechargeController::class, 'confirmBindCardAndPay']); Router::post('/confirm-pay', [RechargeController::class, 'confirmPay']); Router::post('/orders', [RechargeController::class, 'orders']); Router::post('/login', [RechargeController::class, 'login']); diff --git a/public/payment.html b/public/payment.html index 1d20723..c79d1e8 100644 --- a/public/payment.html +++ b/public/payment.html @@ -56,7 +56,7 @@ - + @@ -64,7 +64,7 @@ @@ -96,7 +96,8 @@ stoken: '', smsCode: '', smsNo: '', - outMemberId: '' + outMemberId: '', + amount: 1, }, payConfirmVisible: false, hasUser: false, @@ -196,6 +197,29 @@ }); console.log('submit!'); }, + bindConfirmAndPay() { + this.bindConfirmForm.amount = this.form.amount; + axios.post('/recharge/confirm-bind-card-and-pay', this.bindConfirmForm) + .then( (response) => { + console.log(response); + let result = response.data + if (result.code != 1000) { + return this.$message.error(response.data.message); + } + this.bindConfirmVisible = false; + this.$message.success('支付成功'); + this.$alert('请记住您的订单号:' + result.data.bizData.outOrderNo , '支付成功', { + confirmButtonText: '确定', + callback: action => { + } + }); + }) + .catch((error) => { + this.$message.error('请求错误'); + console.log(error); + }); + console.log('submit!'); + }, payConfirm() { axios.post('/recharge/confirm-pay', this.payConfirmForm) .then( (response) => {