class
AlipayApp{
function
createLinkstring(
$para
,
$showQuotes
= false) {
$arg
=
""
;
$quotes
=
''
;
if
(
$showQuotes
){
$quotes
=
'"'
;
}
foreach
(
$para
as
$key
=>
$val
) {
if
(
$arg
==
''
){
$arg
=
$key
.
'='
.
$quotes
.
$val
.
$quotes
;
}
else
{
$arg
=
$arg
.
'&'
.
$key
.
'='
.
$quotes
.
$val
.
$quotes
;
}
}
if
(get_magic_quotes_gpc()){
$arg
=
stripslashes
(
$arg
);}
return
$arg
;
}
function
argSort(
$para
) {
ksort(
$para
);
reset(
$para
);
return
$para
;
}
function
paraFilter(
$para
) {
$para_filter
=
array
();
while
(list (
$key
,
$val
) = each (
$para
)) {
if
(
$key
==
"sign"
||
$key
==
"sign_type"
||
$val
==
""
)
continue
;
else
$para_filter
[
$key
] =
$para
[
$key
];
}
return
$para_filter
;
}
function
query_timestamp() {
$url
=
$this
->alipay_gateway_new.
"service=query_timestamp&partner="
.trim(
strtolower
(
$this
->alipay_config[
'partner'
])).
"&_input_charset="
.trim(
strtolower
(
$this
->alipay_config[
'input_charset'
]));
$encrypt_key
=
""
;
$doc
=
new
DOMDocument();
$doc
->load(
$url
);
$itemEncrypt_key
=
$doc
->getElementsByTagName(
"encrypt_key"
);
$encrypt_key
=
$itemEncrypt_key
->item(0)->nodeValue;
return
$encrypt_key
;
}
function
logResult(
$word
=
''
) {
date_default_timezone_set(
"PRC"
);
$fp
=
fopen
(
"log.txt"
,
"a"
);
flock
(
$fp
, LOCK_EX) ;
fwrite(
$fp
,
"执行日期:"
.
strftime
(
"%Y%m%d%H%M%S"
,time()).
"
"
.
$word
.
"
"
);
flock
(
$fp
, LOCK_UN);
fclose(
$fp
);
}
function
getHttpResponsePOST(
$url
,
$cacert_url
,
$para
,
$input_charset
=
''
) {
if
(trim(
$input_charset
) !=
''
) {
$url
=
$url
.
"_input_charset="
.
$input_charset
;
}
$curl
= curl_init(
$url
);
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt(
$curl
, CURLOPT_CAINFO,
$cacert_url
);
curl_setopt(
$curl
, CURLOPT_HEADER, 0 );
curl_setopt(
$curl
,CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$curl
,CURLOPT_POST,true);
curl_setopt(
$curl
,CURLOPT_POSTFIELDS,
$para
);
$responseText
= curl_exec(
$curl
);
curl_close(
$curl
);
return
$responseText
;
}
function
getHttpResponseGET(
$url
,
$cacert_url
) {
$curl
= curl_init(
$url
);
curl_setopt(
$curl
, CURLOPT_HEADER, 0 );
curl_setopt(
$curl
,CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt(
$curl
, CURLOPT_CAINFO,
$cacert_url
);
$responseText
= curl_exec(
$curl
);
curl_close(
$curl
);
return
$responseText
;
}
function
charsetEncode(
$input
,
$_output_charset
,
$_input_charset
) {
$output
=
""
;
if
(!isset(
$_output_charset
) )
$_output_charset
=
$_input_charset
;
if
(
$_input_charset
==
$_output_charset
||
$input
==null ) {
$output
=
$input
;
}
elseif
(function_exists(
"mb_convert_encoding"
)) {
$output
= mb_convert_encoding(
$input
,
$_output_charset
,
$_input_charset
);
}
elseif
(function_exists(
"iconv"
)) {
$output
= iconv(
$_input_charset
,
$_output_charset
,
$input
);
}
else
die
(
"sorry, you have no libs support for charset change."
);
return
$output
;
}
function
charsetDecode(
$input
,
$_input_charset
,
$_output_charset
) {
$output
=
""
;
if
(!isset(
$_input_charset
) )
$_input_charset
=
$_input_charset
;
if
(
$_input_charset
==
$_output_charset
||
$input
==null ) {
$output
=
$input
;
}
elseif
(function_exists(
"mb_convert_encoding"
)) {
$output
= mb_convert_encoding(
$input
,
$_output_charset
,
$_input_charset
);
}
elseif
(function_exists(
"iconv"
)) {
$output
= iconv(
$_input_charset
,
$_output_charset
,
$input
);
}
else
die
(
"sorry, you have no libs support for charset changes."
);
return
$output
;
}
function
rsaSign(
$data
,
$private_key
) {
$private_key
=
str_replace
(
"-----BEGIN RSA PRIVATE KEY-----"
,
""
,
$private_key
);
$private_key
=
str_replace
(
"-----END RSA PRIVATE KEY-----"
,
""
,
$private_key
);
$private_key
=
str_replace
(
"
"
,
""
,
$private_key
);
$private_key
=
"-----BEGIN RSA PRIVATE KEY-----"
.PHP_EOL .wordwrap(
$private_key
, 64,
"
"
, true). PHP_EOL.
"-----END RSA PRIVATE KEY-----"
;
$res
=openssl_get_privatekey(
$private_key
);
if
(
$res
)
{
openssl_sign(
$data
,
$sign
,
$res
);
}
else
{
echo
"您的私钥格式不正确!"
.
"<br/>"
.
"The format of your private_key is incorrect!"
;
exit
();
}
openssl_free_key(
$res
);
$sign
=
base64_encode
(
$sign
);
return
$sign
;
}
function
rsaVerify(
$data
,
$alipay_public_key
,
$sign
) {
$alipay_public_key
=
str_replace
(
"-----BEGIN PUBLIC KEY-----"
,
""
,
$alipay_public_key
);
$alipay_public_key
=
str_replace
(
"-----END PUBLIC KEY-----"
,
""
,
$alipay_public_key
);
$alipay_public_key
=
str_replace
(
"
"
,
""
,
$alipay_public_key
);
$alipay_public_key
=
'-----BEGIN PUBLIC KEY-----'
.PHP_EOL.wordwrap(
$alipay_public_key
, 64,
"
"
, true) .PHP_EOL.
'-----END PUBLIC KEY-----'
;
$res
=openssl_get_publickey(
$alipay_public_key
);
if
(
$res
)
{
$result
= (bool)openssl_verify(
$data
,
base64_decode
(
$sign
),
$res
);
}
else
{
echo
"您的支付宝公钥格式不正确!"
.
"<br/>"
.
"The format of your alipay_public_key is incorrect!"
;
exit
();
}
openssl_free_key(
$res
);
return
$result
;
}
}
class
AlipayNotify {
var
$https_verify_url
=
'https://mapi.alipay.com/gateway.do?service=notify_verify&'
;
var
$http_verify_url
=
'http://notify.alipay.com/trade/notify_query.do?'
;
var
$alipay_config
;
function
__construct(
$alipay_config
){
$this
->alipay_config =
$alipay_config
;
}
function
AlipayNotify(
$alipay_config
) {
$this
->__construct(
$alipay_config
);
}
function
getSignVeryfy(
$para_temp
,
$sign
) {
$alipayapp
=
new
Alipayapp();
$para_filter
=
$alipayapp
->paraFilter(
$para_temp
);
$para_sort
=
$alipayapp
->argSort(
$para_filter
);
$prestr
=
$alipayapp
->createLinkstring(
$para_sort
);
$isSgin
= false;
switch
(
strtoupper
(trim(
$this
->alipay_config[
'sign_type'
]))) {
case
"RSA"
:
$isSgin
=
$alipayapp
->rsaVerify(
$prestr
, trim(
$this
->alipay_config[
'alipay_public_key'
]),
$sign
);
break
;
default
:
$isSgin
= false;
}
return
$isSgin
;
}
function
getResponse(
$notify_id
) {
$alipayapp
=
new
Alipayapp();
$transport
=
strtolower
(trim(
$this
->alipay_config[
'transport'
]));
$partner
= trim(
$this
->alipay_config[
'partner'
]);
$veryfy_url
=
''
;
if
(
$transport
==
'https'
) {
$veryfy_url
=
$this
->https_verify_url;
}
else
{
$veryfy_url
=
$this
->http_verify_url;
}
$veryfy_url
=
$veryfy_url
.
"partner="
.
$partner
.
"¬ify_id="
.
$notify_id
;
$responseTxt
=
$alipayapp
->getHttpResponseGET(
$veryfy_url
,
$this
->alipay_config[
'cacert'
]);
return
$responseTxt
;
}
}