| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace ThirdParty\Urs\Webhook;
- use App\Module\ThirdParty\Models\ThirdPartyService as ServiceModel;
- use Illuminate\Http\Request;
- /**
- * URS余额检查Webhook处理器
- *
- * 专门处理余额检查相关的Webhook通知
- */
- class UrsCheckWebhook extends WebhookReceiver
- {
- /**
- * 构造函数
- *
- * @param string $serviceCode 服务代码
- * @param Request $request 请求对象
- * @param ServiceModel $service 服务配置对象
- */
- public function __construct(string $serviceCode, Request $request, ServiceModel $service)
- {
- parent::__construct($serviceCode, $request, $service);
- }
- /**
- * 处理余额检查通知
- *
- * @param string $action 操作类型(固定为check)
- * @param Request $request 请求对象
- * @return array
- * @throws \Exception
- */
- protected function handler(string $action, Request $request): array
- {
- // 验证操作类型
- if ($action !== 'check') {
- throw new \Exception("此处理器只能处理check操作,当前操作: {$action}");
- }
- // 验证必需字段
- $this->validateCheckRequest($request);
- // 处理余额检查通知
- return $this->processCheck($request);
- }
- /**
- * 验证余额检查请求
- *
- * @param Request $request 请求对象
- * @throws \Exception
- */
- protected function validateCheckRequest(Request $request): void
- {
- $requiredFields = [ 'user_id', 'amount' ];
- foreach ($requiredFields as $field) {
- if (!$request->has($field)) {
- throw new \Exception("缺少必需字段: {$field}");
- }
- }
- // 验证用户ID
- $userId = $request->input('user_id');
- if (!is_numeric($userId) || $userId <= 0) {
- throw new \Exception('用户ID格式无效');
- }
- }
- public function processCheck()
- {
- // 是否允许,钻石余额 ,本金总数,手续费总数,所需总数
- return [
- 'check' => false, // 是否允许
- 'diamond_balance' => 100, // 钻石余额
- 'principal_total' => 500, // 本金总数
- 'fee_total' => 50, // 手续费总数
- 'required_total' => 550, // 所需总数
- ];
- }
- }
|