授权确认优化
This commit is contained in:
@@ -182,27 +182,13 @@ class TransferAuthorizationService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 向微信发起授权申请
|
* 向微信发起授权申请
|
||||||
* @param bool $isRetry 是否为换号重试(防止递归)
|
* @param bool $isRetry 是否为重试(防止递归)
|
||||||
*/
|
*/
|
||||||
private static function requestAuthorization($user, string $authNo, string $displayName, EasyWeChatService $wechat, bool $renew, bool $isRetry = false): array
|
private static function requestAuthorization($user, string $authNo, string $displayName, EasyWeChatService $wechat, bool $renew, bool $isRetry = false): array
|
||||||
{
|
{
|
||||||
$userId = $user->id;
|
$userId = $user->id;
|
||||||
$res = $wechat->applyTransferAuthorization($authNo, $user->openid, $displayName);
|
$res = $wechat->applyTransferAuthorization($authNo, $user->openid, $displayName);
|
||||||
|
|
||||||
//单号已被占用(已受理/已终结/重入参数不一致):查单按微信侧实际状态处理
|
|
||||||
if (self::isApplyConflict($res)) {
|
|
||||||
$query = $wechat->queryTransferAuthorization($authNo, false);
|
|
||||||
$resolved = self::resolveByWechatState($userId, $authNo, $query, $wechat, self::getByUserId($userId));
|
|
||||||
if ($resolved !== null) {
|
|
||||||
return $resolved;
|
|
||||||
}
|
|
||||||
//原单已无法拉起授权页:已终结,或重入参数不一致导致该单号不可复用。
|
|
||||||
//换全新单号重新发起,保证用户一定能拿到 package;原未确认单 24 小时后自动关闭
|
|
||||||
if (!$isRetry) {
|
|
||||||
return self::requestAuthorization($user, self::generateAuthNoUnique($userId), $displayName, $wechat, true, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self::isQuantityLimit($res)) {
|
if (self::isQuantityLimit($res)) {
|
||||||
$record = self::getByUserId($userId);
|
$record = self::getByUserId($userId);
|
||||||
if ($record && $record->state === self::STATE_WAIT_USER_CONFIRM && !empty($record->package_info)) {
|
if ($record && $record->state === self::STATE_WAIT_USER_CONFIRM && !empty($record->package_info)) {
|
||||||
@@ -212,7 +198,23 @@ class TransferAuthorizationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (empty($res['package_info'])) {
|
if (empty($res['package_info'])) {
|
||||||
throw new \Exception($res['message'] ?? '发起免确认收款授权失败');
|
//发起失败(单号已被占用/重入参数不一致/系统异常等,不同版本文案不同,故不做关键字匹配):
|
||||||
|
//先查单按微信侧实际状态处理,而不是把原始错误抛给用户
|
||||||
|
$query = $wechat->queryTransferAuthorization($authNo, false);
|
||||||
|
$resolved = self::resolveByWechatState($userId, $authNo, $query, $wechat, self::getByUserId($userId));
|
||||||
|
if ($resolved !== null) {
|
||||||
|
return $resolved;
|
||||||
|
}
|
||||||
|
if (!$isRetry) {
|
||||||
|
//微信侧查无此单(本次发起未落单):用同一单号重试一次
|
||||||
|
if (self::isWechatNotFound($query)) {
|
||||||
|
return self::requestAuthorization($user, $authNo, $displayName, $wechat, $renew, true);
|
||||||
|
}
|
||||||
|
//单已存在但拿不到可拉起授权页的 package(已终结,或重入参数不一致导致该单号不可复用):
|
||||||
|
//换全新单号重新发起,保证用户一定能拿到 package;原未确认单 24 小时后自动关闭
|
||||||
|
return self::requestAuthorization($user, self::generateAuthNoUnique($userId), $displayName, $wechat, true, true);
|
||||||
|
}
|
||||||
|
throw new \Exception('发起免确认收款授权失败:' . ($res['message'] ?? '请稍后重试'));
|
||||||
}
|
}
|
||||||
|
|
||||||
self::saveAuthorization(
|
self::saveAuthorization(
|
||||||
@@ -284,17 +286,6 @@ class TransferAuthorizationService
|
|||||||
return mb_strpos($message, '授权数量达到限制') !== false || mb_strpos($message, '数量达到限制') !== false;
|
return mb_strpos($message, '授权数量达到限制') !== false || mb_strpos($message, '数量达到限制') !== false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function isApplyConflict(array $res): bool
|
|
||||||
{
|
|
||||||
$message = (string)($res['message'] ?? '');
|
|
||||||
|
|
||||||
//单号已被占用:原单已解除/已授权成功,或重复发起时请求参数与原单不一致(重入校验)
|
|
||||||
return $message !== '' && (
|
|
||||||
mb_strpos($message, '已解除或已授权成功') !== false
|
|
||||||
|| mb_strpos($message, '重入参数不一致') !== false
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存/更新授权记录
|
* 保存/更新授权记录
|
||||||
* @param int $userId
|
* @param int $userId
|
||||||
|
|||||||
Reference in New Issue
Block a user