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

207 lines
6.5 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);
$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,
[
'userId' => $params['userId'],
]
);
$baofu->notifySuccess();
}
public function bindCard(RequestInterface $request)
{
[$token, $params] = $this->getTokenAndParams($request);
$baofu = new Baofu();
if (!$baofu->notifyVerify($params, 'card-bind')) {
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,
[
'bindCardFlag' => $bindCardFlag,
'userId' => $requestLog->getDataValue('userId'),
]
);
$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['tradeId'],
'third_order_no' => $params['orderId'],
'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,
[
'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);
$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;
}
public function testNotify(RequestInterface $request) {
return $this->success(['params' => $request->all()]);
}
}