master
parent
e1c57150bd
commit
b47336085f
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Org\Kudian;
|
||||
|
||||
use Org\Kudian\Request\H5payRequest;
|
||||
use Org\Kudian\Request\Request;
|
||||
|
||||
class Api
|
||||
{
|
||||
private $client;
|
||||
|
||||
private static function getClient() {
|
||||
if (!self::$client) {
|
||||
self::$client = new Client();
|
||||
}
|
||||
return self::$client;
|
||||
}
|
||||
|
||||
public static function h5Pay($orderNumber, $amount, $notifyUrl, $backUrl, $payType, $body, $clientIp) {
|
||||
$request = new H5payRequest(Config::get('mch_id'), Config::get('secret_key'));
|
||||
$request->setContent([
|
||||
'fee' => $amount,
|
||||
'out_trade_no' => $orderNumber,
|
||||
'body' => $body,
|
||||
'client_ip' => $clientIp,
|
||||
'notify_url' => $notifyUrl,
|
||||
'payment' => $payType,
|
||||
'sence_info' => [
|
||||
'wap_url' => 'http://www.jianghuifa.cn',
|
||||
'wap_name' => '聚游',
|
||||
'return_url' => $backUrl,
|
||||
],
|
||||
]);
|
||||
return self::getClient()->request($request);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace Org\Kudian;
|
||||
|
||||
use Exception;
|
||||
use Org\Kudian\Request\Request;
|
||||
|
||||
class Client
|
||||
{
|
||||
public function request(Request $request): Response
|
||||
{
|
||||
$response = '';
|
||||
try {
|
||||
$response = $this->post($request->getUrl(), $request->getParams());
|
||||
} catch (Exception $e) {
|
||||
$response = json_encode([
|
||||
'code' => -999,
|
||||
'msg' => '网络错误',
|
||||
'sign_type' => 'MD5',
|
||||
'sign' => '',
|
||||
'result' => json_encode([]),
|
||||
]);
|
||||
}
|
||||
return new Response($response);
|
||||
}
|
||||
|
||||
public function post($url, $params): string
|
||||
{
|
||||
$curl = curl_init();
|
||||
if (stripos($url, 'https://') !== false){
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($curl, CURLOPT_POST, TRUE);
|
||||
$headers = ['Content-type: application/json;charset=utf-8'];
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Org\Kudian;
|
||||
|
||||
class Config
|
||||
{
|
||||
private static $params = [
|
||||
'mch_id' => 'KP10009694',
|
||||
'secret_key' => 'q5zZyyA071zS0SZR6xcz0gP0fFhtojqq',
|
||||
];
|
||||
|
||||
public static function get($key, $default = null)
|
||||
{
|
||||
return self::$params[$key] ?? $default;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Org\Kudian\Request;
|
||||
|
||||
class H5payRequest extends Request
|
||||
{
|
||||
protected $url = '/openapi/pay/kdh5pay';
|
||||
|
||||
public function setContent(array $content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace Org\Kudian\Request;
|
||||
|
||||
use Org\Kudian\Config;
|
||||
use Org\Kudian\Sign;
|
||||
|
||||
class Request
|
||||
{
|
||||
protected $url;
|
||||
|
||||
protected $mchId;
|
||||
protected $timestamp;
|
||||
protected $nonceStr;
|
||||
protected $sign;
|
||||
protected $signType;
|
||||
protected $content;
|
||||
protected $secretKey;
|
||||
|
||||
public function __construct($mchId, $secretKey)
|
||||
{
|
||||
$this->mchId = $mchId;
|
||||
$this->secretKey = $secretKey;
|
||||
$this->timestamp = time();
|
||||
$this->nonceStr = self::generateRandomString();
|
||||
$this->signType = 'MD5';
|
||||
}
|
||||
|
||||
public function setContent(array $content)
|
||||
{
|
||||
$this->content = json_encode($content);
|
||||
}
|
||||
|
||||
final public function reset()
|
||||
{
|
||||
$this->timestamp = time();
|
||||
$this->nonceStr = self::generateRandomString();
|
||||
}
|
||||
|
||||
final protected function generateRandomString($length = 16)
|
||||
{
|
||||
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
$string = '';
|
||||
for ( $i = 0; $i < $length; $i++ )
|
||||
$string .= $chars[mt_rand(0, strlen($chars) - 1)];
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function getParams() {
|
||||
$params = [
|
||||
'mch_id' => $this->mchId,
|
||||
'timestamp' => $this->timestamp,
|
||||
'nonce_str' => $this->nonceStr,
|
||||
'sign_type' => $this->signType,
|
||||
'content' => $this->content,
|
||||
];
|
||||
$params[Sign::SIGN_KEY] = Sign::generate($params, $this->secretKey);
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function getUrl() {
|
||||
return $this->url;
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace Org\Kudian;
|
||||
|
||||
class Response
|
||||
{
|
||||
protected $code;
|
||||
|
||||
protected $msg;
|
||||
|
||||
protected $result;
|
||||
|
||||
protected $sign;
|
||||
|
||||
protected $signType;
|
||||
|
||||
protected $data;
|
||||
|
||||
public function __construct($response)
|
||||
{
|
||||
$this->parse($response);
|
||||
}
|
||||
|
||||
private function parse($response)
|
||||
{
|
||||
$data = null;
|
||||
if (is_array($response)) {
|
||||
$data = $response;
|
||||
} else {
|
||||
$data = json_decode($response);
|
||||
}
|
||||
if (!$data) {
|
||||
$data = [
|
||||
'code' => -998,
|
||||
'msg' => '数据异常',
|
||||
'sign' => '',
|
||||
'sign_type' => 'MD5',
|
||||
'result' => json_encode([]),
|
||||
];
|
||||
}
|
||||
$this->data = $data;
|
||||
$result = $data['result'] ? json_decode($data['result']) : [];
|
||||
$this->result = $result;
|
||||
$this->code = $data['code'];
|
||||
$this->msg = $data['msg'];
|
||||
$this->sign = $data['sign'];
|
||||
$this->signType = $data['sign_type'];
|
||||
}
|
||||
|
||||
public function isSuccess()
|
||||
{
|
||||
return !($this->code < 0);
|
||||
}
|
||||
|
||||
public function getCode()
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
public function getMsg()
|
||||
{
|
||||
return $this->msg;
|
||||
}
|
||||
|
||||
public function getResult($key = null)
|
||||
{
|
||||
if ($key) {
|
||||
return $this->result[$key] ?: null;
|
||||
}
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
public function getSign()
|
||||
{
|
||||
return $this->sign;
|
||||
}
|
||||
|
||||
public function getSignType()
|
||||
{
|
||||
return $this->signType;
|
||||
}
|
||||
|
||||
public function verify()
|
||||
{
|
||||
return Sign::verify($this->data, Config::get('secret_key'));
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Org\Kudian;
|
||||
|
||||
class Sign
|
||||
{
|
||||
const SIGN_KEY = 'sign';
|
||||
|
||||
public static function generate($params, $secretKey)
|
||||
{
|
||||
unset($params[self::SIGN_KEY]);
|
||||
ksort($params);
|
||||
$signStr = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$signStr .= $key . "=" . $value . "&";
|
||||
}
|
||||
$signStr = rtrim($signStr, '&') . "&secret_key=" . $secretKey;
|
||||
$sign = md5($signStr);
|
||||
$sign = strtoupper($sign);
|
||||
return $sign;
|
||||
}
|
||||
|
||||
public static function verify($params, $secretKey)
|
||||
{
|
||||
$sign = $params[self::SIGN_KEY] ?: null;
|
||||
return self::generate($params, $secretKey) == $sign;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue