|
|
|
<?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);
|
|
|
|
$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==";}');
|
|
|
|
if (!Signer::verify($data, $sign[0])) {
|
|
|
|
Log::info('paymentNotifyToOut data: ' . $data . ' sign: ' . serialize($sign));
|
|
|
|
return [
|
|
|
|
'returnCode' => '0001',
|
|
|
|
'returnMsg' => '签名验证失败'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$params = json_decode($data, true);
|
|
|
|
$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' => '处理成功'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$params['returnCode'] = '0000';
|
|
|
|
$params['returnMsg'] = '成功';
|
|
|
|
$result = new Result($params);
|
|
|
|
$this->paymentService->handlePayResult($result, $order);
|
|
|
|
|
|
|
|
$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' => '处理成功'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|