init
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\LingxingErp\Model\Api;
|
||||
|
||||
class Signer
|
||||
{
|
||||
public function sign(array $parameters, string $appId): string
|
||||
{
|
||||
ksort($parameters);
|
||||
$pairs = [];
|
||||
foreach ($parameters as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$value = $this->removeNullArrayValues($value);
|
||||
$pairs[] = $key . '=' . json_encode(
|
||||
$value,
|
||||
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
if (is_bool($value)) {
|
||||
$value = $value ? 'true' : 'false';
|
||||
} elseif ($value === null) {
|
||||
$value = 'null';
|
||||
}
|
||||
$pairs[] = $key . '=' . $value;
|
||||
}
|
||||
|
||||
// phpcs:ignore Magento2.Security.InsecureFunction.FoundWithAlternative -- Required by Lingxing's protocol.
|
||||
$md5 = strtoupper(md5(implode('&', $pairs)));
|
||||
$encrypted = openssl_encrypt($md5, 'AES-128-ECB', $appId, OPENSSL_RAW_DATA);
|
||||
if ($encrypted === false) {
|
||||
throw new \RuntimeException('Unable to generate Lingxing request signature.');
|
||||
}
|
||||
return base64_encode($encrypted);
|
||||
}
|
||||
|
||||
private function removeNullArrayValues(array $value): array
|
||||
{
|
||||
foreach ($value as $key => $item) {
|
||||
if (is_array($item)) {
|
||||
$value[$key] = $this->removeNullArrayValues($item);
|
||||
} elseif ($item === null) {
|
||||
unset($value[$key]);
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user