notify. php,notify.php

/**

* 原生支付(扫码支付)及公众号支付的异步回调通知

* 说明:需要在native.php或者jsapi.php中的填写回调地址。例如:http://www.xxx.com/wx/notify.php

* 付款成功后,微信服务器会将付款结果通知到该页面

*/

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

$mchid = 'xxxxx'; //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送

$appid = 'xxxxx'; //公众号APPID 通过微信支付商户资料审核后邮件发送

$apiKey = 'xxxxx'; //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥

$wxPay = new WxpayService($mchid,$appid,$apiKey);

$result = $wxPay->notify();

if($result){

//完成你的逻辑

//例如连接数据库,获取付款金额$result['cash_fee'],获取订单号$result['out_trade_no'],修改数据库中的订单状态等;

}else{

echo 'pay error';

}

class WxpayService

{

protected $mchid;

protected $appid;

protected $apiKey;

public function __construct($mchid, $appid, $key)

{

$this->mchid = $mchid;

$this->appid = $appid;

$this->apiKey = $key;

}

public function notify()

{

$config = array(

'mch_id' => $this->mchid,

'appid' => $this->appid,

'key' => $this->apiKey,

);

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

if ($postObj === false) {

die('parse xml error');

}

if ($postObj->return_code != 'SUCCESS') {

die($postObj->return_msg);

}

if ($postObj->result_code != 'SUCCESS') {

die($postObj->err_code);

}

$arr = (array)$postObj;

unset($arr['sign']);

if (self::getSign($arr, $config['key']) == $postObj->sign) {

echo '';

return $arr;

}

}

/**

* 获取签名

*/

public static function getSign($params, $key)

{

ksort($params, SORT_STRING);

$unSignParaString = self::formatQueryParaMap($params, false);

$signStr = strtoupper(md5($unSignParaString . "&key=" . $key));

return $signStr;

}

protected static function formatQueryParaMap($paraMap, $urlEncode = false)

{

$buff = "";

ksort($paraMap);

foreach ($paraMap as $k => $v) {

if (null != $v && "null" != $v) {

if ($urlEncode) {

$v = urlencode($v);

}

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

}

}

$reqPar = '';

if (strlen($buff) > 0) {

$reqPar = substr($buff, 0, strlen($buff) - 1);

}

return $reqPar;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值