Functions.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay;
  4. use JetBrains\PhpStorm\Deprecated;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Yansongda\Artful\Contract\ConfigInterface;
  8. use Yansongda\Artful\Exception\ContainerException;
  9. use Yansongda\Artful\Exception\InvalidConfigException;
  10. use Yansongda\Artful\Exception\InvalidParamsException;
  11. use Yansongda\Artful\Exception\ServiceNotFoundException;
  12. use Yansongda\Artful\Plugin\AddPayloadBodyPlugin;
  13. use Yansongda\Artful\Plugin\ParserPlugin;
  14. use Yansongda\Artful\Plugin\StartPlugin;
  15. use Yansongda\Pay\Exception\DecryptException;
  16. use Yansongda\Pay\Exception\Exception;
  17. use Yansongda\Pay\Exception\InvalidSignException;
  18. use Yansongda\Pay\Plugin\Wechat\AddRadarPlugin;
  19. use Yansongda\Pay\Plugin\Wechat\ResponsePlugin;
  20. use Yansongda\Pay\Plugin\Wechat\V3\AddPayloadSignaturePlugin;
  21. use Yansongda\Pay\Plugin\Wechat\V3\WechatPublicCertsPlugin;
  22. use Yansongda\Pay\Provider\Alipay;
  23. use Yansongda\Pay\Provider\Douyin;
  24. use Yansongda\Pay\Provider\Jsb;
  25. use Yansongda\Pay\Provider\Unipay;
  26. use Yansongda\Pay\Provider\Wechat;
  27. use Yansongda\Supports\Collection;
  28. use function Yansongda\Artful\get_radar_body;
  29. use function Yansongda\Artful\get_radar_method;
  30. function get_tenant(array $params = []): string
  31. {
  32. return strval($params['_config'] ?? 'default');
  33. }
  34. function get_public_cert(string $key): string
  35. {
  36. return is_file($key) ? file_get_contents($key) : $key;
  37. }
  38. function get_private_cert(string $key): string
  39. {
  40. if (is_file($key)) {
  41. return file_get_contents($key);
  42. }
  43. return "-----BEGIN RSA PRIVATE KEY-----\n".
  44. wordwrap($key, 64, "\n", true).
  45. "\n-----END RSA PRIVATE KEY-----";
  46. }
  47. function get_radar_url(array $config, ?Collection $payload): ?string
  48. {
  49. return match ($config['mode'] ?? Pay::MODE_NORMAL) {
  50. Pay::MODE_SERVICE => $payload?->get('_service_url') ?? $payload?->get('_url') ?? null,
  51. Pay::MODE_SANDBOX => $payload?->get('_sandbox_url') ?? $payload?->get('_url') ?? null,
  52. default => $payload?->get('_url') ?? null,
  53. };
  54. }
  55. /**
  56. * @throws ContainerException
  57. * @throws ServiceNotFoundException
  58. */
  59. function get_provider_config(string $provider, array $params = []): array
  60. {
  61. /** @var ConfigInterface $config */
  62. $config = Pay::get(ConfigInterface::class);
  63. return $config->get($provider, [])[get_tenant($params)] ?? [];
  64. }
  65. /**
  66. * @throws ContainerException
  67. * @throws ServiceNotFoundException
  68. */
  69. #[Deprecated(reason: '自 v3.7.5 开始废弃', replacement: 'get_provider_config')]
  70. function get_alipay_config(array $params = []): array
  71. {
  72. $alipay = Pay::get(ConfigInterface::class)->get('alipay');
  73. return $alipay[get_tenant($params)] ?? [];
  74. }
  75. function get_alipay_url(array $config, ?Collection $payload): string
  76. {
  77. $url = get_radar_url($config, $payload) ?? '';
  78. if (str_starts_with($url, 'http')) {
  79. return $url;
  80. }
  81. return Alipay::URL[$config['mode'] ?? Pay::MODE_NORMAL];
  82. }
  83. /**
  84. * @throws InvalidConfigException
  85. * @throws InvalidSignException
  86. */
  87. function verify_alipay_sign(array $config, string $contents, string $sign): void
  88. {
  89. if (empty($sign)) {
  90. throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 验证支付宝签名失败-支付宝签名为空', func_get_args());
  91. }
  92. $public = $config['alipay_public_cert_path'] ?? null;
  93. if (empty($public)) {
  94. throw new InvalidConfigException(Exception::CONFIG_ALIPAY_INVALID, '配置异常: 缺少支付宝配置 -- [alipay_public_cert_path]');
  95. }
  96. $result = 1 === openssl_verify(
  97. $contents,
  98. base64_decode($sign),
  99. get_public_cert($public),
  100. OPENSSL_ALGO_SHA256
  101. );
  102. if (!$result) {
  103. throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证支付宝签名失败', func_get_args());
  104. }
  105. }
  106. /**
  107. * @throws ContainerException
  108. * @throws ServiceNotFoundException
  109. */
  110. #[Deprecated(reason: '自 v3.7.5 开始废弃', replacement: 'get_provider_config')]
  111. function get_wechat_config(array $params = []): array
  112. {
  113. $wechat = Pay::get(ConfigInterface::class)->get('wechat');
  114. return $wechat[get_tenant($params)] ?? [];
  115. }
  116. function get_wechat_method(?Collection $payload): string
  117. {
  118. return get_radar_method($payload) ?? 'POST';
  119. }
  120. /**
  121. * @throws InvalidParamsException
  122. */
  123. function get_wechat_url(array $config, ?Collection $payload): string
  124. {
  125. $url = get_radar_url($config, $payload);
  126. if (empty($url)) {
  127. throw new InvalidParamsException(Exception::PARAMS_WECHAT_URL_MISSING, '参数异常: 微信 `_url` 或 `_service_url` 参数缺失:你可能用错插件顺序,应该先使用 `业务插件`');
  128. }
  129. if (str_starts_with($url, 'http')) {
  130. return $url;
  131. }
  132. return Wechat::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url;
  133. }
  134. /**
  135. * @throws InvalidParamsException
  136. */
  137. function get_wechat_body(?Collection $payload): mixed
  138. {
  139. $body = get_radar_body($payload);
  140. if (is_null($body)) {
  141. throw new InvalidParamsException(Exception::PARAMS_WECHAT_BODY_MISSING, '参数异常: 微信 `_body` 参数缺失:你可能用错插件顺序,应该先使用 `AddPayloadBodyPlugin`');
  142. }
  143. return $body;
  144. }
  145. function get_wechat_type_key(array $params): string
  146. {
  147. $key = ($params['_type'] ?? 'mp').'_app_id';
  148. if ('app_app_id' === $key) {
  149. $key = 'app_id';
  150. }
  151. return $key;
  152. }
  153. /**
  154. * @throws InvalidConfigException
  155. */
  156. function get_wechat_sign(array $config, string $contents): string
  157. {
  158. $privateKey = $config['mch_secret_cert'] ?? null;
  159. if (empty($privateKey)) {
  160. throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_cert]');
  161. }
  162. $privateKey = get_private_cert($privateKey);
  163. openssl_sign($contents, $sign, $privateKey, 'sha256WithRSAEncryption');
  164. return base64_encode($sign);
  165. }
  166. /**
  167. * @throws InvalidConfigException
  168. */
  169. function get_wechat_sign_v2(array $config, array $payload, bool $upper = true): string
  170. {
  171. $key = $config['mch_secret_key_v2'] ?? null;
  172. if (empty($key)) {
  173. throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_key_v2]');
  174. }
  175. ksort($payload);
  176. $buff = '';
  177. foreach ($payload as $k => $v) {
  178. $buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k.'='.$v.'&' : '';
  179. }
  180. $sign = md5($buff.'key='.$key);
  181. return $upper ? strtoupper($sign) : $sign;
  182. }
  183. /**
  184. * @throws ContainerException
  185. * @throws DecryptException
  186. * @throws InvalidConfigException
  187. * @throws InvalidParamsException
  188. * @throws InvalidSignException
  189. * @throws ServiceNotFoundException
  190. */
  191. function verify_wechat_sign(ResponseInterface|ServerRequestInterface $message, array $params): void
  192. {
  193. if ($message instanceof ServerRequestInterface && 'localhost' === $message->getUri()->getHost()) {
  194. return;
  195. }
  196. $wechatSerial = $message->getHeaderLine('Wechatpay-Serial');
  197. $timestamp = $message->getHeaderLine('Wechatpay-Timestamp');
  198. $random = $message->getHeaderLine('Wechatpay-Nonce');
  199. $sign = $message->getHeaderLine('Wechatpay-Signature');
  200. $body = (string) $message->getBody();
  201. $content = $timestamp."\n".$random."\n".$body."\n";
  202. $public = get_provider_config('wechat', $params)['wechat_public_cert_path'][$wechatSerial] ?? null;
  203. if (empty($sign)) {
  204. throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 微信签名为空', ['headers' => $message->getHeaders(), 'body' => $body]);
  205. }
  206. $public = get_public_cert(
  207. empty($public) ? reload_wechat_public_certs($params, $wechatSerial) : $public
  208. );
  209. $result = 1 === openssl_verify(
  210. $content,
  211. base64_decode($sign),
  212. $public,
  213. 'sha256WithRSAEncryption'
  214. );
  215. if (!$result) {
  216. throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证微信签名失败', ['headers' => $message->getHeaders(), 'body' => $body]);
  217. }
  218. }
  219. /**
  220. * @throws InvalidConfigException
  221. * @throws InvalidSignException
  222. */
  223. function verify_wechat_sign_v2(array $config, array $destination): void
  224. {
  225. $sign = $destination['sign'] ?? null;
  226. if (empty($sign)) {
  227. throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 微信签名为空', $destination);
  228. }
  229. $key = $config['mch_secret_key_v2'] ?? null;
  230. if (empty($key)) {
  231. throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_key_v2]');
  232. }
  233. if (get_wechat_sign_v2($config, $destination) !== $sign) {
  234. throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证微信签名失败', $destination);
  235. }
  236. }
  237. function encrypt_wechat_contents(string $contents, string $publicKey): ?string
  238. {
  239. if (openssl_public_encrypt($contents, $encrypted, get_public_cert($publicKey), OPENSSL_PKCS1_OAEP_PADDING)) {
  240. return base64_encode($encrypted);
  241. }
  242. return null;
  243. }
  244. function decrypt_wechat_contents(string $encrypted, array $config): ?string
  245. {
  246. if (openssl_private_decrypt(base64_decode($encrypted), $decrypted, get_private_cert($config['mch_secret_cert'] ?? ''), OPENSSL_PKCS1_OAEP_PADDING)) {
  247. return $decrypted;
  248. }
  249. return null;
  250. }
  251. /**
  252. * @throws ContainerException
  253. * @throws DecryptException
  254. * @throws InvalidConfigException
  255. * @throws InvalidParamsException
  256. * @throws ServiceNotFoundException
  257. */
  258. function reload_wechat_public_certs(array $params, ?string $serialNo = null): string
  259. {
  260. $data = Pay::wechat()->pay(
  261. [StartPlugin::class, WechatPublicCertsPlugin::class, AddPayloadBodyPlugin::class, AddPayloadSignaturePlugin::class, AddRadarPlugin::class, ResponsePlugin::class, ParserPlugin::class],
  262. $params
  263. )->get('data', []);
  264. $wechatConfig = get_provider_config('wechat', $params);
  265. foreach ($data as $item) {
  266. $certs[$item['serial_no']] = decrypt_wechat_resource($item['encrypt_certificate'], $wechatConfig)['ciphertext'] ?? '';
  267. }
  268. Pay::get(ConfigInterface::class)->set(
  269. 'wechat.'.get_tenant($params).'.wechat_public_cert_path',
  270. ((array) ($wechatConfig['wechat_public_cert_path'] ?? [])) + ($certs ?? []),
  271. );
  272. if (!is_null($serialNo) && empty($certs[$serialNo])) {
  273. throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 获取微信 wechat_public_cert_path 配置失败');
  274. }
  275. return $certs[$serialNo] ?? '';
  276. }
  277. /**
  278. * @throws ContainerException
  279. * @throws DecryptException
  280. * @throws InvalidConfigException
  281. * @throws InvalidParamsException
  282. * @throws ServiceNotFoundException
  283. */
  284. function get_wechat_public_certs(array $params = [], ?string $path = null): void
  285. {
  286. reload_wechat_public_certs($params);
  287. $config = get_provider_config('wechat', $params);
  288. if (empty($path)) {
  289. var_dump($config['wechat_public_cert_path']);
  290. return;
  291. }
  292. foreach ($config['wechat_public_cert_path'] as $serialNo => $cert) {
  293. file_put_contents($path.'/'.$serialNo.'.crt', $cert);
  294. }
  295. }
  296. /**
  297. * @throws InvalidConfigException
  298. * @throws DecryptException
  299. */
  300. function decrypt_wechat_resource(array $resource, array $config): array
  301. {
  302. $ciphertext = base64_decode($resource['ciphertext'] ?? '');
  303. $secret = $config['mch_secret_key'] ?? null;
  304. if (strlen($ciphertext) <= Wechat::AUTH_TAG_LENGTH_BYTE) {
  305. throw new DecryptException(Exception::DECRYPT_WECHAT_CIPHERTEXT_PARAMS_INVALID, '加解密异常: ciphertext 位数过短');
  306. }
  307. if (is_null($secret) || Wechat::MCH_SECRET_KEY_LENGTH_BYTE != strlen($secret)) {
  308. throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mch_secret_key]');
  309. }
  310. $resource['ciphertext'] = match ($resource['algorithm'] ?? '') {
  311. 'AEAD_AES_256_GCM' => decrypt_wechat_resource_aes_256_gcm($ciphertext, $secret, $resource['nonce'] ?? '', $resource['associated_data'] ?? ''),
  312. default => throw new DecryptException(Exception::DECRYPT_WECHAT_DECRYPTED_METHOD_INVALID, '加解密异常: algorithm 不支持'),
  313. };
  314. return $resource;
  315. }
  316. /**
  317. * @throws DecryptException
  318. */
  319. function decrypt_wechat_resource_aes_256_gcm(string $ciphertext, string $secret, string $nonce, string $associatedData): array|string
  320. {
  321. $decrypted = openssl_decrypt(
  322. substr($ciphertext, 0, -Wechat::AUTH_TAG_LENGTH_BYTE),
  323. 'aes-256-gcm',
  324. $secret,
  325. OPENSSL_RAW_DATA,
  326. $nonce,
  327. substr($ciphertext, -Wechat::AUTH_TAG_LENGTH_BYTE),
  328. $associatedData
  329. );
  330. if (false === $decrypted) {
  331. throw new DecryptException(Exception::DECRYPT_WECHAT_ENCRYPTED_DATA_INVALID, '加解密异常: 解密失败,请检查微信 mch_secret_key 是否正确');
  332. }
  333. if ('certificate' !== $associatedData) {
  334. $decrypted = json_decode($decrypted, true);
  335. if (JSON_ERROR_NONE !== json_last_error()) {
  336. throw new DecryptException(Exception::DECRYPT_WECHAT_ENCRYPTED_DATA_INVALID, '加解密异常: 待解密数据非正常数据');
  337. }
  338. }
  339. return $decrypted;
  340. }
  341. /**
  342. * @throws ContainerException
  343. * @throws DecryptException
  344. * @throws InvalidConfigException
  345. * @throws InvalidParamsException
  346. * @throws ServiceNotFoundException
  347. */
  348. function get_wechat_serial_no(array $params): string
  349. {
  350. if (!empty($params['_serial_no'])) {
  351. return $params['_serial_no'];
  352. }
  353. $config = get_provider_config('wechat', $params);
  354. if (empty($config['wechat_public_cert_path'])) {
  355. reload_wechat_public_certs($params);
  356. $config = get_provider_config('wechat', $params);
  357. }
  358. mt_srand();
  359. return strval(array_rand($config['wechat_public_cert_path']));
  360. }
  361. /**
  362. * @throws InvalidParamsException
  363. */
  364. function get_wechat_public_key(array $config, string $serialNo): string
  365. {
  366. $publicKey = $config['wechat_public_cert_path'][$serialNo] ?? null;
  367. if (empty($publicKey)) {
  368. throw new InvalidParamsException(Exception::PARAMS_WECHAT_SERIAL_NOT_FOUND, '参数异常: 微信公钥序列号为找到 -'.$serialNo);
  369. }
  370. return $publicKey;
  371. }
  372. /**
  373. * @throws InvalidConfigException
  374. */
  375. function get_wechat_miniprogram_pay_sign(array $config, string $url, string $payload): string
  376. {
  377. if (empty($config['mini_app_key_virtual_pay'])) {
  378. throw new InvalidConfigException(Exception::CONFIG_WECHAT_INVALID, '配置异常: 缺少微信配置 -- [mini_app_key_virtual_pay]');
  379. }
  380. return hash_hmac('sha256', $url.'&'.$payload, $config['mini_app_key_virtual_pay']);
  381. }
  382. function get_wechat_miniprogram_user_sign(string $sessionKey, string $payload): string
  383. {
  384. return hash_hmac('sha256', $payload, $sessionKey);
  385. }
  386. /**
  387. * @throws ContainerException
  388. * @throws ServiceNotFoundException
  389. */
  390. #[Deprecated(reason: '自 v3.7.5 开始废弃', replacement: 'get_provider_config')]
  391. function get_unipay_config(array $params = []): array
  392. {
  393. $unipay = Pay::get(ConfigInterface::class)->get('unipay');
  394. return $unipay[get_tenant($params)] ?? [];
  395. }
  396. /**
  397. * @throws InvalidConfigException
  398. * @throws InvalidSignException
  399. */
  400. function verify_unipay_sign(array $config, string $contents, string $sign, ?string $signPublicKeyCert = null): void
  401. {
  402. if (empty($sign)) {
  403. throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 银联签名为空', func_get_args());
  404. }
  405. if (empty($signPublicKeyCert) && empty($public = $config['unipay_public_cert_path'] ?? null)) {
  406. throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [unipay_public_cert_path]');
  407. }
  408. $result = 1 === openssl_verify(
  409. hash('sha256', $contents),
  410. base64_decode($sign),
  411. get_public_cert($signPublicKeyCert ?? $public ?? ''),
  412. 'sha256'
  413. );
  414. if (!$result) {
  415. throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证银联签名失败', func_get_args());
  416. }
  417. }
  418. /**
  419. * @throws InvalidParamsException
  420. */
  421. function get_unipay_url(array $config, ?Collection $payload): string
  422. {
  423. $url = get_radar_url($config, $payload);
  424. if (empty($url)) {
  425. throw new InvalidParamsException(Exception::PARAMS_UNIPAY_URL_MISSING, '参数异常: 银联 `_url` 参数缺失:你可能用错插件顺序,应该先使用 `业务插件`');
  426. }
  427. if (str_starts_with($url, 'http')) {
  428. return $url;
  429. }
  430. return Unipay::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url;
  431. }
  432. /**
  433. * @throws InvalidParamsException
  434. */
  435. function get_unipay_body(?Collection $payload): string
  436. {
  437. $body = get_radar_body($payload);
  438. if (is_null($body)) {
  439. throw new InvalidParamsException(Exception::PARAMS_UNIPAY_BODY_MISSING, '参数异常: 银联 `_body` 参数缺失:你可能用错插件顺序,应该先使用 `AddPayloadBodyPlugin`');
  440. }
  441. return $body;
  442. }
  443. /**
  444. * @throws InvalidConfigException
  445. */
  446. function get_unipay_sign_qra(array $config, array $payload): string
  447. {
  448. $key = $config['mch_secret_key'] ?? null;
  449. if (empty($key)) {
  450. throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [mch_secret_key]');
  451. }
  452. ksort($payload);
  453. $buff = '';
  454. foreach ($payload as $k => $v) {
  455. $buff .= ('sign' != $k && '' != $v && !is_array($v)) ? $k.'='.$v.'&' : '';
  456. }
  457. return strtoupper(md5($buff.'key='.$key));
  458. }
  459. /**
  460. * @throws InvalidConfigException
  461. * @throws InvalidSignException
  462. */
  463. function verify_unipay_sign_qra(array $config, array $destination): void
  464. {
  465. $sign = $destination['sign'] ?? null;
  466. if (empty($sign)) {
  467. throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 银联签名为空', $destination);
  468. }
  469. $key = $config['mch_secret_key'] ?? null;
  470. if (empty($key)) {
  471. throw new InvalidConfigException(Exception::CONFIG_UNIPAY_INVALID, '配置异常: 缺少银联配置 -- [mch_secret_key]');
  472. }
  473. if (get_unipay_sign_qra($config, $destination) !== $sign) {
  474. throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证银联签名失败', $destination);
  475. }
  476. }
  477. function get_jsb_url(array $config, ?Collection $payload): string
  478. {
  479. $url = get_radar_url($config, $payload) ?? '';
  480. if (str_starts_with($url, 'http')) {
  481. return $url;
  482. }
  483. return Jsb::URL[$config['mode'] ?? Pay::MODE_NORMAL];
  484. }
  485. /**
  486. * @throws InvalidConfigException
  487. * @throws InvalidSignException
  488. */
  489. function verify_jsb_sign(array $config, string $content, string $sign): void
  490. {
  491. if (empty($sign)) {
  492. throw new InvalidSignException(Exception::SIGN_EMPTY, '签名异常: 江苏银行签名为空', func_get_args());
  493. }
  494. $publicCert = $config['jsb_public_cert_path'] ?? null;
  495. if (empty($publicCert)) {
  496. throw new InvalidConfigException(Exception::CONFIG_JSB_INVALID, '配置异常: 缺少配置参数 -- [jsb_public_cert_path]');
  497. }
  498. $result = 1 === openssl_verify(
  499. $content,
  500. base64_decode($sign),
  501. get_public_cert($publicCert)
  502. );
  503. if (!$result) {
  504. throw new InvalidSignException(Exception::SIGN_ERROR, '签名异常: 验证江苏银行签名失败', func_get_args());
  505. }
  506. }
  507. /**
  508. * @throws InvalidParamsException
  509. */
  510. function get_douyin_url(array $config, ?Collection $payload): string
  511. {
  512. $url = get_radar_url($config, $payload);
  513. if (empty($url)) {
  514. throw new InvalidParamsException(Exception::PARAMS_DOUYIN_URL_MISSING, '参数异常: 抖音 `_url` 参数缺失:你可能用错插件顺序,应该先使用 `业务插件`');
  515. }
  516. if (str_starts_with($url, 'http')) {
  517. return $url;
  518. }
  519. return Douyin::URL[$config['mode'] ?? Pay::MODE_NORMAL].$url;
  520. }