diff --git a/app/Controller/Payment/NotifyController.php b/app/Controller/Payment/NotifyController.php index af337b2..4f31bd7 100644 --- a/app/Controller/Payment/NotifyController.php +++ b/app/Controller/Payment/NotifyController.php @@ -49,7 +49,7 @@ class NotifyController extends AbstractController $this->userService->rsyncUser($params['loginNo'], $appId, $userId); $result = $this->notify( - $requestLog->getDataValue('notify_url'), + $requestLog->getDataValue('notifyUrl'), $requestLog->app, [ 'user_id' => $params['loginNo'], @@ -78,7 +78,7 @@ class NotifyController extends AbstractController } $result = $this->notify( - $requestLog->getDataValue('notify_url'), + $requestLog->getDataValue('notifyUrl'), $requestLog->app, [ 'bind_card_flag' => $bindCardFlag, @@ -119,7 +119,49 @@ class NotifyController extends AbstractController } $result = $this->notify( - $requestLog->getDataValue('notify_url'), + $requestLog->getDataValue('notifyUrl'), + $requestLog->app, + [ + 'status' => $order->status, + 'userId' => $order->user_id, + 'orderNo' => $order->order_no, + 'outOrderNo' => $order->out_order_no, + 'finishTime' => $order->finished_at, + ] + ); + + $baofu->notifySuccess(); + } + + public function refund(RequestInterface $request) + { + $token = $request->route('token'); + $requestLog = $this->requestService->getRequestLogByToken($token); + + $params = $request->all(); + Log::info('refundNotifyParams:', $params); + $baofu = new Baofu(); + if (!$baofu->notifyVerify($params, 'refund')) { + Log::info('refundNotifyVerifyFail: ' . $params); + return $baofu->notifySuccess(); + } + + $info = [ + 'refund_no' => $params['refundTradeId'], + 'third_refund_no' => $params['refundOrderId'], + 'status' => $params['refundStatus'], + 'refund_amount' => $params['refundMoney'], + 'refund_success_at' => $params['refundSuccTime'], + 'error_message' => $params['errorMsg'], + 'remark' => $params['remark'], + ]; + $order = $this->paymentService->updateRefund($info); + if (empty($order)) { + return $baofu->notifySuccess(); + } + + $result = $this->notify( + $requestLog->getDataValue('notifyUrl'), $requestLog->app, [ 'status' => $order->status, diff --git a/app/Controller/Payment/PayController.php b/app/Controller/Payment/PayController.php index 4c23166..40aa125 100644 --- a/app/Controller/Payment/PayController.php +++ b/app/Controller/Payment/PayController.php @@ -79,8 +79,24 @@ class PayController extends AbstractController return $this->success($data); } - public function refund(RequestInterface $request) { - $data = $this->paymentService->refund($request->all()); + public function refundApply(RequestInterface $request) + { + [$app, $data, $token] = $this->parseReqest($request, UnbindCardRequest::class); + $data = $this->paymentService->refundApply($app, $data); + return $this->success($data); + } + + public function refundConfirm(RequestInterface $request) + { + [$app, $data, $token] = $this->parseReqest($request, UnbindCardRequest::class); + $data = $this->paymentService->refundApply($app, $data); + return $this->success($data); + } + + public function refundCancel(RequestInterface $request) + { + [$app, $data, $token] = $this->parseReqest($request, UnbindCardRequest::class); + $data = $this->paymentService->refundApply($app, $data); return $this->success($data); } } diff --git a/app/Helper/Baofu/Baofu.php b/app/Helper/Baofu/Baofu.php index 62b81b0..9d34758 100644 --- a/app/Helper/Baofu/Baofu.php +++ b/app/Helper/Baofu/Baofu.php @@ -285,8 +285,11 @@ class Baofu $data['orgTradeId'] = $params['orgTradeId']; $data['refundMoney'] = $params['refundMoney']; $data['refundReason'] = $params['refundReason']; - $data['oldSubOutOrderNo'] = $params['oldSubOutOrderNo']; - // $data['refundSplitInfoList'] = json_encode($params['refundSplitInfoList']); + if ($params['refundSplitInfoList']) { + $data['refundSplitInfoList'] = json_encode($params['refundSplitInfoList']); + } else { + $data['oldSubOutOrderNo'] = $params['oldSubOutOrderNo']; + } $data['notifyUrl'] = NotifyList::getNotifyUrl('refund', $token); return $this->api('refund-request', $data); } diff --git a/app/Model/Order.php b/app/Model/Order.php index 3c4b332..c19d6d4 100644 --- a/app/Model/Order.php +++ b/app/Model/Order.php @@ -6,11 +6,11 @@ namespace App\Model; class Order extends Model { - public const STATUS_PREPARE = 1; - public const STATUS_WAIT_PAY = 2; - public const STATUS_APPLY_FAIL = 3; - public const STATUS_PAYED = 6; - public const STATUS_FAILED = 9; + public const STATUS_PREPARE = 'PREPARE'; + public const STATUS_INIT = 'INIT'; + public const STATUS_PROCESS = 'PROCESS'; + public const STATUS_SUCCESS = 'SUCCESS'; + public const STATUS_FAILED = 'FAILED'; protected $table = 'orders'; @@ -18,16 +18,6 @@ class Order extends Model 'org_split_info_list' => 'array', ]; - public function getStatusText() { - $statusMap = [ - self::STATUS_APPLY_FAIL => '申请失败', - self::STATUS_WAIT_PAY => '待支付', - self::STATUS_PAYED => '支付成功', - self::STATUS_FAILED => '支付失败', - ]; - return $statusMap[$this->status]; - } - public function getExpiresIn() { return strtotime($this->expired_at) - time(); } diff --git a/app/Model/Refund.php b/app/Model/Refund.php index 27c6dc4..aab0743 100644 --- a/app/Model/Refund.php +++ b/app/Model/Refund.php @@ -6,11 +6,23 @@ namespace App\Model; class Refund extends Model { - public const STATUS_PREPARE = 1; - public const STATUS_APPLY_SUCCESS = 2; - public const STATUS_APPLY_FAILED = 3; - public const STATUS_REFUND_SUCCESS = 8; - public const STATUS_REFUND_FAILED = 9; + public const STATUS_PREPARE = 'PREPARE'; + public const STATUS_INIT = 'INIT'; + public const STATUS_PROCESS = 'PROCESS'; + public const STATUS_SUCCESS = 'SUCCESS'; + public const STATUS_FAILED = 'FAILED'; protected $table = 'refunds'; + + public static $shortStatusAndStatusMap = [ + 'I' => self::STATUS_INIT, + 'P' => self::STATUS_PROCESS, + 'S' => self::STATUS_SUCCESS, + 'F' => self::STATUS_FAILED, + ]; + + public static function getStatusByShortStatus($shortStatus) + { + return self::$shortStatusAndStatusMap[$shortStatus] ?: null; + } } \ No newline at end of file diff --git a/app/Request/UnBindCardRequest.php b/app/Request/UnBindCardRequest.php deleted file mode 100644 index 7817cc1..0000000 --- a/app/Request/UnBindCardRequest.php +++ /dev/null @@ -1,11 +0,0 @@ -refund_no = StringHelper::generateOrderNo(StringHelper::ORDER_NO_TYPE_REFUND); $refund->refund_amount = $data['refundAmount']; $refund->refund_reason = $data['refundReason']; + $refund->notify_url = $data['notifyUrl'] ?? ''; $refund->org_refund_split_info_list = $data['refundSplitInfoList']; $refund->remark = $data['remark'] ?: ''; $refund->applied_at = date('Y-m-d H:i:s'); @@ -229,7 +230,7 @@ class PaymentService extends AbstractService 'refundReason' => $refund->refund_reason, 'refundSplitInfoList' => $refundSplitInfoList, ]); - $refund->status = 'F'; + $refund->status = 'APPLY_SUCCESS'; $refund->real_refund_amount = $result['refundAmount']; foreach ($result['refundSplitResultList'] as $refundSplitResult) { $subOrderNo = $refundSplitResult['orgSubOutOrderNo']; @@ -238,13 +239,26 @@ class PaymentService extends AbstractService } } catch (BasicException $e) { - $refund->status = 'A'; + $refund->status = 'APPLY_FAILED'; $refund->error_code = $e->getCode(); $refund->error_message = $e->getMessage(); throw $e; } $refund->save(); - + } + + public function updateRefund($params) + { + $refund = Refund::where('refund_no', $params['refund_no'])->where('status', 'APPLY_FAILED')->first(); + if (empty($refund)) { + return null; + } + $refund->third_refund_no = $params['third_refund_no']; + $refund->status = Refund::getStatusByShortStatus($params['status']); + $refund->refund_success_at = date('Y-m-d H:i:s', strtotime($params['refund_success_at'])); + $refund->error_message = $params['error_message']; + $refund->save(); + return $refund; } } diff --git a/config/routes.php b/config/routes.php index 43ea52d..92e3d9a 100644 --- a/config/routes.php +++ b/config/routes.php @@ -11,22 +11,23 @@ Router::get('/favicon.ico', function () { }); Router::addGroup('/payment',function () { + Router::post('/company-register', [PayController::class, 'companyRegister']); Router::post('/register', [PayController::class, 'register']); Router::post('/refund', [PayController::class, 'refund']); Router::post('/bind-card', [PayController::class, 'bindCard']); - Router::post('/bind-card-confirm', [PayController::class, 'bindCardConfirm']); - Router::post('/un-bind-card', [PayController::class, 'unBindCard']); - 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']); - Router::post('/query', [PayController::class, 'query']); - Router::post('/pay', [PayController::class, 'payment']); + Router::post('/unbind-card', [PayController::class, 'unbindCard']); + Router::post('/pwdForget', [PayController::class, 'pwdForget']); + Router::post('/pwdModify', [PayController::class, 'pwdModify']); + Router::post('/payment', [PayController::class, 'payment']); + Router::post('/refund-apply', [PayController::class, 'refundApply']); + Router::post('/refund-confirm', [PayController::class, 'refundConfirm']); + Router::post('/refund-cancel', [PayController::class, 'refundCancel']); }, ['middleware' => [\App\Middleware\RequestLogMiddleware::class, \App\Middleware\AppAuthMiddleWare::class]]); Router::addGroup('/notify',function () { + Router::addRoute(['GET', 'POST'], '/register', [NotifyController::class, 'register']); + Router::addRoute(['GET', 'POST'], '/bind-card', [NotifyController::class, 'bindCard']); Router::addRoute(['GET', 'POST'], '/payment', [NotifyController::class, 'payment']); Router::addRoute(['GET', 'POST'], '/refund', [NotifyController::class, 'refund']); - Router::addRoute(['GET', 'POST'], '/refund', [NotifyController::class, 'refund']); }); diff --git a/payment.sql b/payment.sql index 89d08d5..28dd4b0 100644 --- a/payment.sql +++ b/payment.sql @@ -168,6 +168,7 @@ CREATE TABLE `refunds` ( `out_order_no` varchar(32) NOT NULL, `refund_no` varchar(32) NOT NULL, `out_refund_no` varchar(32) NOT NULL, + `third_refund_no` varchar(32) NOT NULL, `refund_amount` int(11) NOT NULL DEFAULT 0, `refund_reason` varchar(255) NOT NULL DEFAULT '', `refund_type` varchar(16) NOT NULL DEFAULT '', @@ -179,7 +180,7 @@ CREATE TABLE `refunds` ( `applied_at` datetime DEFAULT NULL, `error_code` varchar(20) NOT NULL DEFAULT '', `error_message` varchar(120) NOT NULL DEFAULT '', - `finished_at` datetime DEFAULT NULL, + `refund_success_at` datetime DEFAULT NULL, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) USING BTREE,