PayTaro.php 1.1 KB

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