PayUtils.php 1.7 KB

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