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

166 lines
6.5 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');
$data = json_encode($params);
1 year ago
$data = '{"amount":5000,"procedureFee":23,"payerInfo":{"bankCode":"ICBC","cardType":"D","payerName":"\u9c8d\u4e91\u8f89","cardNo":"621226*********3143"},"payTime":"20230610180818","outTradeNo":"2023061018080300001","transactionNo":"32202306102881341174762","cashAmount":5000,"customerCode":"562276004021027","payState":"00","payerAmount":5000,"channelOrder":"2023061016326911986931600110901","nonceStr":"bda108e1ed1947f488be4b3b23bb6562"}';
$sign = unserialize('a:1:{i:0;s:344:"KKfrhc/4YNJFuphVMe8yo4sT+gQakxNtOfMqSqSeeMAHMp6684jm76p2yRog9pv7UE5WLJ8EEWajEMfLKYSBYIG9c2L3CPp+aJ+ot39SrAbq3BzCTiQvxC7Om6I7pOewU5m+ZVrlL/kLapi6lShx1vzZnGDzjJ0s+QFdY7sfm7NmqEgyZn1Q0L6IguT4/TmM0GNsB4XhO0IozXEa1EYzI//bS2eRtCLGh2TCND+4+mhdaBPsjD3F0g9C7hDU0cLPqOBd1Rzl22F0W8R0u5DzOp2ic4pdjVoWr7HV9ZS3nsnKANktLFQNSaQ3oH/dXyeY9kTfadKymPvDXavgCCmc/Q==";}');
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
$outOrderNo = $params['outTradeNo'] ?: '';
$order = Order::where('out_order_no', $outOrderNo)->first();
if (!$order) {
Log::info('paymentNotifyToOut: ' . '订单号不存在[' . $outOrderNo . ']');
return [
'returnCode' => '0000',
'returnMsg' => '处理成功'
];
}
$app = App::where('app_id', $order->app_id)->first();
if (!$app) {
Log::info('paymentNotifyToOut: ' . 'APP不存在[' . $outOrderNo . '][' . $order->app_id . ']');
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, [
'outTradeNo' => $params['outTradeNo'] ?: '',
'transactionNo' => $params['transactionNo'] ?: '',
'payState' => $params['payState'],
'amount' => $params['amount'] ?: 0,
]);
Log::info('paymentNotifyToOut result: ' . '订单[' . $outOrderNo . '][' . $$result . ']');
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,
]);
Log::info('paymentNotifyToOut result: ' . '订单[' . $outRefundNo . '][' . $$result . ']');
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
}