PayUtils.php 980 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace common\pay\payplat;
  3. class PayUtils
  4. {
  5. /**
  6. * @param array $data
  7. * @param string $secretKey
  8. * @return string
  9. */
  10. public static function makeSign($data)
  11. {
  12. $str = '';
  13. ksort($data);
  14. foreach ($data as $key => $value) {
  15. if ($key == 'sign') {
  16. continue;
  17. }
  18. $value = strval($value);
  19. if ($value === '') { //参数为空不参与签名
  20. continue;
  21. }
  22. $str .= "{$key}={$value}&";
  23. }
  24. $str = rtrim($str, '&');
  25. return strtoupper(md5($str));
  26. }
  27. /**
  28. * @param array $data 参数
  29. * @param string $secretKey
  30. * @return bool
  31. */
  32. public static function checkSign($data)
  33. {
  34. $sign = isset($data['sign']) ? $data['sign'] : null;
  35. if ($sign == null) {
  36. return false;
  37. }
  38. return self::makeSign($data) === $sign;
  39. }
  40. }