102 lines
3.1 KiB
PHP
102 lines
3.1 KiB
PHP
<?php
|
||
|
||
|
||
|
||
|
||
|
||
namespace app\common\pay\wx;
|
||
|
||
|
||
|
||
|
||
|
||
class Wechat
|
||
|
||
{
|
||
|
||
public static $appid = 'wxaf7cb4289e79322a';
|
||
|
||
public static $mchId = '1460116002';
|
||
|
||
public static $apikey = 'r89ZM8ubLdd8sd3L998Imm2up3Mp8SHp';
|
||
|
||
public static $notifyUrl = 'https://wo.vfengs.com/merchant/v1/pay-notify';
|
||
|
||
//获取签名
|
||
|
||
static function getSign($arr, $apikey)
|
||
|
||
{
|
||
|
||
//签名步骤一:按字典序排序参数
|
||
|
||
ksort($arr);
|
||
|
||
$string = self::getStringA($arr);
|
||
|
||
//签名步骤二:在string后加入KEY
|
||
|
||
$string = $string . "&key=" . $apikey;
|
||
|
||
//签名步骤三:MD5加密或者HMAC-SHA256
|
||
|
||
$string = md5($string);
|
||
|
||
//签名步骤四:所有字符转为大写
|
||
|
||
$result = strtoupper($string);
|
||
|
||
return $result;
|
||
|
||
}
|
||
|
||
|
||
|
||
//签名第一步
|
||
|
||
static function getStringA($arr)
|
||
|
||
{
|
||
|
||
$buff = "";
|
||
|
||
foreach ($arr as $k => $v) {
|
||
|
||
if ($k != "sign" && $v != "" && !is_array($v)) {
|
||
|
||
$buff .= $k . "=" . $v . "&";
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
$buff = trim($buff, "&");
|
||
|
||
return $buff;
|
||
|
||
}
|
||
|
||
|
||
|
||
//转换成xml格式数据
|
||
|
||
static function arr2xml($arr)
|
||
|
||
{
|
||
|
||
$xml = "<xml>";
|
||
|
||
foreach ($arr as $key => $val) {
|
||
|
||
if (is_numeric($val)) {
|
||
|
||
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
||
|
||
} else {
|
||
|
||
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
||
|
||
}
|
||
|
||
}
|
||
|
||
$xml .= "</xml>";
|
||
|
||
return $xml;
|
||
|
||
}
|
||
|
||
|
||
|
||
//解析xml数据
|
||
|
||
static function xml2arr($xml)
|
||
|
||
{
|
||
|
||
libxml_disable_entity_loader(true);
|
||
|
||
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
|
||
return $arr;
|
||
|
||
}
|
||
|
||
|
||
|
||
static function postXmlCurl($url, $xml, $second = 30)
|
||
|
||
{
|
||
|
||
$ch = curl_init();
|
||
|
||
//设置超时
|
||
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
|
||
|
||
curl_setopt($ch, CURLOPT_URL, $url);
|
||
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
|
||
|
||
//设置header
|
||
|
||
curl_setopt($ch, CURLOPT_HEADER, FALSE);
|
||
|
||
//要求结果为字符串且输出到屏幕上
|
||
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
||
|
||
//post提交方式
|
||
|
||
curl_setopt($ch, CURLOPT_POST, TRUE);
|
||
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
|
||
|
||
//运行curl
|
||
|
||
$data = curl_exec($ch);
|
||
|
||
//返回结果
|
||
|
||
if ($data) {
|
||
|
||
curl_close($ch);
|
||
|
||
return $data;
|
||
|
||
} else {
|
||
|
||
$error = curl_errno($ch);
|
||
|
||
curl_close($ch);
|
||
|
||
return "curl出错,错误码:$error";
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
static function notifyReturn($returnCode = 'SUCCESS', $returnMsg = 'OK')
|
||
|
||
{
|
||
|
||
$result = [
|
||
|
||
'return_code' => $returnCode,
|
||
|
||
'return_msg' => $returnMsg,
|
||
|
||
];
|
||
|
||
echo self::arr2xml($result);
|
||
|
||
exit;
|
||
|
||
}
|
||
|
||
} |