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