类 alipayservicecheck:解决当面付异步回调的跳转支付问题-明生-尊龙凯时平台在线地址
支付宝当面付只提供了notify_url,可以得到支付成功的json数组但无法自动唤醒跳转。要实现前台支付跳转,一种方法是通过js轮询来查询支付结果。这种方式有点消耗服务器,此外,支付结果可以从notify_url实时得到。
当面付的支付结果只能通过主动轮循调用alipay.trade.query(统一收单线下交易查询接口)查询,或者通过支付成功返回的异步通知来判断。
支付宝提供了一个类alipayservicecheck正好可以解决这种唤醒跳转需求。
控制器中通过类alipayservicecheck直接调用该业务方法,可以返回一串表单字符串。ajax传给前端后,直接追加写入到body中即可实现跳转支付宝支付。
当面付的支付结果只能通过主动轮循调用alipay.trade.query(统一收单线下交易查询接口)查询,或者通过支付成功返回的异步通知来判断。
支付宝提供了一个类alipayservicecheck正好可以解决这种唤醒跳转需求。
控制器中通过类alipayservicecheck直接调用该业务方法,可以返回一串表单字符串。ajax传给前端后,直接追加写入到body中即可实现跳转支付宝支付。
$alipay = new alipayservicecheck($alipayconfig['publickey']);
$_verify = $alipay->rsacheck($_post, $_post['sign_type']);
if ($_verify === true) {
$this_verify = true;
} else {
$this_verify = false;
echo 'success';exit();
}
class alipayservicecheck
{
//支付宝公钥
protected $alipaypublickey;
protected $charset;
public function __construct($alipaypublickey)
{
$this->charset = 'utf8';
$this->alipaypublickey = $alipaypublickey;
}
public function rsacheck($params)
{
$sign = $params['sign'];
$signtype = $params['sign_type'];
unset($params['sign_type']);
unset($params['sign']);
return $this->verify($this->getsigncontent($params), $sign, $signtype);
}
public function verify($data, $sign, $signtype = 'rsa')
{
$pubkey = $this->alipaypublickey;
$res = "-----begin public key-----\n" .
wordwrap($pubkey, 64, "\n", true) .
"\n-----end public key-----";
($res) or die('支付宝rsa公钥错误。请检查公钥文件格式是否正确');
//调用openssl内置方法验签,返回bool值
if ("rsa2" == $signtype) {
$result = (bool) openssl_verify($data, base64_decode($sign), $res, version_compare(php_version, '5.4.0', '<') ? sha256 : openssl_algo_sha256);
} else {
$result = (bool) openssl_verify($data, base64_decode($sign), $res);
}
return $result;
}
protected function checkempty($value)
{
if (!isset($value)) {
return true;
}
if ($value === null) {
return true;
}
if (trim($value) === "") {
return true;
}
return false;
}
public function getsigncontent($params)
{
ksort($params);
$stringtobesigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkempty($v) && "@" != substr($v, 0, 1)) {
// 修复转义导致签名失败
$v = stripslashes($v);
// 转换成目标字符集
$v = $this->characet($v, $this->charset);
if ($i == 0) {
$stringtobesigned .= "$k" . "=" . "$v";
} else {
$stringtobesigned .= "&" . "$k" . "=" . "$v";
}
$i ;
}
}
unset($k, $v);
return $stringtobesigned;
}
public function characet($data, $targetcharset)
{
if (!empty($data)) {
$filetype = $this->charset;
if (strcasecmp($filetype, $targetcharset) != 0) {
$data = mb_convert_encoding($data, $targetcharset, $filetype);
//$data = iconv($filetype, $targetcharset.'//ignore', $data);
}
}
return $data;
}
}
$_verify = $alipay->rsacheck($_post, $_post['sign_type']);
if ($_verify === true) {
$this_verify = true;
} else {
$this_verify = false;
echo 'success';exit();
}
class alipayservicecheck
{
//支付宝公钥
protected $alipaypublickey;
protected $charset;
public function __construct($alipaypublickey)
{
$this->charset = 'utf8';
$this->alipaypublickey = $alipaypublickey;
}
public function rsacheck($params)
{
$sign = $params['sign'];
$signtype = $params['sign_type'];
unset($params['sign_type']);
unset($params['sign']);
return $this->verify($this->getsigncontent($params), $sign, $signtype);
}
public function verify($data, $sign, $signtype = 'rsa')
{
$pubkey = $this->alipaypublickey;
$res = "-----begin public key-----\n" .
wordwrap($pubkey, 64, "\n", true) .
"\n-----end public key-----";
($res) or die('支付宝rsa公钥错误。请检查公钥文件格式是否正确');
//调用openssl内置方法验签,返回bool值
if ("rsa2" == $signtype) {
$result = (bool) openssl_verify($data, base64_decode($sign), $res, version_compare(php_version, '5.4.0', '<') ? sha256 : openssl_algo_sha256);
} else {
$result = (bool) openssl_verify($data, base64_decode($sign), $res);
}
return $result;
}
protected function checkempty($value)
{
if (!isset($value)) {
return true;
}
if ($value === null) {
return true;
}
if (trim($value) === "") {
return true;
}
return false;
}
public function getsigncontent($params)
{
ksort($params);
$stringtobesigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkempty($v) && "@" != substr($v, 0, 1)) {
// 修复转义导致签名失败
$v = stripslashes($v);
// 转换成目标字符集
$v = $this->characet($v, $this->charset);
if ($i == 0) {
$stringtobesigned .= "$k" . "=" . "$v";
} else {
$stringtobesigned .= "&" . "$k" . "=" . "$v";
}
$i ;
}
}
unset($k, $v);
return $stringtobesigned;
}
public function characet($data, $targetcharset)
{
if (!empty($data)) {
$filetype = $this->charset;
if (strcasecmp($filetype, $targetcharset) != 0) {
$data = mb_convert_encoding($data, $targetcharset, $filetype);
//$data = iconv($filetype, $targetcharset.'//ignore', $data);
}
}
return $data;
}
}