1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-09 17:56:49 +02:00
gdk/class/Payjs.php

115 lines
2.8 KiB
PHP
Raw Normal View History

2020-01-25 12:06:43 +01:00
<?php
2020-02-06 11:51:43 +01:00
class GDK_Payjs
2020-01-25 12:06:43 +01:00
{
private $mchid;
private $key;
private $api_url_native;
private $api_url_cashier;
private $api_url_mweb;
2020-01-25 12:06:43 +01:00
private $api_url_check;
public function __construct($config = null)
{
2021-10-10 15:48:53 +02:00
if (!$config) {
exit('config needed');
}
2020-01-25 12:06:43 +01:00
$this->mchid = $config['mchid'];
2021-10-10 15:48:53 +02:00
$this->key = $config['key'];
$api_url = $config['api_url'] ?? 'https://payjs.cn/api/';
$this->api_url_native = $api_url.'native';
$this->api_url_cashier = $api_url.'cashier';
$this->api_url_mweb = $api_url.'mweb';
$this->api_url_close = $api_url.'close';
$this->api_url_check = $api_url.'check';
2020-01-25 12:06:43 +01:00
}
// 扫码支付
public function native(array $data)
{
$this->url = $this->api_url_native;
2021-10-10 15:48:53 +02:00
2020-01-25 12:06:43 +01:00
return $this->post($data);
}
2021-10-10 15:48:53 +02:00
// MWEB(H5) 支付
public function mweb(array $data)
{
$this->url = $this->api_url_mweb;
2021-10-10 15:48:53 +02:00
return $this->post($data);
}
2021-10-10 15:48:53 +02:00
2020-01-25 12:06:43 +01:00
// 收银台模式
public function cashier(array $data)
{
$this->url = $this->api_url_cashier;
2021-10-10 15:48:53 +02:00
$data = $this->sign($data);
return $this->url.'?'.http_build_query($data);
2020-01-25 12:06:43 +01:00
}
// 检查订单
public function check($payjs_order_id)
{
$this->url = $this->api_url_check;
2021-10-10 15:48:53 +02:00
$data = ['payjs_order_id' => $payjs_order_id];
2020-01-25 12:06:43 +01:00
return $this->post($data);
}
// 异步通知接收
public function notify()
{
$data = $_POST;
2021-10-10 15:48:53 +02:00
if (true === $this->checkSign($data)) {
2020-01-25 12:06:43 +01:00
return $data;
}
2021-10-10 15:48:53 +02:00
exit('验签失败');
2020-01-25 12:06:43 +01:00
}
// 数据签名
public function sign(array $data)
{
$data['mchid'] = $this->mchid;
array_filter($data);
ksort($data);
2021-10-10 15:48:53 +02:00
$data['sign'] = strtoupper(md5(urldecode(http_build_query($data).'&key='.$this->key)));
2020-01-25 12:06:43 +01:00
return $data;
}
// 校验数据签名
public function checkSign($data)
{
$in_sign = $data['sign'];
unset($data['sign']);
array_filter($data);
ksort($data);
2021-10-10 15:48:53 +02:00
$sign = strtoupper(md5(urldecode(http_build_query($data).'&key='.$this->key)));
2020-01-25 12:06:43 +01:00
return $in_sign == $sign ? true : false;
}
// 数据发送
2021-10-10 15:48:53 +02:00
public function post($data)
{
$data = $this->sign($data);
2020-01-25 12:06:43 +01:00
$ch = curl_init();
2021-10-10 15:48:53 +02:00
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
2020-01-25 12:06:43 +01:00
curl_setopt($ch, CURLOPT_URL, $this->url);
2020-03-15 14:29:06 +01:00
curl_setopt($ch, CURLOPT_USERAGENT, 'HTTP CLIENT');
2020-01-25 12:06:43 +01:00
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);
2021-10-10 15:48:53 +02:00
2020-01-25 12:06:43 +01:00
return json_decode($rst, true);
}
}