php notify,notify.php

header('Content-type:text/html; Charset=utf-8');

//支付宝公钥,账户中心->密钥管理->开放平台密钥,找到添加了支付功能的应用,根据你的加密类型,查看支付宝公钥

$alipayPublicKey='';

$aliPay = new AlipayService($alipayPublicKey);

//验证签名

$result = $aliPay->rsaCheck($_POST,$_POST['sign_type']);

if($result===true){

//处理你的逻辑,例如获取订单号$_POST['out_trade_no'],订单金额$_POST['total_amount']等

//程序执行完后必须打印输出“success”(不包含引号)。如果商户反馈给支付宝的字符不是success这7个字符,支付宝服务器会不断重发通知,直到超过24小时22分钟。一般情况下,25小时以内完成8次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h);

echo 'success';exit();

}

echo 'error';exit();

class AlipayService

{

//支付宝公钥

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);

}

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', '

} else {

$result = (bool)openssl_verify($data, base64_decode($sign), $res);

}

// if(!$this->checkEmpty($this->alipayPublicKey)) {

// //释放资源

// openssl_free_key($res);

// }

return $result;

}

/**

* 校验$value是否非空

* if not set ,return true;

* if is null , return true;

**/

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 = $this->characet($v, $this->charset);

if ($i == 0) {

$stringToBeSigned .= "$k" . "=" . "$v";

} else {

$stringToBeSigned .= "&" . "$k" . "=" . "$v";

}

$i++;

}

}

unset ($k, $v);

return $stringToBeSigned;

}

/**

* 转换字符集编码

* @param $data

* @param $targetCharset

* @return string

*/

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;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值