PayTaro.php 1.2 KB

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