Files
midi-php/extend/Xilaixi/Xilaixi.php

199 lines
6.2 KiB
PHP
Raw Normal View History

2025-10-09 20:08:02 +08:00
<?php
use think\Env;
2025-10-09 21:57:49 +08:00
use think\Log;
2025-10-09 20:08:02 +08:00
class Xilaixi
{
private $pid;
private $key;
private string $sign_type = 'MD5';
2025-10-09 21:57:49 +08:00
// private string $mapi_url = 'https://wxjs.980city.com/mapi.php';
private string $mapi_url = 'https://wxjs.980city.com/submit.php';
2025-10-09 20:08:02 +08:00
private string $api_url = 'https://wxjs.980city.com/api.php';//https://wxjs.980city.com/api.php?
function __construct(){
2025-10-09 21:57:49 +08:00
$this->pid = Env::get('XilaixiPay.pid');//平台分配的APPID
$this->key = Env::get('XilaixiPay.key');//平台分配的APPID
2025-10-09 20:08:02 +08:00
}
/**
* 西来喜支付
*/
public function XilaixiPay($date, $type) {
//6-西来喜微信7-西来喜支付宝8-西来喜QQ钱包9-西来喜云闪付
if($type == 6){
2025-10-09 21:57:49 +08:00
$types = 'wxpay';
2025-10-09 20:08:02 +08:00
}elseif ($type == 7){
2025-10-09 21:57:49 +08:00
$types = 'alipay';
2025-10-09 20:08:02 +08:00
}elseif ($type == 8){
2025-10-09 21:57:49 +08:00
$types = 'qqpay';
2025-10-09 20:08:02 +08:00
}elseif ($type == 9){
2025-10-09 21:57:49 +08:00
$types = 'bank';
}else{
$types = 'wxpay';
2025-10-09 20:08:02 +08:00
}
2025-10-09 21:57:49 +08:00
$ip = $this->get_client_ip();
if ($ip == '::1')
$ip = '1.1.1.1';
2025-10-09 20:08:02 +08:00
//构造要请求的参数数组,无需改动
$parameter = array(
"pid" => $this->pid,
2025-10-09 21:57:49 +08:00
"type" => $types,
2025-10-09 20:08:02 +08:00
"notify_url" => get_system_config_value("web_site")."/api/Payment/XilaixiPayNotify",
2025-10-09 21:57:49 +08:00
"return_url" => get_system_config_value("web_site")."/api/Payment/XilaixiPayNotify",
2025-10-09 20:08:02 +08:00
"out_trade_no" => $date['order_sn'],//商户订单号,
"name" => $date['remarke'],//订单标题(不能有空格),
"money" => $date['money'],
2025-10-09 21:57:49 +08:00
'clientip' => $ip,
2025-10-09 20:08:02 +08:00
);
2025-10-09 21:57:49 +08:00
Log::record("西来喜支付参数:".json_encode($parameter),"info");
2025-10-09 20:08:02 +08:00
$param = $this->buildRequestParam($parameter);
2025-10-09 21:57:49 +08:00
// $response = $this->getHttpResponse($this->mapi_url, http_build_query($param));
return $this->getHttpRequest($this->mapi_url,$param);
// return json_decode($response, true);
2025-10-09 20:08:02 +08:00
}
private function buildRequestParam($param){
$mysign = $this->getSign($param);
$param['sign'] = $mysign;
$param['sign_type'] = $this->sign_type;
return $param;
}
// 计算签名
private function getSign($param){
ksort($param);
reset($param);
$signstr = '';
foreach($param as $k => $v){
if($k != "sign" && $k != "sign_type" && $v!=''){
$signstr .= $k.'='.$v.'&';
}
}
$signstr = substr($signstr,0,-1);
$signstr .= $this->key;
$sign = md5($signstr);
return $sign;
}
// 请求外部资源
private function getHttpResponse($url, $post = false, $timeout = 10){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept: */*";
$httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
$httpheader[] = "Connection: close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($post){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// 异步回调验证
public function verifyNotify(){
if(empty($_GET)) return false;
$sign = $this->getSign($_GET);
if($sign === $_GET['sign']){
$signResult = true;
}else{
$signResult = false;
}
return $signResult;
}
// 查询订单支付状态
public function orderStatus($trade_no){
$result = $this->queryOrder($trade_no);
if($result['status']==1){
return true;
}else{
return false;
}
}
// 查询订单
public function queryOrder($trade_no){
$url = $this->api_url.'?act=order&pid=' . $this->pid . '&key=' . $this->key . '&trade_no=' . $trade_no;
$response = $this->getHttpResponse($url);
$arr = json_decode($response, true);
return $arr;
}
// 订单退款
public function refund($trade_no, $money){
$url = $this->api_url.'?act=refund';
$post = 'pid=' . $this->pid . '&key=' . $this->key . '&trade_no=' . $trade_no . '&money=' . $money;
$response = $this->getHttpResponse($url, $post);
$arr = json_decode($response, true);
return $arr;
}
2025-10-09 21:57:49 +08:00
//获取当前服务器的IP
function get_client_ip() {
if ($_SERVER ['REMOTE_ADDR']) {
$cip = $_SERVER ['REMOTE_ADDR'];
} elseif (getenv("REMOTE_ADDR")) {
$cip = getenv("REMOTE_ADDR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$cip = getenv("HTTP_CLIENT_IP");
} else {
$cip = "unknown";
}
return $cip;
}
//拼接参数get请求
private function getHttpRequest($url, $param) {
// 将参数数组转换为查询字符串
$query = http_build_query($param);
// 拼接完整的URL
$fullUrl = $url . '?' . $query;
// // 初始化curl
// $ch = curl_init();
//
// // 设置curl选项
// curl_setopt($ch, CURLOPT_URL, $fullUrl);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HEADER, false);
// curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//
// // 设置HTTP头
// $httpheader[] = "Accept: */*";
// $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
// $httpheader[] = "Connection: close";
// curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
//
// // 执行请求
// $response = curl_exec($ch);
//
// // 检查是否有错误
// if (curl_errno($ch)) {
// Log::record("西来喜支付请求错误: " . curl_error($ch), "error");
// }
//
// // 关闭curl
// curl_close($ch);
return $fullUrl;
}
2025-10-09 20:08:02 +08:00
}