From b11f033185b4fdc6e960b45152d64efa3b410417 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 17 Feb 2025 18:00:15 +0800 Subject: [PATCH] feat : pay callback --- app/Model/Order.php | 11 +++ app/Model/UserThird.php | 11 +++ app/Service/Api/Pay/PlacePayService.php | 10 ++- .../Common/Pay/Wx/WxJsRechargeBaseService.php | 49 ++++++------- .../Pay/Wx/WxJsRechargeOrderService.php | 45 +++++++++++- .../Api/CheckOrderCallBackTrait.php | 27 +++++++ app/Service/ServiceTrait/Api/OrderTrait.php | 72 +++++++++++++++++++ 7 files changed, 199 insertions(+), 26 deletions(-) create mode 100644 app/Service/ServiceTrait/Api/CheckOrderCallBackTrait.php diff --git a/app/Model/Order.php b/app/Model/Order.php index 1647137..f71f52b 100644 --- a/app/Model/Order.php +++ b/app/Model/Order.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Model; +use Hyperf\Database\Concerns\BuildsQueries; +use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Collection; use Hyperf\DbConnection\Model\Model; @@ -63,4 +65,13 @@ class Order extends Model { return $this->find($id); } + + /** + * @param string $orderSno + * @return \Hyperf\Database\Model\Model|Builder|BuildsQueries|null + */ + public function getInfoByOrderSno(string $orderSno): \Hyperf\Database\Model\Model|Builder|BuildsQueries|null + { + return $this->where('order_sno',$orderSno)->first(); + } } diff --git a/app/Model/UserThird.php b/app/Model/UserThird.php index 1ed422a..82cfbc1 100644 --- a/app/Model/UserThird.php +++ b/app/Model/UserThird.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Common\ThirdCode; use Hyperf\Database\Model\Builder; use Hyperf\DbConnection\Model\Model; +use Hyperf\Tappable\HigherOrderTapProxy; /** * @property int $id @@ -43,4 +45,13 @@ class UserThird extends Model { return $this->where('open_id', $openId)->first(); } + + /** + * @param int $userId + * @return HigherOrderTapProxy|mixed|string + */ + public function getWxThirdInfoByUserId(int $userId) + { + return $this->where('user_id', $userId)->where('type',ThirdCode::WX_LOGIN)->value('open_id') ?? ''; + } } diff --git a/app/Service/Api/Pay/PlacePayService.php b/app/Service/Api/Pay/PlacePayService.php index 6a4aad3..6112188 100644 --- a/app/Service/Api/Pay/PlacePayService.php +++ b/app/Service/Api/Pay/PlacePayService.php @@ -17,6 +17,7 @@ use App\Constants\Common\PayCode; use App\Exception\ErrException; use App\Model\Order; use App\Model\PayOrder; +use App\Model\UserThird; use App\Service\Api\BaseService; use App\Service\Common\Pay\Wx\WxJsRechargeOrderService; use Hyperf\Di\Annotation\Inject; @@ -134,6 +135,12 @@ class PlacePayService extends BaseService return null; } + /** + * @var UserThird $userThirdModel + */ + #[Inject] + protected UserThird $userThirdModel; + /** * @return WxJsRechargeOrderService */ @@ -145,7 +152,8 @@ class PlacePayService extends BaseService throw new ErrException('充值异常'); } - $rechargeService->openId = '123'; + $rechargeService->openId = $this->userThirdModel->getWxThirdInfoByUserId($this->userId); + if (empty($rechargeService->openId)) throw new ErrException('该账户不可使用微信支付'); return $rechargeService; } diff --git a/app/Service/Common/Pay/Wx/WxJsRechargeBaseService.php b/app/Service/Common/Pay/Wx/WxJsRechargeBaseService.php index eaee1cd..d08fee8 100644 --- a/app/Service/Common/Pay/Wx/WxJsRechargeBaseService.php +++ b/app/Service/Common/Pay/Wx/WxJsRechargeBaseService.php @@ -11,6 +11,7 @@ use Hyperf\HttpServer\Contract\RequestInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; +use Yansongda\Artful\Exception\InvalidParamsException; use Yansongda\Artful\Rocket; use Yansongda\HyperfPay\Pay; use Yansongda\Supports\Collection; @@ -76,30 +77,30 @@ abstract class WxJsRechargeBaseService implements ThirdPayInterface // abstract public function setMchId(); -// /** -// * 获取返回数据 -// * @return void -// * @throws ContainerExceptionInterface -// * @throws NotFoundExceptionInterface -// * @throws ContainerException -// * @throws InvalidParamsException -// */ -// public function getWechatData(): void -// { -// $params = $this->request->all(); -// $this->log->callbackLog(__CLASS__.'微信支付回调'.json_encode($params)); -// -// $this->setConfig(); -// $result = $this->pay->wechat($this->config)->callback($params); -// $this->callbackData = $result['resource']['ciphertext']; -// -// if (empty($this->callbackData)) { -// $this->log->error(__CLASS__.'获取回调失败'.json_encode($params)); -// throw new ErrException('获取回调失败'); -// } -// -// $this->log->info(__CLASS__.'微信支付完成回调'.json_encode($this->callbackData)); -// } + /** + * 获取返回数据 + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface|InvalidParamsException + */ + public function getWechatData(): void + { + $this->setConfig(); + + try { + $this->callbackData = $this->ysdPay->wechat($this->config)->callback($this->request)->toArray()['resource']['ciphertext']; + }catch (Exception $e) { + $this->log->debug(__CLASS__.'微信支付回调解密失败'.json_encode($e->getMessage())); + throw new ErrException('微信支付回调解密失败'); + } + + if (!empty($this->callbackData)) { + $this->log->debug(__CLASS__.'获取回调失败'.json_encode($this->request)); + throw new ErrException('获取回调失败'); + } + + $this->log->info(__CLASS__.'微信支付完成回调'.json_encode($this->callbackData)); + } /** diff --git a/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php b/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php index 5b02212..9d84cb0 100644 --- a/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php +++ b/app/Service/Common/Pay/Wx/WxJsRechargeOrderService.php @@ -2,14 +2,57 @@ namespace App\Service\Common\Pay\Wx; +use App\Service\ServiceTrait\Api\CheckOrderCallBackTrait; +use App\Service\ServiceTrait\Api\OrderTrait; +use Hyperf\DbConnection\Db; use function Hyperf\Config\config; class WxJsRechargeOrderService extends WxJsRechargeBaseService { + use CheckOrderCallBackTrait, + OrderTrait; + + /** + * 订单 id + * @var int + */ + protected int $orderId; + + /** + * @var string + */ + protected string $orderNo; + + /** + * @var mixed + */ + protected mixed $orderInfo; + public function callBackHandle() { - // TODO: Implement callBackHandle() method. + $this-> getWechatData(); + + $this->checkWxCallBackOrder(); + + $this->checkGoodOrder(); + + Db::transaction(function (){ + $this->manageGoodOrder(); + + $this->manageWxOrder(); + }); + + //todo 发送订阅通知 +// $this->sendGainCoinMsg(); + + //发送mq通知报表结算 +// $this->sendShareExMq($this->orderInfo->mid,ShareExCode::RECHARGE_COIN, [ +// 'order_id' => $this->orderInfo->id, +// 'count' => $this->rechargeCoin, +// ]); + + return $this->returnSuccess(); } public function setNotify() diff --git a/app/Service/ServiceTrait/Api/CheckOrderCallBackTrait.php b/app/Service/ServiceTrait/Api/CheckOrderCallBackTrait.php new file mode 100644 index 0000000..cb41bc2 --- /dev/null +++ b/app/Service/ServiceTrait/Api/CheckOrderCallBackTrait.php @@ -0,0 +1,27 @@ +callbackData['return_code']) || + $this->callbackData['return_code'] != 'SUCCESS' || + !isset($this->callbackData['total_fee']) || + $this->callbackData['total_fee'] <= 0 || + !isset($this->callbackData['out_trade_no']) || + empty($this->callbackData['out_trade_no']) || + !isset($this->callbackData['mch_id']) || + empty($this->callbackData['mch_id']) || + !in_array($this->callbackData['trade_type'],['APP','JSAPI']) + ){ + throw new ErrException('此订单回调异常'); + } + + $this->orderNo = $this->callbackData['out_trade_no']; + } +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/Api/OrderTrait.php b/app/Service/ServiceTrait/Api/OrderTrait.php index db8d9e9..1960334 100644 --- a/app/Service/ServiceTrait/Api/OrderTrait.php +++ b/app/Service/ServiceTrait/Api/OrderTrait.php @@ -17,8 +17,11 @@ use App\Cache\Redis\RedisCache; use App\Constants\ApiCode; use App\Constants\Common\GoodCode; use App\Constants\Common\OrderCode; +use App\Constants\Common\PayCode; use App\Constants\ConfigCode; use App\Exception\ErrException; +use App\Model\Order; +use Exception; use Hyperf\Amqp\Producer; use Hyperf\Context\ApplicationContext; use Hyperf\Di\Annotation\Inject; @@ -295,4 +298,73 @@ trait OrderTrait $producer = ApplicationContext::getContainer()->get(Producer::class); $producer->produce($message); } + + /** + * @var Order + */ + #[Inject] + protected Order $orderModel; + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + protected function checkGoodOrder(): void + { + $this->orderInfo = $this->orderModel->getInfoByOrderSno($this->orderNo); + + if (empty($this->orderInfo)) { + $this->log->debug(__CLASS__.':订单不存在,订单号:'.$this->orderNo); + throw new ErrException('订单不存在'); + } + + if ($this->orderInfo->status != OrderCode::WAIT_PAY) { + $this->log->debug(__CLASS__.':订单已充值成功或取消,订单信息:'.json_encode($this->orderInfo->toArray())); + throw new ErrException('订单已充值成功'); + } + + if ($this->orderInfo->action != PayCode::WECHAT_PAY) { + $this->log->debug(__CLASS__.':订单充值渠道错误,订单信息:'.json_encode($this->orderInfo->toArray())); + throw new ErrException('订单充值渠道错误'); + } + } + + /** + * 操作微信订单 + * @return void + */ + protected function manageWxOrder(): void + { +// $this->orderInfo->status = OrderCode::STATUS_PAY; +// $this->orderInfo->isAdd = 1; +// $this->orderInfo->tradeno = $this->callbackData['transaction_id']; +//// $this->orderInfo->payTime = date('Y-m-d H:i:s',strtotime($this->callbackData['time_end'])); +// $this->orderInfo->payTime = strtotime($this->callbackData['success_time']); +// $this->orderInfo->jsonData = [ +// 'payData' => $this->callbackData, +// 'jsonData' => $this->orderInfo->jsonData +// ]; + + if (!$this->orderInfo->save()) { + $this->log->debug(__CLASS__.':Function:manageWxOrder:更新充值订单的状态失败:'.$this->orderInfo->id); + throw new ErrException('更新充值订单的状态失败'); + } + } + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + protected function manageGoodOrder(): void + { + try { + + }catch (Exception $e) { + $this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id); + throw new ErrException($e->getMessage()); + } + } + } \ No newline at end of file