feat : pay callback

This commit is contained in:
2025-02-17 18:00:15 +08:00
parent ba2bb6874e
commit b11f033185
7 changed files with 199 additions and 26 deletions

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace App\Model; namespace App\Model;
use Hyperf\Database\Concerns\BuildsQueries;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection; use Hyperf\Database\Model\Collection;
use Hyperf\DbConnection\Model\Model; use Hyperf\DbConnection\Model\Model;
@@ -63,4 +65,13 @@ class Order extends Model
{ {
return $this->find($id); 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();
}
} }

View File

@@ -4,8 +4,10 @@ declare(strict_types=1);
namespace App\Model; namespace App\Model;
use App\Constants\Common\ThirdCode;
use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Model\Model; use Hyperf\DbConnection\Model\Model;
use Hyperf\Tappable\HigherOrderTapProxy;
/** /**
* @property int $id * @property int $id
@@ -43,4 +45,13 @@ class UserThird extends Model
{ {
return $this->where('open_id', $openId)->first(); 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') ?? '';
}
} }

View File

@@ -17,6 +17,7 @@ use App\Constants\Common\PayCode;
use App\Exception\ErrException; use App\Exception\ErrException;
use App\Model\Order; use App\Model\Order;
use App\Model\PayOrder; use App\Model\PayOrder;
use App\Model\UserThird;
use App\Service\Api\BaseService; use App\Service\Api\BaseService;
use App\Service\Common\Pay\Wx\WxJsRechargeOrderService; use App\Service\Common\Pay\Wx\WxJsRechargeOrderService;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@@ -134,6 +135,12 @@ class PlacePayService extends BaseService
return null; return null;
} }
/**
* @var UserThird $userThirdModel
*/
#[Inject]
protected UserThird $userThirdModel;
/** /**
* @return WxJsRechargeOrderService * @return WxJsRechargeOrderService
*/ */
@@ -145,7 +152,8 @@ class PlacePayService extends BaseService
throw new ErrException('充值异常'); throw new ErrException('充值异常');
} }
$rechargeService->openId = '123'; $rechargeService->openId = $this->userThirdModel->getWxThirdInfoByUserId($this->userId);
if (empty($rechargeService->openId)) throw new ErrException('该账户不可使用微信支付');
return $rechargeService; return $rechargeService;
} }

View File

@@ -11,6 +11,7 @@ use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Yansongda\Artful\Exception\InvalidParamsException;
use Yansongda\Artful\Rocket; use Yansongda\Artful\Rocket;
use Yansongda\HyperfPay\Pay; use Yansongda\HyperfPay\Pay;
use Yansongda\Supports\Collection; use Yansongda\Supports\Collection;
@@ -76,30 +77,30 @@ abstract class WxJsRechargeBaseService implements ThirdPayInterface
// abstract public function setMchId(); // abstract public function setMchId();
// /** /**
// * 获取返回数据 * 获取返回数据
// * @return void * @return void
// * @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
// * @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface|InvalidParamsException
// * @throws ContainerException */
// * @throws InvalidParamsException public function getWechatData(): void
// */ {
// public function getWechatData(): void $this->setConfig();
// {
// $params = $this->request->all(); try {
// $this->log->callbackLog(__CLASS__.'微信支付回调'.json_encode($params)); $this->callbackData = $this->ysdPay->wechat($this->config)->callback($this->request)->toArray()['resource']['ciphertext'];
// }catch (Exception $e) {
// $this->setConfig(); $this->log->debug(__CLASS__.'微信支付回调解密失败'.json_encode($e->getMessage()));
// $result = $this->pay->wechat($this->config)->callback($params); throw new ErrException('微信支付回调解密失败');
// $this->callbackData = $result['resource']['ciphertext']; }
//
// if (empty($this->callbackData)) { if (!empty($this->callbackData)) {
// $this->log->error(__CLASS__.'获取回调失败'.json_encode($params)); $this->log->debug(__CLASS__.'获取回调失败'.json_encode($this->request));
// throw new ErrException('获取回调失败'); throw new ErrException('获取回调失败');
// } }
//
// $this->log->info(__CLASS__.'微信支付完成回调'.json_encode($this->callbackData)); $this->log->info(__CLASS__.'微信支付完成回调'.json_encode($this->callbackData));
// } }
/** /**

View File

@@ -2,14 +2,57 @@
namespace App\Service\Common\Pay\Wx; 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; use function Hyperf\Config\config;
class WxJsRechargeOrderService extends WxJsRechargeBaseService 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() 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() public function setNotify()

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Service\ServiceTrait\Api;
use App\Exception\ErrException;
trait CheckOrderCallBackTrait
{
public function checkWxCallBackOrder(): void
{
if (
!isset($this->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'];
}
}

View File

@@ -17,8 +17,11 @@ use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode; use App\Constants\ApiCode;
use App\Constants\Common\GoodCode; use App\Constants\Common\GoodCode;
use App\Constants\Common\OrderCode; use App\Constants\Common\OrderCode;
use App\Constants\Common\PayCode;
use App\Constants\ConfigCode; use App\Constants\ConfigCode;
use App\Exception\ErrException; use App\Exception\ErrException;
use App\Model\Order;
use Exception;
use Hyperf\Amqp\Producer; use Hyperf\Amqp\Producer;
use Hyperf\Context\ApplicationContext; use Hyperf\Context\ApplicationContext;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
@@ -295,4 +298,73 @@ trait OrderTrait
$producer = ApplicationContext::getContainer()->get(Producer::class); $producer = ApplicationContext::getContainer()->get(Producer::class);
$producer->produce($message); $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());
}
}
} }