Files
epay/plugins/ysepay/inc/YsepayResponse.php
T
opencode b7613ba082 部署仓库初始化:epay 部署配置、补丁与插件
- docker-compose.prod.yml:挂载 epay-ui.css/js/head.php 补丁到 staging(镜像升级不丢)
- docker/nginx.conf:补丁静态资源 no-cache,改版立即生效
- patches/:
  - test.php 修复 paytype JSON 对象导致测试支付页空白
  - epay-ui.css 后台全屏/侧栏加宽/导航两列布局覆盖
  - epay-ui.js 去除页面容器宽度限制 + 侧栏当前菜单自动滚动
  - head.php 资源 ?v= 版本号强制回源
  - functions.php is_public_url 白名单等既有补丁
- plugins/ 支付插件源码
- .gitignore 排除 .env 等敏感与临时文件
2026-10-21 13:05:20 +08:00

150 lines
3.2 KiB
PHP

<?php
class YsepayResponse
{
/**
* 响应签名节点名
*/
const SIGN_NODE = 'sign';
/**
* 响应数据节点后缀
*/
const RESPONSE_SUFFIX = '_response';
/**
* 原始响应
*
* @var string
*/
protected $raw;
/**
* 已解析的响应
*
* @var mixed
*/
protected $parsed;
/**
* 数据节点名称
*/
protected $nodeName;
/**
* 待验签数据
*/
protected $signData;
/**
* @param $raw 原始数据
* @param $apiName 接口名称
*/
public function __construct($raw, $apiName)
{
$this->raw = $raw;
$this->parsed = json_decode($raw, true);
if (!$this->parsed) {
$error = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error();
throw new \Exception('返回数据解析失败:'.$error);
}
$this->parseResponseData($apiName);
}
/**
* 获取原始响应的被签名数据,用于验证签名.
*
* @param $apiName
*/
protected function parseResponseData($apiName)
{
$nodeName = str_replace(".", "_", $apiName) . self::RESPONSE_SUFFIX;
$nodeIndex = strpos($this->raw, $nodeName);
if (!$nodeIndex) {
throw new \Exception('Response data not found');
}
$this->nodeName = $nodeName;
$signDataStartIndex = $nodeIndex + strlen($nodeName) + 2;
$signIndex = strrpos($this->raw, '"'.static::SIGN_NODE.'"');
$signDataEndIndex = $signIndex - 1;
$indexLen = $signDataEndIndex - $signDataStartIndex;
if ($indexLen < 0) {
$signIndex = strrpos($this->raw, "}");
$signDataEndIndex = $signIndex;
$indexLen = $signDataEndIndex - $signDataStartIndex;
}
$this->signData = substr($this->raw, $signDataStartIndex, $indexLen);
}
/**
* 获取待验签数据
*
* @return string
*/
public function getSignData()
{
return $this->signData;
}
/**
* 获取响应内的签名.
*
* @return string
*/
public function getSign()
{
if (isset($this->parsed[static::SIGN_NODE])) {
return $this->parsed[static::SIGN_NODE];
}
return null;
}
/**
* 获取响应内的数据.
*
* @param bool $assoc
*
* @return mixed|object
*/
public function getData($assoc = true)
{
if (!isset($this->parsed[$this->nodeName])){
return null;
}
$result = $this->parsed[$this->nodeName];
if ($assoc == false) {
$result = (object) ($result);
}
return $result;
}
/**
* 判断响应是否成功.
*
* @return bool
*/
public function isSuccess()
{
if (!isset($this->parsed[$this->nodeName])){
return false;
}
$data = $this->parsed[$this->nodeName];
return isset($data['code']) && $data['code'] == '10000';
}
/**
* 获取原始响应.
*
* @return string
*/
public function getRaw()
{
return $this->raw;
}
}