diff --git a/app/Command/MineCommand.php b/app/Command/MineCommand.php index 8b3a070..3427777 100644 --- a/app/Command/MineCommand.php +++ b/app/Command/MineCommand.php @@ -45,7 +45,7 @@ class MineCommand extends HyperfCommand public function handle(): void { - $this->getBalance(); + $this->unionPay(); return; // $this->notify('http://ceshi-shop.hkcpex.com/index.php/pay/notify/baofu_f_register', $this->getApp(), ['userId' => '30684']); $this->notify('http://ceshi-shop.hkcpex.com/index.php/pay/notify/baofu_f_bind_card', $this->getApp(), ['userId' => '23121', 'bindCardFlag' => true]); @@ -89,7 +89,7 @@ class MineCommand extends HyperfCommand return $requestService->createRequestLog('/', $params); } - protected function getApp($appId = '202308070000001') { + protected function getApp($appId = '202308040000002') { return App::where('app_id', $appId)->first(); } @@ -329,4 +329,34 @@ class MineCommand extends HyperfCommand $result = $paymentService->transferPay($data, $this->getApp(), $this->getToken()); var_dump($result); } + + protected function unionPay() + { + /** + * @var PaymentService $paymentService + */ + $paymentService = $this->container->make(PaymentService::class); + $data = [ + 'userId' => 'ELF1990', + 'goodsName' => '测试商品', + 'agreementNo' => '312023081200002052866', + 'notifyUrl' => 'http://www.baidu.com', + 'returnUrl' => 'http://www.baidu.com', + 'amount' => 100, + 'outOrderNo' => time() . rand(1000, 9999), + 'validDate' => '2023-08-15', + 'payMethod' => 'unionPay', + 'payType' => 'ALIPAY_NATIVE', + 'splitInfoList' => [ + [ + 'splitUserId' => 'RLX1990', + 'sellerFlag' => 1, + 'splitAmount' => 100, + 'subOutOrderNo' => time() . rand(1000, 9999), + ] + ] + ]; + $result = $paymentService->payment($data, $this->getApp(), $this->getToken()); + var_dump($result); + } } \ No newline at end of file diff --git a/app/Controller/Payment/NotifyController.php b/app/Controller/Payment/NotifyController.php index 8e70c53..d4e77da 100644 --- a/app/Controller/Payment/NotifyController.php +++ b/app/Controller/Payment/NotifyController.php @@ -131,6 +131,47 @@ class NotifyController extends AbstractController $baofu->notifySuccess(); } + public function confirmAssurePortfolioPay(RequestInterface $request) + { + [$token, $params] = $this->getTokenAndParams($request); + + $baofu = new Baofu(); + if (!$baofu->notifyVerify($params, 'confirm-assure-portfolio-pay')) { + Log::info('confirmAssurePortfolioPayNotifyVerifyFail: ' . $params); + return $baofu->notifySuccess(); + } + + $requestLog = $this->requestService->getRequestLogByToken($token); + + $info = [ + 'member_id' => $params['loginId'], + 'order_no' => $params['tradeId'], + 'third_order_no' => $params['orderId'], + 'status' => $params['orderStatus'], + 'amount' => $params['orderMoney'], + 'finished_at' => $params['finishTime'], + ]; + $order = $this->paymentService->updateOrder($info, 'PROCESS'); + if (empty($order)) { + return $baofu->notifySuccess(); + } + + $result = $this->notify( + $requestLog->getDataValue('notifyUrl'), + $requestLog->app, + [ + 'amount' => $order->amount, + 'status' => $order->status, + 'userId' => $order->user_id, + 'orderNo' => $order->order_no, + 'outOrderNo' => $order->out_order_no, + 'finishTime' => $order->finished_at, + ] + ); + + $baofu->notifySuccess(); + } + public function refund(RequestInterface $request) { [$token, $params] = $this->getTokenAndParams($request); diff --git a/app/Helper/Baofu/Baofu.php b/app/Helper/Baofu/Baofu.php index ba19d8c..48b3f11 100644 --- a/app/Helper/Baofu/Baofu.php +++ b/app/Helper/Baofu/Baofu.php @@ -181,7 +181,7 @@ class Baofu return $this->api('cloud-b2c-assure-recharge', $data); } - public function payment($params, $type, $token) + public function payment($params, $token) { $dataContent = [ 'goodsName' => $params['goodsName'], @@ -255,7 +255,7 @@ class Baofu return $this->api('confirm-assure-pay', $data); } - public function confirmAssurePortfolioPay($params) + public function confirmAssurePortfolioPay($params, $token) { $dataContent = [ 'amount' => $params['amount'], @@ -267,7 +267,7 @@ class Baofu $data = []; $data['loginNo'] = $params['loginNo']; $data['dataContent'] = json_encode($dataContent); - $data['notifyUrl'] = $params['notifyUrl']; + $data['notifyUrl'] = NotifyList::getNotifyUrl('confirm-assure-portfolio-pay', $token); return $this->api('confirm-assure-portfoli-pay', $data); } diff --git a/app/Helper/Baofu/NotifyList.php b/app/Helper/Baofu/NotifyList.php index 228c331..4a907a1 100644 --- a/app/Helper/Baofu/NotifyList.php +++ b/app/Helper/Baofu/NotifyList.php @@ -48,6 +48,10 @@ class NotifyList 'description' => '自助交易确认分账', 'signParams' => 'orgNo|merchantNo|terminalNo|tradeId|subOutOrderNo|orderStatus|loginId|finishTime|confirmType', ], + 'confirm-assure-portfolio-pay' => [ + 'description' => '确认分账', + 'signParams' => 'orgNo|merchantNo|loginNo|terminalNo|requestDate|dataContent', + ], 'refund' => [ 'description' => '退款', 'signParams' => 'orgNo|merchantNo|terminalNo|refundOrderId|refundTradeId|refundMoney|refundStatus|refundSuccTime|refundType|errorMsg', diff --git a/app/Service/PaymentService.php b/app/Service/PaymentService.php index b6c8c5d..acd696b 100644 --- a/app/Service/PaymentService.php +++ b/app/Service/PaymentService.php @@ -37,11 +37,18 @@ class PaymentService extends AbstractService } $data['payType'] = $data['payType'] ?? 'CARD'; - [$order, $orderSplitInfos] = $this->createOrder($app, $data, $user, 'paymentSplit'); + $payMethod = $data['payMethod'] ?? 'paymentSplit'; + [$order, $orderSplitInfos] = $this->createOrder($app, $data, $user, $payMethod); $splitInfoList = $this->buildSplitInfoList($orderSplitInfos); - - return $this->paymentSplit($order, $user, $splitInfoList, $token); + + if ($payMethod == 'paymentSplit') { + return $this->paymentSplit($order, $user, $splitInfoList, $token); + } elseif($payMethod == 'unionPay') { + return $this->unionPay($order, $user, $splitInfoList, $token); + } else { + throw new BusinessException('payMethod不支持'); + } } public function transferPay(array $data, App $app, string $token) @@ -254,17 +261,17 @@ class PaymentService extends AbstractService return [$order, $orderSplitInfos]; } - public function updateOrder($params) { - $order = Order::where('order_no', $params['order_no'])->where('status', 'APPLY_SUCCESS')->first(); + public function updateOrder($params, $status = 'APPLY_SUCCESS') { + $order = Order::where('order_no', $params['order_no'])->where('status', $status)->first(); if (empty($order)) { return null; } $order->third_order_no = $params['third_order_no']; $order->status = $params['status']; $order->finished_at = date('Y-m-d H:i:s', strtotime($params['finished_at'])); - $order->error_message = $params['error_message']; - $order->transaction_id = $params['transaction_id']; - $order->out_transaction_id = $params['out_transaction_id']; + $order->error_message = $params['error_message'] ?? ''; + $order->transaction_id = $params['transaction_id'] ?? ''; + $order->out_transaction_id = $params['out_transaction_id'] ?? ''; $order->save(); return $order; } @@ -452,7 +459,6 @@ class PaymentService extends AbstractService 'assureConfirmSplitInfoList' => $confirmSplitInfoList, 'outOrderNo' => StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_CONFIRM_PAY), 'amount' => $data['amount'], - 'notifyUrl' => $data['notifyUrl'], ], $token); $order->status = 'CONFIRM_SUCCESS'; $order->save(); diff --git a/config/routes.php b/config/routes.php index 6219132..d746925 100644 --- a/config/routes.php +++ b/config/routes.php @@ -33,6 +33,7 @@ Router::addGroup('/notify',function () { Router::addRoute(['GET', 'POST'], '/payment/{token}', [NotifyController::class, 'payment']); Router::addRoute(['GET', 'POST'], '/refund/{token}', [NotifyController::class, 'refund']); Router::addRoute(['POST'], '/test-notify', [NotifyController::class, 'testNotify']); + Router::addRoute(['GET', 'POST'], '/confirm-assure-portfolio-pay', [NotifyController::class, 'confirmAssurePortfolioPay']); }); Router::addRoute(['GET', 'POST'], '/return/{token}', [ReturnController::class, 'go']);