2020-01-12 19:48:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Library;
|
|
|
|
|
|
|
|
use \Curl\Curl;
|
|
|
|
|
|
|
|
class PayTaro
|
|
|
|
{
|
|
|
|
private $appId;
|
|
|
|
private $appSecret;
|
|
|
|
|
|
|
|
public function __construct($appId, $appSecret)
|
|
|
|
{
|
|
|
|
$this->appId = $appId;
|
|
|
|
$this->appSecret = $appSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function pay($params)
|
|
|
|
{
|
|
|
|
ksort($params);
|
2020-01-12 22:38:29 +08:00
|
|
|
$str = http_build_query($params) . $this->appSecret;
|
2020-01-12 19:48:13 +08:00
|
|
|
$params['sign'] = md5($str);
|
|
|
|
$curl = new Curl();
|
|
|
|
$curl->post('http://api.paytaro.com/v1/gateway/fetch', http_build_query($params));
|
|
|
|
if ($curl->error) {
|
|
|
|
abort(500, '接口请求失败');
|
|
|
|
}
|
2020-01-14 00:29:59 +08:00
|
|
|
$result = $curl->response;
|
2020-01-12 19:48:13 +08:00
|
|
|
$curl->close();
|
2020-01-14 00:29:59 +08:00
|
|
|
if (!isset($result->data->trade_no)) {
|
2020-01-12 19:48:13 +08:00
|
|
|
abort(500, '接口请求失败');
|
|
|
|
}
|
2020-01-14 00:29:59 +08:00
|
|
|
return $result->data->pay_url;
|
2020-01-12 19:48:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function verify($params)
|
|
|
|
{
|
|
|
|
$sign = $params['sign'];
|
|
|
|
unset($params['sign']);
|
|
|
|
ksort($params);
|
|
|
|
reset($params);
|
2020-01-14 19:45:19 +08:00
|
|
|
$str = http_build_query($params) . $this->appSecret;
|
2020-01-12 19:48:13 +08:00
|
|
|
if ($sign !== md5($str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|