<?php
$config = [
'appid' => git_get_option('git_eapay_id'), 'appkey' => git_get_option('git_eapay_secret'), ];
class Eapay
{
private $appid;
private $appkey;
private $api_url_cashier;
private $api_url_cashier_url;
public function __construct($config = null)
{
if (!$config) exit('config needed');
$this->appid = $config['appid'];
$this->appkey = $config['appkey'];
$api_url = isset($config['api_url']) ? $config['api_url'] : 'https://api.eapay.cc/v1/order/';
$this->api_url_cashier = $api_url . 'add';
$this->api_url_cashier_url = $api_url . 'pay/no/';
}
public function cashier(array $data)
{
$cashier_url = $this->api_url_cashier_url;
$data = $this->post($data);
if(!$data['status']) exit('requst fail');
$no = $data['data']['no'];
return $cashier_url . $no;
}
public function notify()
{
$data = $_POST;
if ($this->checkSign($data) === true) {
return $data;
} else {
exit('验签失败');
}
}
public function sign(array $data)
{
$data['appid'] = $this->appid;
array_filter($data);
ksort($data);
$data['sign'] = strtoupper(md5(urldecode(http_build_query($data) . '&key=' . $this->appkey)));
return $data;
}
public function checkSign($data)
{
$in_sign = $data['sign'];
unset($data['sign']);
array_filter($data);
ksort($data);
$sign = strtoupper(md5(urldecode(http_build_query($data) . '&key=' . $this->appkey)));
return $in_sign == $sign ? true : false;
}
public function post($data) {
$this->url = $this->api_url_cashier;
$data = $this->sign($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$rst = curl_exec($ch);
curl_close($ch);
return json_decode($rst, true);
}
}
?>