You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
class YipaySigner
|
|
{
|
|
public static function canonical(array $payload)
|
|
{
|
|
unset($payload['sign'], $payload['sign_type']);
|
|
$values = [];
|
|
foreach ($payload as $key => $value) {
|
|
if ($value === '' || $value === null) {
|
|
continue;
|
|
}
|
|
if (!is_scalar($value)) {
|
|
throw new \InvalidArgumentException('易支付签名参数必须是标量');
|
|
}
|
|
$values[(string) $key] = (string) $value;
|
|
}
|
|
ksort($values, SORT_STRING);
|
|
|
|
$pairs = [];
|
|
foreach ($values as $key => $value) {
|
|
$pairs[] = $key . '=' . $value;
|
|
}
|
|
return implode('&', $pairs);
|
|
}
|
|
|
|
public static function sign(array $payload, $secret)
|
|
{
|
|
return strtolower(md5(self::canonical($payload) . (string) $secret));
|
|
}
|
|
|
|
public static function verify(array $payload, $secret)
|
|
{
|
|
$signature = strtolower(trim((string) ($payload['sign'] ?? '')));
|
|
if (!preg_match('/^[a-f0-9]{32}$/', $signature)) {
|
|
return false;
|
|
}
|
|
return hash_equals(self::sign($payload, $secret), $signature);
|
|
}
|
|
}
|