PayUtils.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace common\pay\eagle;
  3. class PayUtils
  4. {
  5. /**
  6. * @param array $data
  7. * @param string $secretKey
  8. * @return string
  9. */
  10. public static function makeSign($data, $secretKey)
  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, '&') . "&key={$secretKey}";
  25. file_put_contents('190903.txt',$str);
  26. file_put_contents('190903md5.txt',strtoupper(md5($str)));
  27. return strtoupper(md5($str));
  28. }
  29. /**
  30. * @param array $data 参数
  31. * @param string $secretKey
  32. * @return bool
  33. */
  34. public static function checkSign($data, $secretKey)
  35. {
  36. $sign = isset($data['sign']) ? $data['sign'] : null;
  37. if ($sign == null) {
  38. return false;
  39. }
  40. return self::makeSign($data, $secretKey) === $sign;
  41. }
  42. /**
  43. * 发送post请求
  44. * @param string $url 请求地址
  45. * @param array $post_data post键值对数据
  46. * @return string
  47. */
  48. public static function sendPost($url, $post_data) {
  49. $postdata = http_build_query($post_data);
  50. $options = array(
  51. 'http' => array(
  52. 'method' => 'POST',
  53. 'header' => 'Content-type:application/x-www-form-urlencoded',
  54. 'content' => $postdata,
  55. 'timeout' => 15 * 60 // 超时时间(单位:s)
  56. )
  57. );
  58. $context = stream_context_create($options);
  59. $result = file_get_contents($url, false, $context);
  60. return $result;
  61. }
  62. }