master
elf 2 years ago
parent 94933c4038
commit 679f3fdfc2

@ -17,6 +17,7 @@ use Base\Repository\GameRepository;
use Base\Service\PromoteCompanyService;
use Base\Tool\Redis;
use Think\Model;
use Org\QzlPay\Api as QzlApi;
class ConsoleController extends Think {
@ -892,4 +893,18 @@ class ConsoleController extends Think {
$month = date('Y-m', strtotime("$month +1 month"));
} while ($month <= $lastMonth);
}
public function testPay() {
$extra = [
'notifyUrl' => 'http://www.baidu.com',
'callbackUrl' => 'http://www.baidu.com',
];
$response = QzlApi::paySubmit(
'' . time() . rand(1000, 9999),
6 * 100,
QzlApi::CHANNEL_WX_H5,
'测试充值',
$extra
);
}
}

@ -0,0 +1,37 @@
<?php
namespace Org\QzlPay;
use Org\QzlPay\Request\H5PayRequest;
use Org\QzlPay\Request\WxH5PayRequest;
class Api
{
use ApiCommonTrait;
public const CHANNEL_WX_H5 = 'wx_h5';
public const CHANNEL_ALI_H5 = 'ali_h5';
public static function paySubmit($orderNo, $amount, $channel, $body, $extra) {
$params = [
'channel' => $channel,
'body' => $body,
'mchOrderNo' => $orderNo,
'amount' => $amount,
'currency' => 'CNY',
'timePaid' => date('YmdHis'),
'timeExpire' => date('YmdHis', time() + 15*60),
'remark' => '',
'extra' => $extra,
];
return self::request('cs.pay.submit', $params);
}
public static function queryOrder($orderNo = '', $cpOrderNo = '') {
$params = [
'mchOrderNo' => $orderNo,
'cpOrderNo' => $cpOrderNo,
];
return self::request('cs.order.query', $params);
}
}

@ -0,0 +1,24 @@
<?php
namespace Org\QzlPay;
trait ApiCommonTrait {
protected static function getCommonParams($apiName) {
return [
'tradeType' => $apiName,
'version' => '2.0',
'mchNo' => Config::get('mchNo'),
];
}
protected static function request($apiName, $params) {
$params = array_merge(self::getCommonParams($apiName), $params);
$params[Signer::SIGN_NAME] = Signer::sign($params, Config::get('paySecret'));
return Client::request(self::getUrl(), $params);
}
protected static function getUrl() {
return Config::get('baseUrl') . '/gateway/api/trade';
}
}

@ -0,0 +1,43 @@
<?php
namespace Org\QzlPay;
use Exception;
class Client
{
public static function request($url, $params): Response
{
try {
$response = self::post($url, $params);
Log::write('REQUEST_RESPONSE: ' . $response);
} catch (Exception $e) {
Log::write('REQUEST_ERROR: ' . $e->getMessage());
$response = json_encode([
'result' => -99,
'errorMsg' => '网络异常',
'signature' => true,
]);
}
return new Response($response);
}
public static function post($url, $params): string
{
Log::write('REQUEST_URL: ' . $url);
Log::write('REQUEST_DATA: ' . json_encode($params));
$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/x-www-form-urlencoded'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}

@ -0,0 +1,15 @@
<?php
namespace Org\QzlPay;
class Config
{
public static $params = [
'baseUrl' => 'http://game.quzhilian.net',
'mchNo' => '68822023041910000101',
'paySecret' => '040f0a9dd7d640f1b9270a0931aa8230',
];
public static function get($key) {
return self::$params[$key] ?? null;
}
}

@ -0,0 +1,12 @@
<?php
namespace Org\QzlPay;
use Think\Log as ThinkLog;
class Log
{
public static function write($message, $level = ThinkLog::INFO) {
$destination = C('LOG_PATH'). 'qzlPay/' . date('y_m_d').'.log';
ThinkLog::write($message, $level, '', $destination);
}
}

@ -0,0 +1,65 @@
<?php
namespace Org\QzlPay;
use Org\SumaPay\Config;
use Org\SumaPay\Sign;
class Response
{
private $status;
private $message;
private $resultCode;
private $data;
public function __construct($content) {
$this->parse($content);
}
public function isSuccess() {
if ($this->status === 0 && $this->resultCode === 0) {
return true;
}
return false;
}
private function parse($response)
{
if (is_array($response)) {
Log::write('RESPONSE_ARRAY: ' . json_encode($response));
$data = $response;
} else {
Log::write('RESPONSE_STR: ' . $response);
$data = json_decode($response, true);
}
if (!$data) {
$data = [
'status' => -98,
'message' => '数据异常',
];
}
$this->data = $data;
$this->status = $data['status'];
$this->message = $data['message'];
if ($data['status'] === 0) {
$this->resultCode = $data['resultCode'];
$this->message = '[' . $data['errCode'] . ']' . $data['errorMsg'];
}
}
public function getData($key) {
return $this->data[$key] ?? null;
}
public function getMessage() {
return $this->message;
}
public function verify()
{
return Signer::verify($this->data, Config::get('paySecret'));
}
}

@ -0,0 +1,32 @@
<?php
namespace Org\QzlPay;
class Signer
{
public const SIGN_NAME = 'sign';
public static function sign($params, $paySecret) {
unset($params[self::SIGN_NAME]);
if (isset($params['extra']) && is_array($params['extra'])) {
$extra = $params['extra'];
unset($params['extra']);
$params = array_merge($params, $extra);
}
ksort($params);
$signPrams = [];
foreach ($params as $key => $value) {
$signPrams[] = $key . '=' . $value;
}
$signPrams[] = 'paySecret=' . $paySecret;
$signString = implode('&', $signPrams);
return strtoupper(md5($signString));
}
public static function verify($params, $paySecret) {
$sign = $params[self::SIGN_NAME] ?? '';
if ($sign == self::sign($params, $paySecret)) {
return true;
}
return false;
}
}
Loading…
Cancel
Save