You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Controller/Payment/NotifyController.php

205 lines
6.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\Payment;
use App\Helper\Baofu\Baofu;
use App\Helper\Log;
use App\Helper\Platform\Notification;
use App\Helper\Platform\Signer as PlatformSigner;
use App\Helper\StringHelper;
use App\Service\PaymentService;
use App\Service\RequestService;
use App\Service\UserService;
use Hyperf\HttpServer\Contract\RequestInterface;
class NotifyController extends AbstractController
{
private PaymentService $paymentService;
private UserService $userService;
private RequestService $requestService;
public function __construct(
PaymentService $paymentService,
UserService $userService,
RequestService $requestService
) {
$this->paymentService = $paymentService;
$this->userService = $userService;
$this->requestService = $requestService;
}
public function register(RequestInterface $request)
{
[$token, $params] = $this->getTokenAndParams($request);
$params = json_decode('{"orgNo":"1274207","merchantNo":"1274207","terminalNo":"82254","contractNo":"3177000505324724","loginNo":"1f32d22ff571ebc692bd50785c883ca8","signature":"362a896de0b5bbcc2e60b6aa77ea8969b4b24b8ee2474bcafd5b2274227dba16d87d071c5faeac058cad41cf7a6cacaa5be47f3c0dfdec834a2f8d5fd684d1c21b879a4b300d69951d2fc7cfc7cbafb8de25e60974c88c293f43bfb133beb89709ddea0e09dd29abb93c829dace39074dea458aa6c5c6e7a3b7e8e487465f9c9"}', true);
$token = '2bfb6ece9414e047909c55160e6aea21';
$baofu = new Baofu();
if (!$baofu->notifyVerify($params, 'register')) {
Log::info('registerNotifyVerifyFail: ', $params);
return $baofu->notifySuccess();
}
$requestLog = $this->requestService->getRequestLogByToken($token);
$userId = $requestLog->getDataValue('userId');
$appId = $requestLog->app_id;
$this->userService->rsyncUser($params['loginNo'], $appId, $userId);
$result = $this->notify(
$requestLog->getDataValue('notifyUrl'),
$requestLog->app,
[
'user_id' => $params['loginNo'],
]
);
$baofu->notifySuccess();
}
public function bindCard(RequestInterface $request)
{
[$token, $params] = $this->getTokenAndParams($request);
$baofu = new Baofu();
if (!$baofu->notifyVerify($params, 'bindCard')) {
Log::info('bindCardNotifyVerifyFail: ' . $params);
return $baofu->notifySuccess();
}
$requestLog = $this->requestService->getRequestLogByToken($token);
$bindCardFlag = $params['bindCardFlag'] && $params['bindCardFlag'] != 'false' ? true : false;
if ($bindCardFlag) {
$this->userService->rsyncBankCards($params['loginNo']);
}
$result = $this->notify(
$requestLog->getDataValue('notifyUrl'),
$requestLog->app,
[
'bind_card_flag' => $bindCardFlag,
'user_id' => $params['loginNo'],
]
);
$baofu->notifySuccess();
}
public function payment(RequestInterface $request)
{
[$token, $params] = $this->getTokenAndParams($request);
$baofu = new Baofu();
if (!$baofu->notifyVerify($params, 'payment')) {
Log::info('paymentNotifyVerifyFail: ' . $params);
return $baofu->notifySuccess();
}
$requestLog = $this->requestService->getRequestLogByToken($token);
$info = [
'member_id' => $params['loginId'],
'order_no' => $params['orderId'],
'third_order_no' => $params['tradeId'],
'status' => $params['orderStatus'],
'amount' => $params['orderMoney'],
'finished_at' => $params['finishTime'],
'error_message' => $params['errorMsg'],
'transaction_id' => $params['transactionId'],
'out_transaction_id' => $params['outTransactionId'],
];
$order = $this->paymentService->updateOrder($info);
if (empty($order)) {
return $baofu->notifySuccess();
}
$result = $this->notify(
$requestLog->getDataValue('notifyUrl'),
$requestLog->app,
[
'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);
$baofu = new Baofu();
if (!$baofu->notifyVerify($params, 'refund')) {
Log::info('refundNotifyVerifyFail: ' . $params);
return $baofu->notifySuccess();
}
$requestLog = $this->requestService->getRequestLogByToken($token);
$info = [
'refund_no' => $params['refundTradeId'],
'third_refund_no' => $params['refundOrderId'],
'status' => $params['refundStatus'],
'refund_amount' => $params['refundMoney'],
'refund_success_at' => $params['refundSuccTime'],
'error_message' => $params['errorMsg'],
'remark' => $params['remark'],
];
$order = $this->paymentService->updateRefund($info);
if (empty($order)) {
return $baofu->notifySuccess();
}
$result = $this->notify(
$requestLog->getDataValue('notifyUrl'),
$requestLog->app,
[
'status' => $order->status,
'userId' => $order->user_id,
'orderNo' => $order->order_no,
'outOrderNo' => $order->out_order_no,
'finishTime' => $order->finished_at,
]
);
$baofu->notifySuccess();
}
protected function getTokenAndParams(RequestInterface $request) {
$token = $request->route('token');
$params = $request->all();
Log::info('NOTIFY_DATA: ' . $request->getUri() . '[' . $token . ']:', $params);
return [$token, $params];
}
protected function notify($url, $app, $data)
{
if (empty($url)) {
return 'empty url';
}
$params = [
'app_id' => $app->app_id,
'nonce_str' => StringHelper::getRandomString(32),
'timestamp' => time(),
];
$params['data'] = json_encode($data);
$params['sign'] = PlatformSigner::sign($params, $app->app_key);
Log::info('notifyToOut params:', $params, 'platform');
Log::info('notifyToOut url:' . $url, [], 'platform');
$result = Notification::post($url, $params);
Log::info('notifyToOut response:' . $result, [], 'platform');
return $result;
}
}