<?php
namespace app\index\common;
use Config;
/**
* 支付的工具类
*/
class Alipay
{
public function generateSign($params, $signType = "RSA") {
return $this->sign($this->getSignContent($params), $signType);
}
protected function sign($data, $signType = "RSA") {
$priKey=Config::get('alipay.merchant_private_key');
$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
"\n-----END RSA PRIVATE KEY-----";
($res) or
die('您使用的私钥格式错误,请检查RSA私钥配置');
if ("RSA2" == $signType) {
openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256
: OPENSSL_ALGO_SHA256
); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
} else {
}
return $sign;
}
/**
* 校验$value是否非空
* if not set ,return true;
* if is null , return true;
**/
protected function checkEmpty($value) {
return true;
if ($value === null)
return true;
return true;
return false;
}
public function getSignContent($params) {
$stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
// 转换成目标字符集
$v = $this->characet($v, Config::get('alipay.charset'));
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
}
return $stringToBeSigned;
}
/**
* 转换字符集编码
* @param $data
* @param $targetCharset
* @return string
*/
function characet($data, $targetCharset) {
$fileType = Config::get('alipay.charset');
//$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
}
}
return $data;
}
public function curlPost
($url = '', $postData = '', $options = array())
{
}
curl_setopt($ch, CURLOPT_TIMEOUT
, 30); //设置cURL允许执行的最长秒数
}
//https请求 不验证证书和host
return $data;
}
}