MGate.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Library;
  3. use \Curl\Curl;
  4. class MGate
  5. {
  6. private $appId;
  7. private $appSecret;
  8. private $url;
  9. public function __construct($url, $appId, $appSecret)
  10. {
  11. $this->appId = $appId;
  12. $this->appSecret = $appSecret;
  13. $this->url = $url;
  14. }
  15. public function pay($params)
  16. {
  17. ksort($params);
  18. $str = http_build_query($params) . $this->appSecret;
  19. $params['sign'] = md5($str);
  20. $curl = new Curl();
  21. $curl->post($this->url . '/v1/gateway/fetch', http_build_query($params));
  22. $result = $curl->response;
  23. if (!$result) {
  24. abort(500, '网络异常');
  25. }
  26. if ($curl->error) {
  27. $errors = (array)$result->errors;
  28. abort(500, $errors[array_keys($errors)[0]][0]);
  29. }
  30. $curl->close();
  31. if (!isset($result->data->trade_no)) {
  32. abort(500, '接口请求失败');
  33. }
  34. return $result->data->pay_url;
  35. }
  36. public function verify($params)
  37. {
  38. $sign = $params['sign'];
  39. unset($params['sign']);
  40. ksort($params);
  41. reset($params);
  42. $str = http_build_query($params) . $this->appSecret;
  43. if ($sign !== md5($str)) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. }