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

164 lines
5.6 KiB
PHTML

2 years ago
<?php
declare(strict_types=1);
namespace App\Controller\Payment;
1 year ago
use App\Helper\Efps\Result;
1 year ago
use App\Helper\Efps\Signer;
use App\Helper\Log;
use App\Helper\Platform\Notification;
1 year ago
use App\Helper\Platform\Signer as PlatformSigner;
use App\Helper\StringHelper;
1 year ago
use App\Model\App;
use App\Model\Order;
use App\Model\RefundOrder;
1 year ago
use App\Service\PaymentService;
2 years ago
use Hyperf\HttpServer\Contract\RequestInterface;
2 years ago
class NotifyController extends AbstractController
{
2 years ago
1 year ago
private PaymentService $paymentService;
public function __construct(PaymentService $paymentService)
2 years ago
{
1 year ago
$this->paymentService = $paymentService;
2 years ago
}
1 year ago
public function payment(RequestInterface $request)
2 years ago
{
$params = $request->all();
1 year ago
Log::info('paymentNotifyToOut params:', $params);
$sign = $request->getHeader('x-efps-sign');
1 year ago
$data = json_encode($params, JSON_UNESCAPED_UNICODE);
1 year ago
if (!Signer::verify($data, $sign[0])) {
Log::info('paymentNotifyToOut data: ' . $data . ' sign: ' . serialize($sign));
1 year ago
return [
'returnCode' => '0001',
'returnMsg' => '签名验证失败'
];
}
1 year ago
$params = json_decode($data, true);
1 year ago
$outTradeNo = $params['outTradeNo'] ?: '';
$order = Order::where('order_no', $outTradeNo)->first();
1 year ago
if (!$order) {
1 year ago
Log::info('paymentNotifyToOut: ' . '订单号不存在[' . $outTradeNo . ']');
1 year ago
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
$app = App::where('app_id', $order->app_id)->first();
if (!$app) {
1 year ago
Log::info('paymentNotifyToOut: ' . 'APP不存在[' . $outTradeNo . '][' . $order->app_id . ']');
1 year ago
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
if ($order->status != Order::STATUS_WAIT_PAY) {
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
1 year ago
$params['returnCode'] = '0000';
$params['returnMsg'] = '成功';
$result = new Result($params);
$this->paymentService->handlePayResult($result, $order);
1 year ago
$result = $this->notify($order->notify_url, $app, [
1 year ago
'outOrderNo' => $order->out_order_no ?: '',
'orderNo' => $params['outTradeNo'] ?: '',
1 year ago
'payState' => $params['payState'],
'amount' => $params['amount'] ?: 0,
]);
1 year ago
Log::info('paymentNotifyToOut result: ' . '订单[' . $outTradeNo . '][' . $result . ']');
1 year ago
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
2 years ago
}
2 years ago
1 year ago
public function refund(RequestInterface $request)
{
2 years ago
$params = $request->all();
1 year ago
Log::info('refundNotifyToOut params:', $params);
$sign = $request->getHeader('x-efps-sign');
$data = json_encode($params);
1 year ago
if (!Signer::verify($data, $sign[0])) {
Log::info('refundNotifyToOut data: ' . $data . ' sign: ' . serialize($sign));
1 year ago
return [
'returnCode' => '0001',
'returnMsg' => '签名验证失败'
];
}
$outRefundNo = $params['outRefundNo'] ?: '';
$order = RefundOrder::where('out_refund_order_no', $outRefundNo)->first();
if (!$order) {
Log::info('refundNotifyToOut: ' . '订单号不存在[' . $outRefundNo . ']');
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
$app = App::where('app_id', $order->app_id)->first();
if (!$app) {
Log::info('refundNotifyToOut: ' . 'APP不存在[' . $outRefundNo . '][' . $order->app_id . ']');
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
if ($order->status != RefundOrder::STATUS_APPLY_SUCCESS) {
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
1 year ago
$params['returnCode'] = '0000';
$params['returnMsg'] = '成功';
$refundState = $params['refundState'] ?? null;
$refundState = is_null($refundState) ? ($params['payState'] ?? null) : $refundState;
$result = new Result($params);
$this->paymentService->handleRefundResult($result, $order);
1 year ago
$result = $this->notify($order->notify_url, $app, [
'outRefundNo' => $params['outRefundNo'] ?: '',
'transactionNo' => $params['transactionNo'] ?: '',
1 year ago
'payState' => $refundState,
1 year ago
'amount' => $params['amount'] ?: 0,
'refundAmount' => $params['refundAmount'] ?: 0,
]);
1 year ago
Log::info('paymentNotifyToOut result: ' . '订单[' . $outRefundNo . '][' . $result . ']');
1 year ago
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
protected function notify($url, $app, $data) {
1 year ago
if (empty($url)) {
return 'empty url';
}
1 year ago
$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;
2 years ago
}
2 years ago
}