v2board/library/PayTaro.php

50 lines
1.2 KiB
PHP
Raw Normal View History

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();
2020-01-25 23:04:52 +08:00
$curl->post('https://api.paytaro.com/v1/gateway/fetch', http_build_query($params));
2020-02-12 14:01:00 +08:00
$result = $curl->response;
2020-01-12 19:48:13 +08:00
if ($curl->error) {
2020-02-12 14:01:40 +08:00
$errors = (array)$result->errors;
abort(500, $errors[array_keys($errors)[0]][0]);
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;
}
}