PayTaro.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. $result = $curl->response;
  21. if (!$result) {
  22. abort(500, '网络异常');
  23. }
  24. if ($curl->error) {
  25. $errors = (array)$result->errors;
  26. abort(500, $errors[array_keys($errors)[0]][0]);
  27. }
  28. $curl->close();
  29. if (!isset($result->data->trade_no)) {
  30. abort(500, '接口请求失败');
  31. }
  32. return $result->data->pay_url;
  33. }
  34. public function verify($params)
  35. {
  36. $sign = $params['sign'];
  37. unset($params['sign']);
  38. ksort($params);
  39. reset($params);
  40. $str = http_build_query($params) . $this->appSecret;
  41. if ($sign !== md5($str)) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. }