diff --git a/app/Command/JinlingCommand.php b/app/Command/JinlingCommand.php index 776c561..fe6a7e5 100644 --- a/app/Command/JinlingCommand.php +++ b/app/Command/JinlingCommand.php @@ -8,8 +8,10 @@ use App\Helper\Efps\Api; use App\Helper\Signer; use App\Helper\StringHelper; use App\Request\BindCardRequest; +use App\Request\PaymentQueryRequest; use App\Request\ProtocolPayConfirmRequest; use App\Request\ProtocolPayPreRequest; +use App\Request\RefundQueryRequest; use App\Request\RegisterRequest; use App\Service\AppService; use App\Service\MerchantService; @@ -49,7 +51,9 @@ class JinlingCommand extends HyperfCommand // $this->comfirmBindCard(); // $this->prepay(); // $this->confirmPay(); - $this->refund(); + // $this->refund(); + // $this->refundQuery(); + $this->paymentQuery(); return; /*$ret = Api::certifiedPayment([ 'outTradeNo' => time() . rand(1000, 9999), @@ -277,4 +281,40 @@ class JinlingCommand extends HyperfCommand $params = $request->getData(); var_dump($params); } + + public function refundQuery() + { + $params = [ + 'app_id' => '202304270000004', + 'timestamp' => time(), + 'nonce_str' => StringHelper::getRandomString(32), + 'data' => json_encode([ + 'outRefundNo' => '16853743952501', + ]), + ]; + $sign = \App\Helper\Platform\Signer::sign($params, 'lSHKbuFngCXHN8Ue1s8QHAAzPvOL3u9O'); + $params['sign'] = $sign; + var_dump($params); + $request = new RefundQueryRequest($params); + $params = $request->getData(); + var_dump($params); + } + + public function paymentQuery() + { + $params = [ + 'app_id' => '202304270000004', + 'timestamp' => time(), + 'nonce_str' => StringHelper::getRandomString(32), + 'data' => json_encode([ + 'outTradeNo' => '16853732799380', + ]), + ]; + $sign = \App\Helper\Platform\Signer::sign($params, 'lSHKbuFngCXHN8Ue1s8QHAAzPvOL3u9O'); + $params['sign'] = $sign; + var_dump($params); + $request = new PaymentQueryRequest($params); + $params = $request->getData(); + var_dump($params); + } } diff --git a/app/Controller/Payment/NotifyController.php b/app/Controller/Payment/NotifyController.php index 94cf45f..01e3b36 100644 --- a/app/Controller/Payment/NotifyController.php +++ b/app/Controller/Payment/NotifyController.php @@ -4,19 +4,24 @@ 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\Model\App; use App\Model\Order; use App\Model\RefundOrder; +use App\Service\PaymentService; use Hyperf\HttpServer\Contract\RequestInterface; class NotifyController extends AbstractController { - public function __construct() + private PaymentService $paymentService; + + public function __construct(PaymentService $paymentService) { + $this->paymentService = $paymentService; } public function payment(RequestInterface $request) @@ -56,20 +61,10 @@ class NotifyController extends AbstractController ]; } - if ($params['payState'] === '00') { - $order->status = Order::STATUS_PAYED; - $order->fee = $params['procedureFee'] ?: 0; - $order->pay_order_no = $params['transactionNo'] ?: ''; - $order->channel_order_no = $params['channelOrder'] ?: ''; - $order->payed_at = date('Y-m-d H:i:s'); - $order->save(); - } elseif ($params['payState'] === '01') { - $order->status = Order::STATUS_FAILED; - $order->error_code = '01'; - $order->error_msg = '处理失败'; - $order->payed_at = date('Y-m-d H:i:s'); - $order->save(); - } + $params['returnCode'] = '0000'; + $params['returnMsg'] = '成功'; + $result = new Result($params); + $this->paymentService->handlePayResult($result, $order); $result = $this->notify($order->notify_url, $app, [ 'outTradeNo' => $params['outTradeNo'] ?: '', @@ -121,23 +116,18 @@ class NotifyController extends AbstractController 'returnMsg' => '处理成功' ]; } - if ($params['payState'] === '00') { - $order->status = RefundOrder::STATUS_REFUND_SUCCESS; - $order->fee = $params['procedureFee'] ?: 0; - $order->refunded_at = date('Y-m-d H:i:s'); - $order->save(); - } elseif ($params['payState'] === '01') { - $order->status = RefundOrder::STATUS_REFUND_FAILED; - $order->error_code = '01'; - $order->error_msg = '处理失败'; - $order->refunded_at = date('Y-m-d H:i:s'); - $order->save(); - } + + $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' => $params['payState'], + 'payState' => $refundState, 'amount' => $params['amount'] ?: 0, 'refundAmount' => $params['refundAmount'] ?: 0, ]); diff --git a/app/Controller/Payment/PayController.php b/app/Controller/Payment/PayController.php index c91c533..d9a2daf 100644 --- a/app/Controller/Payment/PayController.php +++ b/app/Controller/Payment/PayController.php @@ -10,6 +10,7 @@ use App\Model\Order; use App\Model\RefundOrder; use App\Request\BindCardConfirmRequest; use App\Request\BindCardRequest; +use App\Request\PaymentQueryRequest; use App\Request\ProtocolPayConfirmRequest; use App\Request\ProtocolPayPreRequest; use App\Request\RefundQueryRequest; @@ -141,6 +142,13 @@ class PayController extends AbstractController throw new BusinessException($result->getMessage()); } + $order = RefundOrder::where('out_refund_order_no', $result->get('outRefundNo')) + ->where('status', RefundOrder::STATUS_APPLY_SUCCESS) + ->first(); + if ($order) { + $this->paymentService->handleRefundResult($result, $order); + } + return $this->success( $result->getData([ 'outRefundNo', @@ -151,4 +159,30 @@ class PayController extends AbstractController ]) ); } + + public function paymentQuery(RequestInterface $request) { + $req = new PaymentQueryRequest($request->all()); + $data = $req->getData(); + $result = Api::paymentQuery($data); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + + $order = Order::where('out_order_no', $result->get('outTradeNo')) + ->where('status', Order::STATUS_WAIT_PAY) + ->first(); + if ($order) { + $this->paymentService->handlePayResult($result, $order); + } + + return $this->success( + $result->getData([ + 'outTradeNo', + 'transactionNo', + 'payState', + 'procedureFee', + 'amount' + ]) + ); + } } diff --git a/app/Helper/Efps/Api.php b/app/Helper/Efps/Api.php index c91f337..76e0dab 100644 --- a/app/Helper/Efps/Api.php +++ b/app/Helper/Efps/Api.php @@ -217,7 +217,6 @@ class Api extends AbstractApi $remark = $params['remark'] ?? ''; $params = [ - // 'version' => '3.0', 'customerCode' => self::getConfig('customerCode'), 'outRefundNo' => $outRefundNo, 'outTradeNo' => $outTradeNo, @@ -237,7 +236,6 @@ class Api extends AbstractApi $outRefundNo = $params['outRefundNo'] ?? ''; $params = [ - // 'version' => '3.0', 'customerCode' => self::getConfig('customerCode'), 'outRefundNo' => $outRefundNo, 'nonceStr' => StringHelper::getRandomString(32), @@ -245,4 +243,18 @@ class Api extends AbstractApi $sign = Signer::sign(json_encode($params)); return self::request($uri, $params, $sign); } + + public static function paymentQuery($params) + { + $uri = '/api/txs/pay/PaymentQuery'; + $outTradeNo = $params['outTradeNo'] ?? ''; + + $params = [ + 'customerCode' => self::getConfig('customerCode'), + 'outTradeNo' => $outTradeNo, + 'nonceStr' => StringHelper::getRandomString(32), + ]; + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); + } } \ No newline at end of file diff --git a/app/Request/PaymentQueryRequest.php b/app/Request/PaymentQueryRequest.php new file mode 100644 index 0000000..8e3ca87 --- /dev/null +++ b/app/Request/PaymentQueryRequest.php @@ -0,0 +1,13 @@ +get('refundState') === '00') { + $order->status = RefundOrder::STATUS_REFUND_SUCCESS; + $order->fee = $result->get('procedureFee', 0); + $order->refunded_at = date('Y-m-d H:i:s'); + $order->save(); + } elseif ($result->get('refundState') === '01') { + $order->status = RefundOrder::STATUS_REFUND_FAILED; + $order->error_code = '01'; + $order->error_msg = '处理失败'; + $order->refunded_at = date('Y-m-d H:i:s'); + $order->save(); + } + } + private function generateOrderNo() { $now = time(); $key = RedisKey::getGenerateOrderNoKey($now); diff --git a/config/routes.php b/config/routes.php index 27efd56..98b343b 100644 --- a/config/routes.php +++ b/config/routes.php @@ -19,6 +19,7 @@ Router::addGroup('/payment',function () { Router::post('/protocol-pay-pre-request', [PayController::class, 'protocolPayPreRequest']); Router::post('/protocol-pay-confirm', [PayController::class, 'protocolPayConfirm']); Router::post('/refund-query', [PayController::class, 'refundQuery']); + Router::post('/payment-query', [PayController::class, 'paymentQuery']); }, ['middleware' => [\App\Middleware\RequestLogMiddleware::class]]); Router::addGroup('/notify',function () { diff --git a/payment.sql b/payment.sql index 929b33a..3513f83 100644 --- a/payment.sql +++ b/payment.sql @@ -105,7 +105,7 @@ CREATE TABLE `refund_orders` ( `error_msg` varchar(255) NOT NULL DEFAULT '' COMMENT '错误信息', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - KEY `idx_refundorderno` (`refund_order_no`) USING BTREE, + KEY `idx_outrefundorderno` (`out_refund_order_no`) USING BTREE, KEY `idx_createdat` (`created_at`) USING BTREE, PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC COMMENT='退款订单表'; \ No newline at end of file